From 7e78260ca905d5675f305c2d68c67b4f71528c63 Mon Sep 17 00:00:00 2001 From: Philip Langdale Date: Tue, 7 Jun 2011 15:54:13 -0700 Subject: [PATCH] Add unit test for String originated key files. --- .../sshj/keyprovider/OpenSSHKeyFileTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test/java/net/schmizz/sshj/keyprovider/OpenSSHKeyFileTest.java b/src/test/java/net/schmizz/sshj/keyprovider/OpenSSHKeyFileTest.java index b14b0d8e..144059d7 100644 --- a/src/test/java/net/schmizz/sshj/keyprovider/OpenSSHKeyFileTest.java +++ b/src/test/java/net/schmizz/sshj/keyprovider/OpenSSHKeyFileTest.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.util.Arrays; +import java.util.Scanner; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -113,6 +114,19 @@ public class OpenSSHKeyFileTest { assertEquals(KeyUtil.newDSAPrivateKey(x, p, q, g), dsa.getPrivate()); } + @Test + public void fromString() + throws IOException, GeneralSecurityException { + FileKeyProvider dsa = new OpenSSHKeyFile(); + String privateKey = readFile("src/test/resources/id_dsa"); + String publicKey = readFile("src/test/resources/id_dsa.pub"); + dsa.init(privateKey, publicKey, + PasswordUtils.createOneOff(correctPassphrase)); + assertEquals(dsa.getType(), KeyType.DSA); + assertEquals(KeyUtil.newDSAPublicKey(y, p, q, g), dsa.getPublic()); + assertEquals(KeyUtil.newDSAPrivateKey(x, p, q, g), dsa.getPrivate()); + } + @Before public void setup() throws UnsupportedEncodingException, GeneralSecurityException { @@ -120,4 +134,19 @@ public class OpenSSHKeyFileTest { throw new AssertionError("bouncy castle needed"); } + private String readFile(String pathname) + throws IOException { + + StringBuilder fileContents = new StringBuilder(); + Scanner scanner = new Scanner(new File(pathname)); + String lineSeparator = System.getProperty("line.separator"); + try { + while(scanner.hasNextLine()) { + fileContents.append(scanner.nextLine() + lineSeparator); + } + return fileContents.toString(); + } finally { + scanner.close(); + } + } } \ No newline at end of file