55
55
import static java .nio .charset .StandardCharsets .UTF_8 ;
56
56
import static org .junit .Assert .assertEquals ;
57
57
import static org .junit .Assert .assertFalse ;
58
+ import static org .junit .Assert .assertNotNull ;
58
59
import static org .junit .Assert .assertTrue ;
59
60
import static org .junit .Assert .fail ;
60
61
@@ -354,10 +355,146 @@ public void testUserRevoke() throws Exception {
354
355
client .reenroll (user );
355
356
}
356
357
358
+ // Tests attempting to revoke a user with Null reason
359
+ @ Test
360
+ public void testUserRevokeNullReason () throws Exception {
361
+
362
+ thrown .expect (EnrollmentException .class );
363
+ thrown .expectMessage ("Failed to re-enroll user" );
364
+
365
+ Calendar calendar = Calendar .getInstance (); // gets a calendar using the default time zone and locale.
366
+ calendar .add (Calendar .SECOND , -1 );
367
+ Date revokedTinyBitAgoTime = calendar .getTime (); //avoid any clock skewing.
368
+
369
+ SampleUser user = getTestUser (TEST_USER1_ORG );
370
+
371
+ if (!user .isRegistered ()) {
372
+ RegistrationRequest rr = new RegistrationRequest (user .getName (), TEST_USER1_AFFILIATION );
373
+ String password = "testUserRevoke" ;
374
+ rr .setSecret (password );
375
+ rr .addAttribute (new Attribute ("user.role" , "department lead" ));
376
+ rr .addAttribute (new Attribute ("hf.revoker" , "true" ));
377
+ user .setEnrollmentSecret (client .register (rr , admin )); // Admin can register other users.
378
+ if (!user .getEnrollmentSecret ().equals (password )) {
379
+ fail ("Secret returned from RegistrationRequest not match : " + user .getEnrollmentSecret ());
380
+ }
381
+ }
382
+
383
+ sleepALittle ();
384
+
385
+ if (!user .isEnrolled ()) {
386
+ EnrollmentRequest req = new EnrollmentRequest ("profile 2" , "label 2" , null );
387
+ req .addHost ("example3.ibm.com" );
388
+ user .setEnrollment (client .enroll (user .getName (), user .getEnrollmentSecret (), req ));
389
+
390
+ // verify
391
+ String cert = user .getEnrollment ().getCert ();
392
+ verifyOptions (cert , req );
393
+ }
394
+
395
+ sleepALittle ();
396
+
397
+ int startedWithRevokes = -1 ;
398
+
399
+ if (!testConfig .isRunningAgainstFabric10 ()) {
400
+
401
+ startedWithRevokes = getRevokes (null ).length ; //one more after we do this revoke.
402
+ }
403
+
404
+ // revoke all enrollment of this user
405
+ client .revoke (admin , user .getName (), null );
406
+ if (!testConfig .isRunningAgainstFabric10 ()) {
407
+ final int newRevokes = getRevokes (null ).length ;
408
+
409
+ assertEquals (format ("Expected one more revocation %d, but got %d" , startedWithRevokes + 1 , newRevokes ), startedWithRevokes + 1 , newRevokes );
410
+ }
411
+
412
+ // trying to reenroll the revoked user should fail with an EnrollmentException
413
+ client .reenroll (user );
414
+ }
415
+
416
+ // Tests revoking a user with genCRL using the revoke API
417
+ @ Test
418
+ public void testUserRevokeGenCRL () throws Exception {
419
+
420
+ if (testConfig .isRunningAgainstFabric10 ()) {
421
+ return ; // needs v1.1
422
+ }
423
+
424
+ thrown .expect (EnrollmentException .class );
425
+ thrown .expectMessage ("Failed to re-enroll user" );
426
+
427
+ Calendar calendar = Calendar .getInstance (); // gets a calendar using the default time zone and locale.
428
+ calendar .add (Calendar .SECOND , -1 );
429
+ Date revokedTinyBitAgoTime = calendar .getTime (); //avoid any clock skewing.
430
+
431
+ SampleUser user1 = getTestUser (TEST_USER1_ORG );
432
+ SampleUser user2 = getTestUser (TEST_USER1_ORG );
433
+
434
+ SampleUser [] users = new SampleUser []{user1 , user2 };
435
+
436
+ for (SampleUser user : users ) {
437
+ if (!user .isRegistered ()) {
438
+ RegistrationRequest rr = new RegistrationRequest (user .getName (), TEST_USER1_AFFILIATION );
439
+ String password = "testUserRevoke" ;
440
+ rr .setSecret (password );
441
+ rr .addAttribute (new Attribute ("user.role" , "department lead" ));
442
+ rr .addAttribute (new Attribute ("hf.revoker" , "true" ));
443
+ user .setEnrollmentSecret (client .register (rr , admin )); // Admin can register other users.
444
+ if (!user .getEnrollmentSecret ().equals (password )) {
445
+ fail ("Secret returned from RegistrationRequest not match : " + user .getEnrollmentSecret ());
446
+ }
447
+ }
448
+
449
+ sleepALittle ();
450
+
451
+ if (!user .isEnrolled ()) {
452
+ EnrollmentRequest req = new EnrollmentRequest ("profile 2" , "label 2" , null );
453
+ req .addHost ("example3.ibm.com" );
454
+ user .setEnrollment (client .enroll (user .getName (), user .getEnrollmentSecret (), req ));
455
+
456
+ // verify
457
+ String cert = user .getEnrollment ().getCert ();
458
+ verifyOptions (cert , req );
459
+ }
460
+ }
461
+
462
+ sleepALittle ();
463
+
464
+ int startedWithRevokes = -1 ;
465
+
466
+ startedWithRevokes = getRevokes (null ).length ; //one more after we do this revoke.
467
+
468
+ // revoke all enrollment of this user and request back a CRL
469
+ String crl = client .revoke (admin , user1 .getName (), null , true );
470
+ assertNotNull ("Failed to get CRL using the Revoke API" , crl );
471
+
472
+ final int newRevokes = getRevokes (null ).length ;
473
+
474
+ assertEquals (format ("Expected one more revocation %d, but got %d" , startedWithRevokes + 1 , newRevokes ), startedWithRevokes + 1 , newRevokes );
475
+
476
+ final int crlLength = parseCRL (crl ).length ;
477
+
478
+ assertEquals (format ("The number of revokes %d does not equal the number of revoked certificates (%d) in crl" , newRevokes , crlLength ), newRevokes , crlLength );
479
+
480
+ // trying to reenroll the revoked user should fail with an EnrollmentException
481
+ client .reenroll (user1 );
482
+
483
+ String crl2 = client .revoke (admin , user2 .getName (), null , false );
484
+ assertEquals ("CRL not requested, CRL should be empty" , "" , crl2 );
485
+
486
+ }
487
+
488
+
357
489
TBSCertList .CRLEntry [] getRevokes (Date r ) throws Exception {
358
490
359
491
String crl = client .generateCRL (admin , r , null , null , null );
360
492
493
+ return parseCRL (crl );
494
+ }
495
+
496
+ TBSCertList .CRLEntry [] parseCRL (String crl ) throws Exception {
497
+
361
498
Base64 .Decoder b64dec = Base64 .getDecoder ();
362
499
final byte [] decode = b64dec .decode (crl .getBytes (UTF_8 ));
363
500
@@ -433,17 +570,6 @@ public void testEnrollUnknownClient() throws Exception {
433
570
clientWithName .enroll (admin .getName (), TEST_ADMIN_PW );
434
571
}
435
572
436
- // revoke1: revoke(User revoker, Enrollment enrollment, String reason)
437
- @ Test
438
- public void testRevoke1NullReason () throws Exception {
439
-
440
- thrown .expect (RevocationException .class );
441
- thrown .expectMessage ("cannot be null" );
442
-
443
- SampleUser user = getEnrolledUser (TEST_ADMIN_ORG );
444
- client .revoke (admin , user .getEnrollment (), null );
445
- }
446
-
447
573
// revoke2: revoke(User revoker, String revokee, String reason)
448
574
@ Test
449
575
public void testRevoke2UnknownUser () throws Exception {
@@ -454,16 +580,6 @@ public void testRevoke2UnknownUser() throws Exception {
454
580
client .revoke (admin , "unknownUser" , "remove user2" );
455
581
}
456
582
457
- @ Test
458
- public void testRevoke2NullReason () throws Exception {
459
-
460
- thrown .expect (RevocationException .class );
461
- thrown .expectMessage ("cannot be null" );
462
-
463
- SampleUser user = getEnrolledUser (TEST_ADMIN_ORG );
464
- client .revoke (admin , user .getName (), null );
465
- }
466
-
467
583
@ Test
468
584
public void testMockEnrollSuccessFalse () throws Exception {
469
585
0 commit comments