Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_CmdLine.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.cadi.test;
23
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;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.File;
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;
40
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;
48
49 public class JU_CmdLine {
50
51     @Mock
52     private OutputStream thrower;
53
54     private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
55
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";
61     private Symm symm;
62
63     @Before
64     public void setup() throws Exception {
65         MockitoAnnotations.initMocks(this);
66
67         System.setOut(new PrintStream(outContent));
68
69         Properties p = new Properties();
70         p.setProperty("force_exit", "false");
71
72         CmdLine.setSystemExit(false);
73         keyfile = "src/test/resources/keyfile";
74         password = "password";
75
76         File keyF = new File("src/test/resources", "keyfile");
77         FileInputStream fis = new FileInputStream(keyF);
78         try {
79             symm = Symm.obtain(fis);
80         } finally {
81             fis.close();
82         }
83     }
84     
85     @After
86     public void restoreStreams() throws IOException {
87         System.setOut(System.out);
88         System.setIn(System.in);
89     }
90
91     @Test
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));
96
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));
101     }
102
103     @Test
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));
108     }
109
110     @Test
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()));
115     }
116
117     @Test
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));
122     }
123
124     @Test
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()));
129     }
130
131     @Test
132     public void md5Test() throws Exception {
133         CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
134         assertThat(outContent.toString(), is(quickBrownFoxMD5  + System.lineSeparator()));
135     }
136
137     @Test
138     public void sha256Test() throws Exception {
139         CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
140         assertThat(outContent.toString(), is(quickBrownFoxSHA256  + System.lineSeparator()));
141
142         outContent.reset();
143         CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
144         String hash1 = outContent.toString();
145
146         outContent.reset();
147         CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
148         String hash2 = outContent.toString();
149
150         outContent.reset();
151         CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
152         String hash3 = outContent.toString();
153
154         assertThat(hash1, is(hash2));
155         assertThat(hash1, is(not(hash3)));
156     }
157
158     @Test
159     public void keygenTest() throws Exception {
160         CmdLine.main(new String[]{"keygen"});
161         assertThat(outContent.toString().length(), is(2074));
162
163         String filePath = "test/output_key";
164         File testDir = new File("test");
165         if(!testDir.exists()) {
166             testDir.mkdirs();
167         }
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)));
173         keyfile.delete();
174     }
175
176     @Test
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"));
185
186         int length = 10;
187         outContent.reset();
188         CmdLine.main(new String[]{"passgen", String.valueOf(length)});
189         output = outContent.toString().trim();
190         assertThat(output.length(), is(length));
191         
192         length = 5;
193         outContent.reset();
194         CmdLine.main(new String[]{"passgen", String.valueOf(length)});
195         output = outContent.toString().trim();
196         assertThat(output.length(), is(8));
197
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"));
205
206         // Run this a bunch of times for coverage
207         for (int i = 0; i < 1000; i++) {
208             outContent.reset();
209             CmdLine.main(new String[]{"passgen"});
210             output = outContent.toString().trim();
211             assertFalse(hasRepeats(output));
212         }
213     }
214
215     @Test
216     public void urlgenTest() throws Exception {
217         CmdLine.main(new String[]{"urlgen"});
218         String output = outContent.toString().trim();
219         assertThat(output.length(), is(24));
220
221         int length = 5;
222         outContent.reset();
223         CmdLine.main(new String[]{"urlgen", String.valueOf(length)});
224         output = outContent.toString().trim();
225         assertThat(output.length(), is(5));
226     }
227
228     @Test
229     public void showHelpTest() {
230         String lineSeparator = System.lineSeparator();
231         String expected =
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;
245
246         CmdLine.main(new String[]{});
247
248         assertThat(outContent.toString(), is(expected));
249     }
250
251     private boolean containsAny(String str, String searchChars) {
252         for (char c : searchChars.toCharArray()) {
253             if (str.indexOf(c) >= 0) {
254                 return true;
255             }
256         }
257         return false;
258     }
259
260     private boolean hasRepeats(String str) {
261         int c = -1;
262         int last;
263         for (int i = 0; i < str.length(); i++) {
264             last = c;
265             c = str.charAt(i);
266             if (c == last) {
267                 return true;
268             }
269         }
270         return false;
271     }
272
273 }