Project

General

Profile

1
/**
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: tao $'
8
 *     '$Date: 2007-11-06 14:43:04 -0800 (Tue, 06 Nov 2007) $'
9
 * '$Revision: 3584 $'
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
import java.util.Vector;
36

    
37
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
38
import edu.ucsb.nceas.metacat.client.Metacat;
39
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
40
import edu.ucsb.nceas.metacat.client.MetacatException;
41
import edu.ucsb.nceas.metacat.client.MetacatFactory;
42
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
43
import edu.ucsb.nceas.utilities.IOUtil;
44
import junit.framework.Test;
45
import junit.framework.TestCase;
46
import junit.framework.TestSuite;
47
import java.io.File;
48

    
49
/**
50
 * A JUnit test for testing Access Control in Metacat
51
 */
52
public class AccessControlTest
53
    extends TestCase {
54

    
55
    private String metacatUrl = "@systemidserver@@servlet-path@";
56
    private String username = "@mcuser@";
57
    private String password = "@mcpassword@";
58
    private String anotheruser = "@mcanotheruser@";
59
    private String anotherpassword = "@mcanotherpassword@";
60
    private String prefix = "test";
61
    private String newdocid = null;
62
    private String testdocument = "";
63
    private String onlineDocid;
64
    private String onlinetestdatafile1 = "test/onlineDataFile1";
65
    private String onlinetestdatafile2 = "test/onlineDataFile2";
66
    private static final String ALLOWFIRST = "allowFirst";
67
    private static final String DENYFIRST = "denyFirst";
68

    
69
    private Metacat m;
70

    
71
    private boolean SUCCESS = true;
72
    private boolean FAILURE = false;
73

    
74
    /**
75
     * These variables are for eml-2.0.1 only. For other eml versions,
76
     * this function might have to modified
77
     */
78

    
79
    private String testEml_201_Header =
80
        "<?xml version=\"1.0\"?><eml:eml" +
81
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
82
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
83
        " packageId=\"eml.1.1\" system=\"knb\"" +
84
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
85
        " scope=\"system\">";
86

    
87
    private String testEml_200_Header =
88
    "<?xml version=\"1.0\"?><eml:eml" +
89
    " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"" +
90
    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
91
    " packageId=\"eml.1.1\" system=\"knb\"" +
92
    " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\"" +
93
    " scope=\"system\">";
94

    
95
    private String testEmlCreatorBlock =
96
        "<creator scope=\"document\">                                       " +
97
        " <individualName>                                                  " +
98
        "    <surName>Smith</surName>                                       " +
99
        " </individualName>                                                 " +
100
        "</creator>                                                         ";
101

    
102
    private String testEmlContactBlock =
103
        "<contact scope=\"document\">                                       " +
104
        " <individualName>                                                  " +
105
        "    <surName>Jackson</surName>                                     " +
106
        " </individualName>                                                 " +
107
        "</contact>                                                         ";
108

    
109
    private String testEmlInlineBlock1 =
110
        "<inline>                                                           " +
111
        "  <admin>                                                          " +
112
        "    <contact>                                                      " +
113
        "      <name>Operator</name>                                        " +
114
        "      <institution>PSI</institution>                               " +
115
        "    </contact>                                                     " +
116
        "  </admin>                                                         " +
117
        "</inline>                                                          ";
118

    
119
    private String testEmlInlineBlock2 =
120
        "<inline>                                                           " +
121
        "  <instrument>                                                     " +
122
        "    <instName>LCQ</instName>                                       " +
123
        "    <source type=\"ESI\"></source>                                 " +
124
        "    <detector type=\"EM\"></detector>                              " +
125
        "  </instrument>                                                    " +
126
        "</inline>                                                          ";
127

    
128
    /*
129
     * Retrus an access block base on params passed and the defaul perm order - allow first
130
     */
131
    private String getAccessBlock(String principal, boolean grantAccess,
132
                              boolean read, boolean write,
133
                           boolean changePermission, boolean all)
134
    {
135
    	return getAccessBlock(principal,  grantAccess,
136
                read, write,
137
                changePermission, all, ALLOWFIRST);
138
    }
139
    /**
140
     * This function returns an access block based on the params passed
141
     */
142
    private String getAccessBlock(String principal, boolean grantAccess,
143
                                  boolean read, boolean write,
144
                                  boolean changePermission, boolean all, String permOrder) {
145
        String accessBlock = "<access " +
146
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
147
            " order=\""+
148
            permOrder +"\""+
149
            " scope=\"document\""  +
150
            ">";
151

    
152
        accessBlock += generateOneAccessRule(principal,  grantAccess,
153
                                  read, write, changePermission, all);
154
        accessBlock += "</access>";
155

    
156
        return accessBlock;
157

    
158
    }
159
    
160
    /*
161
     * Gets eml access block base on given acccess rules and perm order
162
     */
163
    private String  getAccessBlock(Vector accessRules, String permOrder)
164
    {
165
    	String accessBlock = "<access " +
166
        "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
167
        " order=\""+
168
        permOrder +"\""+
169
        " scope=\"document\""  +
170
        ">";
171
    	// adding rules
172
    	if (accessRules != null  && !accessRules.isEmpty())
173
    	{
174
    		for (int i=0; i<accessRules.size(); i++)
175
    		{
176
    			String rule = (String)accessRules.elementAt(i);
177
    			accessBlock += rule;
178
    			
179
    		}		
180
    	}
181
    	accessBlock += "</access>";
182
    	return accessBlock;
183
    }
184
    
185
    /*
186
     * Generates a access rule for given parameter. Note this xml portion doesn't include
187
     * <access></access> 
188
     */
189
    private String generateOneAccessRule(String principal, boolean grantAccess,
190
            boolean read, boolean write, boolean changePermission, boolean all)
191
    {
192
    	 String accessBlock = "";
193

    
194
	     if (grantAccess) {
195
	         accessBlock = "<allow>";
196
	     }
197
	     else {
198
	         accessBlock = "<deny>";
199
	     }
200
	
201
	     accessBlock = accessBlock + "<principal>" + principal + "</principal>";
202
	
203
	     if (all) {
204
	         accessBlock += "<permission>all</permission>";
205
	     }
206
	     else {
207
	         if (read) {
208
	             accessBlock += "<permission>read</permission>";
209
	         }
210
	         if (write) {
211
	             accessBlock += "<permission>write</permission>";
212
	         }
213
	         if (changePermission) {
214
	             accessBlock += "<permission>changePermission</permission>";
215
	         }
216
	     }
217
	
218
	     if (grantAccess) {
219
	         accessBlock += "</allow>";
220
	     }
221
	     else {
222
	         accessBlock += "</deny>";
223
	     }
224
	     return accessBlock;
225

    
226
    }
227

    
228
    /**
229
     * This function returns a valid eml document with no access rules
230
     * This function is for eml-2.0.1 only. For other eml versions,
231
     * this function might have to modified
232
     */
233
    private String getTestEmlDoc(String title, String inlineData1,
234
                                 String inlineData2, String onlineUrl1,
235
                                 String onlineUrl2, String docAccessBlock,
236
                                 String inlineAccessBlock1,
237
                                 String inlineAccessBlock2,
238
                                 String onlineAccessBlock1,
239
                                 String onlineAccessBlock2) {
240

    
241
        String testDocument = "";
242
        testDocument = testDocument + testEml_201_Header +
243
            "<dataset scope=\"document\"><title>" + title + "</title>" +
244
            testEmlCreatorBlock;
245

    
246
        if (inlineData1 != null) {
247
            testDocument = testDocument
248
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
249
                + inlineData1 + "</distribution>";
250
        }
251
        if (inlineData2 != null) {
252
            testDocument = testDocument
253
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
254
                + inlineData2 + "</distribution>";
255
        }
256
        if (onlineUrl1 != null) {
257
            testDocument = testDocument
258
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
259
                + "<online><url function=\"download\">"
260
                + onlineUrl1 + "</url></online></distribution>";
261
        }
262
        if (onlineUrl2 != null) {
263
            testDocument = testDocument +
264
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
265
                + "<online><url function=\"download\">"
266
                + onlineUrl2 + "</url></online></distribution>";
267
        }
268
        testDocument += testEmlContactBlock;
269

    
270
        if (docAccessBlock != null) {
271
            testDocument += docAccessBlock;
272
        }
273

    
274
        testDocument += "</dataset>";
275

    
276
        if (inlineAccessBlock1 != null) {
277
            testDocument += "<additionalMetadata>";
278
            testDocument += "<describes>inlineEntity1</describes>";
279
            testDocument += inlineAccessBlock1;
280
            testDocument += "</additionalMetadata>";
281
        }
282

    
283
        if (inlineAccessBlock2 != null) {
284
            testDocument += "<additionalMetadata>";
285
            testDocument += "<describes>inlineEntity2</describes>";
286
            testDocument += inlineAccessBlock2;
287
            testDocument += "</additionalMetadata>";
288
        }
289

    
290
        if (onlineAccessBlock1 != null) {
291
            testDocument += "<additionalMetadata>";
292
            testDocument += "<describes>onlineEntity1</describes>";
293
            testDocument += onlineAccessBlock1;
294
            testDocument += "</additionalMetadata>";
295
        }
296

    
297
        if (onlineAccessBlock2 != null) {
298
            testDocument += "<additionalMetadata>";
299
            testDocument += "<describes>onlineEntity2</describes>";
300
            testDocument += onlineAccessBlock2;
301
            testDocument += "</additionalMetadata>";
302
        }
303

    
304
        testDocument += "</eml:eml>";
305

    
306
        //System.out.println("Returning following document" + testDocument);
307
        return testDocument;
308
    }
309

    
310
    /**
311
     * Constructor to build the test
312
     *
313
     * @param name the name of the test method
314
     */
315
    public AccessControlTest(String name) {
316
        super(name);
317
        newdocid = generateDocid();
318
    }
319

    
320
    /**
321
     * Establish a testing framework by initializing appropriate objects
322
     */
323
    public void setUp() {
324
        try {
325
            System.err.println("Test Metacat: " + metacatUrl);
326
            m = MetacatFactory.createMetacatConnection(metacatUrl);
327
        }
328
        catch (MetacatInaccessibleException mie) {
329
            System.err.println("Metacat is: " + metacatUrl);
330
            fail("Metacat connection failed." + mie.getMessage());
331
        }
332
    }
333

    
334
    /**
335
     * Release any objects after tests are complete
336
     */
337
    public void tearDown() {
338
    }
339

    
340
    /**
341
     * Create a suite of tests to be run together
342
     */
343
    public static Test suite() {
344
        TestSuite suite = new TestSuite();
345
        suite.addTest(new AccessControlTest("initialize"));
346
        // Test basic functions
347
        suite.addTest(new AccessControlTest("documentTest"));
348
        suite.addTest(new AccessControlTest("AccessControlTestForPublic"));
349
        suite.addTest(new AccessControlTest("testAllowFirst"));
350
        suite.addTest(new AccessControlTest("testDenyFirst"));
351

    
352
        return suite;
353
    }
354

    
355
    /**
356
     * Run an initial test that always passes to check that the test
357
     * harness is working.
358
     */
359
    public void initialize() {
360
        assertTrue(1 == 1);
361
    }
362
    
363
    
364
    /**
365
     * Tests when permission order is allowFirst, the combination of allow and deny rules  affect 
366
     * user to read, update and delete a document. Here are test cases
367
     *1.An user inserts a document with access  rules (allowFirst) - allow READ rule for another user,
368
     *   deny  READ rule for public. 
369
     *      Another user reads this document - failure
370
     *      Another user updates this document(except access part) -failure
371
     *      Another user updates this document(access part) -failure
372
     *      Another user deletes this document - failure
373
     *2. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
374
     *    deny READ and WRITE rule for public. 
375
     *       Another user reads this document - failure
376
     *       Another user updates this document(except access part) -failure
377
     *       Another user updates this document(access part) -failure
378
     *       Another user deletes this document - failure
379
     *3. The user updates this documents with access rules (allowFirst) - allow ALL rule for another user,
380
     *     deny ALL rule for public. 
381
     *         Another user reads this document - failure
382
     *         Another user updates this document(except access part) -failure
383
     *         Another user updates this document(access part) -failure
384
     *         Another user deletes this document - failure
385
     *4. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
386
     *    deny  WRITE rule for public. 
387
     *       Another user reads this document - success
388
     *       Another user updates this document(except access part) -failure
389
     *       Another user updates this document(access part) -failure
390
     *       Another user deletes this document - failure
391
     *5. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
392
     *    deny READ rule for public. 
393
     *       Another user reads this document - failure
394
     *       Another user updates this document(except access part) - success
395
     *       Another user updates this document(access part) -failure
396
     *       Another user deletes this document - failure
397
     *6. The user updates this documents with access rules (allowFirst) - allow READ rule for another user,
398
     *     deny READ rule for a group (which another user is in the group)
399
     *         Another user reads this document - failure
400
     *         Another user updates this document(except access part) -failure
401
     *         Another user updates this document(access part) -failure
402
     *         Another user deletes this document - failure
403
     *7. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
404
     *      deny READ and WRITE rule for a group (which another user is in the group)
405
     *         Another user reads this document - failure
406
     *         Another user updates this document(except access part) -failure
407
     *         Another user updates this document(access part) -failure
408
     *         Another user deletes this document - failure
409
     *8. The user updates this documents with access rules (allowFirst) - allow ALL rule for another user,
410
     *     deny ALL rule for a group (which another user is in the group)
411
     *          Another user reads this document - failure
412
     *          Another user updates this document(except access part) -failure
413
     *          Another user updates this document(access part) -failure
414
     *          Another user deletes this document - failure
415
     *9. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
416
     *     deny WRITE rule for a group (which another user is in the group)
417
     *          Another user reads this document - success
418
     *          Another user updates this document(except access part) -failure
419
     *          Another user updates this document(access part) -failure
420
     *          Another user deletes this document - failure
421
     *10. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
422
     *     deny READ rule for a group (which another user is in the group)
423
     *          Another user reads this document - failure
424
     *          Another user updates this document(except access part) - success
425
     *          Another user updates this document(access part) -failure
426
     *          Another user deletes this document - failure
427
     */
428
    public void testAllowFirst()
429
    {
430
    	try {
431
	    	newdocid = generateDocid();	        
432
	        //====1 inserts a document with access  rules (allowFirst) - allow READ rule for another user,
433
	        // deny  READ rule for public.
434
	        String accessRule1 = generateOneAccessRule(anotheruser, true,
435
                    true, false, false, false);
436
	        String accessRule2 = generateOneAccessRule("public", false,
437
                    true, false, false, false);
438
	        Vector accessRules = new Vector();
439
	        accessRules.add(accessRule1);
440
	        accessRules.add(accessRule2);
441
	        String access = getAccessBlock(accessRules, ALLOWFIRST);
442
	        System.out.println("the access part is "+access);
443
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
444
                    null, null, null,
445
                    null, access ,
446
                    null, null, null, null);
447
	        // login
448
	        m.login(username, password);
449
	        insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
450
	        m.logout();
451
	        //login as another user
452
	        m.login(anotheruser, anotherpassword);
453
	        //fails to read this document
454
	        readDocidWhichEqualsDoc(newdocid + ".1", testdocument, FAILURE, true);
455
	        //fails to update this document
456
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
457
            //fails to update access part
458
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
459
                    null, null, null,
460
                    null,  getAccessBlock(anotheruser, true,
461
                            true, false, false, false),
462
                    null, null, null, null);
463
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
464
            //fails to delete the document
465
            deleteDocid(newdocid + ".1", FAILURE, true);
466
            //logout
467
            m.logout();
468
            
469
            //====2 inserts a document with access  rules (allowFirst) - allow READ and WRITE rule for another user,
470
	        // deny  READ and WRITE rule for public.
471
	        accessRule1 = generateOneAccessRule(anotheruser, true,
472
                    true, true, false, false);
473
	        accessRule2 = generateOneAccessRule("public", false,
474
                    true, true, false, false);
475
	        accessRules = new Vector();
476
	        accessRules.add(accessRule1);
477
	        accessRules.add(accessRule2);
478
	        access = getAccessBlock(accessRules, ALLOWFIRST);
479
	        System.out.println("the access part is "+access);
480
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
481
                    null, null, null,
482
                    null, access ,
483
                    null, null, null, null);
484
	        // login
485
	        m.login(username, password);
486
	        updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
487
	        m.logout();
488
	        //login as another user
489
	        m.login(anotheruser, anotherpassword);
490
	        //fails to read this document
491
	        readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
492
	        //fails to update this document
493
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
494
            //fails to update access part
495
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
496
                    null, null, null,
497
                    null,  getAccessBlock(anotheruser, true,
498
                            true, false, false, false),
499
                    null, null, null, null);
500
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
501
            //fails to delete the document
502
            deleteDocid(newdocid + ".2", FAILURE, true);
503
            //logout
504
            m.logout();
505
            
506
             //====3 inserts a document with access  rules (allowFirst) - allow ALL rule for another user,
507
	        // deny  ALL rule for public.
508
	        accessRule1 = generateOneAccessRule(anotheruser, true,
509
                    true, true, true, true);
510
	        accessRule2 = generateOneAccessRule("public", false,
511
                    true, true, true, true);
512
	        accessRules = new Vector();
513
	        accessRules.add(accessRule1);
514
	        accessRules.add(accessRule2);
515
	        access = getAccessBlock(accessRules, ALLOWFIRST);
516
	        System.out.println("the access part is "+access);
517
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
518
                    null, null, null,
519
                    null, access ,
520
                    null, null, null, null);
521
	        // login
522
	        m.login(username, password);
523
	        updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
524
	        m.logout();
525
	        //login as another user
526
	        m.login(anotheruser, anotherpassword);
527
	        //fails to read this document
528
	        readDocidWhichEqualsDoc(newdocid + ".3", testdocument, FAILURE, true);
529
	        //fails to update this document
530
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
531
            //fails to update access part
532
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
533
                    null, null, null,
534
                    null,  getAccessBlock(anotheruser, true,
535
                            true, false, false, false),
536
                    null, null, null, null);
537
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
538
            //fails to delete the document
539
            deleteDocid(newdocid + ".3", FAILURE, true);
540
            //logout
541
            m.logout();
542
            
543
            
544
            
545
            
546
            
547
             //  ====4 The user updates this documents with access rules (allowFirst) - allow READ and WRITE 
548
            //rule for another user, deny  WRITE rule for public. 
549
	        accessRule1 = generateOneAccessRule(anotheruser, true,
550
                    true, true, false, false);
551
	        accessRule2 = generateOneAccessRule("public", false, false, true, false, false);
552
	        accessRules = new Vector();
553
	        accessRules.add(accessRule1);
554
	        accessRules.add(accessRule2);
555
	        access = getAccessBlock(accessRules, ALLOWFIRST);
556
	        System.out.println("the access part is "+access);
557
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
558
                    null, null, null,
559
                    null, access ,
560
                    null, null, null, null);
561
	        // login
562
	        m.login(username, password);
563
	        updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
564
	        m.logout();
565
	        //login as another user
566
	        m.login(anotheruser, anotherpassword);
567
	        //fails to read this document
568
	        readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
569
	        //fails to update this document
570
            updateDocid(newdocid + ".5", testdocument, FAILURE, true);
571
            //fails to update access part
572
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
573
                    null, null, null,
574
                    null,  getAccessBlock(anotheruser, true,
575
                            true, false, false, false),
576
                    null, null, null, null);
577
            updateDocid(newdocid + ".5", testdocument, FAILURE, true);
578
            //fails to delete the document
579
            deleteDocid(newdocid + ".4", FAILURE, true);
580
            //logout
581
            m.logout();
582
	        
583
            
584
            //  ====5. The user updates this documents with access rules (allowFirst) - allow READ and WRITE
585
            //rule for another user, deny READ rule for public. 
586
	        accessRule1 = generateOneAccessRule(anotheruser, true,
587
                    true, true, false, false);
588
	        accessRule2 = generateOneAccessRule("public", false, true, false, false, false);
589
	        accessRules = new Vector();
590
	        accessRules.add(accessRule1);
591
	        accessRules.add(accessRule2);
592
	        access = getAccessBlock(accessRules, ALLOWFIRST);
593
	        System.out.println("the access part is "+access);
594
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
595
                    null, null, null,
596
                    null, access ,
597
                    null, null, null, null);
598
	        // login
599
	        m.login(username, password);
600
	        updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
601
	        m.logout();
602
	        //login as another user
603
	        m.login(anotheruser, anotherpassword);
604
	        //fails to read this document
605
	        readDocidWhichEqualsDoc(newdocid + ".5", testdocument, FAILURE, true);
606
	        //fails to update this document
607
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
608
            //fails to update access part
609
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
610
                    null, null, null,
611
                    null,  getAccessBlock(anotheruser, true,
612
                            true, false, false, false),
613
                    null, null, null, null);
614
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
615
            //fails to delete the document
616
            deleteDocid(newdocid + ".6", FAILURE, true);
617
            //logout
618
            m.logout();
619
            
620
            
621
            
622
             //   ====6 inserts a document with access  rules (allowFirst) - allow READ rule for another user,
623
	        // deny  READ rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and another user is 
624
            // in this group).
625
	        accessRule1 = generateOneAccessRule(anotheruser, true,
626
                    true, false, false, false);
627
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
628
                   true, false, false, false);
629
	        accessRules = new Vector();
630
	        accessRules.add(accessRule1);
631
	        accessRules.add(accessRule2);
632
	        access = getAccessBlock(accessRules, ALLOWFIRST);
633
	        System.out.println("the access part is "+access);
634
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
635
                    null, null, null,
636
                    null, access ,
637
                    null, null, null, null);
638
	        // login
639
	        m.login(username, password);
640
	        updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
641
	        m.logout();
642
	        //login as another user
643
	        m.login(anotheruser, anotherpassword);
644
	        //fails to read this document
645
	        readDocidWhichEqualsDoc(newdocid + ".7", testdocument, FAILURE, true);
646
	        //fails to update this document
647
            updateDocid(newdocid + ".8", testdocument, FAILURE, true);
648
            //fails to update access part
649
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
650
                    null, null, null,
651
                    null,  getAccessBlock(anotheruser, true,
652
                            true, false, false, false),
653
                    null, null, null, null);
654
            updateDocid(newdocid + ".8", testdocument, FAILURE, true);
655
            //fails to delete the document
656
            deleteDocid(newdocid + ".7", FAILURE, true);
657
            //logout
658
            m.logout();
659
            
660
            //====7 inserts a document with access  rules (allowFirst) - allow READ and WRITE rule for another 
661
            //user, deny  READ and WRITE rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and
662
            // the other user is in this group)
663
	        accessRule1 = generateOneAccessRule(anotheruser, true,
664
                    true, true, false, false);
665
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
666
                    true, true, false, false);
667
	        accessRules = new Vector();
668
	        accessRules.add(accessRule1);
669
	        accessRules.add(accessRule2);
670
	        access = getAccessBlock(accessRules, ALLOWFIRST);
671
	        System.out.println("the access part is "+access);
672
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
673
                    null, null, null,
674
                    null, access ,
675
                    null, null, null, null);
676
	        // login
677
	        m.login(username, password);
678
	        updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
679
	        m.logout();
680
	        //login as another user
681
	        m.login(anotheruser, anotherpassword);
682
	        //fails to read this document
683
	        readDocidWhichEqualsDoc(newdocid + ".8", testdocument, FAILURE, true);
684
	        //fails to update this document
685
            updateDocid(newdocid + ".9", testdocument, FAILURE, true);
686
            //fails to update access part
687
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
688
                    null, null, null,
689
                    null,  getAccessBlock(anotheruser, true,
690
                            true, false, false, false),
691
                    null, null, null, null);
692
            updateDocid(newdocid + ".9", testdocument, FAILURE, true);
693
            //fails to delete the document
694
            deleteDocid(newdocid + ".8", FAILURE, true);
695
            //logout
696
            m.logout();
697
            
698
             //====8 inserts a document with access  rules (allowFirst) - allow ALL rule for another user,
699
	        // deny  ALL rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and the other user
700
            // is in this group)
701
	        accessRule1 = generateOneAccessRule(anotheruser, true,
702
                    true, true, true, true);
703
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
704
                    true, true, true, true);
705
	        accessRules = new Vector();
706
	        accessRules.add(accessRule1);
707
	        accessRules.add(accessRule2);
708
	        access = getAccessBlock(accessRules, ALLOWFIRST);
709
	        System.out.println("the access part is "+access);
710
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
711
                    null, null, null,
712
                    null, access ,
713
                    null, null, null, null);
714
	        // login
715
	        m.login(username, password);
716
	        updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
717
	        m.logout();
718
	        //login as another user
719
	        m.login(anotheruser, anotherpassword);
720
	        //fails to read this document
721
	        readDocidWhichEqualsDoc(newdocid + ".9", testdocument, FAILURE, true);
722
	        //fails to update this document
723
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
724
            //fails to update access part
725
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
726
                    null, null, null,
727
                    null,  getAccessBlock(anotheruser, true,
728
                            true, false, false, false),
729
                    null, null, null, null);
730
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
731
            //fails to delete the document
732
            deleteDocid(newdocid + ".9", FAILURE, true);
733
            //logout
734
            m.logout();
735
            
736
            
737
            
738
            
739
            
740
             //  ====9 The user updates this documents with access rules (allowFirst) - allow READ and WRITE 
741
            //rule for another user, deny  WRITE rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org
742
            // and another user is in this group). 
743
	        accessRule1 = generateOneAccessRule(anotheruser, true,
744
                    true, true, false, false);
745
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false, false, true, false, false);
746
	        accessRules = new Vector();
747
	        accessRules.add(accessRule1);
748
	        accessRules.add(accessRule2);
749
	        access = getAccessBlock(accessRules, ALLOWFIRST);
750
	        System.out.println("the access part is "+access);
751
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
752
                    null, null, null,
753
                    null, access ,
754
                    null, null, null, null);
755
	        // login
756
	        m.login(username, password);
757
	        updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
758
	        m.logout();
759
	        //login as another user
760
	        m.login(anotheruser, anotherpassword);
761
	        //succeed to read this document
762
	        readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
763
	        //fails to update this document
764
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
765
            //fails to update access part
766
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
767
                    null, null, null,
768
                    null,  getAccessBlock(anotheruser, true,
769
                            true, false, false, false),
770
                    null, null, null, null);
771
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
772
            //fails to delete the document
773
            deleteDocid(newdocid + ".10", FAILURE, true);
774
            //logout
775
            m.logout();
776
	        
777
            
778
            //  ====10. The user updates this documents with access rules (allowFirst) - allow READ and WRITE
779
            //rule for another user, deny READ rule for a group(cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and
780
            //another user is in this group). 
781
	        accessRule1 = generateOneAccessRule(anotheruser, true,
782
                    true, true, false, false);
783
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
784
                    true, false, false, false);
785
	        accessRules = new Vector();
786
	        accessRules.add(accessRule1);
787
	        accessRules.add(accessRule2);
788
	        access = getAccessBlock(accessRules, ALLOWFIRST);
789
	        System.out.println("the access part is "+access);
790
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
791
                    null, null, null,
792
                    null, access ,
793
                    null, null, null, null);
794
	        // login
795
	        m.login(username, password);
796
	        updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
797
	        m.logout();
798
	        //login as another user
799
	        m.login(anotheruser, anotherpassword);
800
	        //fails to read this document
801
	        readDocidWhichEqualsDoc(newdocid + ".11", testdocument, FAILURE, true);
802
	        //succeed to update this document
803
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
804
            //fails to update access part
805
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
806
                    null, null, null,
807
                    null,  getAccessBlock(anotheruser, true,
808
                            true, false, false, false),
809
                    null, null, null, null);
810
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
811
            //fails to delete the document
812
            deleteDocid(newdocid + ".12", FAILURE, true);
813
            //logout
814
            m.logout();
815
	    }
816
	    catch (MetacatAuthException mae) {
817
	        fail("Authorization failed:\n" + mae.getMessage());
818
	    }
819
	    catch (MetacatInaccessibleException mie) {
820
	        fail("Metacat Inaccessible:\n" + mie.getMessage());
821
	    }
822
	    catch (Exception e) {
823
	        fail("General exception:\n" + e.getMessage());
824
	    }
825
    }
826
    
827
    /**
828
     * Tests when permission order is denyFirst, the combination of allow and deny rules  affect 
829
     * user to read, update and delete a document. Here are test cases
830
     *1.An user inserts a document with access  rules (denyFirst) - allow READ rule for another user,
831
     *   deny  READ rule for public. 
832
     *      Another user reads this document - success
833
     *      Another user updates this document(except access part) -failure
834
     *      Another user updates this document(access part) -failure
835
     *      Another user deletes this document - failure
836
     *2. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
837
     *    deny READ and WRITE rule for public. 
838
     *       Another user reads this document - success
839
     *       Another user updates this document(except access part) -success
840
     *       Another user updates this document(access part) -failure
841
     *       Another user deletes this document - failure
842
     *3. The user updates this documents with access rules (denyFirst) - allow ALL rule for another user,
843
     *     deny ALL rule for public. 
844
     *         Another user reads this document - success
845
     *         Another user updates this document(except access part) -success
846
     *         Another user updates this document(access part) -success
847
     *         Another user deletes this document - success
848
     *4. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
849
     *    deny  WRITE rule for public. 
850
     *       Another user reads this document - success
851
     *       Another user updates this document(except access part) -success
852
     *       Another user updates this document(access part) -failure
853
     *       Another user deletes this document - failure
854
     *5. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
855
     *    deny READ rule for public. 
856
     *       Another user reads this document - success
857
     *       Another user updates this document(except access part) - success
858
     *       Another user updates this document(access part) -failure
859
     *       Another user deletes this document - failure
860
     *6. The user updates this documents with access rules (denyFirst) - allow READ rule for another user,
861
     *     deny READ rule for a group (which another user is in the group)
862
     *         Another user reads this document - success
863
     *         Another user updates this document(except access part) -failure
864
     *         Another user updates this document(access part) -failure
865
     *         Another user deletes this document - failure
866
     *7. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
867
     *      deny READ and WRITE rule for a group (which another user is in the group)
868
     *         Another user reads this document - success
869
     *         Another user updates this document(except access part) - success
870
     *         Another user updates this document(access part) -failure
871
     *         Another user deletes this document - failure
872
     *8. The user updates this documents with access rules (denyFirst) - allow ALL rule for another user,
873
     *     deny ALL rule for a group (which another user is in the group)
874
     *          Another user reads this document - success
875
     *          Another user updates this document(except access part) - success
876
     *          Another user updates this document(access part) - success
877
     *          Another user deletes this document - success
878
     *9. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
879
     *     deny WRITE rule for a group (which another user is in the group)
880
     *          Another user reads this document - success
881
     *          Another user updates this document(except access part) - success
882
     *          Another user updates this document(access part) - failure
883
     *          Another user deletes this document - failure
884
     *10. The user updates this documents with access rules (denyFirst) - allow READ and WRITE rule for another user,
885
     *     deny READ rule for a group (which another user is in the group)
886
     *          Another user reads this document - success
887
     *          Another user updates this document(except access part) - success
888
     *          Another user updates this document(access part) -failure
889
     *          Another user deletes this document - failure
890
     */
891
    public void testDenyFirst()
892
    {
893
    	try {
894
	    	newdocid = generateDocid();	        
895
	        //====1 inserts a document with access  rules (denyFirst) - allow READ rule for another user,
896
	        // deny  READ rule for public.
897
	        String accessRule1 = generateOneAccessRule(anotheruser, true,
898
                    true, false, false, false);
899
	        String accessRule2 = generateOneAccessRule("public", false,
900
                    true, false, false, false);
901
	        Vector accessRules = new Vector();
902
	        accessRules.add(accessRule1);
903
	        accessRules.add(accessRule2);
904
	        String access = getAccessBlock(accessRules, DENYFIRST);
905
	        System.out.println("the access part is "+access);
906
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
907
                    null, null, null,
908
                    null, access ,
909
                    null, null, null, null);
910
	        // login
911
	        m.login(username, password);
912
	        insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
913
	        m.logout();
914
	        //login as another user
915
	        m.login(anotheruser, anotherpassword);
916
	        //succeeds to read this document
917
	        readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS, false);
918
	        //fails to update this document
919
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
920
            //fails to update access part
921
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
922
                    null, null, null,
923
                    null,  getAccessBlock(anotheruser, true,
924
                            true, false, false, false),
925
                    null, null, null, null);
926
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
927
            //fails to delete the document
928
            deleteDocid(newdocid + ".1", FAILURE, true);
929
            //logout
930
            m.logout();
931
            
932
            //====2 inserts a document with access  rules (denyFirst) - allow READ and WRITE rule for another user,
933
	        // deny  READ and WRITE rule for public.
934
	        accessRule1 = generateOneAccessRule(anotheruser, true,
935
                    true, true, false, false);
936
	        accessRule2 = generateOneAccessRule("public", false,
937
                    true, true, false, false);
938
	        accessRules = new Vector();
939
	        accessRules.add(accessRule1);
940
	        accessRules.add(accessRule2);
941
	        access = getAccessBlock(accessRules, DENYFIRST);
942
	        System.out.println("the access part is "+access);
943
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
944
                    null, null, null,
945
                    null, access ,
946
                    null, null, null, null);
947
	        // login
948
	        m.login(username, password);
949
	        updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
950
	        m.logout();
951
	        //login as another user
952
	        m.login(anotheruser, anotherpassword);
953
	        //succeeds to read this document
954
	        readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, false);
955
	        //succeeds to update this document
956
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
957
            //fails to update access part
958
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
959
                    null, null, null,
960
                    null,  getAccessBlock(anotheruser, true,
961
                            true, false, false, false),
962
                    null, null, null, null);
963
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
964
            //fails to delete the document
965
            deleteDocid(newdocid + ".3", FAILURE, true);
966
            //logout
967
            m.logout();
968
            
969
             //====3 inserts a document with access  rules (denyFirst) - allow ALL rule for another user,
970
	        // deny  ALL rule for public.
971
	        accessRule1 = generateOneAccessRule(anotheruser, true,
972
                    true, true, true, true);
973
	        accessRule2 = generateOneAccessRule("public", false,
974
                    true, true, true, true);
975
	        accessRules = new Vector();
976
	        accessRules.add(accessRule1);
977
	        accessRules.add(accessRule2);
978
	        access = getAccessBlock(accessRules, DENYFIRST);
979
	        System.out.println("the access part is "+access);
980
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
981
                    null, null, null,
982
                    null, access ,
983
                    null, null, null, null);
984
	        // login
985
	        m.login(username, password);
986
	        updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
987
	        m.logout();
988
	        //login as another user
989
	        m.login(anotheruser, anotherpassword);
990
	        //succeeds to read this document
991
	        readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
992
	        //succeeds to update this document
993
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
994
            //succeed to update access part
995
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
996
                    null, null, null,
997
                    null,  getAccessBlock(anotheruser, true, true, true, true, true),
998
                    null, null, null, null);
999
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1000
            //succeeds to delete the document
1001
            deleteDocid(newdocid + ".6", SUCCESS, false);
1002
            //logout
1003
            m.logout();
1004
            
1005
            
1006
            
1007
            
1008
            newdocid = generateDocid();
1009
             //  ====4 The user updates this documents with access rules (denyFirst) - allow READ and WRITE 
1010
            //rule for another user, deny  WRITE rule for public. 
1011
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1012
                    true, true, false, false);
1013
	        accessRule2 = generateOneAccessRule("public", false, false, true, false, false);
1014
	        accessRules = new Vector();
1015
	        accessRules.add(accessRule1);
1016
	        accessRules.add(accessRule2);
1017
	        access = getAccessBlock(accessRules, DENYFIRST);
1018
	        System.out.println("the access part is "+access);
1019
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1020
                    null, null, null,
1021
                    null, access ,
1022
                    null, null, null, null);
1023
	        // login
1024
	        m.login(username, password);
1025
	        insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1026
	        m.logout();
1027
	        //login as another user
1028
	        m.login(anotheruser, anotherpassword);
1029
	        //succeeds to read this document
1030
	        readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS, false);
1031
	        //succeeds to update this document
1032
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1033
            //fails to update access part
1034
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1035
                    null, null, null,
1036
                    null,  getAccessBlock(anotheruser, true,
1037
                            true, false, false, false),
1038
                    null, null, null, null);
1039
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
1040
            //fails to delete the document
1041
            deleteDocid(newdocid + ".2", FAILURE, true);
1042
            //logout
1043
            m.logout();
1044
	        
1045
            
1046
            //  ====5. The user updates this documents with access rules (allowFirst) - allow READ and WRITE
1047
            //rule for another user, deny READ rule for public. 
1048
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1049
                    true, true, false, false);
1050
	        accessRule2 = generateOneAccessRule("public", false, true, false, false, false);
1051
	        accessRules = new Vector();
1052
	        accessRules.add(accessRule1);
1053
	        accessRules.add(accessRule2);
1054
	        access = getAccessBlock(accessRules, DENYFIRST);
1055
	        System.out.println("the access part is "+access);
1056
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1057
                    null, null, null,
1058
                    null, access ,
1059
                    null, null, null, null);
1060
	        // login
1061
	        m.login(username, password);
1062
	        updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
1063
	        m.logout();
1064
	        //login as another user
1065
	        m.login(anotheruser, anotherpassword);
1066
	        //succeeds to read this document
1067
	        readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1068
	        //succeed to update this document
1069
            updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
1070
            //fails to update access part
1071
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1072
                    null, null, null,
1073
                    null,  getAccessBlock(anotheruser, true,
1074
                            true, false, false, false),
1075
                    null, null, null, null);
1076
            updateDocid(newdocid + ".12", testdocument, FAILURE, true);
1077
            //fails to delete the document
1078
            deleteDocid(newdocid + ".12", FAILURE, true);
1079
            //logout
1080
            m.logout();
1081
            
1082
            
1083
            
1084
             //   ====6 inserts a document with access  rules (denyFirst) - allow READ rule for another user,
1085
	        // deny  READ rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and another user is 
1086
            // in this group).
1087
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1088
                    true, false, false, false);
1089
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
1090
                   true, false, false, false);
1091
	        accessRules = new Vector();
1092
	        accessRules.add(accessRule1);
1093
	        accessRules.add(accessRule2);
1094
	        access = getAccessBlock(accessRules, DENYFIRST);
1095
	        System.out.println("the access part is "+access);
1096
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1097
                    null, null, null,
1098
                    null, access ,
1099
                    null, null, null, null);
1100
	        // login
1101
	        m.login(username, password);
1102
	        updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
1103
	        m.logout();
1104
	        //login as another user
1105
	        m.login(anotheruser, anotherpassword);
1106
	        //succeeds to read this document
1107
	        readDocidWhichEqualsDoc(newdocid + ".13", testdocument, SUCCESS, false);
1108
	        //fails to update this document
1109
            updateDocid(newdocid + ".14", testdocument, FAILURE, true);
1110
            //fails to update access part
1111
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1112
                    null, null, null,
1113
                    null,  getAccessBlock(anotheruser, true,
1114
                            true, false, false, false),
1115
                    null, null, null, null);
1116
            updateDocid(newdocid + ".14", testdocument, FAILURE, true);
1117
            //fails to delete the document
1118
            deleteDocid(newdocid + ".13", FAILURE, true);
1119
            //logout
1120
            m.logout();
1121
            
1122
            //====7 inserts a document with access  rules (allowFirst) - allow READ and WRITE rule for another 
1123
            //user, deny  READ and WRITE rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and
1124
            // the other user is in this group)
1125
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1126
                    true, true, false, false);
1127
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
1128
                    true, true, false, false);
1129
	        accessRules = new Vector();
1130
	        accessRules.add(accessRule1);
1131
	        accessRules.add(accessRule2);
1132
	        access = getAccessBlock(accessRules, DENYFIRST);
1133
	        System.out.println("the access part is "+access);
1134
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1135
                    null, null, null,
1136
                    null, access ,
1137
                    null, null, null, null);
1138
	        // login
1139
	        m.login(username, password);
1140
	        updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
1141
	        m.logout();
1142
	        //login as another user
1143
	        m.login(anotheruser, anotherpassword);
1144
	        //succeeds to read this document
1145
	        readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1146
	        //succeeds to update this document
1147
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
1148
            //fails to update access part
1149
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1150
                    null, null, null,
1151
                    null,  getAccessBlock(anotheruser, true,
1152
                            true, false, false, false),
1153
                    null, null, null, null);
1154
            updateDocid(newdocid + ".16", testdocument, FAILURE, true);
1155
            //fails to delete the document
1156
            deleteDocid(newdocid + ".15", FAILURE, true);
1157
            //logout
1158
            m.logout();
1159
            
1160
             //====8 inserts a document with access  rules (denyFirst) - allow ALL rule for another user,
1161
	        // deny  ALL rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and the other user
1162
            // is in this group)
1163
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1164
                    true, true, true, true);
1165
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
1166
                    true, true, true, true);
1167
	        accessRules = new Vector();
1168
	        accessRules.add(accessRule1);
1169
	        accessRules.add(accessRule2);
1170
	        access = getAccessBlock(accessRules, DENYFIRST);
1171
	        System.out.println("the access part is "+access);
1172
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1173
                    null, null, null,
1174
                    null, access ,
1175
                    null, null, null, null);
1176
	        // login
1177
	        m.login(username, password);
1178
	        updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
1179
	        m.logout();
1180
	        //login as another user
1181
	        m.login(anotheruser, anotherpassword);
1182
	        //succeeds to read this document
1183
	        readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
1184
	        //succeeds to update this document
1185
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
1186
            //succeeds to update access part
1187
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1188
                    null, null, null,
1189
                    null,  getAccessBlock(anotheruser, true,
1190
                            true, true, true, true),
1191
                    null, null, null, null);
1192
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
1193
            //succeeds to delete the document
1194
            deleteDocid(newdocid + ".18", SUCCESS, false);
1195
            //logout
1196
            m.logout();
1197
            
1198
            
1199
            
1200
            
1201
            newdocid = generateDocid();
1202
             //  ====9 The user updates this documents with access rules (denyFirst) - allow READ and WRITE 
1203
            //rule for another user, deny  WRITE rule for a group (cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org
1204
            // and another user is in this group). 
1205
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1206
                    true, true, false, false);
1207
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false, false, true, false, false);
1208
	        accessRules = new Vector();
1209
	        accessRules.add(accessRule1);
1210
	        accessRules.add(accessRule2);
1211
	        access = getAccessBlock(accessRules, DENYFIRST);
1212
	        System.out.println("the access part is "+access);
1213
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1214
                    null, null, null,
1215
                    null, access ,
1216
                    null, null, null, null);
1217
	        // login
1218
	        m.login(username, password);
1219
	        insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1220
	        m.logout();
1221
	        //login as another user
1222
	        m.login(anotheruser, anotherpassword);
1223
	        //succeed to read this document
1224
	        readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS, false);
1225
	        //succeeds to update this document
1226
            updateDocid(newdocid + ".20", testdocument, SUCCESS, false);
1227
            //fails to update access part
1228
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1229
                    null, null, null,
1230
                    null,  getAccessBlock(anotheruser, true,
1231
                            true, false, false, false),
1232
                    null, null, null, null);
1233
            updateDocid(newdocid + ".21", testdocument, FAILURE, true);
1234
            //fails to delete the document
1235
            deleteDocid(newdocid + ".20", FAILURE, true);
1236
            //logout
1237
            m.logout();
1238
	        
1239
            
1240
            //  ====10. The user updates this documents with access rules (denyFirst) - allow READ and WRITE
1241
            //rule for another user, deny READ rule for a group(cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org and
1242
            //another user is in this group). 
1243
	        accessRule1 = generateOneAccessRule(anotheruser, true,
1244
                    true, true, false, false);
1245
	        accessRule2 = generateOneAccessRule("cn=knb-usr,o=nceas,dc=ecoinformatics,dc=org", false,
1246
                    true, false, false, false);
1247
	        accessRules = new Vector();
1248
	        accessRules.add(accessRule1);
1249
	        accessRules.add(accessRule2);
1250
	        access = getAccessBlock(accessRules, DENYFIRST);
1251
	        System.out.println("the access part is "+access);
1252
	        testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1253
                    null, null, null,
1254
                    null, access ,
1255
                    null, null, null, null);
1256
	        // login
1257
	        m.login(username, password);
1258
	        updateDocid(newdocid + ".21", testdocument, SUCCESS, false);
1259
	        m.logout();
1260
	        //login as another user
1261
	        m.login(anotheruser, anotherpassword);
1262
	        //succeeds to read this document
1263
	        readDocidWhichEqualsDoc(newdocid + ".21", testdocument, SUCCESS, false);
1264
	        //succeeds to update this document
1265
            updateDocid(newdocid + ".22", testdocument, SUCCESS, false);
1266
            //fails to update access part
1267
            testdocument = getTestEmlDoc("Testing user can't not read, write and delete a document",
1268
                    null, null, null,
1269
                    null,  getAccessBlock(anotheruser, true,
1270
                            true, false, false, false),
1271
                    null, null, null, null);
1272
            updateDocid(newdocid + ".23", testdocument, FAILURE, true);
1273
            //fails to delete the document
1274
            deleteDocid(newdocid + ".22", FAILURE, true);
1275
            //logout
1276
            m.logout();
1277
	    }
1278
	    catch (MetacatAuthException mae) {
1279
	        fail("Authorization failed:\n" + mae.getMessage());
1280
	    }
1281
	    catch (MetacatInaccessibleException mie) {
1282
	        fail("Metacat Inaccessible:\n" + mie.getMessage());
1283
	    }
1284
	    catch (Exception e) {
1285
	        fail("General exception:\n" + e.getMessage());
1286
	    }
1287
    }
1288

    
1289
    /** *********
1290
     * Test the case when no access is specified and owner is logged in
1291
     * No online or inline data is involved
1292
     * -> an user inserts a document and is able to read it, update it,
1293
     *    set permissions on it and delete it
1294
     * -> another user is not able to do anything with the document
1295
     *    when no access is specified for that user
1296
     * -> test what all the other user can do when read only, write only and
1297
     *    change permissions only permissions are specified
1298
     *
1299
     */
1300
    public void documentTest() {
1301
        try {
1302

    
1303
            newdocid = generateDocid();
1304

    
1305
            // login
1306
            m.login(username, password);
1307

    
1308
            // insert a 2.0.0 document
1309
            testdocument = testEml_200_Header +
1310
                "<dataset scope=\"document\"><title>submitting eml2000</title>" +
1311
            testEmlCreatorBlock + testEmlContactBlock + "</dataset></eml:eml>";
1312

    
1313
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1314

    
1315
            // read the document
1316
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1317

    
1318
            // update it with 2.0.1 document
1319
            testdocument = getTestEmlDoc("Updating eml200 with eml201",
1320
                                         null, null, null, null,
1321
                                         null, null, null, null, null);
1322
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1323
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1324
            m.logout();
1325

    
1326

    
1327
            newdocid = generateDocid();
1328

    
1329
            // login
1330
            m.login(username, password);
1331

    
1332
            // insert a document
1333
            testdocument = getTestEmlDoc("Testing insert", null,
1334
                                         null, null,
1335
                                         null, null, null, null, null, null);
1336
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1337

    
1338
            // read the document
1339
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1340

    
1341
            // update the document
1342
            testdocument = getTestEmlDoc("Testing update", null, null, null,
1343
                                         null, null, null, null, null, null);
1344
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1345
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1346

    
1347
            /////////////////////////////
1348
            // check what the another user can do
1349
            m.logout();
1350
            m.login(anotheruser, anotherpassword);
1351

    
1352
            // check if the user mentioned is able to read/update/delete the document
1353
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
1354
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
1355
            deleteDocid(newdocid + ".2", FAILURE, true);
1356

    
1357
            /////////////////////////////
1358
            // update the document access control - read only
1359
            m.logout();
1360
            m.login(username, password);
1361

    
1362
            testdocument = getTestEmlDoc("Testing update access block",
1363
                                         null, null, null,
1364
                                         null, getAccessBlock(anotheruser, true,
1365
                                         true, false, false, false),
1366
                                         null, null, null, null);
1367
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1368
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1369

    
1370
            // check if the user mentioned is able to read the document
1371
            m.logout();
1372
            m.login(anotheruser, anotherpassword);
1373
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1374

    
1375
            // should not be able to update the document
1376
            testdocument = getTestEmlDoc("Testing update from another user",
1377
                                         null, null, null,
1378
                                         null, getAccessBlock(anotheruser, true,
1379
                                         true, false, false, false),
1380
                                         null, null, null, null);
1381
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1382

    
1383
            // or the permissions
1384
            testdocument = getTestEmlDoc("Testing update access block",
1385
                                         null, null, null,
1386
                                         null, getAccessBlock(anotheruser, true,
1387
                                         false, false, false, true),
1388
                                         null, null, null, null);
1389
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1390

    
1391
            // or delete the document
1392
            deleteDocid(newdocid + ".3", FAILURE, true);
1393

    
1394
            m.logout();
1395
            m.login(username, password);
1396

    
1397
            ///////////////////////////////////
1398
            // update the document access control - write only
1399
            testdocument = getTestEmlDoc("Testing update access block",
1400
                                         null, null, null,
1401
                                         null, getAccessBlock(anotheruser, true,
1402
                                         false, true, false, false),
1403
                                         null, null, null, null);
1404
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1405
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
1406
            //System.out.println(testdocument);
1407
            // check if the user mentioned is able to read the document
1408
            m.logout();
1409
            m.login(anotheruser, anotherpassword);
1410
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, true);
1411

    
1412
            // should be able to update the document
1413

    
1414
            //System.out.println(testdocument);
1415
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1416

    
1417
            // but not the permissions
1418
            testdocument = getTestEmlDoc("Testing update access block",
1419
                                         null, null, null,
1420
                                         null, getAccessBlock(anotheruser, true,
1421
                                         false, true, false, true),
1422
                                         null, null, null, null);
1423
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
1424

    
1425
            // try to delete the document
1426
            deleteDocid(newdocid + ".5", FAILURE, true);
1427
            //deleteDocid(newdocid + ".4", FAILURE, true);
1428

    
1429
            m.logout();
1430
            m.login(username, password);
1431

    
1432
            /////////////////////////////////
1433
            // update the document access control - change permissions only
1434
            testdocument = getTestEmlDoc("Testing update access block",
1435
                                         null, null, null,
1436
                                         null, getAccessBlock(anotheruser, true,
1437
                false, false, true, false),
1438
                                         null, null, null, null);
1439
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1440
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, false);
1441

    
1442
            // check if the user mentioned is able to read the document
1443
            m.logout();
1444
            m.login(anotheruser, anotherpassword);
1445
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, FAILURE, true);
1446

    
1447
            // should not be able to update the document
1448
            testdocument = getTestEmlDoc("Testing update from another user",
1449
                                         null, null, null,
1450
                                         null, getAccessBlock(anotheruser, true,
1451
                false, false, true, false),
1452
                                         null, null, null, null);
1453
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
1454

    
1455
            // but can chg the permissions
1456
            testdocument = getTestEmlDoc("Testing update access block",
1457
                                         null, null, null,
1458
                                         null, getAccessBlock(anotheruser, true,
1459
                                         false, false, false, true),
1460
                                         null, null, null, null);
1461
            // ERRRRRRRROR
1462
            //updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
1463

    
1464
            // try to delete the document
1465
            //deleteDocid(newdocid + ".7", FAILURE, true);
1466
            deleteDocid(newdocid + ".6", FAILURE, true);
1467

    
1468
            m.logout();
1469
            m.login(username, password);
1470

    
1471

    
1472
            /////////////////////////////////
1473
            // update the document access control - read & write
1474
            testdocument = getTestEmlDoc("Testing update access block",
1475
                                         null, null, null,
1476
                                         null, getAccessBlock(anotheruser, true,
1477
                                         true, true, false, false),
1478
                                         null, null, null, null);
1479
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
1480
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1481

    
1482
            // check if the user mentioned is able to read the document
1483
            m.logout();
1484
            m.login(anotheruser, anotherpassword);
1485
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1486

    
1487
            // should be able to update the document
1488
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
1489

    
1490
            // but cant chg the permissions
1491
            testdocument = getTestEmlDoc("Testing update access block",
1492
                                         null, null, null,
1493
                                         null, getAccessBlock(anotheruser, true,
1494
                                         false, false, false, true),
1495
                                         null, null, null, null);
1496
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
1497

    
1498
            // try to delete the document
1499
            deleteDocid(newdocid + ".9", FAILURE, true);
1500

    
1501
            m.logout();
1502
            m.login(username, password);
1503

    
1504

    
1505
            /////////////////////////////////
1506
            // update the document access control - read & change permissions
1507
            testdocument = getTestEmlDoc("Testing update access block",
1508
                                         null, null, null,
1509
                                         null, getAccessBlock(anotheruser, true,
1510
                                         true, false, true, false),
1511
                                         null, null, null, null);
1512
            updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
1513
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1514

    
1515
            // check if the user mentioned is able to read the document
1516
            m.logout();
1517
            m.login(anotheruser, anotherpassword);
1518
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1519
            // should not be able to update the document
1520
            testdocument = getTestEmlDoc("Testing update from another user",
1521
                                         null, null, null,
1522
                                         null, getAccessBlock(anotheruser, true,
1523
                                         false, false, true, false),
1524
                                         null, null, null, null);
1525
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
1526

    
1527
            // but can chg the permissions
1528
            testdocument = getTestEmlDoc("Testing update access block",
1529
                                         null, null, null,
1530
                                         null, getAccessBlock(anotheruser, true,
1531
                                         false, false, false, true),
1532
                                         null, null, null, null);
1533
            //updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
1534

    
1535
            // try to delete the document
1536
            // deleteDocid(newdocid + ".11", FAILURE, true);
1537
            deleteDocid(newdocid + ".10", FAILURE, true);
1538

    
1539
            m.logout();
1540
            m.login(username, password);
1541

    
1542

    
1543

    
1544
            /////////////////////////////////
1545
            // update the document access control - read & change permissions
1546
            testdocument = getTestEmlDoc("Testing update access block",
1547
                                         null, null, null,
1548
                                         null, getAccessBlock(anotheruser, true,
1549
                                         true, false, true, false),
1550
                                         null, null, null, null);
1551
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
1552
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument, SUCCESS, false);
1553

    
1554
            // check if the user mentioned is able to read the document
1555
            m.logout();
1556
            m.login(anotheruser, anotherpassword);
1557
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument,  SUCCESS, false);
1558

    
1559
            // should not be able to update the document
1560
            testdocument = getTestEmlDoc("Testing update from another user",
1561
                                         null, null, null,
1562
                                         null, getAccessBlock(anotheruser, true,
1563
                                         false, false, true, false),
1564
                                         null, null, null, null);
1565
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
1566
            // but can chg the permissions
1567
            testdocument = getTestEmlDoc("Testing update from another user",
1568
                                         null, null, null,
1569
                                         null, getAccessBlock(anotheruser, true,
1570
                                         false, false, false, true),
1571
                                         null, null, null, null);
1572
            // ERRRRRRRRRRRRRRRR
1573
            // updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
1574

    
1575
            // try to delete the document
1576
            //deleteDocid(newdocid + ".13", FAILURE, true);
1577
            deleteDocid(newdocid + ".12", FAILURE, true);
1578

    
1579
            m.logout();
1580
            m.login(username, password);
1581

    
1582

    
1583
            /////////////////////////////////
1584
            // update the document access control - R, W, CP
1585
            testdocument = getTestEmlDoc("Testing update access block",
1586
                                         null, null, null,
1587
                                         null, getAccessBlock(anotheruser, true,
1588
                                         true, true, true, false),
1589
                                         null, null, null, null);
1590
            updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
1591
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1592

    
1593
            // check if the user mentioned is able to read the document
1594
            m.logout();
1595
            m.login(anotheruser, anotherpassword);
1596
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1597

    
1598
            // should not be able to update the document
1599
            testdocument = getTestEmlDoc("Testing update from another user",
1600
                                         null, null, null,
1601
                                         null, getAccessBlock(anotheruser, true,
1602
                false, false, false, true),
1603
                                         null, null, null, null);
1604
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
1605
            readDocidWhichEqualsDoc(newdocid + ".15", testdocument, SUCCESS, false);
1606

    
1607
            // but can chg the permissions
1608
            testdocument = getTestEmlDoc("Testing update from another user",
1609
                                         null, null, null,
1610
                                         null, getAccessBlock(anotheruser, true,
1611
                                         true, false, false, false),
1612
                                         null, null, null, null);
1613
            updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
1614
            readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
1615

    
1616
            // try to delete the document
1617
            deleteDocid(newdocid + ".16", FAILURE, true);
1618

    
1619
            m.logout();
1620
            m.login(username, password);
1621

    
1622
            /////////////////////////////////
1623
            // update the document access control - all
1624
            testdocument = getTestEmlDoc("Testing update access block",
1625
                                         null, null, null,
1626
                                         null, getAccessBlock(anotheruser, true,
1627
                                         false, false, false, true),
1628
                                         null, null, null, null);
1629
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
1630
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
1631

    
1632
            // check if the user mentioned is able to read the document
1633
            m.logout();
1634
            m.login(anotheruser, anotherpassword);
1635
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
1636

    
1637
            // should not be able to update the document
1638
            testdocument = getTestEmlDoc("Testing update from another user",
1639
                                         null, null, null,
1640
                                         null, getAccessBlock(anotheruser, true,
1641
                                         false, false, false, true),
1642
                                         null, null, null, null);
1643
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
1644
            readDocidWhichEqualsDoc(newdocid + ".18", testdocument, SUCCESS, false);
1645

    
1646
            // but can chg the permissions
1647
            testdocument = getTestEmlDoc("Testing update from another user",
1648
                                         null, null, null,
1649
                                         null, getAccessBlock(anotheruser, true,
1650
                                         true, false, false, false),
1651
                                         null, null, null, null);
1652
            updateDocid(newdocid + ".19", testdocument, SUCCESS, false);
1653
            readDocidWhichEqualsDoc(newdocid + ".19", testdocument, SUCCESS, false);
1654

    
1655
            // try to delete the document
1656
            deleteDocid(newdocid + ".19", FAILURE, true);
1657

    
1658
            m.logout();
1659

    
1660
            // delete the document
1661
            m.login(username, password);
1662
            deleteDocid(newdocid + ".19", SUCCESS, false);
1663
            m.logout();
1664
        }
1665
        catch (MetacatAuthException mae) {
1666
            fail("Authorization failed:\n" + mae.getMessage());
1667
        }
1668
        catch (MetacatInaccessibleException mie) {
1669
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1670
        }
1671
        catch (Exception e) {
1672
            fail("General exception:\n" + e.getMessage());
1673
        }
1674

    
1675
    }
1676

    
1677

    
1678
   /** *********
1679
     * Test the case when no access is specified and public is logged in
1680
     * Cases being checked:
1681
     * 1. public tries to read the document - Failure.
1682
     * 2. public tries to update the document - Failure.
1683
     * 3. public tries to update the inline data - Failure.
1684
     * 4. public tries to update the online data file - Failure.
1685
     * 5. public tries to update the access rules for the document - Failure.
1686
     * 6. public tries to update the access rules for the inline data - Failure.
1687
     * 7. public tries to update the access rules for the online data - Failure.
1688
     * 8. public tries to delete the document - Failure.
1689
     */
1690
    public void AccessControlTestForPublic() {
1691
        try {
1692

    
1693
            String accessBlock = getAccessBlock(anotheruser, true,
1694
                                         true, false, false, false);
1695
            newdocid = generateDocid();
1696

    
1697
            // login
1698
            m.login(username, password);
1699

    
1700
            onlineDocid = generateDocid();
1701
            uploadDocid(onlineDocid + ".1",onlinetestdatafile1, SUCCESS, false);
1702

    
1703
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1704
                                     null,"ecogrid://knb/" + onlineDocid + ".1",
1705
                                     null, null, null, null, null, null);
1706

    
1707

    
1708
            // insert a document - get the docid
1709
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1710
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
1711
                                    false);
1712

    
1713
            // logoutand login as other user
1714
            m.logout();
1715

    
1716
            // read the document
1717
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, true);
1718

    
1719
            // update the document
1720
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1721

    
1722
            // update the inline data
1723
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock2,
1724
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1725
                         null, null, null, null, null, null);
1726
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1727

    
1728
            // update the online data
1729
            uploadDocid(onlineDocid + ".2", onlinetestdatafile1, FAILURE, false);
1730

    
1731
            // update the document access control
1732
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1733
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1734
                         null, accessBlock, null, null, null, null);
1735
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1736

    
1737
            // update the document access control for inline data
1738
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1739
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1740
                         null, null, accessBlock, null, null, null);
1741
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1742

    
1743
            // update the document access control for online data
1744
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1745
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1746
                         null, null, null, null, accessBlock, null);
1747
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1748

    
1749
            // delete the document
1750
            deleteDocid(newdocid + ".2", FAILURE, true);
1751
            m.logout();
1752
        }
1753
        catch (MetacatAuthException mae) {
1754
            fail("Authorization failed:\n" + mae.getMessage());
1755
        }
1756
        catch (MetacatInaccessibleException mie) {
1757
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1758
        }
1759
        catch (MetacatException me) {
1760
            fail("Metacat Error:\n" + me.getMessage());
1761
        }
1762
        catch (Exception e) {
1763
            fail("General exception:\n" + e.getMessage());
1764
        }
1765

    
1766
    }
1767

    
1768
    /**
1769
     * Insert a document into metacat. The expected result is passed as result
1770
     */
1771

    
1772
    private String insertDocid(String docid, String docText, boolean result,
1773
                               boolean expectKarmaException) {
1774
        String response = null;
1775
        try {
1776
            response = m.insert(docid,
1777
                                new StringReader(testdocument), null);
1778
            if (result) {
1779
                assertTrue( (response.indexOf("<success>") != -1));
1780
                assertTrue(response.indexOf(docid) != -1);
1781
            }
1782
            else {
1783
                assertTrue( (response.indexOf("<success>") == -1));
1784
            }
1785
            System.err.println(response);
1786
        }
1787
        catch (MetacatInaccessibleException mie) {
1788
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1789
        }
1790
        catch (InsufficientKarmaException ike) {
1791
            if (!expectKarmaException) {
1792
                fail("Insufficient karma:\n" + ike.getMessage());
1793
            }
1794
        }
1795
        catch (MetacatException me) {
1796
            fail("Metacat Error:\n" + me.getMessage());
1797
        }
1798
        catch (Exception e) {
1799
            fail("General exception:\n" + e.getMessage());
1800
        }
1801
        return response;
1802
    }
1803

    
1804
    /**
1805
     * Insert a document into metacat. The expected result is passed as result
1806
     */
1807

    
1808
    private String uploadDocid(String docid, String filePath, boolean result,
1809
                               boolean expectedKarmaException) {
1810
        String response = null;
1811
        try {
1812
            response = m.upload(docid, new File(filePath));
1813
            if (result) {
1814
                assertTrue( (response.indexOf("<success>") != -1));
1815
                assertTrue(response.indexOf(docid) != -1);
1816
            }
1817
            else {
1818
                assertTrue( (response.indexOf("<success>") == -1));
1819
            }
1820
            System.err.println("respose from metacat: " + response);
1821
        }
1822
        catch (MetacatInaccessibleException mie) {
1823
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1824
        }
1825
        catch (InsufficientKarmaException ike) {
1826
            if (!expectedKarmaException) {
1827
                fail("Insufficient karma:\n" + ike.getMessage());
1828
            }
1829
        }
1830
        catch (MetacatException me) {
1831
            if (result) {
1832
                fail("Metacat Error:\n" + me.getMessage());
1833
            } else {
1834
                System.err.println("Metacat Error:\n" + me.getMessage());
1835
            }
1836
        }
1837
        catch (Exception e) {
1838
            fail("General exception:\n" + e.getMessage());
1839
        }
1840
        return response;
1841
    }
1842

    
1843
    /**
1844
     * Update a document in metacat. The expected result is passed as result
1845
     */
1846
    private String updateDocid(String docid, String docText, boolean result,
1847
                               boolean expectedKarmaFailure) {
1848
        String response = null;
1849
        try {
1850
            response = m.update(docid,
1851
                                new StringReader(testdocument), null);
1852

    
1853
            if (result) {
1854
                assertTrue( (response.indexOf("<success>") != -1));
1855
                assertTrue(response.indexOf(docid) != -1);
1856
            }
1857
            else {
1858
                assertTrue( (response.indexOf("<success>") == -1));
1859
            }
1860
            System.err.println(response);
1861
        }
1862
        catch (MetacatInaccessibleException mie) {
1863
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1864
        }
1865
        catch (InsufficientKarmaException ike) {
1866
            if (!expectedKarmaFailure) {
1867
                fail("Insufficient karma:\n" + ike.getMessage());
1868
            }
1869
        }
1870
        catch (MetacatException me) {
1871
            if (result) {
1872
                fail("Metacat Error:\n" + me.getMessage());
1873
            } else {
1874
                System.err.println("Metacat Error:\n" + me.getMessage());
1875
            }
1876
        }
1877
        catch (Exception e) {
1878
            fail("General exception:\n" + e.getMessage());
1879
        }
1880

    
1881
        return response;
1882
    }
1883

    
1884
    /**
1885
     * Delete a document into metacat. The expected result is passed as result
1886
     */
1887
    private void deleteDocid(String docid, boolean result,
1888
                             boolean expextedKarmaFailure) {
1889
        try {
1890
        	Thread.sleep(5000);
1891
            String response = m.delete(docid);
1892
            if (result) {
1893
                assertTrue(response.indexOf("<success>") != -1);
1894
            }
1895
            else {
1896
                assertTrue(response.indexOf("<success>") == -1);
1897
            }
1898
            System.err.println(response);
1899
        }
1900
        catch (MetacatInaccessibleException mie) {
1901
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1902
        }
1903
        catch (InsufficientKarmaException ike) {
1904
            if(!expextedKarmaFailure){
1905
                fail("Insufficient karma:\n" + ike.getMessage());
1906
            }
1907
        }
1908
        catch (MetacatException me) {
1909
            if (result) {
1910
                fail("Metacat Error:\n" + me.getMessage());
1911
            } else {
1912
                System.err.println("Metacat Error:\n" + me.getMessage());
1913
            }
1914
        }
1915
        catch (Exception e) {
1916
            fail("General exception:\n" + e.getMessage());
1917
        }
1918
    }
1919

    
1920
    /**
1921
     * Read a document from metacat. The expected result is passed as result
1922
     */
1923
    private void readDocid(String docid, boolean result,
1924
                           boolean expextedKarmaFailure) {
1925
        try {
1926
            Reader r = m.read(docid);
1927
            String response = IOUtil.getAsString(r, true);
1928

    
1929
            if (!result) {
1930
                assertTrue(response.indexOf("<success>") == -1);
1931
            }
1932
            // System.err.println(response);
1933
        }
1934
        catch (MetacatInaccessibleException mie) {
1935
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1936
        }
1937
        catch (InsufficientKarmaException ike) {
1938
            if (!expextedKarmaFailure) {
1939
                fail("Insufficient karma:\n" + ike.getMessage());
1940
            }
1941
        }
1942
        catch (MetacatException me) {
1943
            fail("Metacat Error:\n" + me.getMessage());
1944
        }
1945
        catch (Exception e) {
1946
            fail("General exception:\n" + e.getMessage());
1947
        }
1948
    }
1949

    
1950
    /**
1951
     * Read a document from metacat and check if it is equal to a given string.
1952
     * The expected result is passed as result
1953
     */
1954

    
1955
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1956
                                         boolean result,
1957
                                         boolean expextedKarmaFailure) {
1958
        try {
1959
            Reader r = m.read(docid);
1960
            String doc = IOUtil.getAsString(r, true);
1961
            if (result) {
1962

    
1963
                if (!testDoc.equals(doc)) {
1964
                    System.out.println("doc    :" + doc);
1965
                    System.out.println("testDoc:" + testDoc);
1966
                }
1967

    
1968
                assertTrue(testDoc.equals(doc));
1969
            }
1970
            else {
1971
                assertTrue(doc.indexOf("<error>") != -1);
1972
            }
1973
        }
1974
        catch (MetacatInaccessibleException mie) {
1975
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1976
        }
1977
        catch (InsufficientKarmaException ike) {
1978
            if (!expextedKarmaFailure) {
1979
                fail("Insufficient karma:\n" + ike.getMessage());
1980
            }
1981
        }
1982
        catch (MetacatException me) {
1983
            fail("Metacat Error:\n" + me.getMessage());
1984
        }
1985
        catch (Exception e) {
1986
            fail("General exception:\n" + e.getMessage());
1987
        }
1988

    
1989
    }
1990

    
1991
    /**
1992
     * Create a hopefully unique docid for testing insert and update. Does
1993
     * not include the 'revision' part of the id.
1994
     *
1995
     * @return a String docid based on the current date and time
1996
     */
1997
    private String generateDocid() {
1998
        StringBuffer docid = new StringBuffer(prefix);
1999
        docid.append(".");
2000

    
2001
        // Create a calendar to get the date formatted properly
2002
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
2003
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
2004
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
2005
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
2006
                       2 * 60 * 60 * 1000);
2007
        Calendar calendar = new GregorianCalendar(pdt);
2008
        Date trialTime = new Date();
2009
        calendar.setTime(trialTime);
2010
        docid.append(calendar.get(Calendar.YEAR));
2011
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
2012
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
2013
        docid.append(calendar.get(Calendar.MINUTE));
2014
        docid.append(calendar.get(Calendar.SECOND));
2015

    
2016
        return docid.toString();
2017
    }
2018
}
(1-1/17)