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-14 15:57:44 -0700 (Tue, 14 Sep 2004) $'
9
 * '$Revision: 2275 $'
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 testEml_201_Header =
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 testEml_200_Header =
85
    "<?xml version=\"1.0\"?><eml:eml" +
86
    " xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"" +
87
    " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
88
    " packageId=\"eml.1.1\" system=\"knb\"" +
89
    " xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\"" +
90
    " scope=\"system\">";
91

    
92
    private String testEmlCreatorBlock =
93
        "<creator scope=\"document\">                                       " +
94
        " <individualName>                                                  " +
95
        "    <surName>Smith</surName>                                       " +
96
        " </individualName>                                                 " +
97
        "</creator>                                                         ";
98

    
99
    private String testEmlContactBlock =
100
        "<contact scope=\"document\">                                       " +
101
        " <individualName>                                                  " +
102
        "    <surName>Jackson</surName>                                     " +
103
        " </individualName>                                                 " +
104
        "</contact>                                                         ";
105

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

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

    
125
    /**
126
     * This function returns an access block based on the params passed
127
     */
128
    private String getAccessBlock(String principal, boolean grantAccess,
129
                                  boolean read, boolean write,
130
                                  boolean changePermission, boolean all) {
131
        String accessBlock = "<access " +
132
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
133
            " order=\"allowFirst\" scope=\"document\">";
134

    
135
        if (grantAccess) {
136
            accessBlock += "<allow>";
137
        }
138
        else {
139
            accessBlock += "<deny>";
140
        }
141

    
142
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
143

    
144
        if (all) {
145
            accessBlock += "<permission>all</permission>";
146
        }
147
        else {
148
            if (read) {
149
                accessBlock += "<permission>read</permission>";
150
            }
151
            if (write) {
152
                accessBlock += "<permission>write</permission>";
153
            }
154
            if (changePermission) {
155
                accessBlock += "<permission>changePermission</permission>";
156
            }
157
        }
158

    
159
        if (grantAccess) {
160
            accessBlock += "</allow>";
161
        }
162
        else {
163
            accessBlock += "</deny>";
164
        }
165
        accessBlock += "</access>";
166

    
167
        return accessBlock;
168

    
169
    }
170

    
171
    /**
172
     * This function returns a valid eml document with no access rules
173
     * This function is for eml-2.0.1 only. For other eml versions,
174
     * this function might have to modified
175
     */
176
    private String getTestEmlDoc(String title, String inlineData1,
177
                                 String inlineData2, String onlineUrl1,
178
                                 String onlineUrl2, String docAccessBlock,
179
                                 String inlineAccessBlock1,
180
                                 String inlineAccessBlock2,
181
                                 String onlineAccessBlock1,
182
                                 String onlineAccessBlock2) {
183

    
184
        String testDocument = "";
185
        testDocument = testDocument + testEml_201_Header +
186
            "<dataset scope=\"document\"><title>" + title + "</title>" +
187
            testEmlCreatorBlock;
188

    
189
        if (inlineData1 != null) {
190
            testDocument = testDocument
191
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
192
                + inlineData1 + "</distribution>";
193
        }
194
        if (inlineData2 != null) {
195
            testDocument = testDocument
196
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
197
                + inlineData2 + "</distribution>";
198
        }
199
        if (onlineUrl1 != null) {
200
            testDocument = testDocument
201
                + "<distribution scope=\"document\" id=\"onlineEntity1\">"
202
                + "<online><url function=\"download\">"
203
                + onlineUrl1 + "</url></online></distribution>";
204
        }
205
        if (onlineUrl2 != null) {
206
            testDocument = testDocument +
207
                "<distribution scope=\"document\" id=\"onlineEntity2\">"
208
                + "<online><url function=\"download\">"
209
                + onlineUrl2 + "</url></online></distribution>";
210
        }
211
        testDocument += testEmlContactBlock;
212

    
213
        if (docAccessBlock != null) {
214
            testDocument += docAccessBlock;
215
        }
216

    
217
        testDocument += "</dataset>";
218

    
219
        if (inlineAccessBlock1 != null) {
220
            testDocument += "<additionalMetadata>";
221
            testDocument += "<describes>inlineEntity1</describes>";
222
            testDocument += inlineAccessBlock1;
223
            testDocument += "</additionalMetadata>";
224
        }
225

    
226
        if (inlineAccessBlock2 != null) {
227
            testDocument += "<additionalMetadata>";
228
            testDocument += "<describes>inlineEntity2</describes>";
229
            testDocument += inlineAccessBlock2;
230
            testDocument += "</additionalMetadata>";
231
        }
232

    
233
        if (onlineAccessBlock1 != null) {
234
            testDocument += "<additionalMetadata>";
235
            testDocument += "<describes>onlineEntity1</describes>";
236
            testDocument += onlineAccessBlock1;
237
            testDocument += "</additionalMetadata>";
238
        }
239

    
240
        if (onlineAccessBlock2 != null) {
241
            testDocument += "<additionalMetadata>";
242
            testDocument += "<describes>onlineEntity2</describes>";
243
            testDocument += onlineAccessBlock2;
244
            testDocument += "</additionalMetadata>";
245
        }
246

    
247
        testDocument += "</eml:eml>";
248

    
249
        //System.out.println("Returning following document" + testDocument);
250
        return testDocument;
251
    }
252

    
253
    /**
254
     * Constructor to build the test
255
     *
256
     * @param name the name of the test method
257
     */
258
    public AccessControlTest(String name) {
259
        super(name);
260
        newdocid = generateDocid();
261
    }
262

    
263
    /**
264
     * Establish a testing framework by initializing appropriate objects
265
     */
266
    public void setUp() {
267
        try {
268
            System.err.println("Test Metacat: " + metacatUrl);
269
            m = MetacatFactory.createMetacatConnection(metacatUrl);
270
        }
271
        catch (MetacatInaccessibleException mie) {
272
            System.err.println("Metacat is: " + metacatUrl);
273
            fail("Metacat connection failed." + mie.getMessage());
274
        }
275
    }
276

    
277
    /**
278
     * Release any objects after tests are complete
279
     */
280
    public void tearDown() {
281
    }
282

    
283
    /**
284
     * Create a suite of tests to be run together
285
     */
286
    public static Test suite() {
287
        TestSuite suite = new TestSuite();
288
        suite.addTest(new AccessControlTest("initialize"));
289
        // Test basic functions
290
        suite.addTest(new AccessControlTest("documentTest"));
291
        suite.addTest(new AccessControlTest("AccessControlTestForPublic"));
292

    
293
        return suite;
294
    }
295

    
296
    /**
297
     * Run an initial test that always passes to check that the test
298
     * harness is working.
299
     */
300
    public void initialize() {
301
        assertTrue(1 == 1);
302
    }
303

    
304
    /** *********
305
     * Test the case when no access is specified and owner is logged in
306
     * No online or inline data is involved
307
     * -> an user inserts a document and is able to read it, update it,
308
     *    set permissions on it and delete it
309
     * -> another user is not able to do anything with the document
310
     *    when no access is specified for that user
311
     * -> test what all the other user can do when read only, write only and
312
     *    change permissions only permissions are specified
313
     *
314
     */
315
    public void documentTest() {
316
        try {
317

    
318
            newdocid = generateDocid();
319

    
320
            // login
321
            m.login(username, password);
322

    
323
            // insert a 2.0.0 document
324
            testdocument = testEml_200_Header +
325
                "<dataset scope=\"document\"><title>submitting eml2000</title>" +
326
            testEmlCreatorBlock + testEmlContactBlock + "</dataset></eml:eml>";
327

    
328
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
329

    
330
            // read the document
331
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
332

    
333
            // update it with 2.0.1 document
334
            testdocument = getTestEmlDoc("Updating eml200 with eml201",
335
                                         null, null, null, null,
336
                                         null, null, null, null, null);
337
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
338
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
339
            m.logout();
340

    
341

    
342
            newdocid = generateDocid();
343

    
344
            // login
345
            m.login(username, password);
346

    
347
            // insert a document
348
            testdocument = getTestEmlDoc("Testing insert", null,
349
                                         null, null,
350
                                         null, null, null, null, null, null);
351
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
352

    
353
            // read the document
354
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
355

    
356
            // update the document
357
            testdocument = getTestEmlDoc("Testing update", null, null, null,
358
                                         null, null, null, null, null, null);
359
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
360
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, false);
361

    
362
            /////////////////////////////
363
            // check what the another user can do
364
            m.logout();
365
            m.login(anotheruser, anotherpassword);
366

    
367
            // check if the user mentioned is able to read/update/delete the document
368
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, true);
369
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
370
            deleteDocid(newdocid + ".2", FAILURE, true);
371

    
372
            /////////////////////////////
373
            // update the document access control - read only
374
            m.logout();
375
            m.login(username, password);
376

    
377
            testdocument = getTestEmlDoc("Testing update access block",
378
                                         null, null, null,
379
                                         null, getAccessBlock(anotheruser, true,
380
                                         true, false, false, false),
381
                                         null, null, null, null);
382
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
383
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
384

    
385
            // check if the user mentioned is able to read the document
386
            m.logout();
387
            m.login(anotheruser, anotherpassword);
388
            readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, false);
389

    
390
            // should not be able to update the document
391
            testdocument = getTestEmlDoc("Testing update from another user",
392
                                         null, null, null,
393
                                         null, getAccessBlock(anotheruser, true,
394
                                         true, false, false, false),
395
                                         null, null, null, null);
396
            updateDocid(newdocid + ".4", testdocument, FAILURE, true);
397

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

    
406
            // or delete the document
407
            deleteDocid(newdocid + ".3", FAILURE, true);
408

    
409
            m.logout();
410
            m.login(username, password);
411

    
412
            ///////////////////////////////////
413
            // update the document access control - write only
414
            testdocument = getTestEmlDoc("Testing update access block",
415
                                         null, null, null,
416
                                         null, getAccessBlock(anotheruser, true,
417
                                         false, true, false, false),
418
                                         null, null, null, null);
419
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
420
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, false);
421
            //System.out.println(testdocument);
422
            // check if the user mentioned is able to read the document
423
            m.logout();
424
            m.login(anotheruser, anotherpassword);
425
            readDocidWhichEqualsDoc(newdocid + ".4", testdocument, FAILURE, true);
426

    
427
            // should be able to update the document
428

    
429
            //System.out.println(testdocument);
430
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
431

    
432
            // but not the permissions
433
            testdocument = getTestEmlDoc("Testing update access block",
434
                                         null, null, null,
435
                                         null, getAccessBlock(anotheruser, true,
436
                                         false, true, false, true),
437
                                         null, null, null, null);
438
            updateDocid(newdocid + ".6", testdocument, FAILURE, true);
439

    
440
            // try to delete the document
441
            deleteDocid(newdocid + ".5", FAILURE, true);
442
            //deleteDocid(newdocid + ".4", FAILURE, true);
443

    
444
            m.logout();
445
            m.login(username, password);
446

    
447
            /////////////////////////////////
448
            // update the document access control - change permissions only
449
            testdocument = getTestEmlDoc("Testing update access block",
450
                                         null, null, null,
451
                                         null, getAccessBlock(anotheruser, true,
452
                false, false, true, false),
453
                                         null, null, null, null);
454
            updateDocid(newdocid + ".6", testdocument, SUCCESS, false);
455
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS, false);
456

    
457
            // check if the user mentioned is able to read the document
458
            m.logout();
459
            m.login(anotheruser, anotherpassword);
460
            readDocidWhichEqualsDoc(newdocid + ".6", testdocument, FAILURE, true);
461

    
462
            // should not be able to update the document
463
            testdocument = getTestEmlDoc("Testing update from another user",
464
                                         null, null, null,
465
                                         null, getAccessBlock(anotheruser, true,
466
                false, false, true, false),
467
                                         null, null, null, null);
468
            updateDocid(newdocid + ".7", testdocument, FAILURE, true);
469

    
470
            // but can chg the permissions
471
            testdocument = getTestEmlDoc("Testing update access block",
472
                                         null, null, null,
473
                                         null, getAccessBlock(anotheruser, true,
474
                                         false, false, false, true),
475
                                         null, null, null, null);
476
            // ERRRRRRRROR
477
            //updateDocid(newdocid + ".7", testdocument, SUCCESS, false);
478

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

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

    
486

    
487
            /////////////////////////////////
488
            // update the document access control - read & write
489
            testdocument = getTestEmlDoc("Testing update access block",
490
                                         null, null, null,
491
                                         null, getAccessBlock(anotheruser, true,
492
                                         true, true, false, false),
493
                                         null, null, null, null);
494
            updateDocid(newdocid + ".8", testdocument, SUCCESS, false);
495
            readDocidWhichEqualsDoc(newdocid + ".8", 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 + ".8", testdocument, SUCCESS, false);
501

    
502
            // should be able to update the document
503
            updateDocid(newdocid + ".9", testdocument, SUCCESS, false);
504

    
505
            // but cant chg the permissions
506
            testdocument = getTestEmlDoc("Testing update access block",
507
                                         null, null, null,
508
                                         null, getAccessBlock(anotheruser, true,
509
                                         false, false, false, true),
510
                                         null, null, null, null);
511
            updateDocid(newdocid + ".10", testdocument, FAILURE, true);
512

    
513
            // try to delete the document
514
            deleteDocid(newdocid + ".9", FAILURE, true);
515

    
516
            m.logout();
517
            m.login(username, password);
518

    
519

    
520
            /////////////////////////////////
521
            // update the document access control - read & change permissions
522
            testdocument = getTestEmlDoc("Testing update access block",
523
                                         null, null, null,
524
                                         null, getAccessBlock(anotheruser, true,
525
                                         true, false, true, false),
526
                                         null, null, null, null);
527
            updateDocid(newdocid + ".10", testdocument, SUCCESS, false);
528
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
529

    
530
            // check if the user mentioned is able to read the document
531
            m.logout();
532
            m.login(anotheruser, anotherpassword);
533
            readDocidWhichEqualsDoc(newdocid + ".10", testdocument, SUCCESS, false);
534
            // should not be able to update the document
535
            testdocument = getTestEmlDoc("Testing update from another user",
536
                                         null, null, null,
537
                                         null, getAccessBlock(anotheruser, true,
538
                                         false, false, true, false),
539
                                         null, null, null, null);
540
            updateDocid(newdocid + ".11", testdocument, FAILURE, true);
541

    
542
            // but can chg the permissions
543
            testdocument = getTestEmlDoc("Testing update access block",
544
                                         null, null, null,
545
                                         null, getAccessBlock(anotheruser, true,
546
                                         false, false, false, true),
547
                                         null, null, null, null);
548
            //updateDocid(newdocid + ".11", testdocument, SUCCESS, false);
549

    
550
            // try to delete the document
551
            // deleteDocid(newdocid + ".11", FAILURE, true);
552
            deleteDocid(newdocid + ".10", FAILURE, true);
553

    
554
            m.logout();
555
            m.login(username, password);
556

    
557

    
558

    
559
            /////////////////////////////////
560
            // update the document access control - read & change permissions
561
            testdocument = getTestEmlDoc("Testing update access block",
562
                                         null, null, null,
563
                                         null, getAccessBlock(anotheruser, true,
564
                                         true, false, true, false),
565
                                         null, null, null, null);
566
            updateDocid(newdocid + ".12", testdocument, SUCCESS, false);
567
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument, SUCCESS, false);
568

    
569
            // check if the user mentioned is able to read the document
570
            m.logout();
571
            m.login(anotheruser, anotherpassword);
572
            readDocidWhichEqualsDoc(newdocid + ".12", testdocument,  SUCCESS, false);
573

    
574
            // should not be able to update the document
575
            testdocument = getTestEmlDoc("Testing update from another user",
576
                                         null, null, null,
577
                                         null, getAccessBlock(anotheruser, true,
578
                                         false, false, true, false),
579
                                         null, null, null, null);
580
            updateDocid(newdocid + ".13", testdocument, FAILURE, true);
581
            // but can chg the permissions
582
            testdocument = getTestEmlDoc("Testing update from another user",
583
                                         null, null, null,
584
                                         null, getAccessBlock(anotheruser, true,
585
                                         false, false, false, true),
586
                                         null, null, null, null);
587
            // ERRRRRRRRRRRRRRRR
588
            // updateDocid(newdocid + ".13", testdocument, SUCCESS, false);
589

    
590
            // try to delete the document
591
            //deleteDocid(newdocid + ".13", FAILURE, true);
592
            deleteDocid(newdocid + ".12", FAILURE, true);
593

    
594
            m.logout();
595
            m.login(username, password);
596

    
597

    
598
            /////////////////////////////////
599
            // update the document access control - R, W, CP
600
            testdocument = getTestEmlDoc("Testing update access block",
601
                                         null, null, null,
602
                                         null, getAccessBlock(anotheruser, true,
603
                                         true, true, true, false),
604
                                         null, null, null, null);
605
            updateDocid(newdocid + ".14", testdocument, SUCCESS, false);
606
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
607

    
608
            // check if the user mentioned is able to read the document
609
            m.logout();
610
            m.login(anotheruser, anotherpassword);
611
            readDocidWhichEqualsDoc(newdocid + ".14", testdocument, SUCCESS, false);
612

    
613
            // should not be able to update the document
614
            testdocument = getTestEmlDoc("Testing update from another user",
615
                                         null, null, null,
616
                                         null, getAccessBlock(anotheruser, true,
617
                false, false, false, true),
618
                                         null, null, null, null);
619
            updateDocid(newdocid + ".15", testdocument, SUCCESS, false);
620
            readDocidWhichEqualsDoc(newdocid + ".15", testdocument, SUCCESS, false);
621

    
622
            // but can chg the permissions
623
            testdocument = getTestEmlDoc("Testing update from another user",
624
                                         null, null, null,
625
                                         null, getAccessBlock(anotheruser, true,
626
                                         true, false, false, false),
627
                                         null, null, null, null);
628
            updateDocid(newdocid + ".16", testdocument, SUCCESS, false);
629
            readDocidWhichEqualsDoc(newdocid + ".16", testdocument, SUCCESS, false);
630

    
631
            // try to delete the document
632
            deleteDocid(newdocid + ".16", FAILURE, true);
633

    
634
            m.logout();
635
            m.login(username, password);
636

    
637
            /////////////////////////////////
638
            // update the document access control - all
639
            testdocument = getTestEmlDoc("Testing update access block",
640
                                         null, null, null,
641
                                         null, getAccessBlock(anotheruser, true,
642
                                         false, false, false, true),
643
                                         null, null, null, null);
644
            updateDocid(newdocid + ".17", testdocument, SUCCESS, false);
645
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
646

    
647
            // check if the user mentioned is able to read the document
648
            m.logout();
649
            m.login(anotheruser, anotherpassword);
650
            readDocidWhichEqualsDoc(newdocid + ".17", testdocument, SUCCESS, false);
651

    
652
            // should not be able to update the document
653
            testdocument = getTestEmlDoc("Testing update from another user",
654
                                         null, null, null,
655
                                         null, getAccessBlock(anotheruser, true,
656
                                         false, false, false, true),
657
                                         null, null, null, null);
658
            updateDocid(newdocid + ".18", testdocument, SUCCESS, false);
659
            readDocidWhichEqualsDoc(newdocid + ".18", testdocument, SUCCESS, false);
660

    
661
            // but can chg the permissions
662
            testdocument = getTestEmlDoc("Testing update from another user",
663
                                         null, null, null,
664
                                         null, getAccessBlock(anotheruser, true,
665
                                         true, false, false, false),
666
                                         null, null, null, null);
667
            updateDocid(newdocid + ".19", testdocument, SUCCESS, false);
668
            readDocidWhichEqualsDoc(newdocid + ".19", testdocument, SUCCESS, false);
669

    
670
            // try to delete the document
671
            deleteDocid(newdocid + ".19", FAILURE, true);
672

    
673
            m.logout();
674

    
675
            // delete the document
676
            m.login(username, password);
677
            deleteDocid(newdocid + ".19", SUCCESS, false);
678
            m.logout();
679
        }
680
        catch (MetacatAuthException mae) {
681
            fail("Authorization failed:\n" + mae.getMessage());
682
        }
683
        catch (MetacatInaccessibleException mie) {
684
            fail("Metacat Inaccessible:\n" + mie.getMessage());
685
        }
686
        catch (Exception e) {
687
            fail("General exception:\n" + e.getMessage());
688
        }
689

    
690
    }
691

    
692

    
693
   /** *********
694
     * Test the case when no access is specified and public is logged in
695
     * Cases being checked:
696
     * 1. public tries to read the document - Failure.
697
     * 2. public tries to update the document - Failure.
698
     * 3. public tries to update the inline data - Failure.
699
     * 4. public tries to update the online data file - Failure.
700
     * 5. public tries to update the access rules for the document - Failure.
701
     * 6. public tries to update the access rules for the inline data - Failure.
702
     * 7. public tries to update the access rules for the online data - Failure.
703
     * 8. public tries to delete the document - Failure.
704
     */
705
    public void AccessControlTestForPublic() {
706
        try {
707

    
708
            String accessBlock = getAccessBlock(anotheruser, true,
709
                                         true, false, false, false);
710
            newdocid = generateDocid();
711

    
712
            // login
713
            m.login(username, password);
714

    
715
            onlineDocid = generateDocid();
716
            uploadDocid(onlineDocid + ".1",onlinetestdatafile1, SUCCESS, false);
717

    
718
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
719
                                     null,"ecogrid://knb/" + onlineDocid + ".1",
720
                                     null, null, null, null, null, null);
721

    
722

    
723
            // insert a document - get the docid
724
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
725
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS,
726
                                    false);
727

    
728
            // logoutand login as other user
729
            m.logout();
730

    
731
            // read the document
732
            readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE, true);
733

    
734
            // update the document
735
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
736

    
737
            // update the inline data
738
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock2,
739
                         null,"ecogrid://knb/" + onlineDocid + ".1",
740
                         null, null, null, null, null, null);
741
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
742

    
743
            // update the online data
744
            uploadDocid(onlineDocid + ".2", onlinetestdatafile1, FAILURE, false);
745

    
746
            // update the document access control
747
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
748
                         null,"ecogrid://knb/" + onlineDocid + ".1",
749
                         null, accessBlock, null, null, null, null);
750
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
751

    
752
            // update the document access control for inline data
753
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
754
                         null,"ecogrid://knb/" + onlineDocid + ".1",
755
                         null, null, accessBlock, null, null, null);
756
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
757

    
758
            // update the document access control for online data
759
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
760
                         null,"ecogrid://knb/" + onlineDocid + ".1",
761
                         null, null, null, null, accessBlock, null);
762
            updateDocid(newdocid + ".2", testdocument, FAILURE, false);
763

    
764
            // delete the document
765
            deleteDocid(newdocid + ".2", FAILURE, true);
766
            m.logout();
767
        }
768
        catch (MetacatAuthException mae) {
769
            fail("Authorization failed:\n" + mae.getMessage());
770
        }
771
        catch (MetacatInaccessibleException mie) {
772
            fail("Metacat Inaccessible:\n" + mie.getMessage());
773
        }
774
        catch (MetacatException me) {
775
            fail("Metacat Error:\n" + me.getMessage());
776
        }
777
        catch (Exception e) {
778
            fail("General exception:\n" + e.getMessage());
779
        }
780

    
781
    }
782

    
783
    /**
784
     * Insert a document into metacat. The expected result is passed as result
785
     */
786

    
787
    private String insertDocid(String docid, String docText, boolean result,
788
                               boolean expectKarmaException) {
789
        String response = null;
790
        try {
791
            response = m.insert(docid,
792
                                new StringReader(testdocument), null);
793
            if (result) {
794
                assertTrue( (response.indexOf("<success>") != -1));
795
                assertTrue(response.indexOf(docid) != -1);
796
            }
797
            else {
798
                assertTrue( (response.indexOf("<success>") == -1));
799
            }
800
            System.err.println(response);
801
        }
802
        catch (MetacatInaccessibleException mie) {
803
            fail("Metacat Inaccessible:\n" + mie.getMessage());
804
        }
805
        catch (InsufficientKarmaException ike) {
806
            if (!expectKarmaException) {
807
                fail("Insufficient karma:\n" + ike.getMessage());
808
            }
809
        }
810
        catch (MetacatException me) {
811
            fail("Metacat Error:\n" + me.getMessage());
812
        }
813
        catch (Exception e) {
814
            fail("General exception:\n" + e.getMessage());
815
        }
816
        return response;
817
    }
818

    
819
    /**
820
     * Insert a document into metacat. The expected result is passed as result
821
     */
822

    
823
    private String uploadDocid(String docid, String filePath, boolean result,
824
                               boolean expectedKarmaException) {
825
        String response = null;
826
        try {
827
            response = m.upload(docid, new File(filePath));
828
            if (result) {
829
                assertTrue( (response.indexOf("<success>") != -1));
830
                assertTrue(response.indexOf(docid) != -1);
831
            }
832
            else {
833
                assertTrue( (response.indexOf("<success>") == -1));
834
            }
835
            System.err.println("respose from metacat: " + response);
836
        }
837
        catch (MetacatInaccessibleException mie) {
838
            fail("Metacat Inaccessible:\n" + mie.getMessage());
839
        }
840
        catch (InsufficientKarmaException ike) {
841
            if (!expectedKarmaException) {
842
                fail("Insufficient karma:\n" + ike.getMessage());
843
            }
844
        }
845
        catch (MetacatException me) {
846
            if (result) {
847
                fail("Metacat Error:\n" + me.getMessage());
848
            } else {
849
                System.err.println("Metacat Error:\n" + me.getMessage());
850
            }
851
        }
852
        catch (Exception e) {
853
            fail("General exception:\n" + e.getMessage());
854
        }
855
        return response;
856
    }
857

    
858
    /**
859
     * Update a document in metacat. The expected result is passed as result
860
     */
861
    private String updateDocid(String docid, String docText, boolean result,
862
                               boolean expectedKarmaFailure) {
863
        String response = null;
864
        try {
865
            response = m.update(docid,
866
                                new StringReader(testdocument), null);
867

    
868
            if (result) {
869
                assertTrue( (response.indexOf("<success>") != -1));
870
                assertTrue(response.indexOf(docid) != -1);
871
            }
872
            else {
873
                assertTrue( (response.indexOf("<success>") == -1));
874
            }
875
            System.err.println(response);
876
        }
877
        catch (MetacatInaccessibleException mie) {
878
            fail("Metacat Inaccessible:\n" + mie.getMessage());
879
        }
880
        catch (InsufficientKarmaException ike) {
881
            if (!expectedKarmaFailure) {
882
                fail("Insufficient karma:\n" + ike.getMessage());
883
            }
884
        }
885
        catch (MetacatException me) {
886
            if (result) {
887
                fail("Metacat Error:\n" + me.getMessage());
888
            } else {
889
                System.err.println("Metacat Error:\n" + me.getMessage());
890
            }
891
        }
892
        catch (Exception e) {
893
            fail("General exception:\n" + e.getMessage());
894
        }
895

    
896
        return response;
897
    }
898

    
899
    /**
900
     * Delete a document into metacat. The expected result is passed as result
901
     */
902
    private void deleteDocid(String docid, boolean result,
903
                             boolean expextedKarmaFailure) {
904
        try {
905
            String response = m.delete(docid);
906
            if (result) {
907
                assertTrue(response.indexOf("<success>") != -1);
908
            }
909
            else {
910
                assertTrue(response.indexOf("<success>") == -1);
911
            }
912
            System.err.println(response);
913
        }
914
        catch (MetacatInaccessibleException mie) {
915
            fail("Metacat Inaccessible:\n" + mie.getMessage());
916
        }
917
        catch (InsufficientKarmaException ike) {
918
            if(!expextedKarmaFailure){
919
                fail("Insufficient karma:\n" + ike.getMessage());
920
            }
921
        }
922
        catch (MetacatException me) {
923
            if (result) {
924
                fail("Metacat Error:\n" + me.getMessage());
925
            } else {
926
                System.err.println("Metacat Error:\n" + me.getMessage());
927
            }
928
        }
929
        catch (Exception e) {
930
            fail("General exception:\n" + e.getMessage());
931
        }
932
    }
933

    
934
    /**
935
     * Read a document from metacat. The expected result is passed as result
936
     */
937
    private void readDocid(String docid, boolean result,
938
                           boolean expextedKarmaFailure) {
939
        try {
940
            Reader r = m.read(docid);
941
            String response = IOUtil.getAsString(r, true);
942

    
943
            if (!result) {
944
                assertTrue(response.indexOf("<success>") == -1);
945
            }
946
            // System.err.println(response);
947
        }
948
        catch (MetacatInaccessibleException mie) {
949
            fail("Metacat Inaccessible:\n" + mie.getMessage());
950
        }
951
        catch (InsufficientKarmaException ike) {
952
            if (!expextedKarmaFailure) {
953
                fail("Insufficient karma:\n" + ike.getMessage());
954
            }
955
        }
956
        catch (MetacatException me) {
957
            fail("Metacat Error:\n" + me.getMessage());
958
        }
959
        catch (Exception e) {
960
            fail("General exception:\n" + e.getMessage());
961
        }
962
    }
963

    
964
    /**
965
     * Read a document from metacat and check if it is equal to a given string.
966
     * The expected result is passed as result
967
     */
968

    
969
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
970
                                         boolean result,
971
                                         boolean expextedKarmaFailure) {
972
        try {
973
            Reader r = m.read(docid);
974
            String doc = IOUtil.getAsString(r, true);
975
            if (result) {
976

    
977
                if (!testDoc.equals(doc)) {
978
                    System.out.println("doc    :" + doc);
979
                    System.out.println("testDoc:" + testDoc);
980
                }
981

    
982
                assertTrue(testDoc.equals(doc));
983
            }
984
            else {
985
                assertTrue(doc.indexOf("<error>") != -1);
986
            }
987
        }
988
        catch (MetacatInaccessibleException mie) {
989
            fail("Metacat Inaccessible:\n" + mie.getMessage());
990
        }
991
        catch (InsufficientKarmaException ike) {
992
            if (!expextedKarmaFailure) {
993
                fail("Insufficient karma:\n" + ike.getMessage());
994
            }
995
        }
996
        catch (MetacatException me) {
997
            fail("Metacat Error:\n" + me.getMessage());
998
        }
999
        catch (Exception e) {
1000
            fail("General exception:\n" + e.getMessage());
1001
        }
1002

    
1003
    }
1004

    
1005
    /**
1006
     * Create a hopefully unique docid for testing insert and update. Does
1007
     * not include the 'revision' part of the id.
1008
     *
1009
     * @return a String docid based on the current date and time
1010
     */
1011
    private String generateDocid() {
1012
        StringBuffer docid = new StringBuffer(prefix);
1013
        docid.append(".");
1014

    
1015
        // Create a calendar to get the date formatted properly
1016
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1017
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1018
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1019
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1020
                       2 * 60 * 60 * 1000);
1021
        Calendar calendar = new GregorianCalendar(pdt);
1022
        Date trialTime = new Date();
1023
        calendar.setTime(trialTime);
1024
        docid.append(calendar.get(Calendar.YEAR));
1025
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1026
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1027
        docid.append(calendar.get(Calendar.MINUTE));
1028
        docid.append(calendar.get(Calendar.SECOND));
1029

    
1030
        return docid.toString();
1031
    }
1032
}
(1-1/13)