From 31da354c1be5e83689b302cf51cd86afb7ff2628 Mon Sep 17 00:00:00 2001 From: Chris Murphy Date: Sun, 18 Jun 2017 12:09:46 +1000 Subject: [PATCH] FAB-4454 Increase test coverage for HFCAClient . Added some more unit test cases to increase coverage Change-Id: I7158183cb1773c2d673d1db072016f4c84504104 Signed-off-by: Chris Murphy --- .../fabric_ca/sdk/HFCAClientTest.java | 568 ++++++++++-------- 1 file changed, 302 insertions(+), 266 deletions(-) diff --git a/src/test/java/org/hyperledger/fabric_ca/sdk/HFCAClientTest.java b/src/test/java/org/hyperledger/fabric_ca/sdk/HFCAClientTest.java index 3347e714..d4b4098b 100644 --- a/src/test/java/org/hyperledger/fabric_ca/sdk/HFCAClientTest.java +++ b/src/test/java/org/hyperledger/fabric_ca/sdk/HFCAClientTest.java @@ -16,382 +16,418 @@ import java.io.File; import java.net.MalformedURLException; +import java.security.KeyPair; import java.util.Properties; +import org.hyperledger.fabric.sdk.Enrollment; import org.hyperledger.fabric.sdk.exception.CryptoException; import org.hyperledger.fabric.sdk.security.CryptoPrimitives; import org.hyperledger.fabric.sdk.security.CryptoSuite; +import org.hyperledger.fabric.sdk.testutils.TestUtils; import org.hyperledger.fabric.sdkintegration.SampleStore; import org.hyperledger.fabric.sdkintegration.SampleUser; import org.hyperledger.fabric_ca.sdk.exception.EnrollmentException; import org.hyperledger.fabric_ca.sdk.exception.InvalidArgumentException; import org.hyperledger.fabric_ca.sdk.exception.RegistrationException; +import org.hyperledger.fabric_ca.sdk.exception.RevocationException; +import org.hyperledger.fabric_ca.sdk.helper.Config; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class HFCAClientTest { - public static class MemberServicesFabricCAImplTest { - private static final String TEST_ADMIN_NAME = "admin"; - private static final String TEST_ADMIN_PW = "adminpw"; - private static final String TEST_ADMIN_ORG = "org1"; - private SampleStore sampleStore; - SampleUser admin; + @Rule + public ExpectedException thrown = ExpectedException.none(); - @Before - public void setup() throws CryptoException, InvalidArgumentException, org.hyperledger.fabric.sdk.exception.InvalidArgumentException, MalformedURLException, EnrollmentException { + private static final String TEST_ADMIN_NAME = "admin"; + private static final String TEST_ADMIN_PW = "adminpw"; + private static final String TEST_ADMIN_ORG = "org1"; - File sampleStoreFile = new File(System.getProperty("java.io.tmpdir") + "/HFCSampletest.properties"); - if (sampleStoreFile.exists()) { //For testing start fresh - sampleStoreFile.delete(); - } - sampleStore = new SampleStore(sampleStoreFile); - sampleStoreFile.deleteOnExit(); + private SampleStore sampleStore; + SampleUser admin; - //SampleUser can be any implementation that implements org.hyperledger.fabric.sdk.User Interface - admin = sampleStore.getMember(TEST_ADMIN_NAME, TEST_ADMIN_ORG); + private static CryptoPrimitives crypto; + @BeforeClass + public static void setupBeforeClass() { + try { + crypto = new CryptoPrimitives(); + crypto.init(); + } catch (Exception e) { + throw new RuntimeException("HFCAClientTest.setupBeforeClass failed!", e); } + } - @Test - public void testCOPCreation() { - - try { - HFCAClient memberServices = HFCAClient.createNewInstance("http://localhost:99", null); - Assert.assertNotNull(memberServices); - Assert.assertSame(HFCAClient.class, memberServices.getClass()); + @Before + public void setup() throws CryptoException, InvalidArgumentException, + org.hyperledger.fabric.sdk.exception.InvalidArgumentException, MalformedURLException, EnrollmentException { - } catch (Exception e) { - Assert.fail("Unexpected Exception " + e.getMessage()); - } + File sampleStoreFile = new File(System.getProperty("java.io.tmpdir") + "/HFCSampletest.properties"); + if (sampleStoreFile.exists()) { // For testing start fresh + sampleStoreFile.delete(); } + sampleStore = new SampleStore(sampleStoreFile); + sampleStoreFile.deleteOnExit(); - @Test - public void testNullURL() { + // SampleUser can be any implementation that implements org.hyperledger.fabric.sdk.User Interface + admin = sampleStore.getMember(TEST_ADMIN_NAME, TEST_ADMIN_ORG); - try { - HFCAClient.createNewInstance(null, null); - Assert.fail("Expected exception"); + } - } catch (Exception e) { - Assert.assertSame(MalformedURLException.class, e.getClass()); + @Test + public void testNewInstance() throws Exception { - } - } + HFCAClient memberServices = HFCAClient.createNewInstance("http://localhost:99", null); - @Test - public void emptyURL() { + Assert.assertNotNull(memberServices); + Assert.assertSame(HFCAClient.class, memberServices.getClass()); + } - try { - HFCAClient.createNewInstance("", null); - Assert.fail("Expected exception"); + @Test + public void testNewInstanceWithName() throws Exception { - } catch (Exception e) { - Assert.assertSame(MalformedURLException.class, e.getClass()); + HFCAClient memberServices = HFCAClient.createNewInstance("name", "http://localhost:99", null); - } - } + Assert.assertNotNull(memberServices); + Assert.assertSame(HFCAClient.class, memberServices.getClass()); - @Test - public void testBadProto() { + } - try { - HFCAClient.createNewInstance("file://localhost", null); - Assert.fail("Expected exception"); + @Test + public void testNewInstanceWithNameAndProperties() throws Exception { - } catch (Exception e) { - Assert.assertSame(IllegalArgumentException.class, e.getClass()); + Properties testProps = new Properties(); + HFCAClient memberServices = HFCAClient.createNewInstance("name", "http://localhost:99", testProps); - } - } + Assert.assertNotNull(memberServices); + Assert.assertSame(HFCAClient.class, memberServices.getClass()); - @Test - public void testBadURLPath() { + } - try { - HFCAClient.createNewInstance("http://localhost/bad", null); - Assert.fail("Expected exception"); + @Test + public void testNewInstanceNullUrl() throws Exception { + thrown.expect(MalformedURLException.class); + HFCAClient.createNewInstance(null, null); + } - } catch (Exception e) { - Assert.assertSame(IllegalArgumentException.class, e.getClass()); + @Test + public void testNewInstanceEmptyUrl() throws Exception { + thrown.expect(MalformedURLException.class); + HFCAClient.createNewInstance("", null); + } - } - } + @Test + public void testNewInstanceBadUrlProto() throws Exception { - @Test - public void testBadURLQuery() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("HFCAClient only supports"); - try { - HFCAClient.createNewInstance("http://localhost?bad", null); - Assert.fail("Expected exception"); + HFCAClient.createNewInstance("file://localhost", null); + } - } catch (Exception e) { - Assert.assertSame(IllegalArgumentException.class, e.getClass()); + @Test + public void testNewInstanceBadUrlPath() throws Exception { - } - } + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("HFCAClient url does not support path"); - @Test - public void testNewInstanceNameUrlProperties() { + HFCAClient.createNewInstance("http://localhost/bad", null); + } - try { - HFCAClient memberServices = HFCAClient.createNewInstance("name", "http://localhost:99", null); - Assert.assertNotNull(memberServices); - Assert.assertSame(HFCAClient.class, memberServices.getClass()); + @Test + public void testNewInstanceNoUrlHost() throws Exception { - } catch (Exception e) { - Assert.assertSame(IllegalArgumentException.class, e.getClass()); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("HFCAClient url needs host"); - } - } + HFCAClient.createNewInstance("http://:99", null); + } - @Test - public void testNewInstanceNameUrlPropertiesSetNullName() { + @Test + public void testNewInstanceBadUrlQuery() throws Exception { - try { - HFCAClient.createNewInstance(null, "http://localhost:99", null); - Assert.fail("Expected exception when name is set to null"); + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("HFCAClient url does not support query"); - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertEquals("name must not be null or an empty string.", e.getMessage()); + HFCAClient.createNewInstance("http://localhost?bad", null); + } - } - } + @Test + public void testNewInstanceNullName() throws Exception { - @Test - public void testNewInstanceNameUrlPropertiesSetEmptyName() { + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("name must not be"); - try { - HFCAClient.createNewInstance("", "http://localhost:99", null); - Assert.fail("Expected exception when name is set to null"); + HFCAClient.createNewInstance(null, "http://localhost:99", null); + } - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertEquals("name must not be null or an empty string.", e.getMessage()); + @Test + public void testNewInstanceEmptyName() throws Exception { - } - } + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("name must not be"); - @Test - public void testNewInstanceNoHost() { - Properties testprops = new Properties(); + HFCAClient.createNewInstance("", "http://localhost:99", null); - try { - HFCAClient.createNewInstance("client", "http://:99", testprops); - Assert.fail("Expected exception when hostname is not specified in the URL"); + } - } catch (Exception e) { - Assert.assertSame(IllegalArgumentException.class, e.getClass()); - Assert.assertEquals("HFCAClient url needs host", e.getMessage()); + @Test + public void testSetCryptoSuite() throws Exception { - } - } + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - @Test - public void testGetCryptoSuite() { - CryptoPrimitives testcrypt = new CryptoPrimitives(); + CryptoPrimitives testcrypt = new CryptoPrimitives(); + client.setCryptoSuite(testcrypt); + Assert.assertEquals(testcrypt, client.getCryptoSuite()); - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.setCryptoSuite(testcrypt); - Assert.assertEquals(testcrypt, client.getCryptoSuite()); + } - } catch (Exception e) { - Assert.fail("Unexpected Exception " + e.getMessage()); - } - } + @Test + public void testRegisterEnrollmentIdNull() throws Exception { - @Test - public void testRegisterNullEnrollId() { + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("EntrollmentID cannot be null or empty"); - try { - RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); - regreq.setEnrollmentID(null); - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.register(regreq, null); - Assert.fail("Expected exception when enrollment ID is null"); + RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); + regreq.setEnrollmentID(null); - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertEquals("EntrollmentID cannot be null or empty", e.getMessage()); + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.register(regreq, null); + } - } - } + @Test + public void testRegisterEnrollmentIdEmpty() throws Exception { - @Test - public void testRegisterEmptyEnrollId() { + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("EntrollmentID cannot be null or empty"); - try { - RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); - regreq.setEnrollmentID(""); - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.register(regreq, null); - Assert.fail("Expected exception when enrollment ID is empty"); + RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); + regreq.setEnrollmentID(""); - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertEquals("EntrollmentID cannot be null or empty", e.getMessage()); + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.register(regreq, null); + } - } - } + @Test + public void testRegisterNullRegistrar() throws Exception { - @Test - public void testRegisterNoServerResponse() { + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("Registrar should be a valid member"); - try { - RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.register(regreq, admin); - Assert.fail("Expected exception when server is not available during registration"); + RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); + regreq.setEnrollmentID("abc"); - } catch (Exception e) { - Assert.assertSame(RegistrationException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("Error while registering the user")); + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.register(regreq, null); + } - } - } + @Test + public void testRegisterNoServerResponse() throws Exception { - @Test - public void testEnrollmentEmptyUser() { + thrown.expect(RegistrationException.class); + thrown.expectMessage("Error while registering the user"); - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.enroll("", TEST_ADMIN_PW); - Assert.fail("Expected exception when user parameter is empty"); + Properties testProps = new Properties(); + HFCAClient client = HFCAClient.createNewInstance("client", "https://localhost:99", testProps); - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("enrollment user is not set")); + CryptoPrimitives testcrypt = new CryptoPrimitives(); + client.setCryptoSuite(testcrypt); - } - } + RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); + client.register(regreq, admin); - @Test - public void testEnrollmentNullUser() { + } - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.enroll(null, TEST_ADMIN_PW); - Assert.fail("Expected exception when user parameter is null"); + @Test + public void testRegisterNoServerResponseAllHostNames() throws Exception { - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("enrollment user is not set")); + thrown.expect(RegistrationException.class); + thrown.expectMessage("Error while registering the user"); - } - } + Properties testProps = new Properties(); + testProps.setProperty("allowAllHostNames", "true"); + HFCAClient client = HFCAClient.createNewInstance("client", "https://localhost:99", testProps); - @Test - public void testEnrollmentEmptySecret() { + CryptoPrimitives testcrypt = new CryptoPrimitives(); + client.setCryptoSuite(testcrypt); - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.enroll(TEST_ADMIN_NAME, ""); - Assert.fail("Expected exception when secret parameter is empty"); + RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); + client.register(regreq, admin); + } - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("enrollment secret is not set")); + @Test + public void testRegisterNoServerResponseNoPemFile() throws Exception { - } - } + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("Unable to add CA certificate"); - @Test - public void testEnrollmentNullSecret() { + Properties testProps = new Properties(); + testProps.setProperty("pemFile", "nofile.pem"); + HFCAClient client = HFCAClient.createNewInstance("client", "https://localhost:99", testProps); - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.enroll(TEST_ADMIN_NAME, null); - Assert.fail("Expected exception when secret parameter is null"); + CryptoPrimitives testcrypt = new CryptoPrimitives(); + client.setCryptoSuite(testcrypt); - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("enrollment secret is not set")); + RegistrationRequest regreq = new RegistrationRequest("name", "affiliation"); + client.register(regreq, admin); + } - } - } + @Test + public void testEnrollmentEmptyUser() throws Exception { - @Test - public void testEnrollmentNoServerResponse() { + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("enrollment user is not set"); - try { - CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite(); - cryptoSuite.init(); - EnrollmentRequest req = new EnrollmentRequest("profile 1", "label 1", null); - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.setCryptoSuite(cryptoSuite); - client.enroll(TEST_ADMIN_NAME, TEST_ADMIN_NAME, req); - Assert.fail("Expected exception when server is not available during enrollment"); + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.enroll("", TEST_ADMIN_PW); + } - } catch (Exception e) { - Assert.assertSame(EnrollmentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("Url:http://localhost:99, Failed to enroll user admin ")); + @Test + public void testEnrollmentNullUser() throws Exception { - } - } + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("enrollment user is not set"); - @Test - public void testReenrollmentNullUser() { + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.enroll(null, TEST_ADMIN_PW); + } - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.reenroll(null); - Assert.fail("Expected exception when user parameter is null"); + @Test + public void testEnrollmentEmptySecret() throws Exception { - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("reenrollment user is missing")); + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("enrollment secret is not set"); - } - } + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.enroll(TEST_ADMIN_NAME, ""); + } - @Test - public void testReenrollmentNullEnrollmentObject() { + @Test + public void testEnrollmentNullSecret() throws Exception { - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - admin.setEnrollment(null); - client.reenroll(admin); - Assert.fail("Expected exception when user enrollment object is null"); + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("enrollment secret is not set"); - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("reenrollment user is not a valid user object")); + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.enroll(TEST_ADMIN_NAME, null); + } - } - } + // Tests enrollment when no server is available + @Test + public void testEnrollmentNoServerResponse() throws Exception { - @Test - public void testRevokeNullUserObject() { + thrown.expect(EnrollmentException.class); + thrown.expectMessage("Failed to enroll user admin"); - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - client.revoke(null, admin.getName(), "keyCompromise"); - Assert.fail("Expected exception when revoker object is null"); + CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite(); + cryptoSuite.init(); - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("revoker is not set")); + EnrollmentRequest req = new EnrollmentRequest("profile 1", "label 1", null); + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.setCryptoSuite(cryptoSuite); - } - } + client.enroll(TEST_ADMIN_NAME, TEST_ADMIN_NAME, req); + } - @Test - public void testRevokeNullEnrollmentObject() { + @Test + public void testEnrollmentNoKeyPair() throws Exception { - try { - HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); - admin.setEnrollment(null); - client.revoke(admin, admin.getEnrollment(), "keyCompromise"); - Assert.fail("Expected exception when enrollment object is null"); + thrown.expect(EnrollmentException.class); + thrown.expectMessage("Failed to enroll user admin"); - } catch (Exception e) { - Assert.assertSame(InvalidArgumentException.class, e.getClass()); - Assert.assertTrue(e.getMessage().contains("revokee enrollment is not set")); + CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite(); + cryptoSuite.init(); - } - } + EnrollmentRequest req = new EnrollmentRequest("profile 1", "label 1", null); + req.setCSR("abc"); + + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.setCryptoSuite(cryptoSuite); + + client.enroll(TEST_ADMIN_NAME, TEST_ADMIN_NAME, req); + } + + @Test + public void testReenrollNullUser() throws Exception { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("reenrollment user is missing"); + + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.reenroll(null); + } + + @Test + public void testReenrollNullEnrollment() throws Exception { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("reenrollment user is not a valid user object"); + + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + admin.setEnrollment(null); + client.reenroll(admin); + } + + @Test + public void testRevoke1Exception() throws Exception { + + thrown.expect(RevocationException.class); + thrown.expectMessage("Error while revoking cert"); + + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + KeyPair keypair = crypto.keyGen(); + Enrollment enrollment = new HFCAEnrollment(keypair, "abc"); + + client.revoke(admin, enrollment, "keyCompromise"); + } + + // revoke1: revoke(User revoker, Enrollment enrollment, String reason) + @Test + public void testRevoke1NullUser() throws Exception { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("revoker is not set"); + + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + KeyPair keypair = crypto.keyGen(); + Enrollment enrollment = new HFCAEnrollment(keypair, "abc"); + + client.revoke(null, enrollment, "keyCompromise"); + } + + @Test + public void testRevoke1NullEnrollment() throws Exception { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("revokee enrollment is not set"); + + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.revoke(admin, (Enrollment) null, "keyCompromise"); + } + + // revoke2: revoke(User revoker, String revokee, String reason) + @Test + public void testRevoke2NullUser() throws Exception { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("revoker is not set"); + + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.revoke(null, admin.getName(), "keyCompromise"); } + + @Test + public void testRevoke2NullEnrollment() throws Exception { + + thrown.expect(InvalidArgumentException.class); + thrown.expectMessage("revokee user is not set"); + + HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null); + client.revoke(admin, (String) null, "keyCompromise"); + } + }