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: daigle $'
8
 *     '$Date: 2008-07-15 10:16:06 -0700 (Tue, 15 Jul 2008) $'
9
 * '$Revision: 4126 $'
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.metacat.service.PropertyService;
44
import edu.ucsb.nceas.metacat.util.SystemUtil;
45
import edu.ucsb.nceas.utilities.IOUtil;
46
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47
import junit.framework.Test;
48
import junit.framework.TestCase;
49
import junit.framework.TestSuite;
50
import java.io.File;
51

    
52
/**
53
 * A JUnit test for testing Access Control in Metacat
54
 */
55
public class AccessControlTest
56
    extends TestCase {
57

    
58
    private static String metacatUrl;
59
    private static String username;
60
	private static String password;
61
	private static String anotheruser;
62
	private static String anotherpassword;
63
	static {
64
		try {
65
		    metacatUrl = SystemUtil.getServletURL();
66
			username = PropertyService.getProperty("test.mcuser");
67
			password = PropertyService.getProperty("test.mcpassword");
68
			anotheruser = PropertyService.getProperty("test.mcanotheruser");
69
			anotherpassword = PropertyService.getProperty("test.mcanotherpassword");
70
		} catch (PropertyNotFoundException pnfe) {
71
			System.err.println("Could not get property in static block: " 
72
					+ pnfe.getMessage());
73
		}
74
	}
75

    
76
    private String prefix = "test";
77
    private String newdocid = null;
78
    private String testdocument = "";
79
    private String onlineDocid;
80
    private String onlinetestdatafile1 = "test/onlineDataFile1";
81
    private String onlinetestdatafile2 = "test/onlineDataFile2";
82
    private static final String ALLOWFIRST = "allowFirst";
83
    private static final String DENYFIRST = "denyFirst";
84

    
85
    private Metacat m;
86

    
87
    private boolean SUCCESS = true;
88
    private boolean FAILURE = false;
89

    
90
    /**
91
     * These variables are for eml-2.0.1 only. For other eml versions,
92
     * this function might have to modified
93
     */
94

    
95
    private String testEml_201_Header =
96
        "<?xml version=\"1.0\"?><eml:eml" +
97
        " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.1\"" +
98
        " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
99
        " packageId=\"eml.1.1\" system=\"knb\"" +
100
        " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.1 eml.xsd\"" +
101
        " scope=\"system\">";
102

    
103
    private String testEml_200_Header =
104
    "<?xml version=\"1.0\"?><eml:eml" +
105
    " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"" +
106
    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
107
    " packageId=\"eml.1.1\" system=\"knb\"" +
108
    " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\"" +
109
    " scope=\"system\">";
110

    
111
    private String testEmlCreatorBlock =
112
        "<creator scope=\"document\">                                       " +
113
        " <individualName>                                                  " +
114
        "    <surName>Smith</surName>                                       " +
115
        " </individualName>                                                 " +
116
        "</creator>                                                         ";
117

    
118
    private String testEmlContactBlock =
119
        "<contact scope=\"document\">                                       " +
120
        " <individualName>                                                  " +
121
        "    <surName>Jackson</surName>                                     " +
122
        " </individualName>                                                 " +
123
        "</contact>                                                         ";
124

    
125
    private String testEmlInlineBlock1 =
126
        "<inline>                                                           " +
127
        "  <admin>                                                          " +
128
        "    <contact>                                                      " +
129
        "      <name>Operator</name>                                        " +
130
        "      <institution>PSI</institution>                               " +
131
        "    </contact>                                                     " +
132
        "  </admin>                                                         " +
133
        "</inline>                                                          ";
134

    
135
    private String testEmlInlineBlock2 =
136
        "<inline>                                                           " +
137
        "  <instrument>                                                     " +
138
        "    <instName>LCQ</instName>                                       " +
139
        "    <source type=\"ESI\"></source>                                 " +
140
        "    <detector type=\"EM\"></detector>                              " +
141
        "  </instrument>                                                    " +
142
        "</inline>                                                          ";
143

    
144
    /*
145
     * Retrus an access block base on params passed and the defaul perm order - allow first
146
     */
147
    private String getAccessBlock(String principal, boolean grantAccess,
148
                              boolean read, boolean write,
149
                           boolean changePermission, boolean all)
150
    {
151
    	return getAccessBlock(principal,  grantAccess,
152
                read, write,
153
                changePermission, all, ALLOWFIRST);
154
    }
155
    /**
156
     * This function returns an access block based on the params passed
157
     */
158
    private String getAccessBlock(String principal, boolean grantAccess,
159
                                  boolean read, boolean write,
160
                                  boolean changePermission, boolean all, String permOrder) {
161
        String accessBlock = "<access " +
162
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
163
            " order=\""+
164
            permOrder +"\""+
165
            " scope=\"document\""  +
166
            ">";
167

    
168
        accessBlock += generateOneAccessRule(principal,  grantAccess,
169
                                  read, write, changePermission, all);
170
        accessBlock += "</access>";
171

    
172
        return accessBlock;
173

    
174
    }
175
    
176
    /*
177
     * Gets eml access block base on given acccess rules and perm order
178
     */
179
    private String  getAccessBlock(Vector accessRules, String permOrder)
180
    {
181
    	String accessBlock = "<access " +
182
        "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
183
        " order=\""+
184
        permOrder +"\""+
185
        " scope=\"document\""  +
186
        ">";
187
    	// adding rules
188
    	if (accessRules != null  && !accessRules.isEmpty())
189
    	{
190
    		for (int i=0; i<accessRules.size(); i++)
191
    		{
192
    			String rule = (String)accessRules.elementAt(i);
193
    			accessBlock += rule;
194
    			
195
    		}		
196
    	}
197
    	accessBlock += "</access>";
198
    	return accessBlock;
199
    }
200
    
201
    /*
202
     * Generates a access rule for given parameter. Note this xml portion doesn't include
203
     * <access></access> 
204
     */
205
    private String generateOneAccessRule(String principal, boolean grantAccess,
206
            boolean read, boolean write, boolean changePermission, boolean all)
207
    {
208
    	 String accessBlock = "";
209

    
210
	     if (grantAccess) {
211
	         accessBlock = "<allow>";
212
	     }
213
	     else {
214
	         accessBlock = "<deny>";
215
	     }
216
	
217
	     accessBlock = accessBlock + "<principal>" + principal + "</principal>";
218
	
219
	     if (all) {
220
	         accessBlock += "<permission>all</permission>";
221
	     }
222
	     else {
223
	         if (read) {
224
	             accessBlock += "<permission>read</permission>";
225
	         }
226
	         if (write) {
227
	             accessBlock += "<permission>write</permission>";
228
	         }
229
	         if (changePermission) {
230
	             accessBlock += "<permission>changePermission</permission>";
231
	         }
232
	     }
233
	
234
	     if (grantAccess) {
235
	         accessBlock += "</allow>";
236
	     }
237
	     else {
238
	         accessBlock += "</deny>";
239
	     }
240
	     return accessBlock;
241

    
242
    }
243

    
244
    /**
245
     * This function returns a valid eml document with no access rules
246
     * This function is for eml-2.0.1 only. For other eml versions,
247
     * this function might have to modified
248
     */
249
    private String getTestEmlDoc(String title, String inlineData1,
250
                                 String inlineData2, String onlineUrl1,
251
                                 String onlineUrl2, String docAccessBlock,
252
                                 String inlineAccessBlock1,
253
                                 String inlineAccessBlock2,
254
                                 String onlineAccessBlock1,
255
                                 String onlineAccessBlock2) {
256

    
257
        String testDocument = "";
258
        testDocument = testDocument + testEml_201_Header +
259
            "<dataset scope=\"document\"><title>" + title + "</title>" +
260
            testEmlCreatorBlock;
261

    
262
        if (inlineData1 != null) {
263
            testDocument = testDocument
264
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
265
                + inlineData1 + "</distribution>";
266
        }
267
        if (inlineData2 != null) {
268
            testDocument = testDocument
269
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
270
                + inlineData2 + "</distribution>";
271
        }
272
        if (onlineUrl1 != null) {
273
            testDocument = testDocument
274
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
275
                + "<online><url function=\"download\">"
276
                + onlineUrl1 + "</url></online></distribution>";
277
        }
278
        if (onlineUrl2 != null) {
279
            testDocument = testDocument +
280
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
281
                + "<online><url function=\"download\">"
282
                + onlineUrl2 + "</url></online></distribution>";
283
        }
284
        testDocument += testEmlContactBlock;
285

    
286
        if (docAccessBlock != null) {
287
            testDocument += docAccessBlock;
288
        }
289

    
290
        testDocument += "</dataset>";
291

    
292
        if (inlineAccessBlock1 != null) {
293
            testDocument += "<additionalMetadata>";
294
            testDocument += "<describes>inlineEntity1</describes>";
295
            testDocument += inlineAccessBlock1;
296
            testDocument += "</additionalMetadata>";
297
        }
298

    
299
        if (inlineAccessBlock2 != null) {
300
            testDocument += "<additionalMetadata>";
301
            testDocument += "<describes>inlineEntity2</describes>";
302
            testDocument += inlineAccessBlock2;
303
            testDocument += "</additionalMetadata>";
304
        }
305

    
306
        if (onlineAccessBlock1 != null) {
307
            testDocument += "<additionalMetadata>";
308
            testDocument += "<describes>onlineEntity1</describes>";
309
            testDocument += onlineAccessBlock1;
310
            testDocument += "</additionalMetadata>";
311
        }
312

    
313
        if (onlineAccessBlock2 != null) {
314
            testDocument += "<additionalMetadata>";
315
            testDocument += "<describes>onlineEntity2</describes>";
316
            testDocument += onlineAccessBlock2;
317
            testDocument += "</additionalMetadata>";
318
        }
319

    
320
        testDocument += "</eml:eml>";
321

    
322
        //System.out.println("Returning following document" + testDocument);
323
        return testDocument;
324
    }
325

    
326
    /**
327
     * Constructor to build the test
328
     *
329
     * @param name the name of the test method
330
     */
331
    public AccessControlTest(String name) {
332
        super(name);
333
        newdocid = generateDocid();
334
    }
335

    
336
    /**
337
     * Establish a testing framework by initializing appropriate objects
338
     */
339
    public void setUp() {
340
        try {
341
            System.err.println("Test Metacat: " + metacatUrl);
342
            m = MetacatFactory.createMetacatConnection(metacatUrl);
343
        }
344
        catch (MetacatInaccessibleException mie) {
345
            System.err.println("Metacat is: " + metacatUrl);
346
            fail("Metacat connection failed." + mie.getMessage());
347
        }
348
    }
349

    
350
    /**
351
     * Release any objects after tests are complete
352
     */
353
    public void tearDown() {
354
    }
355

    
356
    /**
357
     * Create a suite of tests to be run together
358
     */
359
    public static Test suite() {
360
        TestSuite suite = new TestSuite();
361
        suite.addTest(new AccessControlTest("initialize"));
362
        // Test basic functions
363
        suite.addTest(new AccessControlTest("documentTest"));
364
        suite.addTest(new AccessControlTest("AccessControlTestForPublic"));
365
        suite.addTest(new AccessControlTest("testAllowFirst"));
366
        suite.addTest(new AccessControlTest("testDenyFirst"));
367

    
368
        return suite;
369
    }
370

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

    
1305
    /** *********
1306
     * Test the case when no access is specified and owner is logged in
1307
     * No online or inline data is involved
1308
     * -> an user inserts a document and is able to read it, update it,
1309
     *    set permissions on it and delete it
1310
     * -> another user is not able to do anything with the document
1311
     *    when no access is specified for that user
1312
     * -> test what all the other user can do when read only, write only and
1313
     *    change permissions only permissions are specified
1314
     *
1315
     */
1316
    public void documentTest() {
1317
        try {
1318

    
1319
            newdocid = generateDocid();
1320

    
1321
            // login
1322
            m.login(username, password);
1323

    
1324
            // insert a 2.0.0 document
1325
            testdocument = testEml_200_Header +
1326
                "<dataset scope=\"document\"><title>submitting eml2000</title>" +
1327
            testEmlCreatorBlock + testEmlContactBlock + "</dataset></eml:eml>";
1328

    
1329
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1330

    
1331
            // read the document
1332
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1333

    
1334
            // update it with 2.0.1 document
1335
            testdocument = getTestEmlDoc("Updating eml200 with eml201",
1336
                                         null, null, null, null,
1337
                                         null, null, null, null, null);
1338
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1339
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1340
            m.logout();
1341

    
1342

    
1343
            newdocid = generateDocid();
1344

    
1345
            // login
1346
            m.login(username, password);
1347

    
1348
            // insert a document
1349
            testdocument = getTestEmlDoc("Testing insert", null,
1350
                                         null, null,
1351
                                         null, null, null, null, null, null);
1352
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1353

    
1354
            // read the document
1355
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1356

    
1357
            // update the document
1358
            testdocument = getTestEmlDoc("Testing update", null, null, null,
1359
                                         null, null, null, null, null, null);
1360
            Thread.sleep(10000);
1361
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1362
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1363

    
1364
            /////////////////////////////
1365
            // check what the another user can do
1366
            m.logout();
1367
            m.login(anotheruser, anotherpassword);
1368

    
1369
            // check if the user mentioned is able to read/update/delete the document
1370
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
1371
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
1372
            deleteDocid(newdocid + ".2", FAILURE, true);
1373

    
1374
            /////////////////////////////
1375
            // update the document access control - read only
1376
            m.logout();
1377
            m.login(username, password);
1378

    
1379
            testdocument = getTestEmlDoc("Testing update access block",
1380
                                         null, null, null,
1381
                                         null, getAccessBlock(anotheruser, true,
1382
                                         true, false, false, false),
1383
                                         null, null, null, null);
1384
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1385
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1386

    
1387
            // check if the user mentioned is able to read the document
1388
            m.logout();
1389
            m.login(anotheruser, anotherpassword);
1390
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1391

    
1392
            // should not be able to update the document
1393
            testdocument = getTestEmlDoc("Testing update from another user",
1394
                                         null, null, null,
1395
                                         null, getAccessBlock(anotheruser, true,
1396
                                         true, false, false, false),
1397
                                         null, null, null, null);
1398
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1399

    
1400
            // or the permissions
1401
            testdocument = getTestEmlDoc("Testing update access block",
1402
                                         null, null, null,
1403
                                         null, getAccessBlock(anotheruser, true,
1404
                                         false, false, false, true),
1405
                                         null, null, null, null);
1406
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1407

    
1408
            // or delete the document
1409
            deleteDocid(newdocid + ".3", FAILURE, true);
1410

    
1411
            m.logout();
1412
            m.login(username, password);
1413

    
1414
            ///////////////////////////////////
1415
            // update the document access control - write only
1416
            testdocument = getTestEmlDoc("Testing update access block",
1417
                                         null, null, null,
1418
                                         null, getAccessBlock(anotheruser, true,
1419
                                         false, true, false, false),
1420
                                         null, null, null, null);
1421
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1422
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
1423
            //System.out.println(testdocument);
1424
            // check if the user mentioned is able to read the document
1425
            m.logout();
1426
            m.login(anotheruser, anotherpassword);
1427
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, true);
1428

    
1429
            // should be able to update the document
1430

    
1431
            //System.out.println(testdocument);
1432
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1433

    
1434
            // but not the permissions
1435
            testdocument = getTestEmlDoc("Testing update access block",
1436
                                         null, null, null,
1437
                                         null, getAccessBlock(anotheruser, true,
1438
                                         false, true, false, true),
1439
                                         null, null, null, null);
1440
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
1441

    
1442
            // try to delete the document
1443
            deleteDocid(newdocid + ".5", FAILURE, true);
1444
            //deleteDocid(newdocid + ".4", FAILURE, true);
1445

    
1446
            m.logout();
1447
            m.login(username, password);
1448

    
1449
            /////////////////////////////////
1450
            // update the document access control - change permissions only
1451
            testdocument = getTestEmlDoc("Testing update access block",
1452
                                         null, null, null,
1453
                                         null, getAccessBlock(anotheruser, true,
1454
                false, false, true, false),
1455
                                         null, null, null, null);
1456
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1457
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, false);
1458

    
1459
            // check if the user mentioned is able to read the document
1460
            m.logout();
1461
            m.login(anotheruser, anotherpassword);
1462
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, FAILURE, true);
1463

    
1464
            // should not be able to update the document
1465
            testdocument = getTestEmlDoc("Testing update from another user",
1466
                                         null, null, null,
1467
                                         null, getAccessBlock(anotheruser, true,
1468
                false, false, true, false),
1469
                                         null, null, null, null);
1470
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
1471

    
1472
            // but can chg the permissions
1473
            testdocument = getTestEmlDoc("Testing update access block",
1474
                                         null, null, null,
1475
                                         null, getAccessBlock(anotheruser, true,
1476
                                         false, false, false, true),
1477
                                         null, null, null, null);
1478
            // ERRRRRRRROR
1479
            //updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
1480

    
1481
            // try to delete the document
1482
            //deleteDocid(newdocid + ".7", FAILURE, true);
1483
            deleteDocid(newdocid + ".6", FAILURE, true);
1484

    
1485
            m.logout();
1486
            m.login(username, password);
1487

    
1488

    
1489
            /////////////////////////////////
1490
            // update the document access control - read & write
1491
            testdocument = getTestEmlDoc("Testing update access block",
1492
                                         null, null, null,
1493
                                         null, getAccessBlock(anotheruser, true,
1494
                                         true, true, false, false),
1495
                                         null, null, null, null);
1496
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
1497
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1498

    
1499
            // check if the user mentioned is able to read the document
1500
            m.logout();
1501
            m.login(anotheruser, anotherpassword);
1502
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1503

    
1504
            // should be able to update the document
1505
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
1506

    
1507
            // but cant chg the permissions
1508
            testdocument = getTestEmlDoc("Testing update access block",
1509
                                         null, null, null,
1510
                                         null, getAccessBlock(anotheruser, true,
1511
                                         false, false, false, true),
1512
                                         null, null, null, null);
1513
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
1514

    
1515
            // try to delete the document
1516
            deleteDocid(newdocid + ".9", FAILURE, true);
1517

    
1518
            m.logout();
1519
            m.login(username, password);
1520

    
1521

    
1522
            /////////////////////////////////
1523
            // update the document access control - read & change permissions
1524
            testdocument = getTestEmlDoc("Testing update access block",
1525
                                         null, null, null,
1526
                                         null, getAccessBlock(anotheruser, true,
1527
                                         true, false, true, false),
1528
                                         null, null, null, null);
1529
            updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
1530
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1531

    
1532
            // check if the user mentioned is able to read the document
1533
            m.logout();
1534
            m.login(anotheruser, anotherpassword);
1535
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1536
            // should not be able to update the document
1537
            testdocument = getTestEmlDoc("Testing update from another user",
1538
                                         null, null, null,
1539
                                         null, getAccessBlock(anotheruser, true,
1540
                                         false, false, true, false),
1541
                                         null, null, null, null);
1542
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
1543

    
1544
            // but can chg the permissions
1545
            testdocument = getTestEmlDoc("Testing update access block",
1546
                                         null, null, null,
1547
                                         null, getAccessBlock(anotheruser, true,
1548
                                         false, false, false, true),
1549
                                         null, null, null, null);
1550
            //updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
1551

    
1552
            // try to delete the document
1553
            // deleteDocid(newdocid + ".11", FAILURE, true);
1554
            deleteDocid(newdocid + ".10", FAILURE, true);
1555

    
1556
            m.logout();
1557
            m.login(username, password);
1558

    
1559

    
1560

    
1561
            /////////////////////////////////
1562
            // update the document access control - read & change permissions
1563
            testdocument = getTestEmlDoc("Testing update access block",
1564
                                         null, null, null,
1565
                                         null, getAccessBlock(anotheruser, true,
1566
                                         true, false, true, false),
1567
                                         null, null, null, null);
1568
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
1569
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument, SUCCESS, false);
1570

    
1571
            // check if the user mentioned is able to read the document
1572
            m.logout();
1573
            m.login(anotheruser, anotherpassword);
1574
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument,  SUCCESS, false);
1575

    
1576
            // should not be able to update the document
1577
            testdocument = getTestEmlDoc("Testing update from another user",
1578
                                         null, null, null,
1579
                                         null, getAccessBlock(anotheruser, true,
1580
                                         false, false, true, false),
1581
                                         null, null, null, null);
1582
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
1583
            // but can chg the permissions
1584
            testdocument = getTestEmlDoc("Testing update from another user",
1585
                                         null, null, null,
1586
                                         null, getAccessBlock(anotheruser, true,
1587
                                         false, false, false, true),
1588
                                         null, null, null, null);
1589
            // ERRRRRRRRRRRRRRRR
1590
            // updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
1591

    
1592
            // try to delete the document
1593
            //deleteDocid(newdocid + ".13", FAILURE, true);
1594
            deleteDocid(newdocid + ".12", FAILURE, true);
1595

    
1596
            m.logout();
1597
            m.login(username, password);
1598

    
1599

    
1600
            /////////////////////////////////
1601
            // update the document access control - R, W, CP
1602
            testdocument = getTestEmlDoc("Testing update access block",
1603
                                         null, null, null,
1604
                                         null, getAccessBlock(anotheruser, true,
1605
                                         true, true, true, false),
1606
                                         null, null, null, null);
1607
            updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
1608
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1609

    
1610
            // check if the user mentioned is able to read the document
1611
            m.logout();
1612
            m.login(anotheruser, anotherpassword);
1613
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1614

    
1615
            // should not be able to update the document
1616
            testdocument = getTestEmlDoc("Testing update from another user",
1617
                                         null, null, null,
1618
                                         null, getAccessBlock(anotheruser, true,
1619
                false, false, false, true),
1620
                                         null, null, null, null);
1621
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
1622
            readDocidWhichEqualsDoc(newdocid + ".15", testdocument, SUCCESS, false);
1623

    
1624
            // but can chg the permissions
1625
            testdocument = getTestEmlDoc("Testing update from another user",
1626
                                         null, null, null,
1627
                                         null, getAccessBlock(anotheruser, true,
1628
                                         true, false, false, false),
1629
                                         null, null, null, null);
1630
            updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
1631
            readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
1632

    
1633
            // try to delete the document
1634
            deleteDocid(newdocid + ".16", FAILURE, true);
1635

    
1636
            m.logout();
1637
            m.login(username, password);
1638

    
1639
            /////////////////////////////////
1640
            // update the document access control - all
1641
            testdocument = getTestEmlDoc("Testing update access block",
1642
                                         null, null, null,
1643
                                         null, getAccessBlock(anotheruser, true,
1644
                                         false, false, false, true),
1645
                                         null, null, null, null);
1646
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
1647
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
1648

    
1649
            // check if the user mentioned is able to read the document
1650
            m.logout();
1651
            m.login(anotheruser, anotherpassword);
1652
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
1653

    
1654
            // should not be able to update the document
1655
            testdocument = getTestEmlDoc("Testing update from another user",
1656
                                         null, null, null,
1657
                                         null, getAccessBlock(anotheruser, true,
1658
                                         false, false, false, true),
1659
                                         null, null, null, null);
1660
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
1661
            readDocidWhichEqualsDoc(newdocid + ".18", testdocument, SUCCESS, false);
1662

    
1663
            // but can chg the permissions
1664
            testdocument = getTestEmlDoc("Testing update from another user",
1665
                                         null, null, null,
1666
                                         null, getAccessBlock(anotheruser, true,
1667
                                         true, false, false, false),
1668
                                         null, null, null, null);
1669
            updateDocid(newdocid + ".19", testdocument, SUCCESS, false);
1670
            readDocidWhichEqualsDoc(newdocid + ".19", testdocument, SUCCESS, false);
1671

    
1672
            // try to delete the document
1673
            deleteDocid(newdocid + ".19", FAILURE, true);
1674

    
1675
            m.logout();
1676

    
1677
            // delete the document
1678
            m.login(username, password);
1679
            deleteDocid(newdocid + ".19", SUCCESS, false);
1680
            m.logout();
1681
        }
1682
        catch (MetacatAuthException mae) {
1683
            fail("Authorization failed:\n" + mae.getMessage());
1684
        }
1685
        catch (MetacatInaccessibleException mie) {
1686
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1687
        }
1688
        catch (Exception e) {
1689
            fail("General exception:\n" + e.getMessage());
1690
        }
1691

    
1692
    }
1693

    
1694

    
1695
   /** *********
1696
     * Test the case when no access is specified and public is logged in
1697
     * Cases being checked:
1698
     * 1. public tries to read the document - Failure.
1699
     * 2. public tries to update the document - Failure.
1700
     * 3. public tries to update the inline data - Failure.
1701
     * 4. public tries to update the online data file - Failure.
1702
     * 5. public tries to update the access rules for the document - Failure.
1703
     * 6. public tries to update the access rules for the inline data - Failure.
1704
     * 7. public tries to update the access rules for the online data - Failure.
1705
     * 8. public tries to delete the document - Failure.
1706
     */
1707
    public void AccessControlTestForPublic() {
1708
        try {
1709

    
1710
            String accessBlock = getAccessBlock(anotheruser, true,
1711
                                         true, false, false, false);
1712
            newdocid = generateDocid();
1713

    
1714
            // login
1715
            m.login(username, password);
1716

    
1717
            onlineDocid = generateDocid();
1718
            uploadDocid(onlineDocid + ".1",onlinetestdatafile1, SUCCESS, false);
1719

    
1720
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1721
                                     null,"ecogrid://knb/" + onlineDocid + ".1",
1722
                                     null, null, null, null, null, null);
1723

    
1724

    
1725
            // insert a document - get the docid
1726
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1727
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
1728
                                    false);
1729

    
1730
            // logoutand login as other user
1731
            m.logout();
1732

    
1733
            // read the document
1734
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, true);
1735

    
1736
            // update the document
1737
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1738

    
1739
            // update the inline data
1740
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock2,
1741
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1742
                         null, null, null, null, null, null);
1743
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1744

    
1745
            // update the online data
1746
            uploadDocid(onlineDocid + ".2", onlinetestdatafile1, FAILURE, false);
1747

    
1748
            // update the document access control
1749
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1750
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1751
                         null, accessBlock, null, null, null, null);
1752
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1753

    
1754
            // update the document access control for inline data
1755
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1756
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1757
                         null, null, accessBlock, null, null, null);
1758
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1759

    
1760
            // update the document access control for online data
1761
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1762
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1763
                         null, null, null, null, accessBlock, null);
1764
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1765

    
1766
            // delete the document
1767
            deleteDocid(newdocid + ".2", FAILURE, true);
1768
            m.logout();
1769
        }
1770
        catch (MetacatAuthException mae) {
1771
            fail("Authorization failed:\n" + mae.getMessage());
1772
        }
1773
        catch (MetacatInaccessibleException mie) {
1774
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1775
        }
1776
        catch (MetacatException me) {
1777
            fail("Metacat Error:\n" + me.getMessage());
1778
        }
1779
        catch (Exception e) {
1780
            fail("General exception:\n" + e.getMessage());
1781
        }
1782

    
1783
    }
1784

    
1785
    /**
1786
     * Insert a document into metacat. The expected result is passed as result
1787
     */
1788

    
1789
    private String insertDocid(String docid, String docText, boolean result,
1790
                               boolean expectKarmaException) {
1791
        String response = null;
1792
        try {
1793
            response = m.insert(docid,
1794
                                new StringReader(testdocument), null);
1795
            if (result) {
1796
                assertTrue( (response.indexOf("<success>") != -1));
1797
                assertTrue(response.indexOf(docid) != -1);
1798
            }
1799
            else {
1800
                assertTrue( (response.indexOf("<success>") == -1));
1801
            }
1802
            System.err.println(response);
1803
        }
1804
        catch (MetacatInaccessibleException mie) {
1805
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1806
        }
1807
        catch (InsufficientKarmaException ike) {
1808
            if (!expectKarmaException) {
1809
                fail("Insufficient karma:\n" + ike.getMessage());
1810
            }
1811
        }
1812
        catch (MetacatException me) {
1813
            fail("Metacat Error:\n" + me.getMessage());
1814
        }
1815
        catch (Exception e) {
1816
            fail("General exception:\n" + e.getMessage());
1817
        }
1818
        return response;
1819
    }
1820

    
1821
    /**
1822
     * Insert a document into metacat. The expected result is passed as result
1823
     */
1824

    
1825
    private String uploadDocid(String docid, String filePath, boolean result,
1826
                               boolean expectedKarmaException) {
1827
        String response = null;
1828
        try {
1829
            response = m.upload(docid, new File(filePath));
1830
            if (result) {
1831
                assertTrue( (response.indexOf("<success>") != -1));
1832
                assertTrue(response.indexOf(docid) != -1);
1833
            }
1834
            else {
1835
                assertTrue( (response.indexOf("<success>") == -1));
1836
            }
1837
            System.err.println("respose from metacat: " + response);
1838
        }
1839
        catch (MetacatInaccessibleException mie) {
1840
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1841
        }
1842
        catch (InsufficientKarmaException ike) {
1843
            if (!expectedKarmaException) {
1844
                fail("Insufficient karma:\n" + ike.getMessage());
1845
            }
1846
        }
1847
        catch (MetacatException me) {
1848
            if (result) {
1849
                fail("Metacat Error:\n" + me.getMessage());
1850
            } else {
1851
                System.err.println("Metacat Error:\n" + me.getMessage());
1852
            }
1853
        }
1854
        catch (Exception e) {
1855
            fail("General exception:\n" + e.getMessage());
1856
        }
1857
        return response;
1858
    }
1859

    
1860
    /**
1861
     * Update a document in metacat. The expected result is passed as result
1862
     */
1863
    private String updateDocid(String docid, String docText, boolean result,
1864
                               boolean expectedKarmaFailure) {
1865
        String response = null;
1866
        try {
1867
            response = m.update(docid,
1868
                                new StringReader(testdocument), null);
1869

    
1870
            if (result) {
1871
                assertTrue( (response.indexOf("<success>") != -1));
1872
                assertTrue(response.indexOf(docid) != -1);
1873
            }
1874
            else {
1875
                assertTrue( (response.indexOf("<success>") == -1));
1876
            }
1877
            System.err.println(response);
1878
        }
1879
        catch (MetacatInaccessibleException mie) {
1880
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1881
        }
1882
        catch (InsufficientKarmaException ike) {
1883
            if (!expectedKarmaFailure) {
1884
                fail("Insufficient karma:\n" + ike.getMessage());
1885
            }
1886
        }
1887
        catch (MetacatException me) {
1888
            if (result) {
1889
                fail("Metacat Error:\n" + me.getMessage());
1890
            } else {
1891
                System.err.println("Metacat Error:\n" + me.getMessage());
1892
            }
1893
        }
1894
        catch (Exception e) {
1895
            fail("General exception:\n" + e.getMessage());
1896
        }
1897

    
1898
        return response;
1899
    }
1900

    
1901
    /**
1902
     * Delete a document into metacat. The expected result is passed as result
1903
     */
1904
    private void deleteDocid(String docid, boolean result,
1905
                             boolean expextedKarmaFailure) {
1906
        try {
1907
        	Thread.sleep(5000);
1908
            String response = m.delete(docid);
1909
            if (result) {
1910
                assertTrue(response.indexOf("<success>") != -1);
1911
            }
1912
            else {
1913
                assertTrue(response.indexOf("<success>") == -1);
1914
            }
1915
            System.err.println(response);
1916
        }
1917
        catch (MetacatInaccessibleException mie) {
1918
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1919
        }
1920
        catch (InsufficientKarmaException ike) {
1921
            if(!expextedKarmaFailure){
1922
                fail("Insufficient karma:\n" + ike.getMessage());
1923
            }
1924
        }
1925
        catch (MetacatException me) {
1926
            if (result) {
1927
                fail("Metacat Error:\n" + me.getMessage());
1928
            } else {
1929
                System.err.println("Metacat Error:\n" + me.getMessage());
1930
            }
1931
        }
1932
        catch (Exception e) {
1933
            fail("General exception:\n" + e.getMessage());
1934
        }
1935
    }
1936

    
1937
    /**
1938
     * Read a document from metacat. The expected result is passed as result
1939
     */
1940
    private void readDocid(String docid, boolean result,
1941
                           boolean expextedKarmaFailure) {
1942
        try {
1943
            Reader r = m.read(docid);
1944
            String response = IOUtil.getAsString(r, true);
1945

    
1946
            if (!result) {
1947
                assertTrue(response.indexOf("<success>") == -1);
1948
            }
1949
            // System.err.println(response);
1950
        }
1951
        catch (MetacatInaccessibleException mie) {
1952
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1953
        }
1954
        catch (InsufficientKarmaException ike) {
1955
            if (!expextedKarmaFailure) {
1956
                fail("Insufficient karma:\n" + ike.getMessage());
1957
            }
1958
        }
1959
        catch (MetacatException me) {
1960
            fail("Metacat Error:\n" + me.getMessage());
1961
        }
1962
        catch (Exception e) {
1963
            fail("General exception:\n" + e.getMessage());
1964
        }
1965
    }
1966

    
1967
    /**
1968
     * Read a document from metacat and check if it is equal to a given string.
1969
     * The expected result is passed as result
1970
     */
1971

    
1972
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1973
                                         boolean result,
1974
                                         boolean expextedKarmaFailure) {
1975
        try {
1976
            Reader r = m.read(docid);
1977
            String doc = IOUtil.getAsString(r, true);
1978
            if (result) {
1979

    
1980
                if (!testDoc.equals(doc)) {
1981
                    System.out.println("doc       :" + doc);
1982
                    System.out.println("testDoc:" + testDoc);
1983
                }
1984

    
1985
                assertTrue(testDoc.equals(doc));
1986
            }
1987
            else {
1988
                assertTrue(doc.indexOf("<error>") != -1);
1989
            }
1990
        }
1991
        catch (MetacatInaccessibleException mie) {
1992
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1993
        }
1994
        catch (InsufficientKarmaException ike) {
1995
            if (!expextedKarmaFailure) {
1996
                fail("Insufficient karma:\n" + ike.getMessage());
1997
            }
1998
        }
1999
        catch (MetacatException me) {
2000
            fail("Metacat Error:\n" + me.getMessage());
2001
        }
2002
        catch (Exception e) {
2003
            fail("General exception:\n" + e.getMessage());
2004
        }
2005

    
2006
    }
2007

    
2008
    /**
2009
     * Create a hopefully unique docid for testing insert and update. Does
2010
     * not include the 'revision' part of the id.
2011
     *
2012
     * @return a String docid based on the current date and time
2013
     */
2014
    private String generateDocid() {
2015
        StringBuffer docid = new StringBuffer(prefix);
2016
        docid.append(".");
2017

    
2018
        // Create a calendar to get the date formatted properly
2019
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
2020
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
2021
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
2022
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
2023
                       2 * 60 * 60 * 1000);
2024
        Calendar calendar = new GregorianCalendar(pdt);
2025
        Date trialTime = new Date();
2026
        calendar.setTime(trialTime);
2027
        docid.append(calendar.get(Calendar.YEAR));
2028
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
2029
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
2030
        docid.append(calendar.get(Calendar.MINUTE));
2031
        docid.append(calendar.get(Calendar.SECOND));
2032

    
2033
        return docid.toString();
2034
    }
2035
}
(1-1/18)