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 ******************************************************************************/
23 package org.onap.aaf.cadi.test;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.CoreMatchers.not;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertThat;
29 import static org.junit.Assert.assertTrue;
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.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.onap.aaf.cadi.CmdLine;
48 import org.onap.aaf.cadi.Symm;
50 public class JU_CmdLine {
53 private OutputStream thrower;
55 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
57 private String password;
58 private String keyfile;
59 private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog";
60 private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
61 private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
65 public void setup() throws Exception {
66 MockitoAnnotations.initMocks(this);
68 System.setOut(new PrintStream(outContent));
70 Properties p = new Properties();
71 p.setProperty("force_exit", "false");
73 CmdLine.setSystemExit(false);
74 keyfile = "src/test/resources/keyfile";
75 password = "password";
77 File keyF = new File("src/test/resources", "keyfile");
78 FileInputStream fis = new FileInputStream(keyF);
80 symm = Symm.obtain(fis);
87 public void restoreStreams() throws IOException {
88 System.setOut(System.out);
89 System.setIn(System.in);
93 public void digestTest() throws Exception {
94 CmdLine.main(new String[]{"digest", password, keyfile});
95 String decrypted = symm.depass(outContent.toString());
96 assertThat(decrypted, is(password));
98 System.setIn(new ByteArrayInputStream(password.getBytes()));
99 CmdLine.main(new String[]{"digest", "-i", keyfile});
100 decrypted = symm.depass(outContent.toString());
101 assertThat(decrypted, is(password));
105 public void encode64Test() throws Exception {
106 CmdLine.main(new String[]{"encode64", password});
107 String decrypted = Symm.base64.decode(outContent.toString());
108 assertThat(decrypted, is(password));
112 public void decode64Test() throws Exception {
113 String encrypted = Symm.base64.encode(password);
114 CmdLine.main(new String[]{"decode64", encrypted});
115 assertThat(outContent.toString(), is(password + System.lineSeparator()));
119 public void encode64urlTest() throws Exception {
120 CmdLine.main(new String[]{"encode64url", password});
121 String decrypted = Symm.base64url.decode(outContent.toString());
122 assertThat(decrypted, is(password));
126 public void decode64urlTest() throws Exception {
127 String encrypted = Symm.base64url.encode(password);
128 CmdLine.main(new String[]{"decode64url", encrypted});
129 assertThat(outContent.toString(), is(password + System.lineSeparator()));
133 public void md5Test() throws Exception {
134 CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
135 assertThat(outContent.toString(), is(quickBrownFoxMD5 + System.lineSeparator()));
139 public void sha256Test() throws Exception {
140 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
141 assertThat(outContent.toString(), is(quickBrownFoxSHA256 + System.lineSeparator()));
144 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
145 String hash1 = outContent.toString();
148 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
149 String hash2 = outContent.toString();
152 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
153 String hash3 = outContent.toString();
155 assertThat(hash1, is(hash2));
156 assertThat(hash1, is(not(hash3)));
160 public void keygenTest() throws Exception {
161 CmdLine.main(new String[]{"keygen"});
162 assertThat(outContent.toString().length(), is(2074));
164 String filePath = "test/output_key";
165 File testDir = new File("test");
166 if (!testDir.exists()) {
169 CmdLine.main(new String[]{"keygen", filePath});
170 File keyfile = new File(filePath);
171 assertTrue(Files.isReadable(Paths.get(filePath)));
172 assertFalse(Files.isWritable(Paths.get(filePath)));
173 //assertFalse(Files.isExecutable(Paths.get(filePath)));
178 public void passgenTest() throws Exception {
179 CmdLine.main(new String[]{"passgen"});
180 String output = outContent.toString().trim();
181 assertThat(output.length(), is(24));
182 assertTrue(containsAny(output, "+!@#$%^&*(){}[]?:;,."));
183 assertTrue(containsAny(output, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
184 assertTrue(containsAny(output, "abcdefghijklmnopqrstuvwxyz"));
185 assertTrue(containsAny(output, "0123456789"));
189 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
190 output = outContent.toString().trim();
191 assertThat(output.length(), is(length));
195 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
196 output = outContent.toString().trim();
197 assertThat(output.length(), is(8));
199 // Check that the custom hasRepeats method works
200 assertTrue(hasRepeats("aa"));
201 assertTrue(hasRepeats("baa"));
202 assertTrue(hasRepeats("aab"));
203 assertTrue(hasRepeats("baab"));
204 assertFalse(hasRepeats("abc"));
205 assertFalse(hasRepeats("aba"));
207 // Run this a bunch of times for coverage
208 for (int i = 0; i < 1000; i++) {
210 CmdLine.main(new String[]{"passgen"});
211 output = outContent.toString().trim();
212 assertFalse(hasRepeats(output));
217 public void urlgenTest() throws Exception {
218 CmdLine.main(new String[]{"urlgen"});
219 String output = outContent.toString().trim();
220 assertThat(output.length(), is(24));
224 CmdLine.main(new String[]{"urlgen", String.valueOf(length)});
225 output = outContent.toString().trim();
226 assertThat(output.length(), is(5));
230 public void showHelpTest() {
231 String lineSeparator = System.lineSeparator();
233 "Usage: java -jar <this jar> ..." + lineSeparator +
234 " keygen [<keyfile>] (Generates Key on file, or Std Out)" + lineSeparator +
235 " digest [<passwd>|-i|] <keyfile> (Encrypts Password with \"keyfile\"" + lineSeparator +
236 " if passwd = -i, will read StdIn" + lineSeparator +
237 " if passwd is blank, will ask securely)" + lineSeparator +
238 " undigest <enc:...> <keyfile> (Decrypts Encoded with \"keyfile\")" + lineSeparator +
239 " passgen <digits> (Generate Password of given size)" + lineSeparator +
240 " urlgen <digits> (Generate URL field of given size)" + lineSeparator +
241 " encode64 <your text> (Encodes to Base64)" + lineSeparator +
242 " decode64 <base64 encoded text> (Decodes from Base64)" + lineSeparator +
243 " encode64url <your text> (Encodes to Base64 URL charset)" + lineSeparator +
244 " decode64url <base64url encoded text> (Decodes from Base64 URL charset)" + lineSeparator +
245 " sha256 <text> <salts(s)> (Digest String into SHA256 Hash)" + lineSeparator +
246 " md5 <text> (Digest String into MD5 Hash)" + lineSeparator;
248 CmdLine.main(new String[]{});
250 assertThat(outContent.toString(), is(expected));
253 private boolean containsAny(String str, String searchChars) {
254 for (char c : searchChars.toCharArray()) {
255 if (str.indexOf(c) >= 0) {
262 private boolean hasRepeats(String str) {
265 for (int i = 0; i < str.length(); i++) {