+ 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, publicKeyString);
+ PrivateKey privateKey = Factory.toPrivateKey(transMock, 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);
+
+ 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<String>();
+ 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();