1 /*******************************************************************************
2 * ============LICENSE_START====================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
21 ******************************************************************************/
22 package org.onap.aaf.cadi.test;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.OutputStream;
35 import java.io.PrintStream;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.util.Properties;
40 import org.onap.aaf.cadi.CmdLine;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.Symm;
44 public class JU_CmdLine {
47 private OutputStream thrower;
49 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
51 private String password;
52 private String keyfile;
53 private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog";
54 private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
55 private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
59 public void setup() throws Exception {
60 MockitoAnnotations.initMocks(this);
62 System.setOut(new PrintStream(outContent));
64 Properties p = new Properties();
65 p.setProperty("force_exit", "false");
67 CmdLine.access = new PropAccess(p);
68 keyfile = "src/test/resources/keyfile";
69 password = "password";
71 File keyF = new File("src/test/resources", "keyfile");
72 FileInputStream fis = new FileInputStream(keyF);
74 symm = Symm.obtain(fis);
81 public void restoreStreams() throws IOException {
82 System.setOut(System.out);
83 System.setIn(System.in);
87 public void digestTest() throws Exception {
88 CmdLine.main(new String[]{"digest", password, keyfile});
89 String decrypted = symm.depass(outContent.toString());
90 assertThat(decrypted, is(password));
92 System.setIn(new ByteArrayInputStream(password.getBytes()));
93 CmdLine.main(new String[]{"digest", "-i", keyfile});
94 decrypted = symm.depass(outContent.toString());
95 assertThat(decrypted, is(password));
99 // public void regurgitateTest() {
100 // // TODO: We may still want to remove the regurgitate functionality
101 // // from the CmdLine - Ian
102 // fail("Tests not yet implemented");
106 public void encode64Test() throws Exception {
107 CmdLine.main(new String[]{"encode64", password});
108 String decrypted = Symm.base64.decode(outContent.toString());
109 assertThat(decrypted, is(password));
113 public void decode64Test() throws Exception {
114 String encrypted = Symm.base64.encode(password);
115 CmdLine.main(new String[]{"decode64", encrypted});
116 assertThat(outContent.toString(), is(password + "\n"));
120 public void encode64urlTest() throws Exception {
121 CmdLine.main(new String[]{"encode64url", password});
122 String decrypted = Symm.base64url.decode(outContent.toString());
123 assertThat(decrypted, is(password));
127 public void decode64urlTest() throws Exception {
128 String encrypted = Symm.base64url.encode(password);
129 CmdLine.main(new String[]{"decode64url", encrypted});
130 assertThat(outContent.toString(), is(password + "\n"));
134 public void md5Test() throws Exception {
135 CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
136 assertThat(outContent.toString(), is(quickBrownFoxMD5 + "\n"));
140 public void sha256Test() throws Exception {
141 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
142 assertThat(outContent.toString(), is(quickBrownFoxSHA256 + "\n"));
145 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
146 String hash1 = outContent.toString();
149 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
150 String hash2 = outContent.toString();
153 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
154 String hash3 = outContent.toString();
156 assertThat(hash1, is(hash2));
157 assertThat(hash1, is(not(hash3)));
161 public void keygenTest() throws Exception {
162 CmdLine.main(new String[]{"keygen"});
163 assertThat(outContent.toString().length(), is(2074));
165 String filePath = "test/output_key";
166 CmdLine.main(new String[]{"keygen", filePath});
167 File keyfile = new File(filePath);
168 assertTrue(Files.isReadable(Paths.get(filePath)));
169 assertFalse(Files.isWritable(Paths.get(filePath)));
170 assertFalse(Files.isExecutable(Paths.get(filePath)));
175 public void passgenTest() throws Exception {
176 CmdLine.main(new String[]{"passgen"});
177 String output = outContent.toString().trim();
178 assertThat(output.length(), is(24));
179 assertTrue(containsAny(output, "+!@#$%^&*(){}[]?:;,."));
180 assertTrue(containsAny(output, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
181 assertTrue(containsAny(output, "abcdefghijklmnopqrstuvwxyz"));
182 assertTrue(containsAny(output, "0123456789"));
186 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
187 output = outContent.toString().trim();
188 assertThat(output.length(), is(length));
192 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
193 output = outContent.toString().trim();
194 assertThat(output.length(), is(8));
196 // Check that the custom hasRepeats method works
197 assertTrue(hasRepeats("aa"));
198 assertTrue(hasRepeats("baa"));
199 assertTrue(hasRepeats("aab"));
200 assertTrue(hasRepeats("baab"));
201 assertFalse(hasRepeats("abc"));
202 assertFalse(hasRepeats("aba"));
204 // Run this a bunch of times for coverage
205 for (int i = 0; i < 1000; i++) {
207 CmdLine.main(new String[]{"passgen"});
208 output = outContent.toString().trim();
209 assertFalse(hasRepeats(output));
214 public void urlgenTest() throws Exception {
215 CmdLine.main(new String[]{"urlgen"});
216 String output = outContent.toString().trim();
217 assertThat(output.length(), is(24));
221 CmdLine.main(new String[]{"urlgen", String.valueOf(length)});
222 output = outContent.toString().trim();
223 assertThat(output.length(), is(5));
227 public void showHelpTest() {
229 "Usage: java -jar <this jar> ...\n" +
230 " keygen [<keyfile>] (Generates Key on file, or Std Out)\n" +
231 " digest [<passwd>|-i|] <keyfile> (Encrypts Password with \"keyfile\"\n" +
232 " if passwd = -i, will read StdIin\n" +
233 " if passwd is blank, will ask securely)\n" +
234 " passgen <digits> (Generate Password of given size)\n" +
235 " urlgen <digits> (Generate URL field of given size)\n" +
236 " csptest (Tests for CSP compatibility)\n" +
237 " encode64 <your text> (Encodes to Base64)\n" +
238 " decode64 <base64 encoded text> (Decodes from Base64)\n" +
239 " encode64url <your text> (Encodes to Base64 URL charset)\n" +
240 " decode64url <base64url encoded text> (Decodes from Base64 URL charset)\n" +
241 " sha256 <text> <salts(s)> (Digest String into SHA256 Hash)\n" +
242 " md5 <text> (Digest String into MD5 Hash)\n";
244 CmdLine.main(new String[]{});
246 assertThat(outContent.toString(), is(expected));
249 private boolean containsAny(String str, String searchChars) {
250 for (char c : searchChars.toCharArray()) {
251 if (str.indexOf(c) >= 0) {
258 private boolean hasRepeats(String str) {
261 for (int i = 0; i < str.length(); i++) {