Project

General

Profile

1 2239 sgarg
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author$'
8
 *     '$Date$'
9
 * '$Revision$'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25
26
package edu.ucsb.nceas.metacattest;
27
28
import java.io.Reader;
29
import java.io.StringReader;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.GregorianCalendar;
33
import java.util.SimpleTimeZone;
34
import java.util.TimeZone;
35
36
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
37
import edu.ucsb.nceas.metacat.client.Metacat;
38
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
39
import edu.ucsb.nceas.metacat.client.MetacatException;
40
import edu.ucsb.nceas.metacat.client.MetacatFactory;
41
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
42
import edu.ucsb.nceas.utilities.IOUtil;
43
import junit.framework.Test;
44
import junit.framework.TestCase;
45
import junit.framework.TestSuite;
46 2244 sgarg
import java.io.File;
47 2239 sgarg
48
/**
49 2244 sgarg
 * A JUnit test for testing Access Control in Metacat
50 2239 sgarg
 */
51
public class AccessControlTest
52
    extends TestCase {
53 2244 sgarg
54 2256 sgarg
    private String metacatUrl = "@systemidserver@@servlet-path@";
55
    private String username = "@mcuser@";
56
    private String password = "@mcpassword@";
57
    private String anotheruser = "@mcanotheruser@";
58
    private String anotherpassword = "@mcanotherpassword@";
59
    private String prefix = "test";
60
    private String newdocid = null;
61
    private String testdocument = "";
62 2260 sgarg
    private String onlineDocid;
63 2256 sgarg
    private String onlinetestdatafile1 = "test/onlineDataFile1";
64
    private String onlinetestdatafile2 = "test/onlineDataFile2";
65 2244 sgarg
66 2256 sgarg
    private Metacat m;
67 2244 sgarg
68 2256 sgarg
    private boolean SUCCESS = true;
69
    private boolean FAILURE = false;
70 2239 sgarg
71 2256 sgarg
    /**
72
     * These variables are for eml-2.0.1 only. For other eml versions,
73
     * this function might have to modified
74
     */
75 2244 sgarg
76 2275 sgarg
    private String testEml_201_Header =
77 2256 sgarg
        "<?xml version=\"1.0\"?><eml:eml" +
78
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
79
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
80
        " packageId=\"eml.1.1\" system=\"knb\"" +
81
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
82
        " scope=\"system\">";
83 2244 sgarg
84 2275 sgarg
    private String testEml_200_Header =
85
    "<?xml version=\"1.0\"?><eml:eml" +
86
    " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"" +
87
    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
88
    " packageId=\"eml.1.1\" system=\"knb\"" +
89
    " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\"" +
90
    " scope=\"system\">";
91
92 2256 sgarg
    private String testEmlCreatorBlock =
93
        "<creator scope=\"document\">                                       " +
94
        " <individualName>                                                  " +
95
        "    <surName>Smith</surName>                                       " +
96
        " </individualName>                                                 " +
97
        "</creator>                                                         ";
98 2244 sgarg
99 2256 sgarg
    private String testEmlContactBlock =
100
        "<contact scope=\"document\">                                       " +
101
        " <individualName>                                                  " +
102
        "    <surName>Jackson</surName>                                     " +
103
        " </individualName>                                                 " +
104
        "</contact>                                                         ";
105 2244 sgarg
106 2256 sgarg
    private String testEmlInlineBlock1 =
107
        "<inline>                                                           " +
108
        "  <admin>                                                          " +
109
        "    <contact>                                                      " +
110
        "      <name>Operator</name>                                        " +
111
        "      <institution>PSI</institution>                               " +
112
        "    </contact>                                                     " +
113
        "  </admin>                                                         " +
114
        "</inline>                                                          ";
115 2244 sgarg
116 2256 sgarg
    private String testEmlInlineBlock2 =
117
        "<inline>                                                           " +
118
        "  <instrument>                                                     " +
119
        "    <instName>LCQ</instName>                                       " +
120
        "    <source type=\"ESI\"></source>                                 " +
121
        "    <detector type=\"EM\"></detector>                              " +
122
        "  </instrument>                                                    " +
123
        "</inline>                                                          ";
124 2244 sgarg
125 2256 sgarg
    /**
126
     * This function returns an access block based on the params passed
127
     */
128
    private String getAccessBlock(String principal, boolean grantAccess,
129
                                  boolean read, boolean write,
130
                                  boolean changePermission, boolean all) {
131
        String accessBlock = "<access " +
132
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
133
            " order=\"allowFirst\" scope=\"document\">";
134 2244 sgarg
135 2256 sgarg
        if (grantAccess) {
136
            accessBlock += "<allow>";
137
        }
138
        else {
139
            accessBlock += "<deny>";
140
        }
141 2244 sgarg
142 2256 sgarg
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
143 2244 sgarg
144 2256 sgarg
        if (all) {
145
            accessBlock += "<permission>all</permission>";
146
        }
147
        else {
148
            if (read) {
149
                accessBlock += "<permission>read</permission>";
150
            }
151
            if (write) {
152
                accessBlock += "<permission>write</permission>";
153
            }
154
            if (changePermission) {
155
                accessBlock += "<permission>changePermission</permission>";
156
            }
157
        }
158 2244 sgarg
159 2256 sgarg
        if (grantAccess) {
160
            accessBlock += "</allow>";
161
        }
162
        else {
163
            accessBlock += "</deny>";
164
        }
165
        accessBlock += "</access>";
166 2244 sgarg
167 2256 sgarg
        return accessBlock;
168 2239 sgarg
169
    }
170
171 2256 sgarg
    /**
172
     * This function returns a valid eml document with no access rules
173
     * This function is for eml-2.0.1 only. For other eml versions,
174
     * this function might have to modified
175
     */
176
    private String getTestEmlDoc(String title, String inlineData1,
177
                                 String inlineData2, String onlineUrl1,
178
                                 String onlineUrl2, String docAccessBlock,
179
                                 String inlineAccessBlock1,
180
                                 String inlineAccessBlock2,
181
                                 String onlineAccessBlock1,
182
                                 String onlineAccessBlock2) {
183 2239 sgarg
184 2256 sgarg
        String testDocument = "";
185 2275 sgarg
        testDocument = testDocument + testEml_201_Header +
186 2256 sgarg
            "<dataset scope=\"document\"><title>" + title + "</title>" +
187
            testEmlCreatorBlock;
188 2239 sgarg
189 2256 sgarg
        if (inlineData1 != null) {
190
            testDocument = testDocument
191
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
192
                + inlineData1 + "</distribution>";
193
        }
194
        if (inlineData2 != null) {
195
            testDocument = testDocument
196
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
197
                + inlineData2 + "</distribution>";
198
        }
199
        if (onlineUrl1 != null) {
200
            testDocument = testDocument
201
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
202
                + "<online><url function=\"download\">"
203
                + onlineUrl1 + "</url></online></distribution>";
204
        }
205
        if (onlineUrl2 != null) {
206
            testDocument = testDocument +
207
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
208
                + "<online><url function=\"download\">"
209
                + onlineUrl2 + "</url></online></distribution>";
210
        }
211
        testDocument += testEmlContactBlock;
212 2239 sgarg
213 2256 sgarg
        if (docAccessBlock != null) {
214
            testDocument += docAccessBlock;
215
        }
216 2239 sgarg
217 2256 sgarg
        testDocument += "</dataset>";
218
219
        if (inlineAccessBlock1 != null) {
220
            testDocument += "<additionalMetadata>";
221
            testDocument += "<describes>inlineEntity1</describes>";
222
            testDocument += inlineAccessBlock1;
223
            testDocument += "</additionalMetadata>";
224
        }
225
226
        if (inlineAccessBlock2 != null) {
227
            testDocument += "<additionalMetadata>";
228
            testDocument += "<describes>inlineEntity2</describes>";
229
            testDocument += inlineAccessBlock2;
230
            testDocument += "</additionalMetadata>";
231
        }
232
233
        if (onlineAccessBlock1 != null) {
234
            testDocument += "<additionalMetadata>";
235
            testDocument += "<describes>onlineEntity1</describes>";
236
            testDocument += onlineAccessBlock1;
237
            testDocument += "</additionalMetadata>";
238
        }
239
240
        if (onlineAccessBlock2 != null) {
241
            testDocument += "<additionalMetadata>";
242
            testDocument += "<describes>onlineEntity2</describes>";
243
            testDocument += onlineAccessBlock2;
244
            testDocument += "</additionalMetadata>";
245
        }
246
247
        testDocument += "</eml:eml>";
248
249
        //System.out.println("Returning following document" + testDocument);
250
        return testDocument;
251 2239 sgarg
    }
252 2256 sgarg
253
    /**
254
     * Constructor to build the test
255
     *
256
     * @param name the name of the test method
257
     */
258
    public AccessControlTest(String name) {
259
        super(name);
260
        newdocid = generateDocid();
261 2239 sgarg
    }
262 2256 sgarg
263
    /**
264
     * Establish a testing framework by initializing appropriate objects
265
     */
266
    public void setUp() {
267
        try {
268
            System.err.println("Test Metacat: " + metacatUrl);
269
            m = MetacatFactory.createMetacatConnection(metacatUrl);
270
        }
271
        catch (MetacatInaccessibleException mie) {
272
            System.err.println("Metacat is: " + metacatUrl);
273
            fail("Metacat connection failed." + mie.getMessage());
274
        }
275 2239 sgarg
    }
276
277 2256 sgarg
    /**
278
     * Release any objects after tests are complete
279
     */
280
    public void tearDown() {
281 2239 sgarg
    }
282 2256 sgarg
283
    /**
284
     * Create a suite of tests to be run together
285
     */
286
    public static Test suite() {
287
        TestSuite suite = new TestSuite();
288
        suite.addTest(new AccessControlTest("initialize"));
289
        // Test basic functions
290 2260 sgarg
        suite.addTest(new AccessControlTest("documentTest"));
291
        suite.addTest(new AccessControlTest("AccessControlTestForPublic"));
292 2256 sgarg
293
        return suite;
294 2239 sgarg
    }
295 2256 sgarg
296
    /**
297
     * Run an initial test that always passes to check that the test
298
     * harness is working.
299
     */
300
    public void initialize() {
301
        assertTrue(1 == 1);
302 2239 sgarg
    }
303
304 2256 sgarg
    /** *********
305
     * Test the case when no access is specified and owner is logged in
306
     * No online or inline data is involved
307 2260 sgarg
     * -> an user inserts a document and is able to read it, update it,
308
     *    set permissions on it and delete it
309
     * -> another user is not able to do anything with the document
310
     *    when no access is specified for that user
311
     * -> test what all the other user can do when read only, write only and
312
     *    change permissions only permissions are specified
313
     *
314 2256 sgarg
     */
315
    public void documentTest() {
316
        try {
317 2275 sgarg
318 2256 sgarg
            newdocid = generateDocid();
319 2244 sgarg
320 2256 sgarg
            // login
321
            m.login(username, password);
322 2244 sgarg
323 2275 sgarg
            // insert a 2.0.0 document
324
            testdocument = testEml_200_Header +
325
                "<dataset scope=\"document\"><title>submitting eml2000</title>" +
326
            testEmlCreatorBlock + testEmlContactBlock + "</dataset></eml:eml>";
327
328
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
329
330
            // read the document
331
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
332
333
            // update it with 2.0.1 document
334
            testdocument = getTestEmlDoc("Updating eml200 with eml201",
335
                                         null, null, null, null,
336
                                         null, null, null, null, null);
337
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
338
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
339
            m.logout();
340
341
342
            newdocid = generateDocid();
343
344
            // login
345
            m.login(username, password);
346
347 2256 sgarg
            // insert a document
348
            testdocument = getTestEmlDoc("Testing insert", null,
349
                                         null, null,
350
                                         null, null, null, null, null, null);
351
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
352 2260 sgarg
353
            // read the document
354 2256 sgarg
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
355 2244 sgarg
356 2256 sgarg
            // update the document
357
            testdocument = getTestEmlDoc("Testing update", null, null, null,
358
                                         null, null, null, null, null, null);
359
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
360
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
361 2239 sgarg
362 2256 sgarg
            /////////////////////////////
363 2260 sgarg
            // check what the another user can do
364
            m.logout();
365
            m.login(anotheruser, anotherpassword);
366
367
            // check if the user mentioned is able to read/update/delete the document
368
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
369
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
370
            deleteDocid(newdocid + ".2", FAILURE, true);
371
372
            /////////////////////////////
373 2256 sgarg
            // update the document access control - read only
374 2260 sgarg
            m.logout();
375
            m.login(username, password);
376
377 2256 sgarg
            testdocument = getTestEmlDoc("Testing update access block",
378
                                         null, null, null,
379
                                         null, getAccessBlock(anotheruser, true,
380 2260 sgarg
                                         true, false, false, false),
381 2256 sgarg
                                         null, null, null, null);
382
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
383
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
384 2244 sgarg
385 2256 sgarg
            // check if the user mentioned is able to read the document
386
            m.logout();
387
            m.login(anotheruser, anotherpassword);
388
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
389 2239 sgarg
390 2256 sgarg
            // should not be able to update the document
391
            testdocument = getTestEmlDoc("Testing update from another user",
392
                                         null, null, null,
393
                                         null, getAccessBlock(anotheruser, true,
394 2260 sgarg
                                         true, false, false, false),
395 2256 sgarg
                                         null, null, null, null);
396
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
397 2239 sgarg
398 2256 sgarg
            // or the permissions
399 2260 sgarg
            testdocument = getTestEmlDoc("Testing update access block",
400
                                         null, null, null,
401
                                         null, getAccessBlock(anotheruser, true,
402
                                         false, false, false, true),
403
                                         null, null, null, null);
404 2256 sgarg
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
405 2260 sgarg
406
            // or delete the document
407
            deleteDocid(newdocid + ".3", FAILURE, true);
408
409 2256 sgarg
            m.logout();
410
            m.login(username, password);
411 2239 sgarg
412 2256 sgarg
            ///////////////////////////////////
413
            // update the document access control - write only
414
            testdocument = getTestEmlDoc("Testing update access block",
415
                                         null, null, null,
416
                                         null, getAccessBlock(anotheruser, true,
417 2260 sgarg
                                         false, true, false, false),
418 2256 sgarg
                                         null, null, null, null);
419
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
420
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
421
            //System.out.println(testdocument);
422
            // check if the user mentioned is able to read the document
423
            m.logout();
424
            m.login(anotheruser, anotherpassword);
425
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, true);
426 2239 sgarg
427 2256 sgarg
            // should be able to update the document
428 2260 sgarg
429 2256 sgarg
            //System.out.println(testdocument);
430 2263 sgarg
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
431 2239 sgarg
432 2256 sgarg
            // but not the permissions
433 2260 sgarg
            testdocument = getTestEmlDoc("Testing update access block",
434 2256 sgarg
                                         null, null, null,
435
                                         null, getAccessBlock(anotheruser, true,
436 2260 sgarg
                                         false, true, false, true),
437 2256 sgarg
                                         null, null, null, null);
438 2263 sgarg
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
439 2239 sgarg
440 2260 sgarg
            // try to delete the document
441 2263 sgarg
            deleteDocid(newdocid + ".5", FAILURE, true);
442
            //deleteDocid(newdocid + ".4", FAILURE, true);
443 2260 sgarg
444 2256 sgarg
            m.logout();
445
            m.login(username, password);
446 2239 sgarg
447 2256 sgarg
            /////////////////////////////////
448
            // update the document access control - change permissions only
449
            testdocument = getTestEmlDoc("Testing update access block",
450
                                         null, null, null,
451
                                         null, getAccessBlock(anotheruser, true,
452
                false, false, true, false),
453
                                         null, null, null, null);
454 2263 sgarg
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
455
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, false);
456 2256 sgarg
457
            // check if the user mentioned is able to read the document
458
            m.logout();
459
            m.login(anotheruser, anotherpassword);
460 2263 sgarg
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, FAILURE, true);
461 2256 sgarg
462
            // should not be able to update the document
463
            testdocument = getTestEmlDoc("Testing update from another user",
464
                                         null, null, null,
465
                                         null, getAccessBlock(anotheruser, true,
466
                false, false, true, false),
467
                                         null, null, null, null);
468 2263 sgarg
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
469 2256 sgarg
470
            // but can chg the permissions
471 2260 sgarg
            testdocument = getTestEmlDoc("Testing update access block",
472 2256 sgarg
                                         null, null, null,
473
                                         null, getAccessBlock(anotheruser, true,
474 2260 sgarg
                                         false, false, false, true),
475 2256 sgarg
                                         null, null, null, null);
476
            // ERRRRRRRROR
477 2263 sgarg
            //updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
478 2256 sgarg
479 2260 sgarg
            // try to delete the document
480 2263 sgarg
            //deleteDocid(newdocid + ".7", FAILURE, true);
481
            deleteDocid(newdocid + ".6", FAILURE, true);
482 2260 sgarg
483 2256 sgarg
            m.logout();
484
            m.login(username, password);
485
486 2260 sgarg
487 2256 sgarg
            /////////////////////////////////
488 2260 sgarg
            // update the document access control - read & write
489 2256 sgarg
            testdocument = getTestEmlDoc("Testing update access block",
490
                                         null, null, null,
491
                                         null, getAccessBlock(anotheruser, true,
492 2260 sgarg
                                         true, true, false, false),
493 2256 sgarg
                                         null, null, null, null);
494 2263 sgarg
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
495
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
496 2256 sgarg
497
            // check if the user mentioned is able to read the document
498
            m.logout();
499
            m.login(anotheruser, anotherpassword);
500 2263 sgarg
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
501 2256 sgarg
502 2260 sgarg
            // should be able to update the document
503 2263 sgarg
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
504 2256 sgarg
505 2260 sgarg
            // but cant chg the permissions
506
            testdocument = getTestEmlDoc("Testing update access block",
507 2256 sgarg
                                         null, null, null,
508
                                         null, getAccessBlock(anotheruser, true,
509 2260 sgarg
                                         false, false, false, true),
510 2256 sgarg
                                         null, null, null, null);
511 2263 sgarg
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
512 2256 sgarg
513 2260 sgarg
            // try to delete the document
514 2263 sgarg
            deleteDocid(newdocid + ".9", FAILURE, true);
515 2256 sgarg
516
            m.logout();
517
            m.login(username, password);
518 2244 sgarg
519
520 2260 sgarg
            /////////////////////////////////
521
            // update the document access control - read & change permissions
522 2256 sgarg
            testdocument = getTestEmlDoc("Testing update access block",
523
                                         null, null, null,
524 2260 sgarg
                                         null, getAccessBlock(anotheruser, true,
525
                                         true, false, true, false),
526 2256 sgarg
                                         null, null, null, null);
527 2263 sgarg
            updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
528
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
529 2239 sgarg
530 2256 sgarg
            // check if the user mentioned is able to read the document
531
            m.logout();
532
            m.login(anotheruser, anotherpassword);
533 2263 sgarg
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
534 2260 sgarg
            // should not be able to update the document
535 2256 sgarg
            testdocument = getTestEmlDoc("Testing update from another user",
536 2260 sgarg
                                         null, null, null,
537
                                         null, getAccessBlock(anotheruser, true,
538
                                         false, false, true, false),
539 2256 sgarg
                                         null, null, null, null);
540 2263 sgarg
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
541 2239 sgarg
542 2260 sgarg
            // but can chg the permissions
543 2256 sgarg
            testdocument = getTestEmlDoc("Testing update access block",
544
                                         null, null, null,
545 2260 sgarg
                                         null, getAccessBlock(anotheruser, true,
546
                                         false, false, false, true),
547 2256 sgarg
                                         null, null, null, null);
548 2263 sgarg
            //updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
549 2256 sgarg
550 2260 sgarg
            // try to delete the document
551 2263 sgarg
            // deleteDocid(newdocid + ".11", FAILURE, true);
552
            deleteDocid(newdocid + ".10", FAILURE, true);
553 2256 sgarg
554
            m.logout();
555
            m.login(username, password);
556
557
558
559 2260 sgarg
            /////////////////////////////////
560
            // update the document access control - read & change permissions
561 2256 sgarg
            testdocument = getTestEmlDoc("Testing update access block",
562
                                         null, null, null,
563 2260 sgarg
                                         null, getAccessBlock(anotheruser, true,
564
                                         true, false, true, false),
565 2256 sgarg
                                         null, null, null, null);
566 2263 sgarg
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
567
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument, SUCCESS, false);
568 2256 sgarg
569
            // check if the user mentioned is able to read the document
570
            m.logout();
571
            m.login(anotheruser, anotherpassword);
572 2263 sgarg
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument,  SUCCESS, false);
573 2256 sgarg
574 2260 sgarg
            // should not be able to update the document
575 2256 sgarg
            testdocument = getTestEmlDoc("Testing update from another user",
576 2260 sgarg
                                         null, null, null,
577
                                         null, getAccessBlock(anotheruser, true,
578
                                         false, false, true, false),
579 2256 sgarg
                                         null, null, null, null);
580 2263 sgarg
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
581 2260 sgarg
            // but can chg the permissions
582
            testdocument = getTestEmlDoc("Testing update from another user",
583
                                         null, null, null,
584
                                         null, getAccessBlock(anotheruser, true,
585
                                         false, false, false, true),
586 2256 sgarg
                                         null, null, null, null);
587 2260 sgarg
            // ERRRRRRRRRRRRRRRR
588 2263 sgarg
            // updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
589 2256 sgarg
590 2260 sgarg
            // try to delete the document
591 2263 sgarg
            //deleteDocid(newdocid + ".13", FAILURE, true);
592
            deleteDocid(newdocid + ".12", FAILURE, true);
593 2256 sgarg
594
            m.logout();
595
            m.login(username, password);
596 2244 sgarg
597
598 2260 sgarg
            /////////////////////////////////
599
            // update the document access control - R, W, CP
600
            testdocument = getTestEmlDoc("Testing update access block",
601
                                         null, null, null,
602 2256 sgarg
                                         null, getAccessBlock(anotheruser, true,
603 2260 sgarg
                                         true, true, true, false),
604 2256 sgarg
                                         null, null, null, null);
605 2263 sgarg
            updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
606
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
607 2239 sgarg
608 2256 sgarg
            // check if the user mentioned is able to read the document
609
            m.logout();
610
            m.login(anotheruser, anotherpassword);
611 2263 sgarg
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
612 2239 sgarg
613 2256 sgarg
            // should not be able to update the document
614
            testdocument = getTestEmlDoc("Testing update from another user",
615 2260 sgarg
                                         null, null, null,
616 2256 sgarg
                                         null, getAccessBlock(anotheruser, true,
617 2260 sgarg
                false, false, false, true),
618 2256 sgarg
                                         null, null, null, null);
619 2263 sgarg
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
620
            readDocidWhichEqualsDoc(newdocid + ".15", testdocument, SUCCESS, false);
621 2260 sgarg
622
            // but can chg the permissions
623 2256 sgarg
            testdocument = getTestEmlDoc("Testing update from another user",
624 2260 sgarg
                                         null, null, null,
625 2256 sgarg
                                         null, getAccessBlock(anotheruser, true,
626 2260 sgarg
                                         true, false, false, false),
627 2256 sgarg
                                         null, null, null, null);
628 2263 sgarg
            updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
629
            readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
630 2256 sgarg
631 2260 sgarg
            // try to delete the document
632 2263 sgarg
            deleteDocid(newdocid + ".16", FAILURE, true);
633 2256 sgarg
634
            m.logout();
635
            m.login(username, password);
636
637 2260 sgarg
            /////////////////////////////////
638
            // update the document access control - all
639
            testdocument = getTestEmlDoc("Testing update access block",
640
                                         null, null, null,
641 2256 sgarg
                                         null, getAccessBlock(anotheruser, true,
642 2260 sgarg
                                         false, false, false, true),
643 2256 sgarg
                                         null, null, null, null);
644 2260 sgarg
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
645
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
646 2256 sgarg
647
            // check if the user mentioned is able to read the document
648
            m.logout();
649
            m.login(anotheruser, anotherpassword);
650 2260 sgarg
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
651 2256 sgarg
652
            // should not be able to update the document
653
            testdocument = getTestEmlDoc("Testing update from another user",
654 2260 sgarg
                                         null, null, null,
655 2256 sgarg
                                         null, getAccessBlock(anotheruser, true,
656 2260 sgarg
                                         false, false, false, true),
657 2256 sgarg
                                         null, null, null, null);
658 2260 sgarg
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
659
            readDocidWhichEqualsDoc(newdocid + ".18", testdocument, SUCCESS, false);
660
661
            // but can chg the permissions
662 2256 sgarg
            testdocument = getTestEmlDoc("Testing update from another user",
663 2260 sgarg
                                         null, null, null,
664 2256 sgarg
                                         null, getAccessBlock(anotheruser, true,
665 2260 sgarg
                                         true, false, false, false),
666 2256 sgarg
                                         null, null, null, null);
667 2260 sgarg
            updateDocid(newdocid + ".19", testdocument, SUCCESS, false);
668
            readDocidWhichEqualsDoc(newdocid + ".19", testdocument, SUCCESS, false);
669 2256 sgarg
670 2260 sgarg
            // try to delete the document
671
            deleteDocid(newdocid + ".19", FAILURE, true);
672 2256 sgarg
673
            m.logout();
674
675
            // delete the document
676 2260 sgarg
            m.login(username, password);
677
            deleteDocid(newdocid + ".19", SUCCESS, false);
678 2256 sgarg
            m.logout();
679
        }
680
        catch (MetacatAuthException mae) {
681
            fail("Authorization failed:\n" + mae.getMessage());
682
        }
683
        catch (MetacatInaccessibleException mie) {
684
            fail("Metacat Inaccessible:\n" + mie.getMessage());
685
        }
686
        catch (Exception e) {
687
            fail("General exception:\n" + e.getMessage());
688
        }
689
690 2239 sgarg
    }
691
692 2260 sgarg
693
   /** *********
694
     * Test the case when no access is specified and public is logged in
695 2256 sgarg
     * Cases being checked:
696 2260 sgarg
     * 1. public tries to read the document - Failure.
697
     * 2. public tries to update the document - Failure.
698
     * 3. public tries to update the inline data - Failure.
699
     * 4. public tries to update the online data file - Failure.
700
     * 5. public tries to update the access rules for the document - Failure.
701
     * 6. public tries to update the access rules for the inline data - Failure.
702
     * 7. public tries to update the access rules for the online data - Failure.
703
     * 8. public tries to delete the document - Failure.
704 2256 sgarg
     */
705 2260 sgarg
    public void AccessControlTestForPublic() {
706 2256 sgarg
        try {
707 2260 sgarg
708
            String accessBlock = getAccessBlock(anotheruser, true,
709
                                         true, false, false, false);
710 2256 sgarg
            newdocid = generateDocid();
711 2260 sgarg
712 2256 sgarg
            // login
713
            m.login(username, password);
714 2239 sgarg
715 2260 sgarg
            onlineDocid = generateDocid();
716
            uploadDocid(onlineDocid + ".1",onlinetestdatafile1, SUCCESS, false);
717 2244 sgarg
718 2260 sgarg
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
719
                                     null,"ecogrid://knb/" + onlineDocid + ".1",
720
                                     null, null, null, null, null, null);
721 2244 sgarg
722 2256 sgarg
723
            // insert a document - get the docid
724
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
725
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
726
                                    false);
727 2244 sgarg
728 2256 sgarg
            // logoutand login as other user
729
            m.logout();
730 2244 sgarg
731 2256 sgarg
            // read the document
732
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, true);
733
734
            // update the document
735
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
736
737
            // update the inline data
738 2260 sgarg
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock2,
739
                         null,"ecogrid://knb/" + onlineDocid + ".1",
740
                         null, null, null, null, null, null);
741 2256 sgarg
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
742
743
            // update the online data
744 2260 sgarg
            uploadDocid(onlineDocid + ".2", onlinetestdatafile1, FAILURE, false);
745 2256 sgarg
746
            // update the document access control
747 2260 sgarg
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
748
                         null,"ecogrid://knb/" + onlineDocid + ".1",
749
                         null, accessBlock, null, null, null, null);
750 2256 sgarg
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
751
752
            // update the document access control for inline data
753 2260 sgarg
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
754
                         null,"ecogrid://knb/" + onlineDocid + ".1",
755
                         null, null, accessBlock, null, null, null);
756 2256 sgarg
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
757
758
            // update the document access control for online data
759 2260 sgarg
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
760
                         null,"ecogrid://knb/" + onlineDocid + ".1",
761
                         null, null, null, null, accessBlock, null);
762 2256 sgarg
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
763
764
            // delete the document
765 2260 sgarg
            deleteDocid(newdocid + ".2", FAILURE, true);
766 2256 sgarg
            m.logout();
767
        }
768
        catch (MetacatAuthException mae) {
769
            fail("Authorization failed:\n" + mae.getMessage());
770
        }
771
        catch (MetacatInaccessibleException mie) {
772
            fail("Metacat Inaccessible:\n" + mie.getMessage());
773
        }
774
        catch (MetacatException me) {
775
            fail("Metacat Error:\n" + me.getMessage());
776
        }
777
        catch (Exception e) {
778
            fail("General exception:\n" + e.getMessage());
779
        }
780
781 2244 sgarg
    }
782 2256 sgarg
783
    /**
784
     * Insert a document into metacat. The expected result is passed as result
785
     */
786
787
    private String insertDocid(String docid, String docText, boolean result,
788
                               boolean expectKarmaException) {
789
        String response = null;
790
        try {
791
            response = m.insert(docid,
792
                                new StringReader(testdocument), null);
793
            if (result) {
794
                assertTrue( (response.indexOf("<success>") != -1));
795
                assertTrue(response.indexOf(docid) != -1);
796
            }
797
            else {
798
                assertTrue( (response.indexOf("<success>") == -1));
799
            }
800
            System.err.println(response);
801
        }
802
        catch (MetacatInaccessibleException mie) {
803
            fail("Metacat Inaccessible:\n" + mie.getMessage());
804
        }
805
        catch (InsufficientKarmaException ike) {
806
            if (!expectKarmaException) {
807
                fail("Insufficient karma:\n" + ike.getMessage());
808
            }
809
        }
810
        catch (MetacatException me) {
811
            fail("Metacat Error:\n" + me.getMessage());
812
        }
813
        catch (Exception e) {
814
            fail("General exception:\n" + e.getMessage());
815
        }
816
        return response;
817 2244 sgarg
    }
818 2256 sgarg
819
    /**
820
     * Insert a document into metacat. The expected result is passed as result
821
     */
822
823
    private String uploadDocid(String docid, String filePath, boolean result,
824
                               boolean expectedKarmaException) {
825
        String response = null;
826
        try {
827
            response = m.upload(docid, new File(filePath));
828
            if (result) {
829
                assertTrue( (response.indexOf("<success>") != -1));
830
                assertTrue(response.indexOf(docid) != -1);
831
            }
832
            else {
833
                assertTrue( (response.indexOf("<success>") == -1));
834
            }
835
            System.err.println("respose from metacat: " + response);
836
        }
837
        catch (MetacatInaccessibleException mie) {
838
            fail("Metacat Inaccessible:\n" + mie.getMessage());
839
        }
840
        catch (InsufficientKarmaException ike) {
841
            if (!expectedKarmaException) {
842
                fail("Insufficient karma:\n" + ike.getMessage());
843
            }
844
        }
845
        catch (MetacatException me) {
846 2260 sgarg
            if (result) {
847
                fail("Metacat Error:\n" + me.getMessage());
848
            } else {
849
                System.err.println("Metacat Error:\n" + me.getMessage());
850
            }
851 2256 sgarg
        }
852
        catch (Exception e) {
853
            fail("General exception:\n" + e.getMessage());
854
        }
855
        return response;
856 2244 sgarg
    }
857
858 2256 sgarg
    /**
859
     * Update a document in metacat. The expected result is passed as result
860
     */
861
    private String updateDocid(String docid, String docText, boolean result,
862
                               boolean expectedKarmaFailure) {
863
        String response = null;
864
        try {
865
            response = m.update(docid,
866
                                new StringReader(testdocument), null);
867 2244 sgarg
868 2256 sgarg
            if (result) {
869
                assertTrue( (response.indexOf("<success>") != -1));
870
                assertTrue(response.indexOf(docid) != -1);
871
            }
872
            else {
873
                assertTrue( (response.indexOf("<success>") == -1));
874
            }
875
            System.err.println(response);
876
        }
877
        catch (MetacatInaccessibleException mie) {
878
            fail("Metacat Inaccessible:\n" + mie.getMessage());
879
        }
880
        catch (InsufficientKarmaException ike) {
881
            if (!expectedKarmaFailure) {
882
                fail("Insufficient karma:\n" + ike.getMessage());
883
            }
884
        }
885
        catch (MetacatException me) {
886 2260 sgarg
            if (result) {
887 2256 sgarg
                fail("Metacat Error:\n" + me.getMessage());
888 2260 sgarg
            } else {
889
                System.err.println("Metacat Error:\n" + me.getMessage());
890 2256 sgarg
            }
891
        }
892
        catch (Exception e) {
893
            fail("General exception:\n" + e.getMessage());
894
        }
895
896
        return response;
897 2239 sgarg
    }
898 2256 sgarg
899
    /**
900
     * Delete a document into metacat. The expected result is passed as result
901
     */
902 2260 sgarg
    private void deleteDocid(String docid, boolean result,
903
                             boolean expextedKarmaFailure) {
904 2256 sgarg
        try {
905
            String response = m.delete(docid);
906
            if (result) {
907
                assertTrue(response.indexOf("<success>") != -1);
908
            }
909
            else {
910
                assertTrue(response.indexOf("<success>") == -1);
911
            }
912
            System.err.println(response);
913
        }
914
        catch (MetacatInaccessibleException mie) {
915
            fail("Metacat Inaccessible:\n" + mie.getMessage());
916
        }
917
        catch (InsufficientKarmaException ike) {
918 2260 sgarg
            if(!expextedKarmaFailure){
919
                fail("Insufficient karma:\n" + ike.getMessage());
920
            }
921 2256 sgarg
        }
922
        catch (MetacatException me) {
923 2260 sgarg
            if (result) {
924
                fail("Metacat Error:\n" + me.getMessage());
925
            } else {
926
                System.err.println("Metacat Error:\n" + me.getMessage());
927
            }
928 2256 sgarg
        }
929
        catch (Exception e) {
930
            fail("General exception:\n" + e.getMessage());
931
        }
932 2239 sgarg
    }
933
934 2256 sgarg
    /**
935
     * Read a document from metacat. The expected result is passed as result
936
     */
937
    private void readDocid(String docid, boolean result,
938
                           boolean expextedKarmaFailure) {
939
        try {
940
            Reader r = m.read(docid);
941
            String response = IOUtil.getAsString(r, true);
942 2239 sgarg
943 2256 sgarg
            if (!result) {
944
                assertTrue(response.indexOf("<success>") == -1);
945
            }
946
            // System.err.println(response);
947
        }
948
        catch (MetacatInaccessibleException mie) {
949
            fail("Metacat Inaccessible:\n" + mie.getMessage());
950
        }
951
        catch (InsufficientKarmaException ike) {
952
            if (!expextedKarmaFailure) {
953
                fail("Insufficient karma:\n" + ike.getMessage());
954
            }
955
        }
956
        catch (MetacatException me) {
957
            fail("Metacat Error:\n" + me.getMessage());
958
        }
959
        catch (Exception e) {
960
            fail("General exception:\n" + e.getMessage());
961
        }
962 2239 sgarg
    }
963
964 2256 sgarg
    /**
965
     * Read a document from metacat and check if it is equal to a given string.
966
     * The expected result is passed as result
967
     */
968 2244 sgarg
969 2256 sgarg
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
970
                                         boolean result,
971
                                         boolean expextedKarmaFailure) {
972
        try {
973
            Reader r = m.read(docid);
974
            String doc = IOUtil.getAsString(r, true);
975
            if (result) {
976
977
                if (!testDoc.equals(doc)) {
978
                    System.out.println("doc    :" + doc);
979
                    System.out.println("testDoc:" + testDoc);
980
                }
981
982
                assertTrue(testDoc.equals(doc));
983
            }
984
            else {
985
                assertTrue(doc.indexOf("<error>") != -1);
986
            }
987
        }
988
        catch (MetacatInaccessibleException mie) {
989
            fail("Metacat Inaccessible:\n" + mie.getMessage());
990
        }
991
        catch (InsufficientKarmaException ike) {
992
            if (!expextedKarmaFailure) {
993
                fail("Insufficient karma:\n" + ike.getMessage());
994
            }
995
        }
996
        catch (MetacatException me) {
997
            fail("Metacat Error:\n" + me.getMessage());
998
        }
999
        catch (Exception e) {
1000
            fail("General exception:\n" + e.getMessage());
1001
        }
1002
1003 2239 sgarg
    }
1004
1005 2256 sgarg
    /**
1006
     * Create a hopefully unique docid for testing insert and update. Does
1007
     * not include the 'revision' part of the id.
1008
     *
1009
     * @return a String docid based on the current date and time
1010
     */
1011
    private String generateDocid() {
1012
        StringBuffer docid = new StringBuffer(prefix);
1013
        docid.append(".");
1014 2239 sgarg
1015 2256 sgarg
        // Create a calendar to get the date formatted properly
1016
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1017
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1018
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1019
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1020
                       2 * 60 * 60 * 1000);
1021
        Calendar calendar = new GregorianCalendar(pdt);
1022
        Date trialTime = new Date();
1023
        calendar.setTime(trialTime);
1024
        docid.append(calendar.get(Calendar.YEAR));
1025
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1026
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1027
        docid.append(calendar.get(Calendar.MINUTE));
1028
        docid.append(calendar.get(Calendar.SECOND));
1029 2239 sgarg
1030 2256 sgarg
        return docid.toString();
1031
    }
1032 2239 sgarg
}