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-18 10:06:00 -0700 (Fri, 18 Jul 2008) $'
9
 * '$Revision: 4146 $'
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.MCTestCase;
38
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
39
import edu.ucsb.nceas.metacat.client.Metacat;
40
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
41
import edu.ucsb.nceas.metacat.client.MetacatException;
42
import edu.ucsb.nceas.metacat.client.MetacatFactory;
43
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
44
import edu.ucsb.nceas.metacat.service.PropertyService;
45
import edu.ucsb.nceas.utilities.IOUtil;
46
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47
import junit.framework.Test;
48
import junit.framework.TestSuite;
49
import java.io.File;
50

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

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

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

    
84
    private Metacat m;
85

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

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

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

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

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

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

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

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

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

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

    
171
        return accessBlock;
172

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

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

    
241
    }
242

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

    
256
    	debug("getTestEmlDoc(): title=" + title + " inlineData1=" + inlineData1  + 
257
    			" inlineData2=" + inlineData2 + " onlineUrl1=" + onlineUrl1 + 
258
    			 " onlineUrl2=" + onlineUrl2 + " docAccessBlock=" + docAccessBlock + 
259
    			 " inlineAccessBlock1=" + inlineAccessBlock1 + 
260
    			 " inlineAccessBlock2=" + inlineAccessBlock2 + 
261
    			 " onlineAccessBlock1=" + onlineAccessBlock1 + 
262
    			 " onlineAccessBlock2=" + onlineAccessBlock2 );
263
        String testDocument = "";
264
        testDocument = testDocument + testEml_201_Header +
265
            "<dataset scope=\"document\"><title>" + title + "</title>" +
266
            testEmlCreatorBlock;
267

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

    
292
        if (docAccessBlock != null) {
293
            testDocument += docAccessBlock;
294
        }
295

    
296
        testDocument += "</dataset>";
297

    
298
        if (inlineAccessBlock1 != null) {
299
            testDocument += "<additionalMetadata>";
300
            testDocument += "<describes>inlineEntity1</describes>";
301
            testDocument += inlineAccessBlock1;
302
            testDocument += "</additionalMetadata>";
303
        }
304

    
305
        if (inlineAccessBlock2 != null) {
306
            testDocument += "<additionalMetadata>";
307
            testDocument += "<describes>inlineEntity2</describes>";
308
            testDocument += inlineAccessBlock2;
309
            testDocument += "</additionalMetadata>";
310
        }
311

    
312
        if (onlineAccessBlock1 != null) {
313
            testDocument += "<additionalMetadata>";
314
            testDocument += "<describes>onlineEntity1</describes>";
315
            testDocument += onlineAccessBlock1;
316
            testDocument += "</additionalMetadata>";
317
        }
318

    
319
        if (onlineAccessBlock2 != null) {
320
            testDocument += "<additionalMetadata>";
321
            testDocument += "<describes>onlineEntity2</describes>";
322
            testDocument += onlineAccessBlock2;
323
            testDocument += "</additionalMetadata>";
324
        }
325

    
326
        testDocument += "</eml:eml>";
327

    
328
        //System.out.println("Returning following document" + testDocument);
329
        return testDocument;
330
    }
331

    
332
    /**
333
     * Constructor to build the test
334
     *
335
     * @param name the name of the test method
336
     */
337
    public AccessControlTest(String name) {
338
        super(name);
339
        newdocid = generateDocid();
340
    }
341

    
342
    /**
343
     * Establish a testing framework by initializing appropriate objects
344
     */
345
    public void setUp() {
346
        try {
347
            debug("Test Metacat: " + metacatUrl);
348
            m = MetacatFactory.createMetacatConnection(metacatUrl);
349
        }
350
        catch (MetacatInaccessibleException mie) {
351
            System.err.println("Metacat is: " + metacatUrl);
352
            fail("Metacat connection failed." + mie.getMessage());
353
        }
354
    }
355

    
356
    /**
357
     * Release any objects after tests are complete
358
     */
359
    public void tearDown() {
360
    }
361

    
362
    /**
363
     * Create a suite of tests to be run together
364
     */
365
    public static Test suite() {
366
        TestSuite suite = new TestSuite();
367
        suite.addTest(new AccessControlTest("initialize"));
368
        // Test basic functions
369
        suite.addTest(new AccessControlTest("documentTest"));
370
        suite.addTest(new AccessControlTest("AccessControlTestForPublic"));
371
        suite.addTest(new AccessControlTest("testAllowFirst"));
372
        suite.addTest(new AccessControlTest("testDenyFirst"));
373

    
374
        return suite;
375
    }
376

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

    
1392
    /** *********
1393
     * Test the case when no access is specified and owner is logged in
1394
     * No online or inline data is involved
1395
     * -> an user inserts a document and is able to read it, update it,
1396
     *    set permissions on it and delete it
1397
     * -> another user is not able to do anything with the document
1398
     *    when no access is specified for that user
1399
     * -> test what all the other user can do when read only, write only and
1400
     *    change permissions only permissions are specified
1401
     *
1402
     */
1403
    public void documentTest() {
1404
        try {
1405
        	debug("\nRunning: documentTest()");
1406

    
1407
            newdocid = generateDocid();
1408

    
1409
            // login
1410
	        debug("logging in as: username=" + username + " password=" + password);
1411
            m.login(username, password);
1412

    
1413
            // insert a 2.0.0 document
1414
            testdocument = testEml_200_Header +
1415
                "<dataset scope=\"document\"><title>submitting eml2000</title>" +
1416
            testEmlCreatorBlock + testEmlContactBlock + "</dataset></eml:eml>";
1417

    
1418
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1419

    
1420
            // read the document
1421
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1422

    
1423
            // update it with 2.0.1 document
1424
            testdocument = getTestEmlDoc("Updating eml200 with eml201",
1425
                                         null, null, null, null,
1426
                                         null, null, null, null, null);
1427
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1428
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1429
	        debug("logging out");
1430
            m.logout();
1431

    
1432

    
1433
            newdocid = generateDocid();
1434

    
1435
            // login
1436
	        debug("logging in as: username=" + username + " password=" + password);
1437
            m.login(username, password);
1438

    
1439
            // insert a document
1440
            testdocument = getTestEmlDoc("Testing insert", null,
1441
                                         null, null,
1442
                                         null, null, null, null, null, null);
1443
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1444

    
1445
            // read the document
1446
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1447

    
1448
            // update the document
1449
            testdocument = getTestEmlDoc("Testing update", null, null, null,
1450
                                         null, null, null, null, null, null);
1451
            Thread.sleep(10000);
1452
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
1453
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
1454

    
1455
            /////////////////////////////
1456
            // check what the another user can do
1457
	        debug("logging out");
1458
            m.logout();
1459
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1460
            m.login(anotheruser, anotherpassword);
1461

    
1462
            // check if the user mentioned is able to read/update/delete the document
1463
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
1464
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
1465
            deleteDocid(newdocid + ".2", FAILURE, true);
1466

    
1467
            /////////////////////////////
1468
            // update the document access control - read only
1469
	        debug("logging out");
1470
            m.logout();
1471
	        debug("logging in as: username=" + username + " password=" + password);
1472
            m.login(username, password);
1473

    
1474
            testdocument = getTestEmlDoc("Testing update access block",
1475
                                         null, null, null,
1476
                                         null, getAccessBlock(anotheruser, true,
1477
                                         true, false, false, false),
1478
                                         null, null, null, null);
1479
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
1480
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1481

    
1482
            // check if the user mentioned is able to read the document
1483
	        debug("logging out");
1484
            m.logout();
1485
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1486
            m.login(anotheruser, anotherpassword);
1487
            debug(testdocument);
1488
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
1489

    
1490
            // should not be able to update the document
1491
            testdocument = getTestEmlDoc("Testing update from another user",
1492
                                         null, null, null,
1493
                                         null, getAccessBlock(anotheruser, true,
1494
                                         true, false, false, false),
1495
                                         null, null, null, null);
1496
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1497

    
1498
            // or the permissions
1499
            testdocument = getTestEmlDoc("Testing update access block",
1500
                                         null, null, null,
1501
                                         null, getAccessBlock(anotheruser, true,
1502
                                         false, false, false, true),
1503
                                         null, null, null, null);
1504
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
1505

    
1506
            // or delete the document
1507
            deleteDocid(newdocid + ".3", FAILURE, true);
1508

    
1509
	        debug("logging out");
1510
            m.logout();
1511
	        debug("logging in as: username=" + username + " password=" + password);
1512
            m.login(username, password);
1513

    
1514
            ///////////////////////////////////
1515
            // update the document access control - write only
1516
            testdocument = getTestEmlDoc("Testing update access block",
1517
                                         null, null, null,
1518
                                         null, getAccessBlock(anotheruser, true,
1519
                                         false, true, false, false),
1520
                                         null, null, null, null);
1521
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
1522
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
1523
            //System.out.println(testdocument);
1524
            // check if the user mentioned is able to read the document
1525
	        debug("logging out");
1526
            m.logout();
1527
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1528
            m.login(anotheruser, anotherpassword);
1529
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, true);
1530

    
1531
            // should be able to update the document
1532

    
1533
            //System.out.println(testdocument);
1534
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
1535

    
1536
            // but not the permissions
1537
            testdocument = getTestEmlDoc("Testing update access block",
1538
                                         null, null, null,
1539
                                         null, getAccessBlock(anotheruser, true,
1540
                                         false, true, false, true),
1541
                                         null, null, null, null);
1542
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
1543

    
1544
            // try to delete the document
1545
            deleteDocid(newdocid + ".5", FAILURE, true);
1546
            //deleteDocid(newdocid + ".4", FAILURE, true);
1547

    
1548
	        debug("logging out");
1549
            m.logout();
1550
	        debug("logging in as: username=" + username + " password=" + password);
1551
            m.login(username, password);
1552

    
1553
            /////////////////////////////////
1554
            // update the document access control - change permissions only
1555
            testdocument = getTestEmlDoc("Testing update access block",
1556
                                         null, null, null,
1557
                                         null, getAccessBlock(anotheruser, true,
1558
                false, false, true, false),
1559
                                         null, null, null, null);
1560
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
1561
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, false);
1562

    
1563
            // check if the user mentioned is able to read the document
1564
	        debug("logging out");
1565
            m.logout();
1566
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1567
            m.login(anotheruser, anotherpassword);
1568
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, FAILURE, true);
1569

    
1570
            // should not be able to update the document
1571
            testdocument = getTestEmlDoc("Testing update from another user",
1572
                                         null, null, null,
1573
                                         null, getAccessBlock(anotheruser, true,
1574
                false, false, true, false),
1575
                                         null, null, null, null);
1576
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
1577

    
1578
            // but can chg the permissions
1579
            testdocument = getTestEmlDoc("Testing update access block",
1580
                                         null, null, null,
1581
                                         null, getAccessBlock(anotheruser, true,
1582
                                         false, false, false, true),
1583
                                         null, null, null, null);
1584
            // ERRRRRRRROR
1585
            //updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
1586

    
1587
            // try to delete the document
1588
            //deleteDocid(newdocid + ".7", FAILURE, true);
1589
            deleteDocid(newdocid + ".6", FAILURE, true);
1590

    
1591
	        debug("logging out");
1592
            m.logout();
1593
	        debug("logging in as: username=" + username + " password=" + password);
1594
            m.login(username, password);
1595

    
1596

    
1597
            /////////////////////////////////
1598
            // update the document access control - read & write
1599
            testdocument = getTestEmlDoc("Testing update access block",
1600
                                         null, null, null,
1601
                                         null, getAccessBlock(anotheruser, true,
1602
                                         true, true, false, false),
1603
                                         null, null, null, null);
1604
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
1605
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1606

    
1607
            // check if the user mentioned is able to read the document
1608
	        debug("logging out");
1609
            m.logout();
1610
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1611
            m.login(anotheruser, anotherpassword);
1612
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
1613

    
1614
            // should be able to update the document
1615
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
1616

    
1617
            // but cant chg the permissions
1618
            testdocument = getTestEmlDoc("Testing update access block",
1619
                                         null, null, null,
1620
                                         null, getAccessBlock(anotheruser, true,
1621
                                         false, false, false, true),
1622
                                         null, null, null, null);
1623
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
1624

    
1625
            // try to delete the document
1626
            deleteDocid(newdocid + ".9", FAILURE, true);
1627

    
1628
	        debug("logging out");
1629
            m.logout();
1630
	        debug("logging in as: username=" + username + " password=" + password);
1631
            m.login(username, password);
1632

    
1633

    
1634
            /////////////////////////////////
1635
            // update the document access control - read & change permissions
1636
            testdocument = getTestEmlDoc("Testing update access block",
1637
                                         null, null, null,
1638
                                         null, getAccessBlock(anotheruser, true,
1639
                                         true, false, true, false),
1640
                                         null, null, null, null);
1641
            updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
1642
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1643

    
1644
            // check if the user mentioned is able to read the document
1645
	        debug("logging out");
1646
            m.logout();
1647
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1648
            m.login(anotheruser, anotherpassword);
1649
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
1650
            // should not be able to update the document
1651
            testdocument = getTestEmlDoc("Testing update from another user",
1652
                                         null, null, null,
1653
                                         null, getAccessBlock(anotheruser, true,
1654
                                         false, false, true, false),
1655
                                         null, null, null, null);
1656
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
1657

    
1658
            // but can chg the permissions
1659
            testdocument = getTestEmlDoc("Testing update access block",
1660
                                         null, null, null,
1661
                                         null, getAccessBlock(anotheruser, true,
1662
                                         false, false, false, true),
1663
                                         null, null, null, null);
1664
            //updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
1665

    
1666
            // try to delete the document
1667
            // deleteDocid(newdocid + ".11", FAILURE, true);
1668
            deleteDocid(newdocid + ".10", FAILURE, true);
1669

    
1670
	        debug("logging out");
1671
            m.logout();
1672
	        debug("logging in as: username=" + username + " password=" + password);
1673
            m.login(username, password);
1674

    
1675

    
1676

    
1677
            /////////////////////////////////
1678
            // update the document access control - read & change permissions
1679
            testdocument = getTestEmlDoc("Testing update access block",
1680
                                         null, null, null,
1681
                                         null, getAccessBlock(anotheruser, true,
1682
                                         true, false, true, false),
1683
                                         null, null, null, null);
1684
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
1685
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument, SUCCESS, false);
1686

    
1687
            // check if the user mentioned is able to read the document
1688
	        debug("logging out");
1689
            m.logout();
1690
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1691
            m.login(anotheruser, anotherpassword);
1692
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument,  SUCCESS, false);
1693

    
1694
            // should not be able to update the document
1695
            testdocument = getTestEmlDoc("Testing update from another user",
1696
                                         null, null, null,
1697
                                         null, getAccessBlock(anotheruser, true,
1698
                                         false, false, true, false),
1699
                                         null, null, null, null);
1700
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
1701
            // but can chg the permissions
1702
            testdocument = getTestEmlDoc("Testing update from another user",
1703
                                         null, null, null,
1704
                                         null, getAccessBlock(anotheruser, true,
1705
                                         false, false, false, true),
1706
                                         null, null, null, null);
1707
            // ERRRRRRRRRRRRRRRR
1708
            // updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
1709

    
1710
            // try to delete the document
1711
            //deleteDocid(newdocid + ".13", FAILURE, true);
1712
            deleteDocid(newdocid + ".12", FAILURE, true);
1713

    
1714
	        debug("logging out");
1715
            m.logout();
1716
	        debug("logging in as: username=" + username + " password=" + password);
1717
            m.login(username, password);
1718

    
1719

    
1720
            /////////////////////////////////
1721
            // update the document access control - R, W, CP
1722
            testdocument = getTestEmlDoc("Testing update access block",
1723
                                         null, null, null,
1724
                                         null, getAccessBlock(anotheruser, true,
1725
                                         true, true, true, false),
1726
                                         null, null, null, null);
1727
            updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
1728
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1729

    
1730
            // check if the user mentioned is able to read the document
1731
	        debug("logging out");
1732
            m.logout();
1733
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1734
            m.login(anotheruser, anotherpassword);
1735
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
1736

    
1737
            // should not be able to update the document
1738
            testdocument = getTestEmlDoc("Testing update from another user",
1739
                                         null, null, null,
1740
                                         null, getAccessBlock(anotheruser, true,
1741
                false, false, false, true),
1742
                                         null, null, null, null);
1743
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
1744
            readDocidWhichEqualsDoc(newdocid + ".15", testdocument, SUCCESS, false);
1745

    
1746
            // but can chg the permissions
1747
            testdocument = getTestEmlDoc("Testing update from another user",
1748
                                         null, null, null,
1749
                                         null, getAccessBlock(anotheruser, true,
1750
                                         true, false, false, false),
1751
                                         null, null, null, null);
1752
            updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
1753
            readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
1754

    
1755
            // try to delete the document
1756
            deleteDocid(newdocid + ".16", FAILURE, true);
1757

    
1758
	        debug("logging out");
1759
            m.logout();
1760
	        debug("logging in as: username=" + username + " password=" + password);
1761
            m.login(username, password);
1762

    
1763
            /////////////////////////////////
1764
            // update the document access control - all
1765
            testdocument = getTestEmlDoc("Testing update access block",
1766
                                         null, null, null,
1767
                                         null, getAccessBlock(anotheruser, true,
1768
                                         false, false, false, true),
1769
                                         null, null, null, null);
1770
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
1771
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
1772

    
1773
            // check if the user mentioned is able to read the document
1774
	        debug("logging out");
1775
            m.logout();
1776
	        debug("logging in as: anotheruser=" + anotheruser + " anotherpassword=" + anotherpassword);
1777
            m.login(anotheruser, anotherpassword);
1778
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
1779

    
1780
            // should not be able to update the document
1781
            testdocument = getTestEmlDoc("Testing update from another user",
1782
                                         null, null, null,
1783
                                         null, getAccessBlock(anotheruser, true,
1784
                                         false, false, false, true),
1785
                                         null, null, null, null);
1786
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
1787
            readDocidWhichEqualsDoc(newdocid + ".18", testdocument, SUCCESS, false);
1788

    
1789
            // but can chg the permissions
1790
            testdocument = getTestEmlDoc("Testing update from another user",
1791
                                         null, null, null,
1792
                                         null, getAccessBlock(anotheruser, true,
1793
                                         true, false, false, false),
1794
                                         null, null, null, null);
1795
            updateDocid(newdocid + ".19", testdocument, SUCCESS, false);
1796
            readDocidWhichEqualsDoc(newdocid + ".19", testdocument, SUCCESS, false);
1797

    
1798
            // try to delete the document
1799
            deleteDocid(newdocid + ".19", FAILURE, true);
1800

    
1801
	        debug("logging out");
1802
            m.logout();
1803

    
1804
            // delete the document
1805
	        debug("logging in as: username=" + username + " password=" + password);
1806
            m.login(username, password);
1807
            deleteDocid(newdocid + ".19", SUCCESS, false);
1808
	        debug("logging out");
1809
            m.logout();
1810
        }
1811
        catch (MetacatAuthException mae) {
1812
            fail("Authorization failed:\n" + mae.getMessage());
1813
        }
1814
        catch (MetacatInaccessibleException mie) {
1815
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1816
        }
1817
        catch (Exception e) {
1818
            fail("General exception:\n" + e.getMessage());
1819
        }
1820

    
1821
    }
1822

    
1823

    
1824
   /** *********
1825
     * Test the case when no access is specified and public is logged in
1826
     * Cases being checked:
1827
     * 1. public tries to read the document - Failure.
1828
     * 2. public tries to update the document - Failure.
1829
     * 3. public tries to update the inline data - Failure.
1830
     * 4. public tries to update the online data file - Failure.
1831
     * 5. public tries to update the access rules for the document - Failure.
1832
     * 6. public tries to update the access rules for the inline data - Failure.
1833
     * 7. public tries to update the access rules for the online data - Failure.
1834
     * 8. public tries to delete the document - Failure.
1835
     */
1836
    public void AccessControlTestForPublic() {
1837
        try {
1838
        	debug("\nRunning: AccessControlTestForPublic()");
1839

    
1840
            String accessBlock = getAccessBlock(anotheruser, true,
1841
                                         true, false, false, false);
1842
            newdocid = generateDocid();
1843

    
1844
            // login
1845
	        debug("logging in as: username=" + username + " password=" + password);
1846
            m.login(username, password);
1847

    
1848
            onlineDocid = generateDocid();
1849
            uploadDocid(onlineDocid + ".1",onlinetestdatafile1, SUCCESS, false);
1850

    
1851
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1852
                                     null,"ecogrid://knb/" + onlineDocid + ".1",
1853
                                     null, null, null, null, null, null);
1854

    
1855

    
1856
            // insert a document - get the docid
1857
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
1858
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
1859
                                    false);
1860

    
1861
            // logoutand login as other user
1862
	        debug("logging out");
1863
            m.logout();
1864

    
1865
            // read the document
1866
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, true);
1867

    
1868
            // update the document
1869
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1870

    
1871
            // update the inline data
1872
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock2,
1873
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1874
                         null, null, null, null, null, null);
1875
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1876

    
1877
            // update the online data
1878
            uploadDocid(onlineDocid + ".2", onlinetestdatafile1, FAILURE, false);
1879

    
1880
            // update the document access control
1881
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1882
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1883
                         null, accessBlock, null, null, null, null);
1884
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1885

    
1886
            // update the document access control for inline data
1887
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1888
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1889
                         null, null, accessBlock, null, null, null);
1890
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1891

    
1892
            // update the document access control for online data
1893
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
1894
                         null,"ecogrid://knb/" + onlineDocid + ".1",
1895
                         null, null, null, null, accessBlock, null);
1896
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
1897

    
1898
            // delete the document
1899
            deleteDocid(newdocid + ".2", FAILURE, true);
1900
	        debug("logging out");
1901
            m.logout();
1902
        }
1903
        catch (MetacatAuthException mae) {
1904
            fail("Authorization failed:\n" + mae.getMessage());
1905
        }
1906
        catch (MetacatInaccessibleException mie) {
1907
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1908
        }
1909
        catch (MetacatException me) {
1910
            fail("Metacat Error:\n" + me.getMessage());
1911
        }
1912
        catch (Exception e) {
1913
            fail("General exception:\n" + e.getMessage());
1914
        }
1915

    
1916
    }
1917

    
1918
    /**
1919
     * Insert a document into metacat. The expected result is passed as result
1920
     */
1921

    
1922
    private String insertDocid(String docid, String docText, boolean result,
1923
                               boolean expectKarmaException) {
1924
    	debug("insertDocid(): docid=" + docid + " expectedResult=" + result
1925
				+ " expectKarmaException=" + expectKarmaException);
1926
        String response = null;
1927
        try {
1928
            response = m.insert(docid,
1929
                                new StringReader(testdocument), null);
1930
            if (result) {
1931
                assertTrue(response, (response.indexOf("<success>") != -1));
1932
                assertTrue(response, response.indexOf(docid) != -1);
1933
            }
1934
            else {
1935
                assertTrue(response, (response.indexOf("<success>") == -1));
1936
            }
1937
            debug("insertDocid():  response=" + response);
1938
        }
1939
        catch (MetacatInaccessibleException mie) {
1940
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1941
        }
1942
        catch (InsufficientKarmaException ike) {
1943
            if (!expectKarmaException) {
1944
                fail("Insufficient karma:\n" + ike.getMessage());
1945
            }
1946
        }
1947
        catch (MetacatException me) {
1948
            fail("Metacat Error:\n" + me.getMessage());
1949
        }
1950
        catch (Exception e) {
1951
            fail("General exception:\n" + e.getMessage());
1952
        }
1953
        return response;
1954
    }
1955

    
1956
    /**
1957
     * Insert a document into metacat. The expected result is passed as result
1958
     */
1959

    
1960
    private String uploadDocid(String docid, String filePath, boolean result,
1961
                               boolean expectedKarmaException) {
1962
    	debug("uploadDocid(): docid=" + docid + " filePath=" + filePath 
1963
    			+ " expectedResult=" + result + " expectedKarmaException=" + expectedKarmaException);
1964
        String response = null;
1965
        try {
1966
            response = m.upload(docid, new File(filePath));
1967
            if (result) {
1968
                assertTrue(response, (response.indexOf("<success>") != -1));
1969
                assertTrue(response, response.indexOf(docid) != -1);
1970
            }
1971
            else {
1972
                assertTrue(response,  (response.indexOf("<success>") == -1));
1973
            }
1974
            debug("uploadDocid():  response=" + response);
1975
        }
1976
        catch (MetacatInaccessibleException mie) {
1977
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1978
        }
1979
        catch (InsufficientKarmaException ike) {
1980
            if (!expectedKarmaException) {
1981
                fail("Insufficient karma:\n" + ike.getMessage());
1982
            }
1983
        }
1984
        catch (MetacatException me) {
1985
            if (result) {
1986
                fail("Metacat Error:\n" + me.getMessage());
1987
            } else {
1988
                debug("Metacat Error:\n" + me.getMessage());
1989
            }
1990
        }
1991
        catch (Exception e) {
1992
            fail("General exception:\n" + e.getMessage());
1993
        }
1994
        return response;
1995
    }
1996

    
1997
    /**
1998
     * Update a document in metacat. The expected result is passed as result
1999
     */
2000
    private String updateDocid(String docid, String docText, boolean result,
2001
                               boolean expectedKarmaFailure) {
2002
    	debug("updateDocid(): docid=" + docid + " expectedResult=" + result
2003
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
2004
        String response = null;
2005
        try {
2006
            response = m.update(docid,
2007
                                new StringReader(testdocument), null);
2008

    
2009
            if (result) {
2010
                assertTrue(response, (response.indexOf("<success>") != -1));
2011
                assertTrue(response, response.indexOf(docid) != -1);
2012
            }
2013
            else {
2014
                assertTrue(response, (response.indexOf("<success>") == -1));
2015
            }
2016
            debug("updateDocid(): response=" + response);
2017
        }
2018
        catch (MetacatInaccessibleException mie) {
2019
            fail("Metacat Inaccessible:\n" + mie.getMessage());
2020
        }
2021
        catch (InsufficientKarmaException ike) {
2022
            if (!expectedKarmaFailure) {
2023
                fail("Insufficient karma:\n" + ike.getMessage());
2024
            }
2025
        }
2026
        catch (MetacatException me) {
2027
            if (result) {
2028
                fail("Metacat Error:\n" + me.getMessage());
2029
            } else {
2030
                debug("Metacat Error:\n" + me.getMessage());
2031
            }
2032
        }
2033
        catch (Exception e) {
2034
            fail("General exception:\n" + e.getMessage());
2035
        }
2036

    
2037
        return response;
2038
    }
2039

    
2040
    /**
2041
     * Delete a document into metacat. The expected result is passed as result
2042
     */
2043
    private void deleteDocid(String docid, boolean result,
2044
                             boolean expectedKarmaFailure) {
2045
    	debug("deleteDocid(): docid=" + docid + " expectedResult=" + result
2046
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
2047
        try {
2048
        	Thread.sleep(5000);
2049
            String response = m.delete(docid);
2050
            if (result) {
2051
                assertTrue(response, response.indexOf("<success>") != -1);
2052
            }
2053
            else {
2054
                assertTrue(response, response.indexOf("<success>") == -1);
2055
            }
2056
            debug("deleteDocid():  response=" + response);
2057
        }
2058
        catch (MetacatInaccessibleException mie) {
2059
            fail("Metacat Inaccessible:\n" + mie.getMessage());
2060
        }
2061
        catch (InsufficientKarmaException ike) {
2062
            if(!expectedKarmaFailure){
2063
                fail("Insufficient karma:\n" + ike.getMessage());
2064
            }
2065
        }
2066
        catch (MetacatException me) {
2067
            if (result) {
2068
                fail("Metacat Error:\n" + me.getMessage());
2069
            } else {
2070
                debug("Metacat Error:\n" + me.getMessage());
2071
            }
2072
        }
2073
        catch (Exception e) {
2074
            fail("General exception:\n" + e.getMessage());
2075
        }
2076
    }
2077

    
2078
    /**
2079
     * Read a document from metacat. The expected result is passed as result
2080
     */
2081
    private void readDocid(String docid, boolean result,
2082
                           boolean expectedKarmaFailure) {
2083
    	debug("readDocid(): docid=" + docid + " expectedResult=" + result
2084
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
2085
        try {
2086
            Reader r = m.read(docid);
2087
            String response = IOUtil.getAsString(r, true);
2088

    
2089
            if (!result) {
2090
                assertTrue(response, response.indexOf("<success>") == -1);
2091
            }
2092
            debug("readDocid():  response=" + response);
2093
        }
2094
        catch (MetacatInaccessibleException mie) {
2095
            fail("Metacat Inaccessible:\n" + mie.getMessage());
2096
        }
2097
        catch (InsufficientKarmaException ike) {
2098
            if (!expectedKarmaFailure) {
2099
                fail("Insufficient karma:\n" + ike.getMessage());
2100
            }
2101
        }
2102
        catch (MetacatException me) {
2103
            fail("Metacat Error:\n" + me.getMessage());
2104
        }
2105
        catch (Exception e) {
2106
            fail("General exception:\n" + e.getMessage());
2107
        }
2108
    }
2109

    
2110
    /**
2111
     * Read a document from metacat and check if it is equal to a given string.
2112
     * The expected result is passed as result
2113
     */
2114

    
2115
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
2116
                                         boolean result,
2117
                                         boolean expectedKarmaFailure) {
2118
    	debug("readDocidWhichEqualsDoc(): docid=" + docid + " expectedResult=" + result
2119
				+ " expectedKarmaFailure=" + expectedKarmaFailure);
2120
        try {
2121
            Reader r = m.read(docid);
2122
            String doc = IOUtil.getAsString(r, true);
2123
            if (result) {
2124

    
2125
                if (!testDoc.equals(doc)) {
2126
                    System.out.println("doc       :" + doc);
2127
                    System.out.println("testDoc:" + testDoc);
2128
                }
2129

    
2130
                assertTrue(testDoc.equals(doc));
2131
            }
2132
            else {
2133
                assertTrue(doc.indexOf("<error>") != -1);
2134
            }
2135
            debug("readDocidWhichEqualsDoc():  doc=" + doc);
2136
        }
2137
        catch (MetacatInaccessibleException mie) {
2138
            fail("Metacat Inaccessible:\n" + mie.getMessage());
2139
        }
2140
        catch (InsufficientKarmaException ike) {
2141
            if (!expectedKarmaFailure) {
2142
                fail("Insufficient karma:\n" + ike.getMessage());
2143
            }
2144
        }
2145
        catch (MetacatException me) {
2146
            fail("Metacat Error:\n" + me.getMessage());
2147
        }
2148
        catch (Exception e) {
2149
            fail("General exception:\n" + e.getMessage());
2150
        }
2151

    
2152
    }
2153

    
2154
    /**
2155
     * Create a hopefully unique docid for testing insert and update. Does
2156
     * not include the 'revision' part of the id.
2157
     *
2158
     * @return a String docid based on the current date and time
2159
     */
2160
    private String generateDocid() {
2161
        StringBuffer docid = new StringBuffer(prefix);
2162
        docid.append(".");
2163

    
2164
        // Create a calendar to get the date formatted properly
2165
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
2166
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
2167
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
2168
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
2169
                       2 * 60 * 60 * 1000);
2170
        Calendar calendar = new GregorianCalendar(pdt);
2171
        Date trialTime = new Date();
2172
        calendar.setTime(trialTime);
2173
        docid.append(calendar.get(Calendar.YEAR));
2174
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
2175
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
2176
        docid.append(calendar.get(Calendar.MINUTE));
2177
        docid.append(calendar.get(Calendar.SECOND));
2178

    
2179
        return docid.toString();
2180
    }
2181
}
(1-1/18)