Increase coverage of cadi-aaf 07/42807/1
authorIanHowell <ian.howell@att.com>
Fri, 13 Apr 2018 20:05:52 +0000 (15:05 -0500)
committerIanHowell <ian.howell@att.com>
Fri, 13 Apr 2018 20:05:56 +0000 (15:05 -0500)
Issue-ID: AAF-223
Change-Id: Ie58cec591d7107dc360088657dbcf279c84fe3f7
Signed-off-by: IanHowell <ian.howell@att.com>
cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_Factory.java
cadi/aaf/src/test/resources/exampleCertificate.cer [new file with mode: 0755]

index e4eaf7f..3c72d8f 100644 (file)
@@ -27,16 +27,35 @@ import static org.mockito.Mockito.*;
 import org.junit.*;
 import org.mockito.*;
 
+import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
 import java.security.KeyPair;
+import java.security.Principal;
+import java.security.PrivateKey;
 import java.security.PublicKey;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
 
 import javax.crypto.Cipher;
 
+import org.onap.aaf.cadi.cm.CertException;
 import org.onap.aaf.cadi.cm.Factory;
 import org.onap.aaf.cadi.cm.Factory.StripperInputStream;
+import org.onap.aaf.cadi.cm.Factory.Base64InputStream;
 import org.onap.aaf.misc.env.Env;
 import org.onap.aaf.misc.env.LogTarget;
 import org.onap.aaf.misc.env.TimeTaken;
@@ -53,24 +72,71 @@ public class JU_Factory {
        @Mock
        LogTarget logTargetMock;
 
+       @Mock
+       X509Certificate x509CertMock;
+
+       @Mock
+       Certificate certMock;
+
+       @Mock
+       Principal subjectDN;
+
+       private final String resourceDirName = "src/test/resources";
+       private File resourceDir;
+       private File publicKeyFile;
+       private File privateKeyFile;
+       private File certFile;
+       
+       private static final String message = "The quick brown fox jumps over the lazy dog.";
+
+       private static final String subjectDNText = "subjectDN";
+       private static final String certText = "Some text that might be included in a certificate";
+
        @Before
-       public void setup() {
+       public void setup() throws CertificateEncodingException {
                MockitoAnnotations.initMocks(this);
 
+               resourceDir = new File(resourceDirName);
+               resourceDir.mkdirs();
+               publicKeyFile = new File(resourceDirName, "/publicKey");
+               privateKeyFile = new File(resourceDirName, "/privateKey");
+               publicKeyFile.delete();
+               privateKeyFile.delete();
+
+               certFile = new File(resourceDirName + "/exampleCertificate.cer");
+
                when(transMock.start(anyString(), anyInt())).thenReturn(timeTakenMock);
                when(transMock.debug()).thenReturn(logTargetMock);
+
+               when(subjectDN.toString()).thenReturn(subjectDNText);
+
+               when(x509CertMock.getSubjectDN()).thenReturn(subjectDN);
+               when(x509CertMock.getEncoded()).thenReturn(certText.getBytes());
+
+               when(certMock.getEncoded()).thenReturn(certText.getBytes());
+       }
+
+       @After
+       public void tearDown() {
+               publicKeyFile = new File(resourceDirName, "/publicKey");
+               privateKeyFile = new File(resourceDirName, "/privateKey");
+               publicKeyFile.delete();
+               privateKeyFile.delete();
+
+               if (resourceDir.list().length == 0) {
+                       resourceDir.delete();
+               }
        }
 
        @Test
        public void generateKeyPairTest() throws Exception {
-               String message = "The quick brown fox jumps over the lazy dog.";
-
-               Cipher encryptor = Cipher.getInstance(Factory.KEY_ALGO);
-               Cipher decryptor = Cipher.getInstance(Factory.KEY_ALGO);
+               // This instatiation isn't actually necessary, but it gets coverage
+               Cipher encryptor = Factory.pkCipher();
+               Cipher decryptor = Factory.pkCipher();
 
                KeyPair kp1 = Factory.generateKeyPair(transMock);
-               encryptor.init(Cipher.ENCRYPT_MODE, kp1.getPublic());
-               decryptor.init(Cipher.DECRYPT_MODE, kp1.getPrivate());
+               encryptor = Factory.pkCipher(kp1.getPublic(), true);
+               decryptor = Factory.pkCipher(kp1.getPrivate(), false);
                byte[] encrypedMessage1 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8));
                String output1 = new String(decryptor.doFinal(encrypedMessage1));
                assertThat(output1, is(message));
@@ -78,35 +144,213 @@ public class JU_Factory {
                // coverage
                when(transMock.start("Generate KeyPair", Env.SUB)).thenReturn(null);
                KeyPair kp2 = Factory.generateKeyPair(transMock);
-               encryptor.init(Cipher.ENCRYPT_MODE, kp2.getPublic());
-               decryptor.init(Cipher.DECRYPT_MODE, kp2.getPrivate());
+               encryptor = Factory.pkCipher(kp2.getPublic(), true);
+               decryptor = Factory.pkCipher(kp2.getPrivate(), false);
                byte[] encrypedMessage2 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8));
                String output2 = new String(decryptor.doFinal(encrypedMessage2));
                assertThat(output2, is(message));
 
                KeyPair kp3 = Factory.generateKeyPair(null);
-               encryptor.init(Cipher.ENCRYPT_MODE, kp3.getPublic());
-               decryptor.init(Cipher.DECRYPT_MODE, kp3.getPrivate());
+               encryptor = Factory.pkCipher(kp3.getPublic(), true);
+               decryptor = Factory.pkCipher(kp3.getPrivate(), false);
                byte[] encrypedMessage3 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8));
                String output3 = new String(decryptor.doFinal(encrypedMessage3));
                assertThat(output3, is(message));
        }
 
        @Test
-       public void keyToStringTest() throws IOException {
+       public void keyStringManipTest() throws Exception {
                KeyPair kp = Factory.generateKeyPair(transMock);
 
                String publicKeyString = Factory.toString(transMock, kp.getPublic());
                String privateKeyString = Factory.toString(transMock, kp.getPrivate());
 
-               String[] publicKeyLines = publicKeyString.split("\n", 0);
-               assertThat(publicKeyLines.length, is(9));
-               assertThat(publicKeyLines[0], is("-----BEGIN PUBLIC KEY-----"));
-               assertThat(publicKeyLines[8], is("-----END PUBLIC KEY-----"));
+               assertThat(publicKeyString.startsWith("-----BEGIN PUBLIC KEY-----"), is(true));
+               assertThat(publicKeyString.endsWith("-----END PUBLIC KEY-----\n"), is(true));
+
+               assertThat(privateKeyString.startsWith("-----BEGIN PRIVATE KEY-----"), is(true));
+               assertThat(privateKeyString.endsWith("-----END PRIVATE KEY-----\n"), is(true));
+
+               PublicKey publicKey = Factory.toPublicKey(transMock, cleanupString(publicKeyString));
+               PrivateKey privateKey = Factory.toPrivateKey(transMock, cleanupString(privateKeyString));
+
+               Cipher encryptor = Factory.pkCipher(publicKey, true);
+               Cipher decryptor = Factory.pkCipher(privateKey, false);
+               byte[] encrypedMessage = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8));
+               String output = new String(decryptor.doFinal(encrypedMessage));
+               assertThat(output, is(message));
+       }
+
+       @Test
+       public void keyFileManipTest() throws Exception {
+               KeyPair kp = Factory.generateKeyPair(transMock);
+
+               String privateKeyString = Factory.toString(transMock, kp.getPrivate());
+               writeToFile(privateKeyFile, privateKeyString, "Header:this line has a header");
+
+               PublicKey publicKey = kp.getPublic();
+               PrivateKey privateKey = Factory.toPrivateKey(transMock, privateKeyFile);
 
-               String[] privateKeyLines = privateKeyString.split("\n", 0);
-               assertThat(privateKeyLines.length, is(28));
-               assertThat(privateKeyLines[0], is("-----BEGIN PRIVATE KEY-----"));
-               assertThat(privateKeyLines[27], is("-----END PRIVATE KEY-----"));
+               Cipher encryptor = Factory.pkCipher(publicKey, true);
+               Cipher decryptor = Factory.pkCipher(privateKey, false);
+               byte[] encrypedMessage = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8));
+               String output = new String(decryptor.doFinal(encrypedMessage));
+               assertThat(output, is(message));
+       }
+
+       @Test
+       public void certToStringTest() throws IOException, CertException, CertificateEncodingException {
+               String certString;
+               when(logTargetMock.isLoggable()).thenReturn(true);
+
+               certString = Factory.toString(transMock, x509CertMock);
+               assertThat(certString.startsWith("-----BEGIN CERTIFICATE-----"), is(true));
+               assertThat(certString.endsWith("-----END CERTIFICATE-----\n"), is(true));
+
+               certString = Factory.toString(transMock, certMock);
+               assertThat(certString.startsWith("-----BEGIN CERTIFICATE-----"), is(true));
+               assertThat(certString.endsWith("-----END CERTIFICATE-----\n"), is(true));
+
+               try {
+                       certString = Factory.toString(transMock, (Certificate)null);
+                       fail("Should have thrown an exception");
+               } catch (CertException e) {
+                       assertThat(e.getMessage(), is("Certificate not built"));
+               }
+
+               when(certMock.getEncoded()).thenThrow(new CertificateEncodingException());
+               try {
+                       certString = Factory.toString(transMock, certMock);
+                       fail("Should have thrown an exception");
+               } catch (CertException e) {
+               }
+
+               // coverage
+               when(logTargetMock.isLoggable()).thenReturn(false);
+               certString = Factory.toString(transMock, x509CertMock);
        }
+
+       @Test
+       public void toX509Test() throws CertificateException, IOException, CertException {
+               String output;
+               Collection<? extends Certificate> certs;
+               when(logTargetMock.isLoggable()).thenReturn(true);
+
+               String certString = readFromFile(certFile, false);
+
+               certs = Factory.toX509Certificate(certString);
+               // Contrived way of getting a Certificate out of a Collection
+               output = Factory.toString(transMock, certs.toArray(new Certificate[0])[0]);
+               assertThat(output, is(certString));
+
+               certs = Factory.toX509Certificate(transMock, certFile);
+               // Contrived way of getting a Certificate out of a Collection
+               output = Factory.toString(transMock, certs.toArray(new Certificate[0])[0]);
+               assertThat(output, is(certString));
+
+               List<String> certStrings = new ArrayList();
+               certStrings.add(certString);
+               certStrings.add(certString);
+               certs = Factory.toX509Certificate(certStrings);
+               // Contrived way of getting a Certificate out of a Collection
+               // it doesn't matter which one we get - they're the same
+               output = Factory.toString(transMock, certs.toArray(new Certificate[0])[0]);
+               assertThat(output, is(certString));
+       }
+
+       @Test
+       public void stripperTest() throws Exception {
+               KeyPair kp = Factory.generateKeyPair(transMock);
+               String privateKeyString = Factory.toString(transMock, kp.getPrivate());
+               writeToFile(privateKeyFile, privateKeyString, "Header:this line has a header");
+
+               StripperInputStream stripper = new StripperInputStream(privateKeyFile);
+
+               String expected = cleanupString(privateKeyString);
+               byte[] buffer = new byte[10000];
+               stripper.read(buffer);
+               String output = new String(buffer, 0, expected.length());
+               assertThat(output, is(expected));
+               stripper.close();
+
+               // coverage
+               stripper = new StripperInputStream(new FileInputStream(privateKeyFile));
+               stripper.close();
+               stripper = new StripperInputStream(new BufferedReader(new FileReader(privateKeyFile)));
+               stripper.close();
+               stripper.close();  // also coverage...
+       }
+
+       @Test
+       public void binaryTest() throws IOException {
+               String output = new String(Factory.binary(certFile));
+               String expected = readFromFile(certFile, true);
+               assertThat(output, is(expected));
+       }
+
+       @Test
+       public void signatureTest() throws Exception {
+               KeyPair kp = Factory.generateKeyPair(transMock);
+               String signedString = "Something that needs signing";
+               byte[] signedBytes = Factory.sign(transMock, signedString.getBytes(), kp.getPrivate());
+               String output = Factory.toSignatureString(signedBytes);
+               assertThat(output.startsWith("-----BEGIN SIGNATURE-----"), is(true));
+               assertThat(output.endsWith("-----END SIGNATURE-----\n"), is(true));
+               assertThat(Factory.verify(transMock, signedString.getBytes(), signedBytes, kp.getPublic()), is(true));
+       }
+
+       // TODO: Ian - finish these tests
+       // @Test
+       // public void base64ISTest() throws Exception {
+       //      KeyPair kp = Factory.generateKeyPair(transMock);
+
+       //      String privateKeyString = Factory.toString(transMock, kp.getPrivate());
+       //      String cleaned = cleanupString(privateKeyString);
+       //      System.out.println(cleaned);
+       //      writeToFile(privateKeyFile, cleaned);
+       //      Base64InputStream b64is = new Base64InputStream(privateKeyFile);
+       //      byte[] buffer = new byte[10000];
+       //      b64is.read(buffer);
+       //      System.out.println(new String(buffer));
+       //      b64is.close();
+       // }
+
+       // @Test
+       // public void getSecurityProviderTest() {
+       // }
+
+       private String cleanupString(String str) {
+               String[] lines = str.split("\n", 0);
+               List<String> rawLines = new ArrayList<String>();
+               for (int i = 0; i < lines.length - 2; i++) {
+                       rawLines.add(lines[i + 1]);
+               }
+               return String.join("", rawLines);
+       }
+
+       private void writeToFile(File file, String contents) throws Exception {
+               writeToFile(file, contents, null);
+       }
+
+       private void writeToFile(File file, String contents, String header) throws Exception {
+               PrintWriter writer = new PrintWriter(file, "UTF-8");
+               if (header != null) {
+                       writer.println(header);
+               }
+               writer.println(contents);
+               writer.close();
+       }
+
+       private String readFromFile(File file, boolean addCR) throws IOException {
+               BufferedReader br = new BufferedReader(new FileReader(file));
+               StringBuilder sb = new StringBuilder();
+               String line;
+               while ((line = br.readLine()) != null) {
+                       String lineEnd = (addCR) ? "\r\n" : "\n";
+                       sb.append(line + lineEnd);
+               }
+               br.close();
+               return sb.toString();
+       }
+
 }
diff --git a/cadi/aaf/src/test/resources/exampleCertificate.cer b/cadi/aaf/src/test/resources/exampleCertificate.cer
new file mode 100755 (executable)
index 0000000..76ed12d
--- /dev/null
@@ -0,0 +1,59 @@
+-----BEGIN CERTIFICATE-----\r
+MIIKkTCCCXmgAwIBAgIQBO9R08Vvthj6ifmVeLw94DANBgkqhkiG9w0BAQsFADBe\r
+MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\r
+d3cuZGlnaWNlcnQuY29tMR0wGwYDVQQDExRHZW9UcnVzdCBSU0EgQ0EgMjAxODAe\r
+Fw0xODAzMjUwMDAwMDBaFw0xOTA0MjMxMjAwMDBaMIGaMQswCQYDVQQGEwJVUzET\r
+MBEGA1UECBMKQ2FsaWZvcm5pYTEXMBUGA1UEBxMOUmVkd29vZCBTaG9yZXMxGzAZ\r
+BgNVBAoTEk9yYWNsZSBDb3Jwb3JhdGlvbjEnMCUGA1UECxMeQ29udGVudCBNYW5h\r
+Z2VtZW50IFNlcnZpY2VzIElUMRcwFQYDVQQDEw53d3cub3JhY2xlLmNvbTCCASIw\r
+DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMWDD1iqkoFspX8qhH6HvcEkOtG7\r
+x5yQQzGO2qr4FzZxzBUsi4fOm33e+wD+H+zRi2u/5GPKr6f6GNT50IQJR6Bg5ReX\r
+co3nbL0XmqjxMmncM7UkgGwFynzW/AMZ6TobMhtKFiHgCHanXGxMJgZmw9JW0qkf\r
+pHRsTa/OBnxV16tu4cAIHoVb5ZkJH0qSXKSdhj4KKM6Uajr4QxS7YQQTr51MAlDa\r
+Xe3tWTaJv3TjMpzD23a8xfhNRDWp6Ji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