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.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.not;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.assertTrue;
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.io.OutputStream;
36 import java.io.PrintStream;
37 import java.nio.file.Files;
38 import java.nio.file.Paths;
39 import java.util.Properties;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.aaf.cadi.CmdLine;
47 import org.onap.aaf.cadi.Symm;
49 public class JU_CmdLine {
52 private OutputStream thrower;
54 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
56 private String password;
57 private String keyfile;
58 private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog";
59 private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
60 private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
64 public void setup() throws Exception {
65 MockitoAnnotations.initMocks(this);
67 System.setOut(new PrintStream(outContent));
69 Properties p = new Properties();
70 p.setProperty("force_exit", "false");
72 CmdLine.setSystemExit(false);
73 keyfile = "src/test/resources/keyfile";
74 password = "password";
76 File keyF = new File("src/test/resources", "keyfile");
77 FileInputStream fis = new FileInputStream(keyF);
79 symm = Symm.obtain(fis);
86 public void restoreStreams() throws IOException {
87 System.setOut(System.out);
88 System.setIn(System.in);
92 public void digestTest() throws Exception {
93 CmdLine.main(new String[]{"digest", password, keyfile});
94 String decrypted = symm.depass(outContent.toString());
95 assertThat(decrypted, is(password));
97 System.setIn(new ByteArrayInputStream(password.getBytes()));
98 CmdLine.main(new String[]{"digest", "-i", keyfile});
99 decrypted = symm.depass(outContent.toString());
100 assertThat(decrypted, is(password));
104 public void encode64Test() throws Exception {
105 CmdLine.main(new String[]{"encode64", password});
106 String decrypted = Symm.base64.decode(outContent.toString());
107 assertThat(decrypted, is(password));
111 public void decode64Test() throws Exception {
112 String encrypted = Symm.base64.encode(password);
113 CmdLine.main(new String[]{"decode64", encrypted});
114 assertThat(outContent.toString(), is(password + System.lineSeparator()));
118 public void encode64urlTest() throws Exception {
119 CmdLine.main(new String[]{"encode64url", password});
120 String decrypted = Symm.base64url.decode(outContent.toString());
121 assertThat(decrypted, is(password));
125 public void decode64urlTest() throws Exception {
126 String encrypted = Symm.base64url.encode(password);
127 CmdLine.main(new String[]{"decode64url", encrypted});
128 assertThat(outContent.toString(), is(password + System.lineSeparator()));
132 public void md5Test() throws Exception {
133 CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
134 assertThat(outContent.toString(), is(quickBrownFoxMD5 + System.lineSeparator()));
138 public void sha256Test() throws Exception {
139 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
140 assertThat(outContent.toString(), is(quickBrownFoxSHA256 + System.lineSeparator()));
143 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
144 String hash1 = outContent.toString();
147 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
148 String hash2 = outContent.toString();
151 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
152 String hash3 = outContent.toString();
154 assertThat(hash1, is(hash2));
155 assertThat(hash1, is(not(hash3)));
159 public void keygenTest() throws Exception {
160 CmdLine.main(new String[]{"keygen"});
161 assertThat(outContent.toString().length(), is(2074));
163 String filePath = "test/output_key";
164 File testDir = new File("test");
165 if(!testDir.exists()) {
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() {
230 String lineSeparator = System.lineSeparator();
232 "Usage: java -jar <this jar> ..." + lineSeparator +
233 " keygen [<keyfile>] (Generates Key on file, or Std Out)" + lineSeparator +
234 " digest [<passwd>|-i|] <keyfile> (Encrypts Password with \"keyfile\"" + lineSeparator +
235 " if passwd = -i, will read StdIn" + lineSeparator +
236 " if passwd is blank, will ask securely)" + lineSeparator +
237 " passgen <digits> (Generate Password of given size)" + lineSeparator +
238 " urlgen <digits> (Generate URL field of given size)" + lineSeparator +
239 " encode64 <your text> (Encodes to Base64)" + lineSeparator +
240 " decode64 <base64 encoded text> (Decodes from Base64)" + lineSeparator +
241 " encode64url <your text> (Encodes to Base64 URL charset)" + lineSeparator +
242 " decode64url <base64url encoded text> (Decodes from Base64 URL charset)" + lineSeparator +
243 " sha256 <text> <salts(s)> (Digest String into SHA256 Hash)" + lineSeparator +
244 " md5 <text> (Digest String into MD5 Hash)" + lineSeparator;
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++) {