make JettyWebServer accept WebContext path without slash (and test it)
[aaa.git] / aaa-cert / src / test / java / org / opendaylight / aaa / cert / test / AaaCertProviderTest.java
1 /*
2  * Copyright (c) 2016, 2017 Inocybe Technologies. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.cert.test;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.security.Security;
15 import java.util.concurrent.ExecutionException;
16 import org.bouncycastle.jce.provider.BouncyCastleProvider;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.opendaylight.aaa.cert.impl.AaaCertProvider;
20 import org.opendaylight.aaa.cert.impl.KeyStoreConstant;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.aaa.cert.rev151126.aaa.cert.service.config.CtlKeystore;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.aaa.cert.rev151126.aaa.cert.service.config.CtlKeystoreBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.aaa.cert.rev151126.aaa.cert.service.config.TrustKeystore;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.yang.aaa.cert.rev151126.aaa.cert.service.config.TrustKeystoreBuilder;
25 import org.powermock.core.classloader.annotations.PowerMockIgnore;
26 import org.powermock.modules.junit4.PowerMockRunner;
27
28 @RunWith(PowerMockRunner.class)
29 @PowerMockIgnore("javax.xml.*")
30 public class AaaCertProviderTest {
31
32     private static AaaCertProvider aaaCertProv;
33     private static CtlKeystore ctlKeyStore;
34     private static TrustKeystore trustKeyStore;
35
36     private final String dummyAlias = TestUtils.dummyAlias;
37     private final String dummyCert = TestUtils.dummyCert;
38
39     @Test
40     public void testCertificate() throws InterruptedException, ExecutionException {
41         // Set up Tests
42         String name = "CN=ODL, OU=Dev, O=LinuxFoundation, L=QC Montreal, C=CA";
43         Security.addProvider(new BouncyCastleProvider());
44         ctlKeyStore = new CtlKeystoreBuilder()
45                 .setAlias("fooTest")
46                 .setDname(name)
47                 .setName("ctlTest.jks")
48                 .setStorePassword("passWord")
49                 .setValidity(KeyStoreConstant.DEFAULT_VALIDITY)
50                 .setKeyAlg(KeyStoreConstant.DEFAULT_KEY_ALG)
51                 .setKeysize(KeyStoreConstant.DEFAULT_KEY_SIZE)
52                 .setSignAlg(KeyStoreConstant.DEFAULT_SIGN_ALG)
53                 .build();
54         trustKeyStore = new TrustKeystoreBuilder()
55                 .setName("trustTest.jks")
56                 .setStorePassword("passWord")
57                 .build();
58         aaaCertProv = new AaaCertProvider(ctlKeyStore, trustKeyStore);
59
60         // getOldKeyStoreInfo
61         final CtlKeystore ctl = aaaCertProv.getOdlKeyStoreInfo();
62         assertNotNull(ctl);
63         assertTrue(ctl.equals(ctlKeyStore));
64
65         // getTrustKeyStoreInfo
66         final TrustKeystore trust = aaaCertProv.getTrustKeyStoreInfo();
67         assertNotNull(trust);
68         assertTrue(trust.equals(trustKeyStore));
69
70         // createKeyStores
71         boolean result = aaaCertProv.createKeyStores();
72         assertTrue(result);
73         assertNotNull(aaaCertProv.getODLKeyStore());
74         assertNotNull(aaaCertProv.getTrustKeyStore());
75
76         // genODLCertificateReq
77         String cert = aaaCertProv.genODLKeyStoreCertificateReq(true);
78         assertTrue(cert != null && !cert.isEmpty());
79         assertTrue(cert.contains(KeyStoreConstant.BEGIN_CERTIFICATE_REQUEST));
80         cert = aaaCertProv.genODLKeyStoreCertificateReq(false);
81         assertTrue(!cert.contains(KeyStoreConstant.BEGIN_CERTIFICATE_REQUEST));
82
83         // genODLCertificateReqWithPassword
84         cert = aaaCertProv.genODLKeyStoreCertificateReq(ctlKeyStore.getStorePassword(), true);
85         assertTrue(cert != null && !cert.isEmpty());
86         assertTrue(cert.contains(KeyStoreConstant.BEGIN_CERTIFICATE_REQUEST));
87
88         // getODLCertificate
89         cert = aaaCertProv.getODLKeyStoreCertificate(true);
90         assertTrue(cert != null && !cert.isEmpty());
91         assertTrue(cert.contains(KeyStoreConstant.END_CERTIFICATE));
92         cert = aaaCertProv.getODLKeyStoreCertificate(false);
93         assertTrue(!cert.contains(KeyStoreConstant.END_CERTIFICATE));
94
95         // getODLCerticateWithPassword
96         cert = aaaCertProv.getODLKeyStoreCertificate(ctlKeyStore.getStorePassword(), true);
97         assertTrue(cert != null && !cert.isEmpty());
98         assertTrue(cert.contains(KeyStoreConstant.END_CERTIFICATE));
99
100         // addCertificateTrustStore
101         result = aaaCertProv.addCertificateTrustStore(dummyAlias, dummyCert);
102         assertTrue(result);
103         cert = aaaCertProv.getCertificateTrustStore(dummyAlias, true);
104         assertTrue(cert != null && !cert.isEmpty());
105
106         // addCertificateTrustStoreWithPassword
107         result = aaaCertProv.addCertificateTrustStore(trustKeyStore.getStorePassword(), dummyAlias, dummyCert);
108         assertTrue(result);
109         cert = aaaCertProv.getCertificateTrustStore(dummyAlias, true);
110         assertTrue(cert != null && !cert.isEmpty());
111
112         // getCertificateTrustStore
113         cert = aaaCertProv.getCertificateTrustStore(dummyAlias, true);
114         assertTrue(cert != null && !cert.isEmpty());
115         cert = aaaCertProv.getCertificateTrustStore(dummyAlias, false);
116         assertTrue(!cert.contains(KeyStoreConstant.END_CERTIFICATE));
117
118         // getCertificateWithPasswordTrusStore
119         cert = aaaCertProv.getCertificateTrustStore(trustKeyStore.getStorePassword(), dummyAlias, true);
120         assertTrue(cert != null && !cert.isEmpty());
121     }
122 }