+
+ @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();
+ }
+