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-04-02 16:28:31 -0700 (Wed, 02 Apr 2008) $'
9
 * '$Revision: 3780 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest;
27

    
28
import java.io.Reader;
29
import java.io.StringReader;
30
import java.util.Calendar;
31
import java.util.Date;
32
import java.util.GregorianCalendar;
33
import java.util.SimpleTimeZone;
34
import java.util.TimeZone;
35
import java.util.Vector;
36

    
37
import edu.ucsb.nceas.metacat.MetaCatUtil;
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.utilities.IOUtil;
45
import junit.framework.Test;
46
import junit.framework.TestCase;
47
import junit.framework.TestSuite;
48
import java.io.File;
49

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

    
56
    private String metacatUrl = MetaCatUtil.getOption("metacatUrl");
57
    private String username = MetaCatUtil.getOption("mcuser");
58
    private String password = MetaCatUtil.getOption("mcpassword");
59
    private String anotheruser = MetaCatUtil.getOption("mcanotheruser");
60
    private String anotherpassword = MetaCatUtil.getOption("mcanotherpassword");
61
    private String prefix = "test";
62
    private String newdocid = null;
63
    private String testdocument = "";
64
    private String onlineDocid;
65
    private String onlinetestdatafile1 = "test/onlineDataFile1";
66
    private String onlinetestdatafile2 = "test/onlineDataFile2";
67
    private static final String ALLOWFIRST = "allowFirst";
68
    private static final String DENYFIRST = "denyFirst";
69

    
70
    private Metacat m;
71

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

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

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

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

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

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

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

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

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

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

    
157
        return accessBlock;
158

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

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

    
227
    }
228

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
353
        return suite;
354
    }
355

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

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

    
1304
            newdocid = generateDocid();
1305

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

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

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

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

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

    
1327

    
1328
            newdocid = generateDocid();
1329

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1413
            // should be able to update the document
1414

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

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

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

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

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

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

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

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

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

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

    
1472

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

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

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

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

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

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

    
1505

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

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

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

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

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

    
1543

    
1544

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

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

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

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

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

    
1583

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

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

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

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

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

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

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

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

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

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

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

    
1659
            m.logout();
1660

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

    
1676
    }
1677

    
1678

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

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

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

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

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

    
1708

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

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

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

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

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

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

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

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

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

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

    
1767
    }
1768

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

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

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

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

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

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

    
1882
        return response;
1883
    }
1884

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

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

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

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

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

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

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

    
1990
    }
1991

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

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

    
2017
        return docid.toString();
2018
    }
2019
}
(1-1/18)