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-08-18 13:12:39 -0700 (Wed, 18 Aug 2004) $'
9
 * '$Revision: 2239 $'
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.FileReader;
29
import java.io.IOException;
30
import java.io.Reader;
31
import java.io.StringReader;
32
import java.util.Calendar;
33
import java.util.Date;
34
import java.util.GregorianCalendar;
35
import java.util.SimpleTimeZone;
36
import java.util.TimeZone;
37

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

    
49
/**
50
 * A JUnit test for testing Step class processing
51
 */
52
public class AccessControlTest
53
    extends TestCase {
54
  private String metacatUrl = "@systemidserver@@servlet-path@";
55
  private String wrongMetacatUrl =
56
      "http://somepalce.somewhere.com/some/servlet/metacat";
57
  private String username = "@mcuser@";
58
  private String password = "@mcpassword@";
59
  private String anotheruser = "@mcanotheruser@";
60
  private String anotherpassword = "@mcanotherpassword@";
61
  private String failpass = "uidfnkj43987yfdn";
62
  private String prefix = "test";
63
  private String newdocid = null;
64
  private String testfile = "test/jones.204.22.xml";
65
  private String queryFile = "test/query.xml";
66
  private String testdocument = "";
67
  private Metacat m;
68
  private boolean SUCCESS = true;
69
  private boolean FAILURE = false;
70

    
71
  /**
72
   * Constructor to build the test
73
   *
74
   * @param name the name of the test method
75
   */
76
  public AccessControlTest(String name) {
77
    super(name);
78
    newdocid = generateDocid();
79
  }
80

    
81
  /**
82
   * Establish a testing framework by initializing appropriate objects
83
   */
84
  public void setUp() {
85
    try {
86
      FileReader fr = new FileReader(testfile);
87
      testdocument = IOUtil.getAsString(fr, true);
88
    }
89
    catch (IOException ioe) {
90
      fail("Can't read test data to run the test: " + testfile);
91
    }
92

    
93
    try {
94
      System.err.println("Test Metacat: " + metacatUrl);
95
      m = MetacatFactory.createMetacatConnection(metacatUrl);
96
    }
97
    catch (MetacatInaccessibleException mie) {
98
      System.err.println("Metacat is: " + metacatUrl);
99
      fail("Metacat connection failed." + mie.getMessage());
100
    }
101
  }
102

    
103
  /**
104
   * Release any objects after tests are complete
105
   */
106
  public void tearDown() {
107
  }
108

    
109
  /**
110
   * Create a suite of tests to be run together
111
   */
112
  public static Test suite() {
113
    TestSuite suite = new TestSuite();
114
    suite.addTest(new AccessControlTest("initialize"));
115
    // Test basic functions
116
    suite.addTest(new AccessControlTest("login"));
117
    suite.addTest(new AccessControlTest("insert"));
118
    suite.addTest(new AccessControlTest("read"));
119
    suite.addTest(new AccessControlTest("update"));
120
    suite.addTest(new AccessControlTest("delete"));
121
    // Tests when no access is specified
122
    suite.addTest(new AccessControlTest("noAccessSpecified_Owner"));
123
    suite.addTest(new AccessControlTest("noAccessSpecified_Random"));
124
    suite.addTest(new AccessControlTest("noAccessSpecified_Public"));
125

    
126
    return suite;
127
  }
128

    
129
  /**
130
   * Run an initial test that always passes to check that the test
131
   * harness is working.
132
   */
133
  public void initialize() {
134
    assertTrue(1 == 1);
135
  }
136

    
137
  /**
138
   * Test the login() function with valid credentials
139
   */
140
  public void login() {
141
    // Try a valid login
142
    try {
143
      String response = m.login(username, password);
144
      System.err.println("Login response: " + response);
145
      assertTrue(response != null);
146
      assertTrue(response.indexOf("<login>") != -1);
147
      String sessionId = m.getSessionId();
148
      System.err.println("Session ID: " + m.getSessionId());
149
      assertTrue(sessionId != null);
150
      assertTrue(response.indexOf(m.getSessionId()) != -1);
151
    }
152
    catch (MetacatAuthException mae) {
153
      fail("Authorization failed:\n" + mae.getMessage());
154
    }
155
    catch (MetacatInaccessibleException mie) {
156
      fail("Metacat Inaccessible:\n" + mie.getMessage());
157
    }
158
  }
159

    
160
  /**
161
   * Test the insert() function with a known document
162
   */
163
  public void insert() {
164
    try {
165
      String identifier = newdocid + ".1";
166
      m.login(username, password);
167
      String response = m.insert(identifier,
168
                                 new StringReader(testdocument), null);
169
      assertTrue(response.indexOf("<success>") != -1);
170
      assertTrue(response.indexOf(identifier) != -1);
171
      System.err.println(response);
172

    
173
    }
174
    catch (MetacatAuthException mae) {
175
      fail("Authorization failed:\n" + mae.getMessage());
176
    }
177
    catch (MetacatInaccessibleException mie) {
178
      fail("Metacat Inaccessible:\n" + mie.getMessage());
179
    }
180
    catch (InsufficientKarmaException ike) {
181
      assertTrue(1 == 1);
182
      fail("Insufficient karma:\n" + ike.getMessage());
183
    }
184
    catch (MetacatException me) {
185
      fail("Metacat Error:\n" + me.getMessage());
186
    }
187
    catch (Exception e) {
188
      fail("General exception:\n" + e.getMessage());
189
    }
190
  }
191

    
192
  /**
193
   * The read() function
194
   */
195
  public void read() {
196
    try {
197
      m.login(username, password);
198
      Reader r = m.read(newdocid + ".1");
199
      String doc = IOUtil.getAsString(r, true);
200
      doc = doc + "\n";
201
      System.err.println(doc);
202
      assertTrue(doc.equals(testdocument));
203
    }
204
    catch (MetacatAuthException mae) {
205
      fail("Authorization failed:\n" + mae.getMessage());
206
    }
207
    catch (MetacatInaccessibleException mie) {
208
      fail("Metacat Inaccessible:\n" + mie.getMessage());
209
    }
210
    catch (Exception e) {
211
      fail("General exception:\n" + e.getMessage());
212
    }
213
  }
214

    
215
  /**
216
   * Test the update() function with a known document
217
   */
218
  public void update() {
219
    try {
220
      String identifier = newdocid + ".2";
221
      m.login(username, password);
222
      String response = m.update(identifier,
223
                                 new StringReader(testdocument), null);
224
      assertTrue(response.indexOf("<success>") != -1);
225
      assertTrue(response.indexOf(identifier) != -1);
226
      System.err.println(response);
227

    
228
    }
229
    catch (MetacatAuthException mae) {
230
      fail("Authorization failed:\n" + mae.getMessage());
231
    }
232
    catch (MetacatInaccessibleException mie) {
233
      fail("Metacat Inaccessible:\n" + mie.getMessage());
234
    }
235
    catch (InsufficientKarmaException ike) {
236
      fail("Insufficient karma:\n" + ike.getMessage());
237
    }
238
    catch (MetacatException me) {
239
      fail("Metacat Error:\n" + me.getMessage());
240
    }
241
    catch (Exception e) {
242
      fail("General exception:\n" + e.getMessage());
243
    }
244
  }
245

    
246
  /**
247
   * Test the delete() function with a known document
248
   */
249
  public void delete() {
250
    try {
251
      String identifier = newdocid + ".2";
252
      m.login(username, password);
253
      String response = m.delete(identifier);
254
      assertTrue(response.indexOf("<success>") != -1);
255
      System.err.println(response);
256

    
257
    }
258
    catch (MetacatAuthException mae) {
259
      fail("Authorization failed:\n" + mae.getMessage());
260
    }
261
    catch (MetacatInaccessibleException mie) {
262
      fail("Metacat Inaccessible:\n" + mie.getMessage());
263
    }
264
    catch (InsufficientKarmaException ike) {
265
      fail("Insufficient karma:\n" + ike.getMessage());
266
    }
267
    catch (MetacatException me) {
268
      fail("Metacat Error:\n" + me.getMessage());
269
    }
270
    catch (Exception e) {
271
      fail("General exception:\n" + e.getMessage());
272
    }
273
  }
274

    
275
  /**
276
   * Test the case when no access is specified and owner is logged in
277
   */
278
  public void noAccessRuleSpecified_Owner() {
279
    try {
280
      newdocid = generateDocid();
281
      String identifier = newdocid + ".1";
282
      // login
283
      m.login(username, password);
284

    
285
      // insert a document - get the docid
286
      insertDocid(newdocid + ".1", testdocument, SUCCESS);
287
      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
288

    
289
      // update the document
290
      updateDocid(newdocid + ".2", testdocument, SUCCESS);
291
      readDocidWhichEqualsDoc(newdocid + ".2", testdocument, SUCCESS);
292

    
293
      // update the inline data
294
      updateDocid(newdocid + ".3", testdocument, SUCCESS);
295
      readDocidWhichEqualsDoc(newdocid + ".3", testdocument, SUCCESS);
296

    
297
      // update the online data
298
      updateDocid(newdocid + ".4", testdocument, SUCCESS);
299
      readDocidWhichEqualsDoc(newdocid + ".4", testdocument, SUCCESS);
300

    
301
      // update the document access control
302
      updateDocid(newdocid + ".5", testdocument, SUCCESS);
303
      readDocidWhichEqualsDoc(newdocid + ".5", testdocument, SUCCESS);
304

    
305
      // update the document access control for inline data
306
      updateDocid(newdocid + ".6", testdocument, SUCCESS);
307
      readDocidWhichEqualsDoc(newdocid + ".6", testdocument, SUCCESS);
308

    
309
      // update the document access control for online data
310
      updateDocid(newdocid + ".7", testdocument, SUCCESS);
311
      readDocidWhichEqualsDoc(newdocid + ".7", testdocument, SUCCESS);
312

    
313
      // delete the document
314
      deleteDocid(newdocid + ".7", SUCCESS);
315
      m.logout();
316
    }
317
    catch (MetacatAuthException mae) {
318
      fail("Authorization failed:\n" + mae.getMessage());
319
    }
320
    catch (MetacatInaccessibleException mie) {
321
      fail("Metacat Inaccessible:\n" + mie.getMessage());
322
    }
323
    catch (Exception e) {
324
      fail("General exception:\n" + e.getMessage());
325
    }
326

    
327
  }
328

    
329
  /**
330
   * Test the case when no access is specified and another user is logged in
331
   */
332
  public void noAccessRuleSpecified_Random() {
333
    try {
334
      newdocid = generateDocid();
335
      String identifier = newdocid + ".1";
336
      // login
337
      m.login(username, password);
338

    
339
      // insert a document - get the docid
340
      insertDocid(newdocid + ".1", testdocument, SUCCESS);
341
      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
342

    
343
      // logoutand login as other user
344
      m.logout();
345
      m.login(anotheruser, anotherpassword);
346

    
347
      // read the document
348
      readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE);
349

    
350
      // update the document
351
      updateDocid(newdocid + ".2", testdocument, FAILURE);
352

    
353
      // update the inline data
354
      updateDocid(newdocid + ".2", testdocument, FAILURE);
355

    
356
      // update the online data
357
      updateDocid(newdocid + ".2", testdocument, FAILURE);
358

    
359
      // update the document access control
360
      updateDocid(newdocid + ".2", testdocument, FAILURE);
361

    
362
      // update the document access control for inline data
363
      updateDocid(newdocid + ".2", testdocument, FAILURE);
364

    
365
      // update the document access control for online data
366
      updateDocid(newdocid + ".2", testdocument, FAILURE);
367

    
368
      // delete the document
369
      deleteDocid(newdocid + ".2", SUCCESS);
370
      m.logout();
371
    }
372
    catch (MetacatAuthException mae) {
373
      fail("Authorization failed:\n" + mae.getMessage());
374
    }
375
    catch (MetacatInaccessibleException mie) {
376
      fail("Metacat Inaccessible:\n" + mie.getMessage());
377
    }
378
    catch (MetacatException me) {
379
      fail("Metacat Error:\n" + me.getMessage());
380
    }
381
    catch (Exception e) {
382
      fail("General exception:\n" + e.getMessage());
383
    }
384

    
385
  }
386

    
387
  /**
388
   * Test the case when no access is specified and public is logged in
389
   */
390
  public void noAccessRuleSpecified_Public() {
391
    try {
392
      newdocid = generateDocid();
393
      String identifier = newdocid + ".1";
394
      // login
395
      m.login(username, password);
396

    
397
      // insert a document - get the docid
398
      insertDocid(newdocid + ".1", testdocument, SUCCESS);
399
      readDocidWhichEqualsDoc(newdocid + ".1", testdocument, SUCCESS);
400

    
401
      // logoutand login as other user
402
      m.logout();
403

    
404
      // read the document
405
      readDocidWhichEqualsDoc(newdocid + ".1", null, FAILURE);
406

    
407
      // update the document
408
      updateDocid(newdocid + ".2", testdocument, FAILURE);
409

    
410
      // update the inline data
411
      updateDocid(newdocid + ".2", testdocument, FAILURE);
412

    
413
      // update the online data
414
      updateDocid(newdocid + ".2", testdocument, FAILURE);
415

    
416
      // update the document access control
417
      updateDocid(newdocid + ".2", testdocument, FAILURE);
418

    
419
      // update the document access control for inline data
420
      updateDocid(newdocid + ".2", testdocument, FAILURE);
421

    
422
      // update the document access control for online data
423
      updateDocid(newdocid + ".2", testdocument, FAILURE);
424

    
425
      // delete the document
426
      deleteDocid(newdocid + ".2", SUCCESS);
427
      m.logout();
428
    }
429
    catch (MetacatAuthException mae) {
430
      fail("Authorization failed:\n" + mae.getMessage());
431
    }
432
    catch (MetacatInaccessibleException mie) {
433
      fail("Metacat Inaccessible:\n" + mie.getMessage());
434
    }
435
    catch (MetacatException me) {
436
      fail("Metacat Error:\n" + me.getMessage());
437
    }
438
    catch (Exception e) {
439
      fail("General exception:\n" + e.getMessage());
440
    }
441

    
442
  }
443

    
444
  public String insertDocid(String docid, String docText, boolean result) {
445
    String response = null;
446
    try {
447
      response = m.insert(docid,
448
                          new StringReader(testdocument), null);
449
      if (result) {
450
        assertTrue( (response.indexOf("<success>") != -1));
451
        assertTrue(response.indexOf(docid) != -1);
452
      }
453
      else {
454
        assertTrue( (response.indexOf("<success>") == -1));
455
      }
456
      System.err.println(response);
457
    }
458
    catch (MetacatInaccessibleException mie) {
459
      fail("Metacat Inaccessible:\n" + mie.getMessage());
460
    }
461
    catch (InsufficientKarmaException ike) {
462
      fail("Insufficient karma:\n" + ike.getMessage());
463
    }
464
    catch (MetacatException me) {
465
      fail("Metacat Error:\n" + me.getMessage());
466
    }
467
    catch (Exception e) {
468
      fail("General exception:\n" + e.getMessage());
469
    }
470
    return response;
471
  }
472

    
473
  public String updateDocid(String docid, String docText, boolean result) {
474
    String response = null;
475
    try {
476
      response = m.update(docid,
477
                          new StringReader(docText), null);
478
      if (result) {
479
        assertTrue( (response.indexOf("<success>") != -1));
480
        assertTrue(response.indexOf(docid) != -1);
481
      }
482
      else {
483
        assertTrue( (response.indexOf("<success>") == -1));
484
      }
485
      System.err.println(response);
486
    }
487
    catch (MetacatInaccessibleException mie) {
488
      fail("Metacat Inaccessible:\n" + mie.getMessage());
489
    }
490
    catch (InsufficientKarmaException ike) {
491
      fail("Insufficient karma:\n" + ike.getMessage());
492
    }
493
    catch (MetacatException me) {
494
      fail("Metacat Error:\n" + me.getMessage());
495
    }
496
    catch (Exception e) {
497
      fail("General exception:\n" + e.getMessage());
498
    }
499

    
500
    return response;
501
  }
502

    
503
  public void deleteDocid(String docid, boolean result) {
504
    try {
505
      String response = m.delete(docid);
506
      if (result) {
507
        assertTrue(response.indexOf("<success>") != -1);
508
      }
509
      else {
510
        assertTrue(response.indexOf("<success>") == -1);
511
      }
512
      System.err.println(response);
513
    }
514
    catch (MetacatInaccessibleException mie) {
515
      fail("Metacat Inaccessible:\n" + mie.getMessage());
516
    }
517
    catch (InsufficientKarmaException ike) {
518
      fail("Insufficient karma:\n" + ike.getMessage());
519
    }
520
    catch (MetacatException me) {
521
      fail("Metacat Error:\n" + me.getMessage());
522
    }
523
    catch (Exception e) {
524
      fail("General exception:\n" + e.getMessage());
525
    }
526
  }
527

    
528
  public void readDocidWhichEqualsDoc(String docid, String testDoc, boolean result) {
529
    try {
530
      Reader r = m.read(docid);
531
      String doc = IOUtil.getAsString(r, true);
532
      doc = doc + "\n";
533
      if (result) {
534
        assertTrue(testDoc.equals(doc));
535
      } else {
536
        assertTrue(doc.indexOf("<error>") != -1);
537
      }
538
    }
539
    catch (MetacatInaccessibleException mie) {
540
      fail("Metacat Inaccessible:\n" + mie.getMessage());
541
    }
542
    catch (InsufficientKarmaException ike) {
543
      fail("Insufficient karma:\n" + ike.getMessage());
544
    }
545
    catch (MetacatException me) {
546
      fail("Metacat Error:\n" + me.getMessage());
547
    }
548
    catch (Exception e) {
549
      fail("General exception:\n" + e.getMessage());
550
    }
551

    
552
  }
553

    
554
  /**
555
   * Create a hopefully unique docid for testing insert and update. Does
556
   * not include the 'revision' part of the id.
557
   *
558
   * @return a String docid based on the current date and time
559
   */
560
  private String generateDocid() {
561
    StringBuffer docid = new StringBuffer(prefix);
562
    docid.append(".");
563

    
564
    // Create a calendar to get the date formatted properly
565
    String[] ids = TimeZone.getAvailableIDs( -8 * 60 * 60 * 1000);
566
    SimpleTimeZone pdt = new SimpleTimeZone( -8 * 60 * 60 * 1000, ids[0]);
567
    pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
568
    pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
569
    Calendar calendar = new GregorianCalendar(pdt);
570
    Date trialTime = new Date();
571
    calendar.setTime(trialTime);
572
    docid.append(calendar.get(Calendar.YEAR));
573
    docid.append(calendar.get(Calendar.DAY_OF_YEAR));
574
    docid.append(calendar.get(Calendar.HOUR_OF_DAY));
575
    docid.append(calendar.get(Calendar.MINUTE));
576
    docid.append(calendar.get(Calendar.SECOND));
577

    
578
    return docid.toString();
579
  }
580
}
(1-1/8)