Bug 2231 - Secure transport for PCEP
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / controller / config / yang / pcep / impl / PCEPDispatcherImplModuleTest.java
index fcc86a8d87504e2be88b329eb9dececeb32abe1b..ffc4af21e8b6071a976bc46aac34d8f400c255b2 100644 (file)
@@ -23,6 +23,8 @@ import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgrou
 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleMXBean;
 import org.opendaylight.controller.config.yang.pcep.spi.SimplePCEPExtensionProviderContextModuleFactory;
 import org.opendaylight.controller.config.yang.pcep.spi.SimplePCEPExtensionProviderContextModuleMXBean;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.impl.rev130627.PathType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.impl.rev130627.StoreType;
 
 public class PCEPDispatcherImplModuleTest extends AbstractConfigTest {
 
@@ -36,6 +38,10 @@ public class PCEPDispatcherImplModuleTest extends AbstractConfigTest {
     private static final String EXTENSION_INSTANCE_NAME = "pcep-extension-privider";
     private static final String EXTENSION_FACTORYNAME = SimplePCEPExtensionProviderContextModuleFactory.NAME;
 
+    private static final String STORE_PASSWORD = "opendaylight";
+    private static final String KEYSTORE = "ctl.jks";
+    private static final String TRUSTSTORE = "truststore.jks";
+
     @Before
     public void setUp() throws Exception {
         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext, new PCEPDispatcherImplModuleFactory(), new PCEPSessionProposalFactoryImplModuleFactory(), new NettyThreadgroupModuleFactory(), new SimplePCEPExtensionProviderContextModuleFactory()));
@@ -61,6 +67,105 @@ public class PCEPDispatcherImplModuleTest extends AbstractConfigTest {
         }
     }
 
+    @Test
+    public void testValidationExceptionCertificatePassword() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(null, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
+                    PathType.PATH, StoreType.JKS));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("certificate password"));
+        }
+    }
+
+    @Test
+    public void testValidationExceptionKeystoreLocation() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(STORE_PASSWORD, null, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
+                    PathType.PATH, StoreType.JKS));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("keystore location"));
+        }
+    }
+
+    @Test
+    public void testValidationExceptionKeystorePassword() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, null, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
+                    PathType.PATH, StoreType.JKS));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("keystore password"));
+        }
+    }
+
+    @Test
+    public void testValidationExceptionKeystorePathType() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, null, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
+                    PathType.PATH, StoreType.JKS));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("keystore path type"));
+        }
+    }
+
+    @Test
+    public void testValidationExceptionKeystoreType() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, null, TRUSTSTORE, STORE_PASSWORD,
+                    PathType.PATH, StoreType.JKS));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("keystore type"));
+        }
+    }
+
+    @Test
+    public void testValidationExceptionTruststoreLocation() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, null, STORE_PASSWORD,
+                    PathType.PATH, StoreType.JKS));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("truststore location"));
+        }
+    }
+
+    @Test
+    public void testValidationExceptionTruststorePassword() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, null,
+                    PathType.PATH, StoreType.JKS));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("truststore password"));
+        }
+    }
+
+    @Test
+    public void testValidationExceptionTruststorePathType() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
+                    null, StoreType.JKS));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("truststore path type"));
+        }
+    }
+
+    @Test
+    public void testValidationExceptionTruststoreType() throws Exception {
+        try {
+            createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
+                    PathType.PATH, null));
+            fail();
+        } catch (final ValidationException e) {
+            assertTrue(e.getMessage().contains("truststore type"));
+        }
+    }
+
     @Test
     public void testCreateBean() throws Exception {
         final CommitStatus status = createDispatcherInstance(5);
@@ -91,12 +196,26 @@ public class PCEPDispatcherImplModuleTest extends AbstractConfigTest {
         assertStatus(status, 0, 1, 4);
     }
 
+    @Test
+    public void testCreateBeanWithTls() throws Exception {
+        final CommitStatus status = createDispatcherInstance(5, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS,
+                TRUSTSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS));
+        assertBeanCount(1, FACTORY_NAME);
+        assertStatus(status, 5, 0, 0);
+    }
+
     private CommitStatus createDispatcherInstance(final Integer maxUnknownMessages) throws Exception {
         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
         createDispatcherInstance(transaction, maxUnknownMessages);
         return transaction.commit();
     }
 
+    private CommitStatus createDispatcherInstance(final Integer maxUnknownMessages, final Tls tls) throws Exception {
+        final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
+        createDispatcherInstance(transaction, maxUnknownMessages, tls);
+        return transaction.commit();
+    }
+
     public static ObjectName createDispatcherInstance(final ConfigTransactionJMXClient transaction, final Integer maxUnknownMessages)
             throws Exception {
         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
@@ -109,6 +228,14 @@ public class PCEPDispatcherImplModuleTest extends AbstractConfigTest {
         return nameCreated;
     }
 
+    private static ObjectName createDispatcherInstance(final ConfigTransactionJMXClient transaction, final Integer maxUnknownMessages,
+            final Tls tls) throws Exception {
+        final ObjectName objName = createDispatcherInstance(transaction, maxUnknownMessages);
+        final PCEPDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(objName, PCEPDispatcherImplModuleMXBean.class);
+        mxBean.setTls(tls);
+        return objName;
+    }
+
     private static ObjectName createExtensionsInstance(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
         final ObjectName nameCreated = transaction.createModule(EXTENSION_FACTORYNAME, EXTENSION_INSTANCE_NAME);
         transaction.newMXBeanProxy(nameCreated, SimplePCEPExtensionProviderContextModuleMXBean.class);
@@ -124,4 +251,20 @@ public class PCEPDispatcherImplModuleTest extends AbstractConfigTest {
         return nameCreated;
     }
 
+    private static Tls createTls(final String certificatePassword, final String keystore, final String keystorePassword,
+            final PathType keystorePathType, final StoreType keystoreType, final String truststore,
+            final String truststorePassword, final PathType truststorePathType, final StoreType truststoreType) {
+        final Tls tls = new Tls();
+        tls.setCertificatePassword(certificatePassword);
+        tls.setKeystore(keystore);
+        tls.setKeystorePassword(keystorePassword);
+        tls.setKeystorePathType(keystorePathType);
+        tls.setKeystoreType(keystoreType);
+        tls.setTruststore(truststore);
+        tls.setTruststorePassword(truststorePassword);
+        tls.setTruststorePathType(truststorePathType);
+        tls.setTruststoreType(truststoreType);
+        return tls;
+    }
+
 }