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

    
26
package edu.ucsb.nceas.metacattest;
27

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

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

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

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

    
66
    private Metacat m;
67

    
68
    private boolean SUCCESS = true;
69
    private boolean FAILURE = false;
70

    
71
    /**
72
     * These variables are for eml-2.0.1 only. For other eml versions,
73
     * this function might have to modified
74
     */
75

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

    
84
    private String testEmlCreatorBlock =
85
        "<creator scope=\"document\">                                       " +
86
        " <individualName>                                                  " +
87
        "    <surName>Smith</surName>                                       " +
88
        " </individualName>                                                 " +
89
        "</creator>                                                         ";
90

    
91
    private String testEmlContactBlock =
92
        "<contact scope=\"document\">                                       " +
93
        " <individualName>                                                  " +
94
        "    <surName>Jackson</surName>                                     " +
95
        " </individualName>                                                 " +
96
        "</contact>                                                         ";
97

    
98
    private String testEmlInlineBlock1 =
99
        "<inline>                                                           " +
100
        "  <admin>                                                          " +
101
        "    <contact>                                                      " +
102
        "      <name>Operator</name>                                        " +
103
        "      <institution>PSI</institution>                               " +
104
        "    </contact>                                                     " +
105
        "  </admin>                                                         " +
106
        "</inline>                                                          ";
107

    
108
    private String testEmlInlineBlock2 =
109
        "<inline>                                                           " +
110
        "  <instrument>                                                     " +
111
        "    <instName>LCQ</instName>                                       " +
112
        "    <source type=\"ESI\"></source>                                 " +
113
        "    <detector type=\"EM\"></detector>                              " +
114
        "  </instrument>                                                    " +
115
        "</inline>                                                          ";
116

    
117
    /**
118
     * This function returns an access block based on the params passed
119
     */
120
    private String getAccessBlock(String principal, boolean grantAccess,
121
                                  boolean read, boolean write,
122
                                  boolean changePermission, boolean all) {
123
        String accessBlock = "<access " +
124
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
125
            " order=\"allowFirst\" scope=\"document\">";
126

    
127
        if (grantAccess) {
128
            accessBlock += "<allow>";
129
        }
130
        else {
131
            accessBlock += "<deny>";
132
        }
133

    
134
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
135

    
136
        if (all) {
137
            accessBlock += "<permission>all</permission>";
138
        }
139
        else {
140
            if (read) {
141
                accessBlock += "<permission>read</permission>";
142
            }
143
            if (write) {
144
                accessBlock += "<permission>write</permission>";
145
            }
146
            if (changePermission) {
147
                accessBlock += "<permission>changePermission</permission>";
148
            }
149
        }
150

    
151
        if (grantAccess) {
152
            accessBlock += "</allow>";
153
        }
154
        else {
155
            accessBlock += "</deny>";
156
        }
157
        accessBlock += "</access>";
158

    
159
        return accessBlock;
160

    
161
    }
162

    
163
    /**
164
     * This function returns a valid eml document with no access rules
165
     * This function is for eml-2.0.1 only. For other eml versions,
166
     * this function might have to modified
167
     */
168
    private String getTestEmlDoc(String title, String inlineData1,
169
                                 String inlineData2, String onlineUrl1,
170
                                 String onlineUrl2, String docAccessBlock,
171
                                 String inlineAccessBlock1,
172
                                 String inlineAccessBlock2,
173
                                 String onlineAccessBlock1,
174
                                 String onlineAccessBlock2) {
175

    
176
        String testDocument = "";
177
        testDocument = testDocument + testEmlHeader +
178
            "<dataset scope=\"document\"><title>" + title + "</title>" +
179
            testEmlCreatorBlock;
180

    
181
        if (inlineData1 != null) {
182
            testDocument = testDocument
183
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
184
                + inlineData1 + "</distribution>";
185
        }
186
        if (inlineData2 != null) {
187
            testDocument = testDocument
188
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
189
                + inlineData2 + "</distribution>";
190
        }
191
        if (onlineUrl1 != null) {
192
            testDocument = testDocument
193
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
194
                + "<online><url function=\"download\">"
195
                + onlineUrl1 + "</url></online></distribution>";
196
        }
197
        if (onlineUrl2 != null) {
198
            testDocument = testDocument +
199
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
200
                + "<online><url function=\"download\">"
201
                + onlineUrl2 + "</url></online></distribution>";
202
        }
203
        testDocument += testEmlContactBlock;
204

    
205
        if (docAccessBlock != null) {
206
            testDocument += docAccessBlock;
207
        }
208

    
209
        testDocument += "</dataset>";
210

    
211
        if (inlineAccessBlock1 != null) {
212
            testDocument += "<additionalMetadata>";
213
            testDocument += "<describes>inlineEntity1</describes>";
214
            testDocument += inlineAccessBlock1;
215
            testDocument += "</additionalMetadata>";
216
        }
217

    
218
        if (inlineAccessBlock2 != null) {
219
            testDocument += "<additionalMetadata>";
220
            testDocument += "<describes>inlineEntity2</describes>";
221
            testDocument += inlineAccessBlock2;
222
            testDocument += "</additionalMetadata>";
223
        }
224

    
225
        if (onlineAccessBlock1 != null) {
226
            testDocument += "<additionalMetadata>";
227
            testDocument += "<describes>onlineEntity1</describes>";
228
            testDocument += onlineAccessBlock1;
229
            testDocument += "</additionalMetadata>";
230
        }
231

    
232
        if (onlineAccessBlock2 != null) {
233
            testDocument += "<additionalMetadata>";
234
            testDocument += "<describes>onlineEntity2</describes>";
235
            testDocument += onlineAccessBlock2;
236
            testDocument += "</additionalMetadata>";
237
        }
238

    
239
        testDocument += "</eml:eml>";
240

    
241
        //System.out.println("Returning following document" + testDocument);
242
        return testDocument;
243
    }
244

    
245
    /**
246
     * Constructor to build the test
247
     *
248
     * @param name the name of the test method
249
     */
250
    public AccessControlTest(String name) {
251
        super(name);
252
        newdocid = generateDocid();
253
    }
254

    
255
    /**
256
     * Establish a testing framework by initializing appropriate objects
257
     */
258
    public void setUp() {
259
        try {
260
            System.err.println("Test Metacat: " + metacatUrl);
261
            m = MetacatFactory.createMetacatConnection(metacatUrl);
262
        }
263
        catch (MetacatInaccessibleException mie) {
264
            System.err.println("Metacat is: " + metacatUrl);
265
            fail("Metacat connection failed." + mie.getMessage());
266
        }
267
    }
268

    
269
    /**
270
     * Release any objects after tests are complete
271
     */
272
    public void tearDown() {
273
    }
274

    
275
    /**
276
     * Create a suite of tests to be run together
277
     */
278
    public static Test suite() {
279
        TestSuite suite = new TestSuite();
280
        suite.addTest(new AccessControlTest("initialize"));
281
        // Test basic functions
282
        suite.addTest(new AccessControlTest("documentTest"));
283
        suite.addTest(new AccessControlTest("AccessControlTestForPublic"));
284

    
285
        return suite;
286
    }
287

    
288
    /**
289
     * Run an initial test that always passes to check that the test
290
     * harness is working.
291
     */
292
    public void initialize() {
293
        assertTrue(1 == 1);
294
    }
295

    
296
    /** *********
297
     * Test the case when no access is specified and owner is logged in
298
     * No online or inline data is involved
299
     * -> an user inserts a document and is able to read it, update it,
300
     *    set permissions on it and delete it
301
     * -> another user is not able to do anything with the document
302
     *    when no access is specified for that user
303
     * -> test what all the other user can do when read only, write only and
304
     *    change permissions only permissions are specified
305
     *
306
     */
307
    public void documentTest() {
308
        try {
309
            newdocid = generateDocid();
310

    
311
            // login
312
            m.login(username, password);
313

    
314
            // insert a document
315
            testdocument = getTestEmlDoc("Testing insert", null,
316
                                         null, null,
317
                                         null, null, null, null, null, null);
318
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
319

    
320
            // read the document
321
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
322

    
323
            // update the document
324
            testdocument = getTestEmlDoc("Testing update", null, null, null,
325
                                         null, null, null, null, null, null);
326
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
327
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
328

    
329
            /////////////////////////////
330
            // check what the another user can do
331
            m.logout();
332
            m.login(anotheruser, anotherpassword);
333

    
334
            // check if the user mentioned is able to read/update/delete the document
335
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
336
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
337
            deleteDocid(newdocid + ".2", FAILURE, true);
338

    
339
            /////////////////////////////
340
            // update the document access control - read only
341
            m.logout();
342
            m.login(username, password);
343

    
344
            testdocument = getTestEmlDoc("Testing update access block",
345
                                         null, null, null,
346
                                         null, getAccessBlock(anotheruser, true,
347
                                         true, false, false, false),
348
                                         null, null, null, null);
349
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
350
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
351

    
352
            // check if the user mentioned is able to read the document
353
            m.logout();
354
            m.login(anotheruser, anotherpassword);
355
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
356

    
357
            // should not be able to update the document
358
            testdocument = getTestEmlDoc("Testing update from another user",
359
                                         null, null, null,
360
                                         null, getAccessBlock(anotheruser, true,
361
                                         true, false, false, false),
362
                                         null, null, null, null);
363
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
364

    
365
            // or the permissions
366
            testdocument = getTestEmlDoc("Testing update access block",
367
                                         null, null, null,
368
                                         null, getAccessBlock(anotheruser, true,
369
                                         false, false, false, true),
370
                                         null, null, null, null);
371
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
372

    
373
            // or delete the document
374
            deleteDocid(newdocid + ".3", FAILURE, true);
375

    
376
            m.logout();
377
            m.login(username, password);
378

    
379
            ///////////////////////////////////
380
            // update the document access control - write only
381
            testdocument = getTestEmlDoc("Testing update access block",
382
                                         null, null, null,
383
                                         null, getAccessBlock(anotheruser, true,
384
                                         false, true, false, false),
385
                                         null, null, null, null);
386
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
387
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
388
            //System.out.println(testdocument);
389
            // check if the user mentioned is able to read the document
390
            m.logout();
391
            m.login(anotheruser, anotherpassword);
392
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, true);
393

    
394
            // should be able to update the document
395

    
396
            //System.out.println(testdocument);
397
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
398

    
399
            // but not the permissions
400
            testdocument = getTestEmlDoc("Testing update access block",
401
                                         null, null, null,
402
                                         null, getAccessBlock(anotheruser, true,
403
                                         false, true, false, true),
404
                                         null, null, null, null);
405
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
406

    
407
            // try to delete the document
408
            deleteDocid(newdocid + ".5", FAILURE, true);
409
            //deleteDocid(newdocid + ".4", FAILURE, true);
410

    
411
            m.logout();
412
            m.login(username, password);
413

    
414
            /////////////////////////////////
415
            // update the document access control - change permissions only
416
            testdocument = getTestEmlDoc("Testing update access block",
417
                                         null, null, null,
418
                                         null, getAccessBlock(anotheruser, true,
419
                false, false, true, false),
420
                                         null, null, null, null);
421
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
422
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, false);
423

    
424
            // check if the user mentioned is able to read the document
425
            m.logout();
426
            m.login(anotheruser, anotherpassword);
427
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, FAILURE, true);
428

    
429
            // should not be able to update the document
430
            testdocument = getTestEmlDoc("Testing update from another user",
431
                                         null, null, null,
432
                                         null, getAccessBlock(anotheruser, true,
433
                false, false, true, false),
434
                                         null, null, null, null);
435
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
436

    
437
            // but can chg the permissions
438
            testdocument = getTestEmlDoc("Testing update access block",
439
                                         null, null, null,
440
                                         null, getAccessBlock(anotheruser, true,
441
                                         false, false, false, true),
442
                                         null, null, null, null);
443
            // ERRRRRRRROR
444
            //updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
445

    
446
            // try to delete the document
447
            //deleteDocid(newdocid + ".7", FAILURE, true);
448
            deleteDocid(newdocid + ".6", FAILURE, true);
449

    
450
            m.logout();
451
            m.login(username, password);
452

    
453

    
454
            /////////////////////////////////
455
            // update the document access control - read & write
456
            testdocument = getTestEmlDoc("Testing update access block",
457
                                         null, null, null,
458
                                         null, getAccessBlock(anotheruser, true,
459
                                         true, true, false, false),
460
                                         null, null, null, null);
461
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
462
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
463

    
464
            // check if the user mentioned is able to read the document
465
            m.logout();
466
            m.login(anotheruser, anotherpassword);
467
            readDocidWhichEqualsDoc(newdocid + ".8", testdocument, SUCCESS, false);
468

    
469
            // should be able to update the document
470
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
471

    
472
            // but cant chg the permissions
473
            testdocument = getTestEmlDoc("Testing update access block",
474
                                         null, null, null,
475
                                         null, getAccessBlock(anotheruser, true,
476
                                         false, false, false, true),
477
                                         null, null, null, null);
478
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
479

    
480
            // try to delete the document
481
            deleteDocid(newdocid + ".9", FAILURE, true);
482

    
483
            m.logout();
484
            m.login(username, password);
485

    
486

    
487
            /////////////////////////////////
488
            // update the document access control - read & change permissions
489
            testdocument = getTestEmlDoc("Testing update access block",
490
                                         null, null, null,
491
                                         null, getAccessBlock(anotheruser, true,
492
                                         true, false, true, false),
493
                                         null, null, null, null);
494
            updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
495
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
496

    
497
            // check if the user mentioned is able to read the document
498
            m.logout();
499
            m.login(anotheruser, anotherpassword);
500
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
501
            // should not be able to update the document
502
            testdocument = getTestEmlDoc("Testing update from another user",
503
                                         null, null, null,
504
                                         null, getAccessBlock(anotheruser, true,
505
                                         false, false, true, false),
506
                                         null, null, null, null);
507
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
508

    
509
            // but can chg the permissions
510
            testdocument = getTestEmlDoc("Testing update access block",
511
                                         null, null, null,
512
                                         null, getAccessBlock(anotheruser, true,
513
                                         false, false, false, true),
514
                                         null, null, null, null);
515
            //updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
516

    
517
            // try to delete the document
518
            // deleteDocid(newdocid + ".11", FAILURE, true);
519
            deleteDocid(newdocid + ".10", FAILURE, true);
520

    
521
            m.logout();
522
            m.login(username, password);
523

    
524

    
525

    
526
            /////////////////////////////////
527
            // update the document access control - read & change permissions
528
            testdocument = getTestEmlDoc("Testing update access block",
529
                                         null, null, null,
530
                                         null, getAccessBlock(anotheruser, true,
531
                                         true, false, true, false),
532
                                         null, null, null, null);
533
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
534
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument, SUCCESS, false);
535

    
536
            // check if the user mentioned is able to read the document
537
            m.logout();
538
            m.login(anotheruser, anotherpassword);
539
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument,  SUCCESS, false);
540

    
541
            // should not be able to update the document
542
            testdocument = getTestEmlDoc("Testing update from another user",
543
                                         null, null, null,
544
                                         null, getAccessBlock(anotheruser, true,
545
                                         false, false, true, false),
546
                                         null, null, null, null);
547
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
548
            // but can chg the permissions
549
            testdocument = getTestEmlDoc("Testing update from another user",
550
                                         null, null, null,
551
                                         null, getAccessBlock(anotheruser, true,
552
                                         false, false, false, true),
553
                                         null, null, null, null);
554
            // ERRRRRRRRRRRRRRRR
555
            // updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
556

    
557
            // try to delete the document
558
            //deleteDocid(newdocid + ".13", FAILURE, true);
559
            deleteDocid(newdocid + ".12", FAILURE, true);
560

    
561
            m.logout();
562
            m.login(username, password);
563

    
564

    
565
            /////////////////////////////////
566
            // update the document access control - R, W, CP
567
            testdocument = getTestEmlDoc("Testing update access block",
568
                                         null, null, null,
569
                                         null, getAccessBlock(anotheruser, true,
570
                                         true, true, true, false),
571
                                         null, null, null, null);
572
            updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
573
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
574

    
575
            // check if the user mentioned is able to read the document
576
            m.logout();
577
            m.login(anotheruser, anotherpassword);
578
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
579

    
580
            // should not be able to update the document
581
            testdocument = getTestEmlDoc("Testing update from another user",
582
                                         null, null, null,
583
                                         null, getAccessBlock(anotheruser, true,
584
                false, false, false, true),
585
                                         null, null, null, null);
586
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
587
            readDocidWhichEqualsDoc(newdocid + ".15", testdocument, SUCCESS, false);
588

    
589
            // but can chg the permissions
590
            testdocument = getTestEmlDoc("Testing update from another user",
591
                                         null, null, null,
592
                                         null, getAccessBlock(anotheruser, true,
593
                                         true, false, false, false),
594
                                         null, null, null, null);
595
            updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
596
            readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
597

    
598
            // try to delete the document
599
            deleteDocid(newdocid + ".16", FAILURE, true);
600

    
601
            m.logout();
602
            m.login(username, password);
603

    
604
            /////////////////////////////////
605
            // update the document access control - all
606
            testdocument = getTestEmlDoc("Testing update access block",
607
                                         null, null, null,
608
                                         null, getAccessBlock(anotheruser, true,
609
                                         false, false, false, true),
610
                                         null, null, null, null);
611
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
612
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
613

    
614
            // check if the user mentioned is able to read the document
615
            m.logout();
616
            m.login(anotheruser, anotherpassword);
617
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
618

    
619
            // should not be able to update the document
620
            testdocument = getTestEmlDoc("Testing update from another user",
621
                                         null, null, null,
622
                                         null, getAccessBlock(anotheruser, true,
623
                                         false, false, false, true),
624
                                         null, null, null, null);
625
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
626
            readDocidWhichEqualsDoc(newdocid + ".18", testdocument, SUCCESS, false);
627

    
628
            // but can chg the permissions
629
            testdocument = getTestEmlDoc("Testing update from another user",
630
                                         null, null, null,
631
                                         null, getAccessBlock(anotheruser, true,
632
                                         true, false, false, false),
633
                                         null, null, null, null);
634
            updateDocid(newdocid + ".19", testdocument, SUCCESS, false);
635
            readDocidWhichEqualsDoc(newdocid + ".19", testdocument, SUCCESS, false);
636

    
637
            // try to delete the document
638
            deleteDocid(newdocid + ".19", FAILURE, true);
639

    
640
            m.logout();
641

    
642
            // delete the document
643
            m.login(username, password);
644
            deleteDocid(newdocid + ".19", SUCCESS, false);
645
            m.logout();
646
        }
647
        catch (MetacatAuthException mae) {
648
            fail("Authorization failed:\n" + mae.getMessage());
649
        }
650
        catch (MetacatInaccessibleException mie) {
651
            fail("Metacat Inaccessible:\n" + mie.getMessage());
652
        }
653
        catch (Exception e) {
654
            fail("General exception:\n" + e.getMessage());
655
        }
656

    
657
    }
658

    
659

    
660
   /** *********
661
     * Test the case when no access is specified and public is logged in
662
     * Cases being checked:
663
     * 1. public tries to read the document - Failure.
664
     * 2. public tries to update the document - Failure.
665
     * 3. public tries to update the inline data - Failure.
666
     * 4. public tries to update the online data file - Failure.
667
     * 5. public tries to update the access rules for the document - Failure.
668
     * 6. public tries to update the access rules for the inline data - Failure.
669
     * 7. public tries to update the access rules for the online data - Failure.
670
     * 8. public tries to delete the document - Failure.
671
     */
672
    public void AccessControlTestForPublic() {
673
        try {
674

    
675
            String accessBlock = getAccessBlock(anotheruser, true,
676
                                         true, false, false, false);
677
            newdocid = generateDocid();
678

    
679
            // login
680
            m.login(username, password);
681

    
682
            onlineDocid = generateDocid();
683
            uploadDocid(onlineDocid + ".1",onlinetestdatafile1, SUCCESS, false);
684

    
685
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
686
                                     null,"ecogrid://knb/" + onlineDocid + ".1",
687
                                     null, null, null, null, null, null);
688

    
689

    
690
            // insert a document - get the docid
691
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
692
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
693
                                    false);
694

    
695
            // logoutand login as other user
696
            m.logout();
697

    
698
            // read the document
699
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, true);
700

    
701
            // update the document
702
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
703

    
704
            // update the inline data
705
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock2,
706
                         null,"ecogrid://knb/" + onlineDocid + ".1",
707
                         null, null, null, null, null, null);
708
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
709

    
710
            // update the online data
711
            uploadDocid(onlineDocid + ".2", onlinetestdatafile1, FAILURE, false);
712

    
713
            // update the document access control
714
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
715
                         null,"ecogrid://knb/" + onlineDocid + ".1",
716
                         null, accessBlock, null, null, null, null);
717
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
718

    
719
            // update the document access control for inline data
720
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
721
                         null,"ecogrid://knb/" + onlineDocid + ".1",
722
                         null, null, accessBlock, null, null, null);
723
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
724

    
725
            // update the document access control for online data
726
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
727
                         null,"ecogrid://knb/" + onlineDocid + ".1",
728
                         null, null, null, null, accessBlock, null);
729
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
730

    
731
            // delete the document
732
            deleteDocid(newdocid + ".2", FAILURE, true);
733
            m.logout();
734
        }
735
        catch (MetacatAuthException mae) {
736
            fail("Authorization failed:\n" + mae.getMessage());
737
        }
738
        catch (MetacatInaccessibleException mie) {
739
            fail("Metacat Inaccessible:\n" + mie.getMessage());
740
        }
741
        catch (MetacatException me) {
742
            fail("Metacat Error:\n" + me.getMessage());
743
        }
744
        catch (Exception e) {
745
            fail("General exception:\n" + e.getMessage());
746
        }
747

    
748
    }
749

    
750
    /**
751
     * Insert a document into metacat. The expected result is passed as result
752
     */
753

    
754
    private String insertDocid(String docid, String docText, boolean result,
755
                               boolean expectKarmaException) {
756
        String response = null;
757
        try {
758
            response = m.insert(docid,
759
                                new StringReader(testdocument), null);
760
            if (result) {
761
                assertTrue( (response.indexOf("<success>") != -1));
762
                assertTrue(response.indexOf(docid) != -1);
763
            }
764
            else {
765
                assertTrue( (response.indexOf("<success>") == -1));
766
            }
767
            System.err.println(response);
768
        }
769
        catch (MetacatInaccessibleException mie) {
770
            fail("Metacat Inaccessible:\n" + mie.getMessage());
771
        }
772
        catch (InsufficientKarmaException ike) {
773
            if (!expectKarmaException) {
774
                fail("Insufficient karma:\n" + ike.getMessage());
775
            }
776
        }
777
        catch (MetacatException me) {
778
            fail("Metacat Error:\n" + me.getMessage());
779
        }
780
        catch (Exception e) {
781
            fail("General exception:\n" + e.getMessage());
782
        }
783
        return response;
784
    }
785

    
786
    /**
787
     * Insert a document into metacat. The expected result is passed as result
788
     */
789

    
790
    private String uploadDocid(String docid, String filePath, boolean result,
791
                               boolean expectedKarmaException) {
792
        String response = null;
793
        try {
794
            response = m.upload(docid, new File(filePath));
795
            if (result) {
796
                assertTrue( (response.indexOf("<success>") != -1));
797
                assertTrue(response.indexOf(docid) != -1);
798
            }
799
            else {
800
                assertTrue( (response.indexOf("<success>") == -1));
801
            }
802
            System.err.println("respose from metacat: " + response);
803
        }
804
        catch (MetacatInaccessibleException mie) {
805
            fail("Metacat Inaccessible:\n" + mie.getMessage());
806
        }
807
        catch (InsufficientKarmaException ike) {
808
            if (!expectedKarmaException) {
809
                fail("Insufficient karma:\n" + ike.getMessage());
810
            }
811
        }
812
        catch (MetacatException me) {
813
            if (result) {
814
                fail("Metacat Error:\n" + me.getMessage());
815
            } else {
816
                System.err.println("Metacat Error:\n" + me.getMessage());
817
            }
818
        }
819
        catch (Exception e) {
820
            fail("General exception:\n" + e.getMessage());
821
        }
822
        return response;
823
    }
824

    
825
    /**
826
     * Update a document in metacat. The expected result is passed as result
827
     */
828
    private String updateDocid(String docid, String docText, boolean result,
829
                               boolean expectedKarmaFailure) {
830
        String response = null;
831
        try {
832
            response = m.update(docid,
833
                                new StringReader(testdocument), null);
834

    
835
            if (result) {
836
                assertTrue( (response.indexOf("<success>") != -1));
837
                assertTrue(response.indexOf(docid) != -1);
838
            }
839
            else {
840
                assertTrue( (response.indexOf("<success>") == -1));
841
            }
842
            System.err.println(response);
843
        }
844
        catch (MetacatInaccessibleException mie) {
845
            fail("Metacat Inaccessible:\n" + mie.getMessage());
846
        }
847
        catch (InsufficientKarmaException ike) {
848
            if (!expectedKarmaFailure) {
849
                fail("Insufficient karma:\n" + ike.getMessage());
850
            }
851
        }
852
        catch (MetacatException me) {
853
            if (result) {
854
                fail("Metacat Error:\n" + me.getMessage());
855
            } else {
856
                System.err.println("Metacat Error:\n" + me.getMessage());
857
            }
858
        }
859
        catch (Exception e) {
860
            fail("General exception:\n" + e.getMessage());
861
        }
862

    
863
        return response;
864
    }
865

    
866
    /**
867
     * Delete a document into metacat. The expected result is passed as result
868
     */
869
    private void deleteDocid(String docid, boolean result,
870
                             boolean expextedKarmaFailure) {
871
        try {
872
            String response = m.delete(docid);
873
            if (result) {
874
                assertTrue(response.indexOf("<success>") != -1);
875
            }
876
            else {
877
                assertTrue(response.indexOf("<success>") == -1);
878
            }
879
            System.err.println(response);
880
        }
881
        catch (MetacatInaccessibleException mie) {
882
            fail("Metacat Inaccessible:\n" + mie.getMessage());
883
        }
884
        catch (InsufficientKarmaException ike) {
885
            if(!expextedKarmaFailure){
886
                fail("Insufficient karma:\n" + ike.getMessage());
887
            }
888
        }
889
        catch (MetacatException me) {
890
            if (result) {
891
                fail("Metacat Error:\n" + me.getMessage());
892
            } else {
893
                System.err.println("Metacat Error:\n" + me.getMessage());
894
            }
895
        }
896
        catch (Exception e) {
897
            fail("General exception:\n" + e.getMessage());
898
        }
899
    }
900

    
901
    /**
902
     * Read a document from metacat. The expected result is passed as result
903
     */
904
    private void readDocid(String docid, boolean result,
905
                           boolean expextedKarmaFailure) {
906
        try {
907
            Reader r = m.read(docid);
908
            String response = IOUtil.getAsString(r, true);
909

    
910
            if (!result) {
911
                assertTrue(response.indexOf("<success>") == -1);
912
            }
913
            // System.err.println(response);
914
        }
915
        catch (MetacatInaccessibleException mie) {
916
            fail("Metacat Inaccessible:\n" + mie.getMessage());
917
        }
918
        catch (InsufficientKarmaException ike) {
919
            if (!expextedKarmaFailure) {
920
                fail("Insufficient karma:\n" + ike.getMessage());
921
            }
922
        }
923
        catch (MetacatException me) {
924
            fail("Metacat Error:\n" + me.getMessage());
925
        }
926
        catch (Exception e) {
927
            fail("General exception:\n" + e.getMessage());
928
        }
929
    }
930

    
931
    /**
932
     * Read a document from metacat and check if it is equal to a given string.
933
     * The expected result is passed as result
934
     */
935

    
936
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
937
                                         boolean result,
938
                                         boolean expextedKarmaFailure) {
939
        try {
940
            Reader r = m.read(docid);
941
            String doc = IOUtil.getAsString(r, true);
942
            if (result) {
943

    
944
                if (!testDoc.equals(doc)) {
945
                    System.out.println("doc    :" + doc);
946
                    System.out.println("testDoc:" + testDoc);
947
                }
948

    
949
                assertTrue(testDoc.equals(doc));
950
            }
951
            else {
952
                assertTrue(doc.indexOf("<error>") != -1);
953
            }
954
        }
955
        catch (MetacatInaccessibleException mie) {
956
            fail("Metacat Inaccessible:\n" + mie.getMessage());
957
        }
958
        catch (InsufficientKarmaException ike) {
959
            if (!expextedKarmaFailure) {
960
                fail("Insufficient karma:\n" + ike.getMessage());
961
            }
962
        }
963
        catch (MetacatException me) {
964
            fail("Metacat Error:\n" + me.getMessage());
965
        }
966
        catch (Exception e) {
967
            fail("General exception:\n" + e.getMessage());
968
        }
969

    
970
    }
971

    
972
    /**
973
     * Create a hopefully unique docid for testing insert and update. Does
974
     * not include the 'revision' part of the id.
975
     *
976
     * @return a String docid based on the current date and time
977
     */
978
    private String generateDocid() {
979
        StringBuffer docid = new StringBuffer(prefix);
980
        docid.append(".");
981

    
982
        // Create a calendar to get the date formatted properly
983
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
984
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
985
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
986
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
987
                       2 * 60 * 60 * 1000);
988
        Calendar calendar = new GregorianCalendar(pdt);
989
        Date trialTime = new Date();
990
        calendar.setTime(trialTime);
991
        docid.append(calendar.get(Calendar.YEAR));
992
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
993
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
994
        docid.append(calendar.get(Calendar.MINUTE));
995
        docid.append(calendar.get(Calendar.SECOND));
996

    
997
        return docid.toString();
998
    }
999
}
(1-1/10)