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 for Inline data in Metacat
50
 */
51
public class InlineDataAccessTest
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

    
63
    private Metacat m;
64

    
65
    private boolean SUCCESS = true;
66
    private boolean FAILURE = false;
67

    
68
    /**
69
     * These variables are for eml-2.0.1 only. For other eml versions,
70
     * this function might have to modified
71
     */
72

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

    
81
    private String testEmlCreatorBlock =
82
        "<creator scope=\"document\">                                       " +
83
        " <individualName>                                                  " +
84
        "    <surName>Smith</surName>                                       " +
85
        " </individualName>                                                 " +
86
        "</creator>                                                         ";
87

    
88
    private String testEmlContactBlock =
89
        "<contact scope=\"document\">                                       " +
90
        " <individualName>                                                  " +
91
        "    <surName>Jackson</surName>                                     " +
92
        " </individualName>                                                 " +
93
        "</contact>                                                         ";
94

    
95
    private String testEmlInlineBlock1 =
96
        "  <admin>                                                          " +
97
        "    <contact>                                                      " +
98
        "      <name>Operator</name>                                        " +
99
        "      <institution>PSI</institution>                               " +
100
        "    </contact>                                                     " +
101
        "  </admin>                                                         ";
102

    
103
    private String testEmlInlineBlock2 =
104
        "  <instrument>                                                     " +
105
        "    <instName>LCQ</instName>                                       " +
106
        "    <source type=\"ESI\"></source>                                 " +
107
        "    <detector type=\"EM\"></detector>                              " +
108
        "  </instrument>                                                    ";
109

    
110
    /**
111
     * This function returns an access block based on the params passed
112
     */
113
    private String getAccessBlock(String principal, boolean grantAccess,
114
                                  boolean read, boolean write,
115
                                  boolean changePermission, boolean all) {
116
        String accessBlock = "<access " +
117
            "authSystem=\"ldap://ldap.ecoinformatics.org:389/dc=ecoinformatics,dc=org\"" +
118
            " order=\"allowFirst\" scope=\"document\">";
119

    
120
        if (grantAccess) {
121
            accessBlock += "<allow>";
122
        }
123
        else {
124
            accessBlock += "<deny>";
125
        }
126

    
127
        accessBlock = accessBlock + "<principal>" + principal + "</principal>";
128

    
129
        if (all) {
130
            accessBlock += "<permission>all</permission>";
131
        }
132
        else {
133
            if (read) {
134
                accessBlock += "<permission>read</permission>";
135
            }
136
            if (write) {
137
                accessBlock += "<permission>write</permission>";
138
            }
139
            if (changePermission) {
140
                accessBlock += "<permission>changePermission</permission>";
141
            }
142
        }
143

    
144
        if (grantAccess) {
145
            accessBlock += "</allow>";
146
        }
147
        else {
148
            accessBlock += "</deny>";
149
        }
150
        accessBlock += "</access>";
151

    
152
        return accessBlock;
153

    
154
    }
155

    
156
    /**
157
     * This function returns a valid eml document with no access rules
158
     * This function is for eml-2.0.1 only. For other eml versions,
159
     * this function might have to modified
160
     */
161
    private String getTestEmlDoc(String title, String inlineData1,
162
                                 String inlineData2, String OnlineUrl1,
163
                                 String OnlineUrl2, String docAccessBlock,
164
                                 String inlineAccessBlock1,
165
                                 String inlineAccessBlock2,
166
                                 String OnlineAccessBlock1,
167
                                 String OnlineAccessBlock2) {
168

    
169
        String testDocument = "";
170
        testDocument = testDocument + testEmlHeader +
171
            "<dataset scope=\"document\"><title>" + title + "</title>" +
172
            testEmlCreatorBlock;
173

    
174
        if (inlineData1 != null) {
175
            testDocument = testDocument
176
                + "<distribution scope=\"document\" id=\"inlineEntity1\">"
177
                + "<inline>" + inlineData1 + "</inline></distribution>";
178
        }
179
        if (inlineData2 != null) {
180
            testDocument = testDocument
181
                + "<distribution scope=\"document\" id=\"inlineEntity2\">"
182
                + "<inline>" + inlineData2 + "</inline></distribution>";
183
        }
184
        if (OnlineUrl1 != null) {
185
            testDocument = testDocument
186
                + "<distribution scope=\"document\" id=\"InlineEntity1\">"
187
                + "<Inline><url function=\"download\">"
188
                + OnlineUrl1 + "</url></Inline></distribution>";
189
        }
190
        if (OnlineUrl2 != null) {
191
            testDocument = testDocument +
192
                "<distribution scope=\"document\" id=\"InlineEntity2\">"
193
                + "<Inline><url function=\"download\">"
194
                + OnlineUrl2 + "</url></Inline></distribution>";
195
        }
196
        testDocument += testEmlContactBlock;
197

    
198
        if (docAccessBlock != null) {
199
            testDocument += docAccessBlock;
200
        }
201

    
202
        testDocument += "</dataset>";
203

    
204
        if (inlineAccessBlock1 != null) {
205
            testDocument += "<additionalMetadata>";
206
            testDocument += "<describes>inlineEntity1</describes>";
207
            testDocument += inlineAccessBlock1;
208
            testDocument += "</additionalMetadata>";
209
        }
210

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

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

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

    
232
        testDocument += "</eml:eml>";
233

    
234
        //System.out.println("Returning following document" + testDocument);
235
        return testDocument;
236
    }
237

    
238
    /**
239
     * Constructor to build the test
240
     *
241
     * @param name the name of the test method
242
     */
243
    public InlineDataAccessTest(String name) {
244
        super(name);
245
        newdocid = generateDocid();
246
    }
247

    
248
    /**
249
     * Establish a testing framework by initializing appropriate objects
250
     */
251
    public void setUp() {
252
        try {
253
            System.err.println("Test Metacat: " + metacatUrl);
254
            m = MetacatFactory.createMetacatConnection(metacatUrl);
255
        }
256
        catch (MetacatInaccessibleException mie) {
257
            System.err.println("Metacat is: " + metacatUrl);
258
            fail("Metacat connection failed." + mie.getMessage());
259
        }
260
    }
261

    
262
    /**
263
     * Release any objects after tests are complete
264
     */
265
    public void tearDown() {
266
    }
267

    
268
    /**
269
     * Create a suite of tests to be run together
270
     */
271
    public static Test suite() {
272
        TestSuite suite = new TestSuite();
273
        suite.addTest(new InlineDataAccessTest("initialize"));
274

    
275
        suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_1"));
276
      //  suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_2"));
277
      //  suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_3"));
278
     //   suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_4"));
279
     //   suite.addTest(new InlineDataAccessTest("inlineDataCasesTest_5"));
280

    
281
        return suite;
282
    }
283

    
284

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

    
293
    /** *********
294
     * Checking the following cases:
295
     * when only Inline data is uploaded by a user and
296
     * -> he tries to read it  - success
297
     * -> he tries to add same docid again  - failure
298
     * -> he tries to update it  - success
299
     * -> he removes it and adds it again - success
300
     * -> he tries to delete it  - success
301
     * -> he tries to read it after deleteing - failure
302
     */
303
    public void inlineDataCasesTest_1() {
304
        try {
305
            newdocid = generateDocid();
306
            m.login(username, password);
307

    
308
            // insert a document
309
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
310
                                         null, null, null, null,
311
                                         null, null, null, null);
312

    
313
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
314
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
315

    
316
            // insert same document again
317
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
318

    
319
            // update by modifying inline data
320
            testdocument = getTestEmlDoc("Testing update inline",
321
                                         testEmlInlineBlock2,
322
                                         null, null, null, null,
323
                                         null, null, null, null);
324
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
325
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS, true);
326

    
327
            // update by removing inline data
328
   //         testdocument = getTestEmlDoc("Testing update inline",
329
   //                                      null,
330
   //                                      null, null, null, null,
331
   //                                      null, null, null, null);
332
   //         updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
333
   //         readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS, true);
334

    
335
            // update by introducing inline data
336
//            testdocument = getTestEmlDoc("Testing update inline",
337
      //                                   testEmlInlineBlock1,
338
  //                                       null, null, null, null,
339
    //                                     null, null, null, null);
340
      //      updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
341
     //       readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS, true);
342

    
343
            // read inline data only
344
//            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
345
 //                                        testEmlInlineBlock1, SUCCESS, false);
346

    
347
            // delete the inline data
348
            deleteDocid(newdocid + ".2", SUCCESS, false);
349
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument, FAILURE, false);
350
            m.logout();
351
        }
352
        catch (MetacatAuthException mae) {
353
            fail("Authorization failed:\n" + mae.getMessage());
354
        }
355
        catch (MetacatInaccessibleException mie) {
356
            fail("Metacat Inaccessible:\n" + mie.getMessage());
357
        }
358
        catch (Exception e) {
359
            fail("General exception:\n" + e.getMessage());
360
        }
361
    }
362

    
363
    /** *********
364
     * Checking the following cases:
365
     * when only inline data is uploaded by a user and another user
366
     * -> tries to read it  - failure
367
     * -> tries to read inline data only - failure
368
     * -> tries to update - failure
369
     * -> tries to delete it  - failure
370
     */
371
    public void inlineDataCasesTest_2() {
372
        try {
373
            newdocid = generateDocid();
374
            m.login(username, password);
375

    
376
            // insert a document
377
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
378
                                         null, null, null, null,
379
                                         null, null, null, null);
380

    
381
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
382
            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
383

    
384
            // login as another user
385
            m.logout();
386
            m.login(anotheruser, anotherpassword);
387

    
388
            // try to read document or data only
389
            readDocidWhichEqualsDoc(newdocid, testdocument, FAILURE, true);
390
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
391
                                         testEmlInlineBlock1, FAILURE, true);
392

    
393
            // try to update the document
394
            testdocument = getTestEmlDoc("Testing insert", testEmlInlineBlock1,
395
                                         null, null, null, null,
396
                                         null, null, null, null);
397
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
398

    
399
            // try to delete the document
400
            deleteDocid(newdocid + ".1", FAILURE, true);
401
            m.logout();
402

    
403
            // delete the document
404
            m.login(username, password);
405
            deleteDocid(newdocid + ".1", SUCCESS, false);
406
            m.logout();
407

    
408
        }
409
        catch (MetacatAuthException mae) {
410
            fail("Authorization failed:\n" + mae.getMessage());
411
        }
412
        catch (MetacatInaccessibleException mie) {
413
            fail("Metacat Inaccessible:\n" + mie.getMessage());
414
        }
415
        catch (Exception e) {
416
            fail("General exception:\n" + e.getMessage());
417
        }
418
    }
419

    
420
    /** *********
421
     * Checking the following cases:
422
     * when only inline data is uploaded by a user with the following different
423
     * access controls in another document
424
     *   1.read
425
     *   2.write
426
     *   3.change permission
427
     *   4.all
428
     * And another user tries to do the following:
429
     * -> tries to read it
430
     * -> tries to update it
431
     * -> tries to set permissions on it
432
     * -> tries to delete it
433
     */
434
    public void inlineDataCasesTest_3() {
435
        try {
436

    
437
            /////////Case 1./////////////////////
438
            // insert an inline document - read only
439
            m.login(username, password);
440
            newdocid = generateDocid();
441

    
442
            // insert a document which gives read access to the inline document
443
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
444
                                         testEmlInlineBlock1, null, null,
445
                                         null, getAccessBlock(anotheruser, true,
446
                true, false, false, false),
447
                                         null, null, null, null);
448
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
449
            m.logout();
450

    
451
            // login as another user
452
            m.login(anotheruser, anotherpassword);
453

    
454
            // try to read the document and the inline data
455
            readDocid(newdocid + ".1", SUCCESS, false);
456
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
457
                                         testEmlInlineBlock1, SUCCESS, false);
458

    
459
            // try to update the inline data
460
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
461
                                         testEmlInlineBlock2, null, null,
462
                                         null, getAccessBlock(anotheruser, true,
463
                true, false, false, false),
464
                                         null, null, null, null);
465
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
466

    
467
            // try to set the permissions for the inline data
468
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
469
                                         testEmlInlineBlock1, null, null,
470
                                         null, getAccessBlock(anotheruser, true,
471
                false, false, false, true),
472
                                         null, null, null, null);
473
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
474

    
475
            // try to delete the document
476
            deleteDocid(newdocid + ".1", FAILURE, true);
477
            m.logout();
478

    
479
            // delete the document
480
            m.login(username, password);
481
            deleteDocid(newdocid + ".1", SUCCESS, false);
482
            m.logout();
483

    
484
            /////////Case 2./////////////////////
485
            // insert an inline document - write only
486
            m.login(username, password);
487
            newdocid = generateDocid();
488

    
489
            // insert a document which gives read access to the inline document
490
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
491
                                         testEmlInlineBlock1, null, null,
492
                                         null, getAccessBlock(anotheruser, true,
493
                                         false, true, false, false),
494
                                         null, null, null, null);
495
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
496
            m.logout();
497

    
498
            // login as another user
499
            m.login(anotheruser, anotherpassword);
500

    
501
            // try to read the document and the inline data
502
            readDocid(newdocid + ".1", FAILURE, true);
503
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
504
                                         testEmlInlineBlock1, FAILURE, true);
505

    
506
            // try to update the inline data
507
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
508
                                         testEmlInlineBlock2, null, null,
509
                                         null, getAccessBlock(anotheruser, true,
510
                                         false, true, false, false),
511
                                         null, null, null, null);
512
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
513

    
514
            // try to set the permissions for the inline data
515
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
516
                                         testEmlInlineBlock1, null, null,
517
                                         null, getAccessBlock(anotheruser, true,
518
                false, false, false, true),
519
                                         null, null, null, null);
520
            updateDocid(newdocid + ".3", testdocument, FAILURE, true);
521

    
522
            // try to delete the document
523
            deleteDocid(newdocid + ".2", FAILURE, true);
524
            m.logout();
525

    
526
            // delete the document
527
            m.login(username, password);
528
            deleteDocid(newdocid + ".2", SUCCESS, false);
529
            m.logout();
530

    
531
            /////////Case 3./////////////////////
532
            // insert an inline document - change permission only
533
            m.login(username, password);
534
            newdocid = generateDocid();
535

    
536
            // insert a document which gives read access to the inline document
537
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
538
                                         testEmlInlineBlock1, null, null,
539
                                         null, getAccessBlock(anotheruser, true,
540
                false, false, true, false),
541
                                         null, null, null, null);
542
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
543
            m.logout();
544

    
545
            // login as another user
546
            m.login(anotheruser, anotherpassword);
547

    
548
            // try to read the document and the inline data
549
            readDocid(newdocid + ".1", FAILURE, true);
550
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
551
                                         testEmlInlineBlock1, FAILURE, true);
552

    
553
            // try to update the inline data
554
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
555
                                         testEmlInlineBlock2, null, null,
556
                                         null, getAccessBlock(anotheruser, true,
557
                false, false, true, false),
558
                                         null, null, null, null);
559
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
560

    
561
            // try to set the permissions for the inline data
562
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
563
                                         testEmlInlineBlock1, null, null,
564
                                         null, getAccessBlock(anotheruser, true,
565
                false, false, false, true),
566
                                         null, null, null, null);
567
            // ERRRRRRRRRRRR
568
            //updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
569

    
570
            // try to delete the document
571
            deleteDocid(newdocid + ".1", FAILURE, true);
572
            m.logout();
573

    
574
            // delete the document
575
            m.login(username, password);
576
            deleteDocid(newdocid + ".1", SUCCESS, false);
577
            m.logout();
578

    
579

    
580
            /////////Case 4./////////////////////
581
            // insert an inline document - change permission only
582
            m.login(username, password);
583
            newdocid = generateDocid();
584

    
585
            // insert a document which gives read access to the inline document
586
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
587
                                         testEmlInlineBlock1, null, null,
588
                                         null, getAccessBlock(anotheruser, true,
589
                                         false, false, false, true),
590
                                         null, null, null, null);
591
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
592
            m.logout();
593

    
594
            // login as another user
595
            m.login(anotheruser, anotherpassword);
596

    
597
            // try to read the document and the inline data
598
            readDocid(newdocid + ".1", SUCCESS, false);
599
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
600
                                         testEmlInlineBlock1, SUCCESS, false);
601

    
602
            // try to update the inline data
603
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
604
                                         testEmlInlineBlock2, null, null,
605
                                         null, getAccessBlock(anotheruser, true,
606
                                         false, false, false, true),
607
                                         null, null, null, null);
608
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
609

    
610
            // try to set the permissions for the inline data
611
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
612
                                         testEmlInlineBlock1, null, null,
613
                                         null, getAccessBlock(anotheruser, true,
614
                                         true, true, true, false),
615
                                         null, null, null, null);
616
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
617

    
618
            // try to delete the document
619
            deleteDocid(newdocid + ".3", SUCCESS, false);
620
            m.logout();
621

    
622
        }
623
        catch (MetacatAuthException mae) {
624
            fail("Authorization failed:\n" + mae.getMessage());
625
        }
626
        catch (MetacatInaccessibleException mie) {
627
            fail("Metacat Inaccessible:\n" + mie.getMessage());
628
        }
629
        catch (Exception e) {
630
            fail("General exception:\n" + e.getMessage());
631
        }
632
    }
633

    
634
    /** *********
635
     * Checking the following cases:
636
     * when only Inline data is uploaded by a user with the following different
637
     * access controls specified in addiotnal metadata in another document
638
     *   1.read
639
     *   2.write
640
     *   3.change permission
641
     *   4.all
642
     * And another user tries to do the following:
643
     * -> tries to read it
644
     * -> tries to update it
645
     * -> tries to set permissions on it
646
     * -> tries to delete it
647
     */
648
    public void inlineDataCasesTest_4() {
649
        try {
650
            /////////Case 1./////////////////////
651
            // insert an inline document - read only
652
            m.login(username, password);
653
            newdocid = generateDocid();
654

    
655
            // insert a document which gives read access to the inline document
656
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
657
                                         testEmlInlineBlock1, null, null, null,
658
                                         null, getAccessBlock(anotheruser, true,
659
                                         true, false, false, false),
660
                                         null, null, null);
661
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
662
            m.logout();
663

    
664
            // login as another user
665
            m.login(anotheruser, anotherpassword);
666

    
667
            // try to read the document and the inline data
668
            readDocid(newdocid + ".1", FAILURE, true);
669
            // ERRRRRRRRRRRRRRR
670
            //readInlineDataWhichEqualsDoc(newdocid + ".1.1",
671
            //                             testEmlInlineBlock1, SUCCESS, false);
672

    
673
            // try to update the inline data
674
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
675
                                         testEmlInlineBlock2, null, null,null,
676
                                         null, getAccessBlock(anotheruser, true,
677
                                         true, false, false, false),
678
                                         null, null, null);
679
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
680

    
681
            // try to set the permissions for the inline data
682
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
683
                                         testEmlInlineBlock1, null, null,null,
684
                                         null, getAccessBlock(anotheruser, true,
685
                                         false, false, false, true),
686
                                         null, null, null);
687
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
688

    
689
            // try to delete the document
690
            deleteDocid(newdocid + ".1", FAILURE, true);
691
            m.logout();
692

    
693
            // delete the document
694
            m.login(username, password);
695
            deleteDocid(newdocid + ".1", SUCCESS, false);
696
            m.logout();
697

    
698
            /////////Case 2./////////////////////
699
            // insert an inline document - write only
700
            m.login(username, password);
701
            newdocid = generateDocid();
702

    
703
            // insert a document which gives read access to the inline document
704
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
705
                                         testEmlInlineBlock1, null, null, null,
706
                                         null, getAccessBlock(anotheruser, true,
707
                                         false, true, false, false),
708
                                         null, null, null);
709
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
710
            m.logout();
711

    
712
            // login as another user
713
            m.login(anotheruser, anotherpassword);
714

    
715
            // try to read the document and the inline data
716
            readDocid(newdocid + ".1", FAILURE, true);
717
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
718
                                         testEmlInlineBlock1, FAILURE, true);
719

    
720
            // try to update the inline data
721
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
722
                                         testEmlInlineBlock2, null, null, null,
723
                                         null, getAccessBlock(anotheruser, true,
724
                                         false, true, false, false),
725
                                         null, null, null);
726
            //ERRRRRRRRRRRRRRRRRRRRRRRR
727
            // updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
728

    
729
            // try to set the permissions for the inline data
730
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
731
                                         testEmlInlineBlock1, null, null, null,
732
                                         null, getAccessBlock(anotheruser, true,
733
                                         false, false, false, true),
734
                                         null, null, null);
735
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
736

    
737
            // try to delete the document
738
            deleteDocid(newdocid + ".1", FAILURE, true);
739
            m.logout();
740

    
741
            // delete the document
742
            m.login(username, password);
743
            deleteDocid(newdocid + ".1", SUCCESS, false);
744
            m.logout();
745

    
746
            /////////Case 3./////////////////////
747
            // insert an inline document - change permission only
748
            m.login(username, password);
749
            newdocid = generateDocid();
750

    
751
            // insert a document which gives read access to the inline document
752
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
753
                                         testEmlInlineBlock1, null, null, null,
754
                                         null, getAccessBlock(anotheruser, true,
755
                                         false, false, true, false),
756
                                         null, null, null);
757
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
758
            m.logout();
759

    
760
            // login as another user
761
            m.login(anotheruser, anotherpassword);
762

    
763
            // try to read the document and the inline data
764
            readDocid(newdocid + ".1", FAILURE, true);
765
            readInlineDataWhichEqualsDoc(newdocid + ".1.1",
766
                                         testEmlInlineBlock1, FAILURE, true);
767

    
768
            // try to update the inline data
769
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
770
                                         testEmlInlineBlock2, null, null, null,
771
                                         null, getAccessBlock(anotheruser, true,
772
                                         false, false, true, false),
773
                                         null, null, null);
774
            updateDocid(newdocid + ".2", testdocument, FAILURE, true);
775

    
776
            // try to set the permissions for the inline data
777
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
778
                                         testEmlInlineBlock1, null, null, null,
779
                                         null, getAccessBlock(anotheruser, true,
780
                                         false, false, false, true),
781
                                         null, null, null);
782
            // ERRRRRRRRRRRR
783
            //updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
784

    
785
            // try to delete the document
786
            deleteDocid(newdocid + ".1", FAILURE, true);
787
            m.logout();
788

    
789
            // delete the document
790
            m.login(username, password);
791
            deleteDocid(newdocid + ".1", SUCCESS, false);
792
            m.logout();
793

    
794

    
795
            /////////Case 4./////////////////////
796
            // insert an inline document - change permission only
797
            m.login(username, password);
798
            newdocid = generateDocid();
799

    
800
            // insert a document which gives read access to the inline document
801
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
802
                                         testEmlInlineBlock1, null, null, null,
803
                                         null, getAccessBlock(anotheruser, true,
804
                                         false, false, false, true),
805
                                         null, null, null);
806
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
807
            m.logout();
808

    
809
            // login as another user
810
            m.login(anotheruser, anotherpassword);
811

    
812
            // try to read the document and the inline data
813
            readDocid(newdocid + ".1", FAILURE, true);
814
            // ERRRRRRRRRRRRRRR
815
            //readInlineDataWhichEqualsDoc(newdocid + ".1.1",
816
            //                             testEmlInlineBlock1, SUCCESS, false);
817

    
818
            // try to update the inline data
819
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing update",
820
                                         testEmlInlineBlock2, null, null, null,
821
                                         null, getAccessBlock(anotheruser, true,
822
                                         false, false, false, true),
823
                                         null, null, null);
824
            // ERRRRRR
825
            // updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
826

    
827
            // try to set the permissions for the inline data
828
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
829
                                         testEmlInlineBlock1, null, null, null,
830
                                         null, getAccessBlock(anotheruser, true,
831
                                         true, true, true, false),
832
                                         null, null, null);
833
            // ERRRRRRRRRR
834
            // updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
835

    
836
            // try to delete the document
837
            deleteDocid(newdocid + ".3", FAILURE, true);
838
            m.logout();
839

    
840
            // delete the document
841
            m.login(username, password);
842
            deleteDocid(newdocid + ".1", SUCCESS, false);
843
            m.logout();
844

    
845
        }
846
        catch (MetacatAuthException mae) {
847
            fail("Authorization failed:\n" + mae.getMessage());
848
        }
849
        catch (MetacatInaccessibleException mie) {
850
            fail("Metacat Inaccessible:\n" + mie.getMessage());
851
        }
852
        catch (Exception e) {
853
            fail("General exception:\n" + e.getMessage());
854
        }
855
    }
856

    
857
    /** *********
858
     * Checking the following cases:
859
     * -> when no inline data is specified in the document but
860
     *    rules are specified in additional metadata
861
     * -> when a user has RW permission for inline data, can he delete it
862
     * -> when inline data with document refering to it is uploaded with read
863
     *    access for metadata and no access for data
864
     */
865
    public void inlineDataCasesTest_5() {
866
        try {
867

    
868
            m.login(username, password);
869

    
870
            /////////Case 1
871
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
872
                                         null, null,
873
                                         null, null, null,
874
                                         getAccessBlock(anotheruser, true,
875
                true, false, false, false), null, null, null);
876
            newdocid = generateDocid();
877

    
878
            // try to insert the wrong document
879
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
880
            m.logout();
881

    
882

    
883
            /////////Case 2
884
            m.login(username, password);
885
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
886
                                         testEmlInlineBlock1, null, null,
887
                                         null, getAccessBlock(anotheruser, true,
888
                                         true, true, false, false), null, null,
889
                                         null, null);
890
            newdocid = generateDocid();
891
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
892
            m.logout();
893

    
894
            m.login(anotheruser, anotherpassword);
895
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
896
                                         null, null, null,
897
                                         null, getAccessBlock(anotheruser, true,
898
                                         true, true, false, false), null, null,
899
                                         null, null);
900
            /// ERRRRRRRRRRRR
901
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
902

    
903
            /////////Case 3
904

    
905
            // insert a document
906
            m.login(username, password);
907
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
908
                                         testEmlInlineBlock1, null, null,
909
                                         null, getAccessBlock(anotheruser, true,
910
                                         false, false, false, true),
911
                                         getAccessBlock(anotheruser, false,
912
                                         false, false, false, true), null,
913
                                         null, null);
914
            newdocid = generateDocid();
915
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
916
            m.logout();
917

    
918

    
919
            // try to read the Inline data
920
            m.login(anotheruser, anotherpassword);
921
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
922
                             "", null, null,
923
                             null, getAccessBlock(anotheruser, true,
924
                             false, false, false, true),
925
                             getAccessBlock(anotheruser, false,
926
                             false, false, false, true), null,
927
                             null, null);
928
            readDocidWhichEqualsDoc(newdocid + ".1", testdocument,
929
                                    SUCCESS, false);
930

    
931
            // try to update the rules for inline data
932
            /// ERRRRRRRRRRRR it lets you do that
933
            testdocument = getTestEmlDoc("InlineDataAccessTest: Doing insert",
934
                                         testEmlInlineBlock1, null, null,
935
                                         null, getAccessBlock(anotheruser, true,
936
                                         false, false, false, true),
937
                                         getAccessBlock(anotheruser, true,
938
                                         false, false, false, true), null,
939
                                         null, null);
940
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
941
            readDocidWhichEqualsDoc(newdocid + ".2", testdocument,
942
                                    SUCCESS, false);
943

    
944
            m.logout();
945
        }
946
        catch (MetacatAuthException mae) {
947
            fail("Authorization failed:\n" + mae.getMessage());
948
        }
949
        catch (MetacatInaccessibleException mie) {
950
            fail("Metacat Inaccessible:\n" + mie.getMessage());
951
        }
952
        catch (Exception e) {
953
            fail("General exception:\n" + e.getMessage());
954
        }
955
    }
956

    
957
    /**
958
     * Insert a document into metacat. The expected result is passed as result
959
     */
960

    
961
    private String insertDocid(String docid, String docText, boolean result,
962
                               boolean expectKarmaException) {
963
        String response = null;
964
        try {
965
            response = m.insert(docid,
966
                                new StringReader(testdocument), null);
967
            if (result) {
968
                assertTrue( (response.indexOf("<success>") != -1));
969
                assertTrue(response.indexOf(docid) != -1);
970
            }
971
            else {
972
                assertTrue( (response.indexOf("<success>") == -1));
973
            }
974
            System.err.println(response);
975
        }
976
        catch (MetacatInaccessibleException mie) {
977
            fail("Metacat Inaccessible:\n" + mie.getMessage());
978
        }
979
        catch (InsufficientKarmaException ike) {
980
            if (!expectKarmaException) {
981
                fail("Insufficient karma:\n" + ike.getMessage());
982
            }
983
        }
984
        catch (MetacatException me) {
985
            if (result) {
986
                fail("Metacat Error:\n" + me.getMessage());
987
            }
988
            else {
989
                System.err.println("Metacat Error: " + me.getMessage());
990
            }
991
        }
992
        catch (Exception e) {
993
            fail("General exception:\n" + e.getMessage());
994
        }
995
        return response;
996
    }
997

    
998
    /**
999
     * Insert a document into metacat. The expected result is passed as result
1000
     */
1001

    
1002
    private String uploadDocid(String docid, String filePath, boolean result,
1003
                               boolean expectedKarmaException) {
1004
        String response = null;
1005
        try {
1006
            response = m.upload(docid, new File(filePath));
1007
            if (result) {
1008
                assertTrue( (response.indexOf("<success>") != -1));
1009
                assertTrue(response.indexOf(docid) != -1);
1010
            }
1011
            else {
1012
                assertTrue( (response.indexOf("<success>") == -1));
1013
            }
1014
            System.err.println("respose from metacat: " + response);
1015
        }
1016
        catch (MetacatInaccessibleException mie) {
1017
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1018
        }
1019
        catch (InsufficientKarmaException ike) {
1020
            if (!expectedKarmaException) {
1021
                fail("Insufficient karma:\n" + ike.getMessage());
1022
            }
1023
        }
1024
        catch (MetacatException me) {
1025
            if (result) {
1026
                fail("Metacat Error:\n" + me.getMessage());
1027
            }
1028
            else {
1029
                System.err.println("Metacat Error: " + me.getMessage());
1030
            }
1031
        }
1032
        catch (Exception e) {
1033
            fail("General exception:\n" + e.getMessage());
1034
        }
1035
        return response;
1036
    }
1037

    
1038
    /**
1039
     * Update a document in metacat. The expected result is passed as result
1040
     */
1041
    private String updateDocid(String docid, String docText, boolean result,
1042
                               boolean expectedKarmaFailure) {
1043
        String response = null;
1044
        try {
1045
            response = m.update(docid,
1046
                                new StringReader(testdocument), null);
1047

    
1048
            if (result) {
1049
                assertTrue( (response.indexOf("<success>") != -1));
1050
                assertTrue(response.indexOf(docid) != -1);
1051
            }
1052
            else {
1053
                assertTrue( (response.indexOf("<success>") == -1));
1054
            }
1055
            System.err.println(response);
1056
        }
1057
        catch (MetacatInaccessibleException mie) {
1058
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1059
        }
1060
        catch (InsufficientKarmaException ike) {
1061
            if (!expectedKarmaFailure) {
1062
                fail("Insufficient karma:\n" + ike.getMessage());
1063
            }
1064
        }
1065
        catch (MetacatException me) {
1066
            if (! (expectedKarmaFailure &&
1067
                   (me.getMessage().indexOf(
1068
                "User try to update a access module which it doesn't have \"ALL\" permission") !=
1069
                    -1))) {
1070
                fail("Metacat Error:\n" + me.getMessage());
1071
            }
1072
        }
1073
        catch (Exception e) {
1074
            fail("General exception:\n" + e.getMessage());
1075
        }
1076

    
1077
        return response;
1078
    }
1079

    
1080
    /**
1081
     * Delete a document into metacat. The expected result is passed as result
1082
     */
1083
    private void deleteDocid(String docid, boolean result,
1084
                             boolean expectedKarmaFailure) {
1085
        try {
1086
            String response = m.delete(docid);
1087
            if (result) {
1088
                assertTrue(response.indexOf("<success>") != -1);
1089
            }
1090
            else {
1091
                assertTrue(response.indexOf("<success>") == -1);
1092
            }
1093
            System.err.println(response);
1094
        }
1095
        catch (MetacatInaccessibleException mie) {
1096
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1097
        }
1098
        catch (InsufficientKarmaException ike) {
1099
            if (!expectedKarmaFailure) {
1100
                fail("Insufficient karma:\n" + ike.getMessage());
1101
            }
1102

    
1103
        }
1104
        catch (MetacatException me) {
1105
            if (result) {
1106
                fail("Metacat Error:\n" + me.getMessage());
1107
            }
1108
            else {
1109
                System.err.println("Metacat Error:\n" + me.getMessage());
1110
            }
1111
        }
1112
        catch (Exception e) {
1113
            fail("General exception:\n" + e.getMessage());
1114
        }
1115
    }
1116

    
1117
    /**
1118
     * Read inline data from metacat. The expected result is passed as result
1119
     */
1120
    private void readInlineDataWhichEqualsDoc(String docid, String testDoc,
1121
                                              boolean result,
1122
                                              boolean expextedKarmaFailure) {
1123
        try {
1124
            Reader r = m.readInlineData(docid);
1125
            String doc = IOUtil.getAsString(r, true);
1126

    
1127
            if (result) {
1128
                if (!testDoc.equals(doc)) {
1129
                    System.out.println("doc    :" + doc);
1130
                    System.out.println("testDoc:" + testDoc);
1131
                }
1132

    
1133
                assertTrue(testDoc.equals(doc));
1134
            }
1135
            else {
1136
                assertTrue(doc.indexOf("<error>") != -1);
1137
            }
1138
        }
1139
        catch (MetacatInaccessibleException mie) {
1140
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1141
        }
1142
        catch (InsufficientKarmaException ike) {
1143
            if (!expextedKarmaFailure) {
1144
                fail("Insufficient karma:\n" + ike.getMessage());
1145
            }
1146
        }
1147
        catch (MetacatException me) {
1148
            if (result) {
1149
                fail("Metacat Error:\n" + me.getMessage());
1150
            }
1151
            else {
1152
                System.err.println("Metacat Error:\n" + me.getMessage());
1153
            }
1154
        }
1155
        catch (Exception e) {
1156
            fail("General exception:\n" + e.getMessage());
1157
        }
1158
    }
1159

    
1160
    /**
1161
     * Read a document from metacat. The expected result is passed as result
1162
     */
1163
    private void readDocid(String docid, boolean result,
1164
                           boolean expextedKarmaFailure) {
1165
        try {
1166
            Reader r = m.read(docid);
1167
            String response = IOUtil.getAsString(r, true);
1168

    
1169
            if (!result) {
1170
                assertTrue(response.indexOf("<success>") == -1);
1171
            }
1172
            System.err.println(response);
1173
        }
1174
        catch (MetacatInaccessibleException mie) {
1175
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1176
        }
1177
        catch (InsufficientKarmaException ike) {
1178
            if (!expextedKarmaFailure) {
1179
                fail("Insufficient karma:\n" + ike.getMessage());
1180
            }
1181
        }
1182
        catch (MetacatException me) {
1183
            if (result) {
1184
                fail("Metacat Error:\n" + me.getMessage());
1185
            }
1186
            else {
1187
                System.err.println("Metacat Error:\n" + me.getMessage());
1188
            }
1189
        }
1190
        catch (Exception e) {
1191
            fail("General exception:\n" + e.getMessage());
1192
        }
1193
    }
1194

    
1195
    /**
1196
     * Read a document from metacat and check if it is equal to a given string.
1197
     * The expected result is passed as result
1198
     */
1199

    
1200
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1201
                                         boolean result,
1202
                                         boolean expextedKarmaFailure) {
1203
        try {
1204
            Reader r = m.read(docid);
1205
            String doc = IOUtil.getAsString(r, true);
1206
            if (result) {
1207

    
1208
                if (!testDoc.equals(doc)) {
1209
                    System.out.println("doc    :" + doc);
1210
                    System.out.println("testDoc:" + testDoc);
1211
                }
1212

    
1213
                assertTrue(testDoc.equals(doc));
1214
            }
1215
            else {
1216
                assertTrue(doc.indexOf("<error>") != -1);
1217
            }
1218
        }
1219
        catch (MetacatInaccessibleException mie) {
1220
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1221
        }
1222
        catch (InsufficientKarmaException ike) {
1223
            if (!expextedKarmaFailure) {
1224
                fail("Insufficient karma:\n" + ike.getMessage());
1225
            }
1226
        }
1227
        catch (MetacatException me) {
1228
            if (result) {
1229
                fail("Metacat Error:\n" + me.getMessage());
1230
            }
1231
            else {
1232
                System.out.println("Metacat Error:\n" + me.getMessage());
1233
            }
1234
        }
1235
        catch (Exception e) {
1236
            fail("General exception:\n" + e.getMessage());
1237
        }
1238

    
1239
    }
1240

    
1241
    /**
1242
     * Create a hopefully unique docid for testing insert and update. Does
1243
     * not include the 'revision' part of the id.
1244
     *
1245
     * @return a String docid based on the current date and time
1246
     */
1247
    private String generateDocid() {
1248
        StringBuffer docid = new StringBuffer(prefix);
1249
        docid.append(".");
1250

    
1251
        // Create a calendar to get the date formatted properly
1252
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1253
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1254
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1255
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1256
                       2 * 60 * 60 * 1000);
1257
        Calendar calendar = new GregorianCalendar(pdt);
1258
        Date trialTime = new Date();
1259
        calendar.setTime(trialTime);
1260
        docid.append(calendar.get(Calendar.YEAR));
1261
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1262
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1263
        docid.append(calendar.get(Calendar.MINUTE));
1264
        docid.append(calendar.get(Calendar.SECOND));
1265

    
1266
        return docid.toString();
1267
    }
1268
}
(3-3/10)