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: tao $'
8
 *     '$Date: 2008-05-01 16:10:53 -0700 (Thu, 01 May 2008) $'
9
 * '$Revision: 3815 $'
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.MetaCatUtil;
37
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
38
import edu.ucsb.nceas.metacat.client.Metacat;
39
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
40
import edu.ucsb.nceas.metacat.client.MetacatException;
41
import edu.ucsb.nceas.metacat.client.MetacatFactory;
42
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
43
import edu.ucsb.nceas.utilities.IOUtil;
44
import junit.framework.Test;
45
import junit.framework.TestCase;
46
import junit.framework.TestSuite;
47
import java.io.File;
48

    
49
/**
50
 * A JUnit test for testing Access Control for online data in Metacat
51
 */
52
public class OnlineDataAccessTest
53
    extends TestCase {
54

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

    
67
    private Metacat m;
68

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

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

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

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

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

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

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

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

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

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

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

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

    
160
        return accessBlock;
161

    
162
    }
163

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
276
    /**
277
     * Create a suite of tests to be run together
278
     */
279
    public static Test suite() {
280
        TestSuite suite = new TestSuite();
281
        suite.addTest(new OnlineDataAccessTest("initialize"));
282

    
283
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_1"));
284
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_2"));
285
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_3"));
286
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_4"));
287
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_5"));
288
        suite.addTest(new OnlineDataAccessTest("onlineDataCasesTest_6"));
289

    
290
        return suite;
291
    }
292

    
293

    
294

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

    
303

    
304
    /** *********
305
     * Checking the following cases:
306
     * when only online data is uploaded by a user and
307
     * -> he tries to read it  - success
308
     * -> he tries to add same docid again  - failure
309
     * -> he tries to update it  - success
310
     * -> he tries to set permissions on it  - success
311
     * -> he tries to delete it  - success
312
     * -> he tries to read it after deleteing - failure
313
     */
314
    public void onlineDataCasesTest_1() {
315
        try {
316

    
317
            // upload online data
318
            onlineDocid = generateDocid();
319
            m.login(username, password);
320
            uploadDocid(onlineDocid + ".1",
321
                        onlinetestdatafile1, SUCCESS, false);
322

    
323
            // try to read the data
324
            readDocid(onlineDocid + ".1", SUCCESS, false);
325

    
326
            // try to upload another data with same id
327
            uploadDocid(onlineDocid + ".1",
328
                        onlinetestdatafile2, FAILURE, false);
329

    
330
            // try to upload another data with updated id
331
            uploadDocid(onlineDocid + ".2",
332
                        onlinetestdatafile2, SUCCESS, false);
333

    
334
            // try to set the permissions for the uploaded document
335
            // the docid given is for the online document
336
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
337
                                         null, null,
338
                                         "ecogrid://knb/" + onlineDocid + ".1",
339
                                         null, getAccessBlock(anotheruser, true,
340
                true, false, false, false),
341
                                         null, null, null, null);
342
            newdocid = generateDocid();
343
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
344
            m.logout();
345

    
346
            // check if the permissions were set properly
347
            m.login(anotheruser, anotherpassword);
348
            readDocid(onlineDocid + ".1", SUCCESS, false);
349
            readDocid(onlineDocid + ".2", SUCCESS, false);
350
            m.logout();
351

    
352
            m.login(username, password);
353

    
354
            // delete the document - able to delete .1
355
            // but not able to delete .2 as no rules 
356
	    // written to access table when a document 
357
	    // is 'uploaded'
358
            deleteDocid(onlineDocid + ".1", SUCCESS, false);
359
            deleteDocid(onlineDocid + ".2", FAILURE, true);
360

    
361
            // try to read the documents now
362
            readDocid(onlineDocid + ".1", FAILURE, true);
363
            readDocid(onlineDocid + ".2", FAILURE, true);
364

    
365
            m.logout();
366

    
367
        }
368
        catch (MetacatAuthException mae) {
369
            fail("Authorization failed:\n" + mae.getMessage());
370
        }
371
        catch (MetacatInaccessibleException mie) {
372
            fail("Metacat Inaccessible:\n" + mie.getMessage());
373
        }
374
        catch (Exception e) {
375
            fail("General exception:\n" + e.getMessage());
376
        }
377
    }
378

    
379

    
380
    /** *********
381
     * Checking the following cases:
382
     * when only online data is uploaded by a user and another user
383
     * -> tries to read it  - failure
384
     * -> tries to add same docid again  - failure
385
     * -> tries to update it  - failure
386
     * -> tries to set permissions on it  - failure
387
     * -> tries to delete it  - failure
388
     */
389
    public void onlineDataCasesTest_2() {
390
        try {
391

    
392
            // upload an online document
393
            onlineDocid = generateDocid();
394
            m.login(username, password);
395
            uploadDocid(onlineDocid + ".1",
396
                        onlinetestdatafile1, SUCCESS, false);
397

    
398
            uploadDocid(onlineDocid + ".2",
399
                        onlinetestdatafile2, SUCCESS, false);
400

    
401
            // login as another user
402
            m.logout();
403
            m.login(anotheruser, anotherpassword);
404

    
405
            // try to read the data
406
            readDocid(onlineDocid + ".2", FAILURE, true);
407

    
408
            // try to upload another document with same id
409
            uploadDocid(onlineDocid + ".2",
410
                        onlinetestdatafile2, FAILURE, false);
411

    
412
            // try to upload another document with updated id
413
            uploadDocid(onlineDocid + ".3",
414
                        onlinetestdatafile2, FAILURE, true);
415

    
416

    
417
            // try to set the permissions for the uploaded document
418
            // the docid given is for the online document
419
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
420
                                         null, null,
421
                                         "ecogrid://knb/" + onlineDocid + ".1",
422
                                         "ecogrid://knb/" + onlineDocid + ".2",
423
                                         getAccessBlock(anotheruser, true,
424
                                         true, false, false, false),
425
                                         null, null, null, null);
426
            newdocid = generateDocid();
427
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
428

    
429
            // delete the document - should not be able to delete .1
430
            // but should be able to delete .2
431
            deleteDocid(onlineDocid + ".1", FAILURE, true);
432
            deleteDocid(onlineDocid + ".2", FAILURE, true);
433

    
434
            m.logout();
435

    
436
        }
437
        catch (MetacatAuthException mae) {
438
            fail("Authorization failed:\n" + mae.getMessage());
439
        }
440
        catch (MetacatInaccessibleException mie) {
441
            fail("Metacat Inaccessible:\n" + mie.getMessage());
442
        }
443
        catch (Exception e) {
444
            fail("General exception:\n" + e.getMessage());
445
        }
446
    }
447

    
448

    
449
    /** *********
450
     * Checking the following cases:
451
     * when only online data is uploaded by a user with the following different
452
     * access controls in another document
453
     *   1.read
454
     *   2.write
455
     *   3.change permission
456
     *   4.all
457
     * And another user tries to do the following:
458
     * -> tries to read it
459
     * -> tries to update it
460
     * -> tries to set permissions on it
461
     * -> tries to delete it
462
     */
463
    public void onlineDataCasesTest_3() {
464
        try {
465

    
466
            /////////Case 1./////////////////////
467
            // upload an online document - read only
468
            onlineDocid = generateDocid();
469
            m.login(username, password);
470
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
471

    
472
            // upload a document which gives read access to the online document
473
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
474
                                         null, null,
475
                                         "ecogrid://knb/" + onlineDocid + ".1",
476
                                         null, getAccessBlock(anotheruser, true,
477
                true, false, false, false),
478
                                         null, null, null, null);
479
            newdocid = generateDocid();
480
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
481
            m.logout();
482

    
483
            // login as another user
484
            m.login(anotheruser, anotherpassword);
485

    
486
            // try to read the online data
487
            readDocid(onlineDocid + ".1", SUCCESS, false);
488

    
489
            // try to upload another data with updated id
490
            uploadDocid(onlineDocid + ".2",
491
                        onlinetestdatafile2, FAILURE, true);
492

    
493
            // try to set the permissions for the uploaded document
494
            // the docid given is for the online document
495
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
496
                                         null, null,
497
                                         "ecogrid://knb/" + onlineDocid + ".1",
498
                                         null, getAccessBlock(anotheruser, true,
499
                false, false, false, true),
500
                                         null, null, null, null);
501
            newdocid = generateDocid();
502
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
503

    
504
            // delete the document
505
            deleteDocid(onlineDocid + ".1", FAILURE, true);
506
            m.logout();
507

    
508
            /////////Case 2/////////////////////
509
            // upload an online document - write only
510
            onlineDocid = generateDocid();
511
            m.login(username, password);
512
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
513

    
514
            // upload a document which gives read access to the online document
515
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
516
                                         null, null,
517
                                         "ecogrid://knb/" + onlineDocid + ".1",
518
                                         null, getAccessBlock(anotheruser, true,
519
                false, true, false, false),
520
                                         null, null, null, null);
521
            newdocid = generateDocid();
522
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
523
            m.logout();
524

    
525
            // login as another user
526
            m.login(anotheruser, anotherpassword);
527

    
528
            // try to read the online data
529
            readDocid(onlineDocid + ".1", FAILURE, true);
530

    
531
            // try to upload another data with updated id
532
            uploadDocid(onlineDocid + ".2",
533
                        onlinetestdatafile2, SUCCESS, false);
534

    
535
            // try to set the permissions for the uploaded document
536
            // the docid given is for the online document
537
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
538
                                         null, null,
539
                                         "ecogrid://knb/" + onlineDocid + ".1",
540
                                         null, getAccessBlock(anotheruser, true,
541
                                         false, false, false, true),
542
                                         null, null, null, null);
543
            newdocid = generateDocid();
544
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
545

    
546
            // delete the document
547
            deleteDocid(onlineDocid + ".2", FAILURE, true);
548
            m.logout();
549

    
550
            /////////Case 3/////////////////////
551
            // upload an online document - change permission only
552
            onlineDocid = generateDocid();
553
            m.login(username, password);
554
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
555

    
556
            // upload a document which gives read access to the online document
557
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Another insert",
558
                                         null, null,
559
                                         "ecogrid://knb/" + onlineDocid + ".1",
560
                                         null, getAccessBlock(anotheruser, true,
561
                                         false, false, true, false),
562
                                         null, null, null, null);
563
            newdocid = generateDocid();
564
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
565
            m.logout();
566

    
567
            // login as another user
568
            m.login(anotheruser, anotherpassword);
569

    
570
            // try to read the online data
571
            readDocid(onlineDocid + ".1", FAILURE, true);
572

    
573
            // try to upload another data with updated id
574
            uploadDocid(onlineDocid + ".2",
575
                          onlinetestdatafile2, FAILURE, true);
576

    
577
            // try to set the permissions for the uploaded document
578
            // the docid given is for the online document
579
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
580
                                         null, null,
581
                                         "ecogrid://knb/" + onlineDocid + ".1",
582
                                         null, getAccessBlock(anotheruser, true,
583
                                         false, false, false, true),
584
                                         null, null, null, null);
585
            newdocid = generateDocid();
586
            // ERRRRRRRRRRRRRRRR
587
            // User does not have permission to update of access rules for data
588
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
589

    
590
            // delete the document
591
            deleteDocid(onlineDocid + ".1", FAILURE, true);
592
            m.logout();
593

    
594
            /////////Case 4/////////////////////
595
            // upload an online document all
596
            onlineDocid = generateDocid();
597
            m.login(username, password);
598
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
599

    
600
            // upload a document which gives read access to the online document
601
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
602
                                         null, null,
603
                                         "ecogrid://knb/" + onlineDocid + ".1",
604
                                         null, getAccessBlock(anotheruser, true,
605
                                         false, false, false, true),
606
                                         null, null, null, null);
607
            newdocid = generateDocid();
608
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
609
            m.logout();
610

    
611
            // login as another user
612
            m.login(anotheruser, anotherpassword);
613

    
614
            // try to read the online data
615
            readDocid(onlineDocid + ".1", SUCCESS, false);
616

    
617
            // try to upload another data with updated id
618
            uploadDocid(onlineDocid + ".2",
619
                        onlinetestdatafile2, SUCCESS, false);
620

    
621
            // try to set the permissions for the uploaded document
622
            // the docid given is for the online document
623
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
624
                                         null, null,
625
                                         "ecogrid://knb/" + onlineDocid + ".1",
626
                                         null, getAccessBlock(anotheruser, true,
627
                                         true, false, false, false),
628
                                         null, null, null, null);
629
            newdocid = generateDocid();
630
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
631

    
632
            m.logout();
633
            // delete the document
634
            deleteDocid(onlineDocid + ".1", FAILURE, false);
635

    
636
            m.logout();
637

    
638
        }
639
        catch (MetacatAuthException mae) {
640
            fail("Authorization failed:\n" + mae.getMessage());
641
        }
642
        catch (MetacatInaccessibleException mie) {
643
            fail("Metacat Inaccessible:\n" + mie.getMessage());
644
        }
645
        catch (Exception e) {
646
            fail("General exception:\n" + e.getMessage());
647
        }
648
    }
649

    
650

    
651

    
652
    /** *********
653
     * Checking the following cases:
654
     * when only online data is uploaded by a user with the following different
655
     * access controls specified in addiotnal metadata in another document
656
     *   1.read
657
     *   2.write
658
     *   3.change permission
659
     *   4.all
660
     * And another user tries to do the following:
661
     * -> tries to read it
662
     * -> tries to update it
663
     * -> tries to set permissions on it
664
     * -> tries to delete it
665
     */
666
    public void onlineDataCasesTest_4() {
667
        try {
668

    
669
            /////////Case 1./////////////////////
670
            // upload an online document - read only
671
            onlineDocid = generateDocid();
672
            m.login(username, password);
673
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
674

    
675
            // upload a document which gives read access to the online document
676
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
677
                                         null, null,
678
                                         "ecogrid://knb/" + onlineDocid + ".1",
679
                                         null, null, null, null,
680
                                         getAccessBlock(anotheruser, true,
681
                                         true, false, false, false), null);
682
            newdocid = generateDocid();
683
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
684
            m.logout();
685

    
686
            // login as another user
687
            m.login(anotheruser, anotherpassword);
688

    
689
            // try to read the online data
690
            readDocid(onlineDocid + ".1", SUCCESS, false);
691

    
692
            // try to upload another data with updated id
693
            uploadDocid(onlineDocid + ".2",
694
                        onlinetestdatafile2, FAILURE, true);
695

    
696
            // try to set the permissions for the uploaded document
697
            // the docid given is for the online document
698
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
699
                                         null, null,
700
                                         "ecogrid://knb/" + onlineDocid + ".1",
701
                                         null, null, null, null,
702
                                         getAccessBlock(anotheruser, true,
703
                                         false, false, false, true), null);
704
            newdocid = generateDocid();
705
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
706

    
707
            // delete the document
708
            deleteDocid(onlineDocid + ".1", FAILURE, true);
709
            m.logout();
710

    
711
            /////////Case 2/////////////////////
712
            // upload an online document - write only
713
            onlineDocid = generateDocid();
714
            m.login(username, password);
715
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
716

    
717
            // upload a document which gives read access to the online document
718
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
719
                                         null, null,
720
                                         "ecogrid://knb/" + onlineDocid + ".1",
721
                                         null, null, null, null,
722
                                         getAccessBlock(anotheruser, true,
723
                                         false, true, false, false), null);
724
            newdocid = generateDocid();
725
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
726
            m.logout();
727

    
728
            // login as another user
729
            m.login(anotheruser, anotherpassword);
730

    
731
            // try to read the online data
732
            readDocid(onlineDocid + ".1", FAILURE, true);
733

    
734
            // try to upload another data with updated id
735
            uploadDocid(onlineDocid + ".2",
736
                        onlinetestdatafile2, SUCCESS, false);
737

    
738
            // try to set the permissions for the uploaded document
739
            // the docid given is for the online document
740
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
741
                                         null, null,
742
                                         "ecogrid://knb/" + onlineDocid + ".1",
743
                                         null, null, null, null,
744
                                         getAccessBlock(anotheruser, true,
745
                                         false, false, false, true), null);
746
            newdocid = generateDocid();
747
            insertDocid(newdocid + ".1", testdocument, FAILURE, true);
748

    
749
            // delete the document
750
            deleteDocid(onlineDocid + ".2", FAILURE, true);
751
            m.logout();
752

    
753
            /////////Case 3/////////////////////
754
            // upload an online document - change permission only
755
            onlineDocid = generateDocid();
756
            m.login(username, password);
757
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
758

    
759
            // upload a document which gives read access to the online document
760
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
761
                                         null, null,
762
                                         "ecogrid://knb/" + onlineDocid + ".1",
763
                                         null, null, null, null,
764
                                         getAccessBlock(anotheruser, true,
765
                                         false, false, true, false), null);
766
            newdocid = generateDocid();
767
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
768
            m.logout();
769

    
770
            // login as another user
771
            m.login(anotheruser, anotherpassword);
772

    
773
            // try to read the online data
774
            readDocid(onlineDocid + ".1", FAILURE, true);
775

    
776
            // try to upload another data with updated id
777
            uploadDocid(onlineDocid + ".2",
778
                        onlinetestdatafile2, FAILURE, true);
779

    
780
            // try to set the permissions for the uploaded document
781
            // the docid given is for the online document
782
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
783
                                         null, null,
784
                                         "ecogrid://knb/" + onlineDocid + ".1",
785
                                         null, null, null, null,
786
                                         getAccessBlock(anotheruser, true,
787
                                         false, false, false, true), null);
788
            newdocid = generateDocid();
789
            // ERRRRRRRRRRRRRRRR
790
            // User does not have permission to update of access rules for data
791
            // insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
792

    
793
            // delete the document
794
            deleteDocid(onlineDocid + ".1", FAILURE, true);
795
            m.logout();
796

    
797
            /////////Case 4/////////////////////
798
            // upload an online document all
799
            onlineDocid = generateDocid();
800
            m.login(username, password);
801
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
802

    
803
            // upload a document which gives read access to the online document
804
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
805
                                         null, null,
806
                                         "ecogrid://knb/" + onlineDocid + ".1",
807
                                         null, null, null, null,
808
                                         getAccessBlock(anotheruser, true,
809
                                         false, false, false, true), null);
810
            newdocid = generateDocid();
811
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
812
            m.logout();
813

    
814
            // login as another user
815
            m.login(anotheruser, anotherpassword);
816

    
817
            // try to read the online data
818
            readDocid(onlineDocid + ".1", SUCCESS, false);
819

    
820
            // try to upload another data with updated id
821
            uploadDocid(onlineDocid + ".2",
822
                        onlinetestdatafile2, SUCCESS, false);
823

    
824
            // try to set the permissions for the uploaded document
825
            // the docid given is for the online document
826
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
827
                                         null, null,
828
                                         "ecogrid://knb/" + onlineDocid + ".1",
829
                                         null, null, null, null,
830
                                         getAccessBlock(anotheruser, true,
831
                                         true, false, false, false), null);
832
            newdocid = generateDocid();
833
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
834

    
835
            m.logout();
836
            // delete the document
837
            deleteDocid(onlineDocid + ".1", FAILURE, false);
838

    
839
            m.logout();
840

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

    
853
    /** *********
854
     * Checking the following cases:
855
     * -> when online data with document refering to it is uploaded with
856
     *    rules in additional metadata for an wrong entity id which
857
     *    doesnt exist
858
     * -> when online data with document refering to it is uploaded with
859
     *    rules in additional metadata for an entity which doesnt
860
     *    exist - wrong url
861
     */
862
    public void onlineDataCasesTest_5() {
863
        try {
864

    
865
            // upload online data
866
            onlineDocid = generateDocid();
867
            m.login(username, password);
868

    
869
            /////////Case 1
870
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
871
                                         "ecogrid://knb/" + onlineDocid + ".1",
872
                                         null, null, null, null, null,
873
                                         getAccessBlock(anotheruser, true,
874
                                         true, false, false, false));
875
            newdocid = generateDocid();
876
            insertDocid(newdocid + ".1", testdocument, FAILURE, false);
877

    
878
            /////////Case 2
879
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert", null, null,
880
                                         "ecogrid://knb/" + onlineDocid + ".1",
881
                                         null, null, null, null,
882
                                         getAccessBlock(anotheruser, true,
883
                                         true, false, false, false), null);
884
            newdocid = generateDocid();
885

    
886
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
887
            m.logout();
888

    
889
        }
890
        catch (MetacatAuthException mae) {
891
            fail("Authorization failed:\n" + mae.getMessage());
892
        }
893
        catch (MetacatInaccessibleException mie) {
894
            fail("Metacat Inaccessible:\n" + mie.getMessage());
895
        }
896
        catch (Exception e) {
897
            fail("General exception:\n" + e.getMessage());
898
        }
899
    }
900

    
901

    
902
    /** *********
903
     * Checking the following cases:
904
     * -> when a document is added with no online data - it is updated (has
905
     *    access)  - then data is added - and a url to it is added and docid
906
     *    is updated - then the access is updated in document
907
     *    does it result in rules being applied on the data
908
     *    (Andrea Chadden was having problem is a similar case)
909
     * -> when online data with document refering to it is uploaded with read
910
     *    access for document and no access for docid and vice versa
911
     */
912
    public void onlineDataCasesTest_6() {
913
        try {
914

    
915
            // insert a document
916
            m.login(username, password);
917
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
918
                                         null, null, null,
919
                                         null, getAccessBlock(anotheruser, true,
920
                                         true, false, false, false), null, null,
921
                                         null, null);
922
            newdocid = generateDocid();
923
            insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
924
            m.logout();
925

    
926

    
927
            // update document
928
            m.login(username, password);
929
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing update",
930
                                         null, null, null,
931
                                         null, getAccessBlock(anotheruser, true,
932
                                         true, false, false, false), null, null,
933
                                         null, null);
934
            updateDocid(newdocid + ".2", testdocument, SUCCESS, false);
935
            m.logout();
936

    
937

    
938
            // upload data and update the document
939
            onlineDocid = generateDocid();
940
            m.login(username, password);
941
            uploadDocid(onlineDocid + ".1", onlinetestdatafile1, SUCCESS, false);
942
            m.logout();
943

    
944
            // try to read the online data
945
            m.login(anotheruser, anotherpassword);
946
            readDocid(onlineDocid + ".1", FAILURE, true);
947
            m.logout();
948

    
949
            // upload data and update the document
950
            m.login(username, password);
951
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing update",
952
                                         null, null,
953
                                         "ecogrid://knb/" + onlineDocid + ".1",
954
                                         null, getAccessBlock(anotheruser, true,
955
                                         true, false, false, false), null, null,
956
                                         null, null);
957
            updateDocid(newdocid + ".3", testdocument, SUCCESS, false);
958
            m.logout();
959

    
960
            // set read for document - no read for data
961
            m.login(username, password);
962
            testdocument = getTestEmlDoc("OnlineDataAccessTest: Doing insert",
963
                                         null, null,
964
                                         "ecogrid://knb/" + onlineDocid + ".1",
965
                                         null, getAccessBlock(anotheruser, true,
966
                                         true, false, false, false), null, null,
967
                                         getAccessBlock(anotheruser, false,
968
                                         false, false, false, true), null);
969
            updateDocid(newdocid + ".4", testdocument, SUCCESS, false);
970
            m.logout();
971

    
972
            // try to read the online data
973
            m.login(anotheruser, anotherpassword);
974
            readDocid(newdocid + ".4", SUCCESS, false);
975
            readDocid(onlineDocid + ".1", FAILURE, true);
976
            m.logout();
977

    
978

    
979
            // set read for document - no read for data
980
            m.login(username, password);
981
            testdocument = getTestEmlDoc("Doing update", null, null,
982
                                         "ecogrid://knb/" + onlineDocid + ".1",
983
                                         null, getAccessBlock(anotheruser, false,
984
                                         false, false, false, true), null, null,
985
                                         getAccessBlock(anotheruser, true,
986
                                         true, false, false, false), null);
987
            updateDocid(newdocid + ".5", testdocument, SUCCESS, false);
988
            m.logout();
989

    
990
            // try to read the online data
991
            m.login(anotheruser, anotherpassword);
992
            readDocid(newdocid + ".5", FAILURE, true);
993
            readDocid(onlineDocid + ".1", SUCCESS, false);
994
            m.logout();
995
        }
996
        catch (MetacatAuthException mae) {
997
            fail("Authorization failed:\n" + mae.getMessage());
998
        }
999
        catch (MetacatInaccessibleException mie) {
1000
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1001
        }
1002
        catch (Exception e) {
1003
            fail("General exception:\n" + e.getMessage());
1004
        }
1005
    }
1006

    
1007

    
1008
    /**
1009
     * Insert a document into metacat. The expected result is passed as result
1010
     */
1011

    
1012
    private String insertDocid(String docid, String docText, boolean result,
1013
                               boolean expectKarmaException) {
1014
        String response = null;
1015
        try {
1016
            response = m.insert(docid,
1017
                                new StringReader(testdocument), null);
1018
            if (result) {
1019
                assertTrue( (response.indexOf("<success>") != -1));
1020
                assertTrue(response.indexOf(docid) != -1);
1021
            }
1022
            else {
1023
                assertTrue( (response.indexOf("<success>") == -1));
1024
            }
1025
            System.err.println(response);
1026
        }
1027
        catch (MetacatInaccessibleException mie) {
1028
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1029
        }
1030
        catch (InsufficientKarmaException ike) {
1031
            if (!expectKarmaException) {
1032
                fail("Insufficient karma:\n" + ike.getMessage());
1033
            }
1034
        }
1035
        catch (MetacatException me) {
1036
            if (result) {
1037
               fail("Metacat Error:\n" + me.getMessage());
1038
           }
1039
           else {
1040
               System.err.println("Metacat Error: " + me.getMessage());
1041
           }
1042
        }
1043
        catch (Exception e) {
1044
            fail("General exception:\n" + e.getMessage());
1045
        }
1046
        return response;
1047
    }
1048

    
1049
    /**
1050
     * Insert a document into metacat. The expected result is passed as result
1051
     */
1052

    
1053
    private String uploadDocid(String docid, String filePath, boolean result,
1054
                               boolean expectedKarmaException) {
1055
        String response = null;
1056
        try {
1057
            response = m.upload(docid, new File(filePath));
1058
            if (result) {
1059
                assertTrue( (response.indexOf("<success>") != -1));
1060
                assertTrue(response.indexOf(docid) != -1);
1061
            }
1062
            else {
1063
                assertTrue( (response.indexOf("<success>") == -1));
1064
            }
1065
            System.err.println("respose from metacat: " + response);
1066
        }
1067
        catch (MetacatInaccessibleException mie) {
1068
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1069
        }
1070
        catch (InsufficientKarmaException ike) {
1071
            if (!expectedKarmaException) {
1072
                fail("Insufficient karma:\n" + ike.getMessage());
1073
            }
1074
        }
1075
        catch (MetacatException me) {
1076
            if (result) {
1077
                fail("Metacat Error:\n" + me.getMessage());
1078
            }
1079
            else {
1080
                System.err.println("Metacat Error: " + me.getMessage());
1081
            }
1082
        }
1083
        catch (Exception e) {
1084
            fail("General exception:\n" + e.getMessage());
1085
        }
1086
        return response;
1087
    }
1088

    
1089
    /**
1090
     * Update a document in metacat. The expected result is passed as result
1091
     */
1092
    private String updateDocid(String docid, String docText, boolean result,
1093
                               boolean expectedKarmaFailure) {
1094
        String response = null;
1095
        try {
1096
            response = m.update(docid,
1097
                                new StringReader(testdocument), null);
1098

    
1099
            if (result) {
1100
                assertTrue( (response.indexOf("<success>") != -1));
1101
                assertTrue(response.indexOf(docid) != -1);
1102
            }
1103
            else {
1104
                assertTrue( (response.indexOf("<success>") == -1));
1105
            }
1106
            System.err.println(response);
1107
        }
1108
        catch (MetacatInaccessibleException mie) {
1109
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1110
        }
1111
        catch (InsufficientKarmaException ike) {
1112
            if (!expectedKarmaFailure) {
1113
                fail("Insufficient karma:\n" + ike.getMessage());
1114
            }
1115
        }
1116
        catch (MetacatException me) {
1117
            if (! (expectedKarmaFailure &&
1118
                   (me.getMessage().indexOf(
1119
                "User try to update a access module which it doesn't have \"ALL\" permission") !=
1120
                    -1))) {
1121
                fail("Metacat Error:\n" + me.getMessage());
1122
            }
1123
        }
1124
        catch (Exception e) {
1125
            fail("General exception:\n" + e.getMessage());
1126
        }
1127

    
1128
        return response;
1129
    }
1130

    
1131
    /**
1132
     * Delete a document into metacat. The expected result is passed as result
1133
     */
1134
    private void deleteDocid(String docid, boolean result,
1135
                             boolean expectedKarmaFailure) {
1136
        try {
1137
            String response = m.delete(docid);
1138
            if (result) {
1139
                assertTrue(response.indexOf("<success>") != -1);
1140
            }
1141
            else {
1142
                assertTrue(response.indexOf("<success>") == -1);
1143
            }
1144
            System.err.println(response);
1145
        }
1146
        catch (MetacatInaccessibleException mie) {
1147
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1148
        }
1149
        catch (InsufficientKarmaException ike) {
1150
            if (!expectedKarmaFailure) {
1151
                    fail("Insufficient karma:\n" + ike.getMessage());
1152
                }
1153

    
1154
            }
1155
        catch (MetacatException me) {
1156
            if (result) {
1157
                fail("Metacat Error:\n" + me.getMessage());
1158
            }
1159
            else {
1160
                System.err.println("Metacat Error:\n" + me.getMessage());
1161
            }
1162
        }
1163
        catch (Exception e) {
1164
            fail("General exception:\n" + e.getMessage());
1165
        }
1166
    }
1167

    
1168
    /**
1169
     * Read a document from metacat. The expected result is passed as result
1170
     */
1171
    private void readDocid(String docid, boolean result,
1172
                           boolean expextedKarmaFailure) {
1173
        try {
1174
            Reader r = m.read(docid);
1175
            String response = IOUtil.getAsString(r, true);
1176

    
1177
            if (!result) {
1178
                assertTrue(response.indexOf("<success>") == -1);
1179
            }
1180
            // System.err.println(response);
1181
        }
1182
        catch (MetacatInaccessibleException mie) {
1183
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1184
        }
1185
        catch (InsufficientKarmaException ike) {
1186
            if (!expextedKarmaFailure) {
1187
                fail("Insufficient karma:\n" + ike.getMessage());
1188
            }
1189
        }
1190
        catch (MetacatException me) {
1191
            if (result) {
1192
                fail("Metacat Error:\n" + me.getMessage());
1193
            }
1194
            else {
1195
                System.err.println("Metacat Error:\n" + me.getMessage());
1196
            }
1197
        }
1198
        catch (Exception e) {
1199
            fail("General exception:\n" + e.getMessage());
1200
        }
1201
    }
1202

    
1203
    /**
1204
     * Read a document from metacat and check if it is equal to a given string.
1205
     * The expected result is passed as result
1206
     */
1207

    
1208
    private void readDocidWhichEqualsDoc(String docid, String testDoc,
1209
                                         boolean result,
1210
                                         boolean expextedKarmaFailure) {
1211
        try {
1212
            Reader r = m.read(docid);
1213
            String doc = IOUtil.getAsString(r, true);
1214
            if (result) {
1215

    
1216
                if (!testDoc.equals(doc)) {
1217
                    System.out.println("doc    :" + doc);
1218
                    System.out.println("testDoc:" + testDoc);
1219
                }
1220

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

    
1242
    }
1243

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

    
1254
        // Create a calendar to get the date formatted properly
1255
        String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
1256
        SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
1257
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
1258
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
1259
                       2 * 60 * 60 * 1000);
1260
        Calendar calendar = new GregorianCalendar(pdt);
1261
        Date trialTime = new Date();
1262
        calendar.setTime(trialTime);
1263
        docid.append(calendar.get(Calendar.YEAR));
1264
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
1265
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
1266
        docid.append(calendar.get(Calendar.MINUTE));
1267
        docid.append(calendar.get(Calendar.SECOND));
1268
   	    //sometimes this number is not unique, so we append a random number
1269
    	int random = (new Double(Math.random()*100)).intValue();
1270
    	docid.append(random);
1271
        
1272
        return docid.toString();
1273
    }
1274
}
(10-10/18)