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.mockito.Matchers.*;
25 import static org.junit.Assert.*;
26 import static org.hamcrest.CoreMatchers.*;
29 import static org.mockito.Mockito.*;
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
34 import java.io.FileInputStream;
35 import java.io.IOException;
36 import java.io.OutputStream;
37 import java.io.PrintStream;
38 import java.nio.file.Files;
39 import java.nio.file.Paths;
40 import java.util.Properties;
42 import org.onap.aaf.cadi.CmdLine;
43 import org.onap.aaf.cadi.PropAccess;
44 import org.onap.aaf.cadi.Symm;
46 public class JU_CmdLine {
49 private OutputStream thrower;
51 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
53 private String password;
54 private String keyfile;
55 private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog";
56 private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
57 private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
61 public void setup() throws Exception {
62 MockitoAnnotations.initMocks(this);
64 System.setOut(new PrintStream(outContent));
66 Properties p = new Properties();
67 p.setProperty("force_exit", "false");
69 CmdLine.access = new PropAccess(p);
71 password = "password";
72 keyfile = "test/keyfile";
74 FileInputStream fis = new FileInputStream(keyfile);
76 symm = Symm.obtain(fis);
83 public void restoreStreams() throws IOException {
84 System.setOut(System.out);
85 System.setIn(System.in);
89 public void digestTest() throws Exception {
90 CmdLine.main(new String[]{"digest", password, keyfile});
91 String decrypted = symm.depass(outContent.toString());
92 assertThat(decrypted, is(password));
94 System.setIn(new ByteArrayInputStream(password.getBytes()));
95 CmdLine.main(new String[]{"digest", "-i", keyfile});
96 decrypted = symm.depass(outContent.toString());
97 assertThat(decrypted, is(password));
101 public void regurgitateTest() {
102 // TODO: We may still want to remove the regurgitate functionality
103 // from the CmdLine - Ian
104 fail("Tests not yet implemented");
108 public void encode64Test() throws Exception {
109 CmdLine.main(new String[]{"encode64", password});
110 String decrypted = Symm.base64.decode(outContent.toString());
111 assertThat(decrypted, is(password));
115 public void decode64Test() throws Exception {
116 String encrypted = Symm.base64.encode(password);
117 CmdLine.main(new String[]{"decode64", encrypted});
118 assertThat(outContent.toString(), is(password + "\n"));
122 public void encode64urlTest() throws Exception {
123 CmdLine.main(new String[]{"encode64url", password});
124 String decrypted = Symm.base64url.decode(outContent.toString());
125 assertThat(decrypted, is(password));
129 public void decode64urlTest() throws Exception {
130 String encrypted = Symm.base64url.encode(password);
131 CmdLine.main(new String[]{"decode64url", encrypted});
132 assertThat(outContent.toString(), is(password + "\n"));
136 public void md5Test() throws Exception {
137 CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
138 assertThat(outContent.toString(), is(quickBrownFoxMD5 + "\n"));
142 public void sha256Test() throws Exception {
143 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
144 assertThat(outContent.toString(), is(quickBrownFoxSHA256 + "\n"));
147 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
148 String hash1 = outContent.toString();
151 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
152 String hash2 = outContent.toString();
155 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
156 String hash3 = outContent.toString();
158 assertThat(hash1, is(hash2));
159 assertThat(hash1, is(not(hash3)));
163 public void keygenTest() throws Exception {
164 CmdLine.main(new String[]{"keygen"});
165 assertThat(outContent.toString().length(), is(2074));
167 String filePath = "test/output_key";
168 CmdLine.main(new String[]{"keygen", filePath});
169 File keyfile = new File(filePath);
170 assertTrue(Files.isReadable(Paths.get(filePath)));
171 assertFalse(Files.isWritable(Paths.get(filePath)));
172 assertFalse(Files.isExecutable(Paths.get(filePath)));
177 public void passgenTest() throws Exception {
178 CmdLine.main(new String[]{"passgen"});
179 String output = outContent.toString().trim();
180 assertThat(output.length(), is(24));
181 assertTrue(containsAny(output, "+!@#$%^&*(){}[]?:;,."));
182 assertTrue(containsAny(output, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
183 assertTrue(containsAny(output, "abcdefghijklmnopqrstuvwxyz"));
184 assertTrue(containsAny(output, "0123456789"));
188 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
189 output = outContent.toString().trim();
190 assertThat(output.length(), is(length));
194 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
195 output = outContent.toString().trim();
196 assertThat(output.length(), is(8));
198 // Check that the custom hasRepeats method works
199 assertTrue(hasRepeats("aa"));
200 assertTrue(hasRepeats("baa"));
201 assertTrue(hasRepeats("aab"));
202 assertTrue(hasRepeats("baab"));
203 assertFalse(hasRepeats("abc"));
204 assertFalse(hasRepeats("aba"));
206 // Run this a bunch of times for coverage
207 for (int i = 0; i < 1000; i++) {
209 CmdLine.main(new String[]{"passgen"});
210 output = outContent.toString().trim();
211 assertFalse(hasRepeats(output));
216 public void urlgenTest() throws Exception {
217 CmdLine.main(new String[]{"urlgen"});
218 String output = outContent.toString().trim();
219 assertThat(output.length(), is(24));
223 CmdLine.main(new String[]{"urlgen", String.valueOf(length)});
224 output = outContent.toString().trim();
225 assertThat(output.length(), is(5));
229 public void showHelpTest() {
231 "Usage: java -jar <this jar> ...\n" +
232 " keygen [<keyfile>] (Generates Key on file, or Std Out)\n" +
233 " digest [<passwd>|-i|] <keyfile> (Encrypts Password with \"keyfile\"\n" +
234 " if passwd = -i, will read StdIin\n" +
235 " if passwd is blank, will ask securely)\n" +
236 " passgen <digits> (Generate Password of given size)\n" +
237 " urlgen <digits> (Generate URL field of given size)\n" +
238 " csptest (Tests for CSP compatibility)\n" +
239 " encode64 <your text> (Encodes to Base64)\n" +
240 " decode64 <base64 encoded text> (Decodes from Base64)\n" +
241 " encode64url <your text> (Encodes to Base64 URL charset)\n" +
242 " decode64url <base64url encoded text> (Decodes from Base64 URL charset)\n" +
243 " sha256 <text> <salts(s)> (Digest String into SHA256 Hash)\n" +
244 " md5 <text> (Digest String into MD5 Hash)\n";
246 CmdLine.main(new String[]{});
248 assertThat(outContent.toString(), is(expected));
251 private boolean containsAny(String str, String searchChars) {
252 for (char c : searchChars.toCharArray()) {
253 if (str.indexOf(c) >= 0) {
260 private boolean hasRepeats(String str) {
263 for (int i = 0; i < str.length(); i++) {