X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Fcore%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Ftest%2FJU_CmdLine.java;h=56b5538f80d5e02de8517695357f82660b3efb26;hb=1296352d8eafee57f982a4342ad79ada4aa56d28;hp=efcc1b295ce284c36a779b7083557347511bf048;hpb=1070cc920c5cfa8af29f83137da79fba28341609;p=aaf%2Fauthz.git diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_CmdLine.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_CmdLine.java index efcc1b29..56b5538f 100644 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_CmdLine.java +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/test/JU_CmdLine.java @@ -19,6 +19,7 @@ * * * * ******************************************************************************/ + package org.onap.aaf.cadi.test; import static org.hamcrest.CoreMatchers.is; @@ -48,226 +49,227 @@ import org.onap.aaf.cadi.Symm; public class JU_CmdLine { - @Mock - private OutputStream thrower; - - private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - - private String password; - private String keyfile; - private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog"; - private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6"; - private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"; - private Symm symm; - - @Before - public void setup() throws Exception { - MockitoAnnotations.initMocks(this); - - System.setOut(new PrintStream(outContent)); - - Properties p = new Properties(); - p.setProperty("force_exit", "false"); - - CmdLine.setSystemExit(false); - keyfile = "src/test/resources/keyfile"; - password = "password"; - - File keyF = new File("src/test/resources", "keyfile"); - FileInputStream fis = new FileInputStream(keyF); - try { - symm = Symm.obtain(fis); - } finally { - fis.close(); - } - } - - @After - public void restoreStreams() throws IOException { - System.setOut(System.out); - System.setIn(System.in); - } - - @Test - public void digestTest() throws Exception { - CmdLine.main(new String[]{"digest", password, keyfile}); - String decrypted = symm.depass(outContent.toString()); - assertThat(decrypted, is(password)); - - System.setIn(new ByteArrayInputStream(password.getBytes())); - CmdLine.main(new String[]{"digest", "-i", keyfile}); - decrypted = symm.depass(outContent.toString()); - assertThat(decrypted, is(password)); - } - - @Test - public void encode64Test() throws Exception { - CmdLine.main(new String[]{"encode64", password}); - String decrypted = Symm.base64.decode(outContent.toString()); - assertThat(decrypted, is(password)); - } - - @Test - public void decode64Test() throws Exception { - String encrypted = Symm.base64.encode(password); - CmdLine.main(new String[]{"decode64", encrypted}); - assertThat(outContent.toString(), is(password + "\n")); - } - - @Test - public void encode64urlTest() throws Exception { - CmdLine.main(new String[]{"encode64url", password}); - String decrypted = Symm.base64url.decode(outContent.toString()); - assertThat(decrypted, is(password)); - } - - @Test - public void decode64urlTest() throws Exception { - String encrypted = Symm.base64url.encode(password); - CmdLine.main(new String[]{"decode64url", encrypted}); - assertThat(outContent.toString(), is(password + "\n")); - } - - @Test - public void md5Test() throws Exception { - CmdLine.main(new String[]{"md5", quickBrownFoxPlain}); - assertThat(outContent.toString(), is(quickBrownFoxMD5 + "\n")); - } - - @Test - public void sha256Test() throws Exception { - CmdLine.main(new String[]{"sha256", quickBrownFoxPlain}); - assertThat(outContent.toString(), is(quickBrownFoxSHA256 + "\n")); - - outContent.reset(); - CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"}); - String hash1 = outContent.toString(); - - outContent.reset(); - CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"}); - String hash2 = outContent.toString(); - - outContent.reset(); - CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"}); - String hash3 = outContent.toString(); - - assertThat(hash1, is(hash2)); - assertThat(hash1, is(not(hash3))); - } - - @Test - public void keygenTest() throws Exception { - CmdLine.main(new String[]{"keygen"}); - assertThat(outContent.toString().length(), is(2074)); - - String filePath = "test/output_key"; - File testDir = new File("test"); - if(!testDir.exists()) { - testDir.mkdirs(); - } - CmdLine.main(new String[]{"keygen", filePath}); - File keyfile = new File(filePath); - assertTrue(Files.isReadable(Paths.get(filePath))); - assertFalse(Files.isWritable(Paths.get(filePath))); - assertFalse(Files.isExecutable(Paths.get(filePath))); - keyfile.delete(); - } - - @Test - public void passgenTest() throws Exception { - CmdLine.main(new String[]{"passgen"}); - String output = outContent.toString().trim(); - assertThat(output.length(), is(24)); - assertTrue(containsAny(output, "+!@#$%^&*(){}[]?:;,.")); - assertTrue(containsAny(output, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")); - assertTrue(containsAny(output, "abcdefghijklmnopqrstuvwxyz")); - assertTrue(containsAny(output, "0123456789")); - - int length = 10; - outContent.reset(); - CmdLine.main(new String[]{"passgen", String.valueOf(length)}); - output = outContent.toString().trim(); - assertThat(output.length(), is(length)); - - length = 5; - outContent.reset(); - CmdLine.main(new String[]{"passgen", String.valueOf(length)}); - output = outContent.toString().trim(); - assertThat(output.length(), is(8)); - - // Check that the custom hasRepeats method works - assertTrue(hasRepeats("aa")); - assertTrue(hasRepeats("baa")); - assertTrue(hasRepeats("aab")); - assertTrue(hasRepeats("baab")); - assertFalse(hasRepeats("abc")); - assertFalse(hasRepeats("aba")); - - // Run this a bunch of times for coverage - for (int i = 0; i < 1000; i++) { - outContent.reset(); - CmdLine.main(new String[]{"passgen"}); - output = outContent.toString().trim(); - assertFalse(hasRepeats(output)); - } - } - - @Test - public void urlgenTest() throws Exception { - CmdLine.main(new String[]{"urlgen"}); - String output = outContent.toString().trim(); - assertThat(output.length(), is(24)); - - int length = 5; - outContent.reset(); - CmdLine.main(new String[]{"urlgen", String.valueOf(length)}); - output = outContent.toString().trim(); - assertThat(output.length(), is(5)); - } - - @Test - public void showHelpTest() { - String expected = - "Usage: java -jar ...\n" + - " keygen [] (Generates Key on file, or Std Out)\n" + - " digest [|-i|] (Encrypts Password with \"keyfile\"\n" + - " if passwd = -i, will read StdIin\n" + - " if passwd is blank, will ask securely)\n" + - " passgen (Generate Password of given size)\n" + - " urlgen (Generate URL field of given size)\n" + - " csptest (Tests for CSP compatibility)\n" + - " encode64 (Encodes to Base64)\n" + - " decode64 (Decodes from Base64)\n" + - " encode64url (Encodes to Base64 URL charset)\n" + - " decode64url (Decodes from Base64 URL charset)\n" + - " sha256 (Digest String into SHA256 Hash)\n" + - " md5 (Digest String into MD5 Hash)\n"; - - CmdLine.main(new String[]{}); - - assertThat(outContent.toString(), is(expected)); - } - - private boolean containsAny(String str, String searchChars) { - for (char c : searchChars.toCharArray()) { - if (str.indexOf(c) >= 0) { - return true; - } - } - return false; - } - - private boolean hasRepeats(String str) { - int c = -1; - int last; - for (int i = 0; i < str.length(); i++) { - last = c; - c = str.charAt(i); - if (c == last) { - return true; - } - } - return false; - } + @Mock + private OutputStream thrower; + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + + private String password; + private String keyfile; + private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog"; + private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6"; + private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"; + private Symm symm; + + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + + System.setOut(new PrintStream(outContent)); + + Properties p = new Properties(); + p.setProperty("force_exit", "false"); + + CmdLine.setSystemExit(false); + keyfile = "src/test/resources/keyfile"; + password = "password"; + + File keyF = new File("src/test/resources", "keyfile"); + FileInputStream fis = new FileInputStream(keyF); + try { + symm = Symm.obtain(fis); + } finally { + fis.close(); + } + } + + @After + public void restoreStreams() throws IOException { + System.setOut(System.out); + System.setIn(System.in); + } + + @Test + public void digestTest() throws Exception { + CmdLine.main(new String[]{"digest", password, keyfile}); + String decrypted = symm.depass(outContent.toString()); + assertThat(decrypted, is(password)); + + System.setIn(new ByteArrayInputStream(password.getBytes())); + CmdLine.main(new String[]{"digest", "-i", keyfile}); + decrypted = symm.depass(outContent.toString()); + assertThat(decrypted, is(password)); + } + + @Test + public void encode64Test() throws Exception { + CmdLine.main(new String[]{"encode64", password}); + String decrypted = Symm.base64.decode(outContent.toString()); + assertThat(decrypted, is(password)); + } + + @Test + public void decode64Test() throws Exception { + String encrypted = Symm.base64.encode(password); + CmdLine.main(new String[]{"decode64", encrypted}); + assertThat(outContent.toString(), is(password + System.lineSeparator())); + } + + @Test + public void encode64urlTest() throws Exception { + CmdLine.main(new String[]{"encode64url", password}); + String decrypted = Symm.base64url.decode(outContent.toString()); + assertThat(decrypted, is(password)); + } + + @Test + public void decode64urlTest() throws Exception { + String encrypted = Symm.base64url.encode(password); + CmdLine.main(new String[]{"decode64url", encrypted}); + assertThat(outContent.toString(), is(password + System.lineSeparator())); + } + + @Test + public void md5Test() throws Exception { + CmdLine.main(new String[]{"md5", quickBrownFoxPlain}); + assertThat(outContent.toString(), is(quickBrownFoxMD5 + System.lineSeparator())); + } + + @Test + public void sha256Test() throws Exception { + CmdLine.main(new String[]{"sha256", quickBrownFoxPlain}); + assertThat(outContent.toString(), is(quickBrownFoxSHA256 + System.lineSeparator())); + + outContent.reset(); + CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"}); + String hash1 = outContent.toString(); + + outContent.reset(); + CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"}); + String hash2 = outContent.toString(); + + outContent.reset(); + CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"}); + String hash3 = outContent.toString(); + + assertThat(hash1, is(hash2)); + assertThat(hash1, is(not(hash3))); + } + + @Test + public void keygenTest() throws Exception { + CmdLine.main(new String[]{"keygen"}); + assertThat(outContent.toString().length(), is(2074)); + + String filePath = "test/output_key"; + File testDir = new File("test"); + if (!testDir.exists()) { + testDir.mkdirs(); + } + CmdLine.main(new String[]{"keygen", filePath}); + File keyfile = new File(filePath); + assertTrue(Files.isReadable(Paths.get(filePath))); + assertFalse(Files.isWritable(Paths.get(filePath))); + //assertFalse(Files.isExecutable(Paths.get(filePath))); + keyfile.delete(); + } + + @Test + public void passgenTest() throws Exception { + CmdLine.main(new String[]{"passgen"}); + String output = outContent.toString().trim(); + assertThat(output.length(), is(24)); + assertTrue(containsAny(output, "+!@#$%^&*(){}[]?:;,.")); + assertTrue(containsAny(output, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")); + assertTrue(containsAny(output, "abcdefghijklmnopqrstuvwxyz")); + assertTrue(containsAny(output, "0123456789")); + + int length = 10; + outContent.reset(); + CmdLine.main(new String[]{"passgen", String.valueOf(length)}); + output = outContent.toString().trim(); + assertThat(output.length(), is(length)); + + length = 5; + outContent.reset(); + CmdLine.main(new String[]{"passgen", String.valueOf(length)}); + output = outContent.toString().trim(); + assertThat(output.length(), is(8)); + + // Check that the custom hasRepeats method works + assertTrue(hasRepeats("aa")); + assertTrue(hasRepeats("baa")); + assertTrue(hasRepeats("aab")); + assertTrue(hasRepeats("baab")); + assertFalse(hasRepeats("abc")); + assertFalse(hasRepeats("aba")); + + // Run this a bunch of times for coverage + for (int i = 0; i < 1000; i++) { + outContent.reset(); + CmdLine.main(new String[]{"passgen"}); + output = outContent.toString().trim(); + assertFalse(hasRepeats(output)); + } + } + + @Test + public void urlgenTest() throws Exception { + CmdLine.main(new String[]{"urlgen"}); + String output = outContent.toString().trim(); + assertThat(output.length(), is(24)); + + int length = 5; + outContent.reset(); + CmdLine.main(new String[]{"urlgen", String.valueOf(length)}); + output = outContent.toString().trim(); + assertThat(output.length(), is(5)); + } + + @Test + public void showHelpTest() { + String lineSeparator = System.lineSeparator(); + String expected = + "Usage: java -jar ..." + lineSeparator + + " keygen [] (Generates Key on file, or Std Out)" + lineSeparator + + " digest [|-i|] (Encrypts Password with \"keyfile\"" + lineSeparator + + " if passwd = -i, will read StdIn" + lineSeparator + + " if passwd is blank, will ask securely)" + lineSeparator + + " undigest (Decrypts Encoded with \"keyfile\")" + lineSeparator + + " passgen (Generate Password of given size)" + lineSeparator + + " urlgen (Generate URL field of given size)" + lineSeparator + + " encode64 (Encodes to Base64)" + lineSeparator + + " decode64 (Decodes from Base64)" + lineSeparator + + " encode64url (Encodes to Base64 URL charset)" + lineSeparator + + " decode64url (Decodes from Base64 URL charset)" + lineSeparator + + " sha256 (Digest String into SHA256 Hash)" + lineSeparator + + " md5 (Digest String into MD5 Hash)" + lineSeparator; + + CmdLine.main(new String[]{}); + + assertThat(outContent.toString(), is(expected)); + } + + private boolean containsAny(String str, String searchChars) { + for (char c : searchChars.toCharArray()) { + if (str.indexOf(c) >= 0) { + return true; + } + } + return false; + } + + private boolean hasRepeats(String str) { + int c = -1; + int last; + for (int i = 0; i < str.length(); i++) { + last = c; + c = str.charAt(i); + if (c == last) { + return true; + } + } + return false; + } }