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.FileOutputStream;
36 import java.io.IOException;
37 import java.io.OutputStream;
38 import java.io.PrintStream;
39 import java.nio.file.Files;
40 import java.nio.file.Paths;
41 import java.util.Properties;
43 import org.onap.aaf.cadi.CmdLine;
44 import org.onap.aaf.cadi.PropAccess;
45 import org.onap.aaf.cadi.Symm;
46 import org.onap.aaf.cadi.util.Chmod;
48 public class JU_CmdLine {
51 private OutputStream thrower;
53 private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
55 private String password;
56 private String keyfile;
57 private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog";
58 private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
59 private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
63 public void setup() throws Exception {
64 MockitoAnnotations.initMocks(this);
66 System.setOut(new PrintStream(outContent));
68 Properties p = new Properties();
69 p.setProperty("force_exit", "false");
71 CmdLine.access = new PropAccess(p);
72 File test = new File("test");
74 if(!test.isDirectory()) {
82 File keyF= new File(test,"keyfile");
84 FileOutputStream fos = new FileOutputStream(keyF);
86 fos.write(Symm.keygen());
92 keyfile = "test/keyfile";
93 password = "password";
95 FileInputStream fis = new FileInputStream(keyF);
97 symm = Symm.obtain(fis);
104 public void restoreStreams() throws IOException {
105 System.setOut(System.out);
106 System.setIn(System.in);
110 public void digestTest() throws Exception {
111 CmdLine.main(new String[]{"digest", password, keyfile});
112 String decrypted = symm.depass(outContent.toString());
113 assertThat(decrypted, is(password));
115 System.setIn(new ByteArrayInputStream(password.getBytes()));
116 CmdLine.main(new String[]{"digest", "-i", keyfile});
117 decrypted = symm.depass(outContent.toString());
118 assertThat(decrypted, is(password));
122 // public void regurgitateTest() {
123 // // TODO: We may still want to remove the regurgitate functionality
124 // // from the CmdLine - Ian
125 // fail("Tests not yet implemented");
129 public void encode64Test() throws Exception {
130 CmdLine.main(new String[]{"encode64", password});
131 String decrypted = Symm.base64.decode(outContent.toString());
132 assertThat(decrypted, is(password));
136 public void decode64Test() throws Exception {
137 String encrypted = Symm.base64.encode(password);
138 CmdLine.main(new String[]{"decode64", encrypted});
139 assertThat(outContent.toString(), is(password + "\n"));
143 public void encode64urlTest() throws Exception {
144 CmdLine.main(new String[]{"encode64url", password});
145 String decrypted = Symm.base64url.decode(outContent.toString());
146 assertThat(decrypted, is(password));
150 public void decode64urlTest() throws Exception {
151 String encrypted = Symm.base64url.encode(password);
152 CmdLine.main(new String[]{"decode64url", encrypted});
153 assertThat(outContent.toString(), is(password + "\n"));
157 public void md5Test() throws Exception {
158 CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
159 assertThat(outContent.toString(), is(quickBrownFoxMD5 + "\n"));
163 public void sha256Test() throws Exception {
164 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
165 assertThat(outContent.toString(), is(quickBrownFoxSHA256 + "\n"));
168 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
169 String hash1 = outContent.toString();
172 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
173 String hash2 = outContent.toString();
176 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
177 String hash3 = outContent.toString();
179 assertThat(hash1, is(hash2));
180 assertThat(hash1, is(not(hash3)));
184 public void keygenTest() throws Exception {
185 CmdLine.main(new String[]{"keygen"});
186 assertThat(outContent.toString().length(), is(2074));
188 String filePath = "test/output_key";
189 CmdLine.main(new String[]{"keygen", filePath});
190 File keyfile = new File(filePath);
191 assertTrue(Files.isReadable(Paths.get(filePath)));
192 assertFalse(Files.isWritable(Paths.get(filePath)));
193 assertFalse(Files.isExecutable(Paths.get(filePath)));
198 public void passgenTest() throws Exception {
199 CmdLine.main(new String[]{"passgen"});
200 String output = outContent.toString().trim();
201 assertThat(output.length(), is(24));
202 assertTrue(containsAny(output, "+!@#$%^&*(){}[]?:;,."));
203 assertTrue(containsAny(output, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
204 assertTrue(containsAny(output, "abcdefghijklmnopqrstuvwxyz"));
205 assertTrue(containsAny(output, "0123456789"));
209 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
210 output = outContent.toString().trim();
211 assertThat(output.length(), is(length));
215 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
216 output = outContent.toString().trim();
217 assertThat(output.length(), is(8));
219 // Check that the custom hasRepeats method works
220 assertTrue(hasRepeats("aa"));
221 assertTrue(hasRepeats("baa"));
222 assertTrue(hasRepeats("aab"));
223 assertTrue(hasRepeats("baab"));
224 assertFalse(hasRepeats("abc"));
225 assertFalse(hasRepeats("aba"));
227 // Run this a bunch of times for coverage
228 for (int i = 0; i < 1000; i++) {
230 CmdLine.main(new String[]{"passgen"});
231 output = outContent.toString().trim();
232 assertFalse(hasRepeats(output));
237 public void urlgenTest() throws Exception {
238 CmdLine.main(new String[]{"urlgen"});
239 String output = outContent.toString().trim();
240 assertThat(output.length(), is(24));
244 CmdLine.main(new String[]{"urlgen", String.valueOf(length)});
245 output = outContent.toString().trim();
246 assertThat(output.length(), is(5));
250 public void showHelpTest() {
252 "Usage: java -jar <this jar> ...\n" +
253 " keygen [<keyfile>] (Generates Key on file, or Std Out)\n" +
254 " digest [<passwd>|-i|] <keyfile> (Encrypts Password with \"keyfile\"\n" +
255 " if passwd = -i, will read StdIin\n" +
256 " if passwd is blank, will ask securely)\n" +
257 " passgen <digits> (Generate Password of given size)\n" +
258 " urlgen <digits> (Generate URL field of given size)\n" +
259 " csptest (Tests for CSP compatibility)\n" +
260 " encode64 <your text> (Encodes to Base64)\n" +
261 " decode64 <base64 encoded text> (Decodes from Base64)\n" +
262 " encode64url <your text> (Encodes to Base64 URL charset)\n" +
263 " decode64url <base64url encoded text> (Decodes from Base64 URL charset)\n" +
264 " sha256 <text> <salts(s)> (Digest String into SHA256 Hash)\n" +
265 " md5 <text> (Digest String into MD5 Hash)\n";
267 CmdLine.main(new String[]{});
269 assertThat(outContent.toString(), is(expected));
272 private boolean containsAny(String str, String searchChars) {
273 for (char c : searchChars.toCharArray()) {
274 if (str.indexOf(c) >= 0) {
281 private boolean hasRepeats(String str) {
284 for (int i = 0; i < str.length(); i++) {