Create method of Logging to O/S from Container
[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.mockito.Matchers.*;
25 import static org.junit.Assert.*;
26 import static org.hamcrest.CoreMatchers.*;
27 import org.junit.*;
28 import org.mockito.*;
29 import static org.mockito.Mockito.*;
30
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.File;
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;
42
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;
47
48 public class JU_CmdLine {
49
50         @Mock
51         private OutputStream thrower;
52
53         private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
54
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";
60         private Symm symm;
61
62         @Before
63         public void setup() throws Exception {
64                 MockitoAnnotations.initMocks(this);
65
66             System.setOut(new PrintStream(outContent));
67
68                 Properties p = new Properties();
69                 p.setProperty("force_exit", "false");
70
71                 CmdLine.access = new PropAccess(p);
72                 File test = new File("test");
73                 if(test.exists()) {
74                         if(!test.isDirectory()) {
75                                 test.delete();
76                                 test.mkdirs();
77                         }
78                 } else {
79                         test.mkdirs();
80                 }
81                 
82                 File keyF= new File(test,"keyfile");
83                 if(!keyF.exists()) {
84                         FileOutputStream fos = new FileOutputStream(keyF);
85                         try {
86                                 fos.write(Symm.keygen());
87                                 fos.flush();
88                         } finally {
89                                 fos.close();
90                         }
91                 }
92                 keyfile = "test/keyfile";
93                 password = "password";
94
95                 FileInputStream fis = new FileInputStream(keyF);
96                 try {
97                         symm = Symm.obtain(fis);
98                 } finally {
99                         fis.close();
100                 }
101         }
102         
103         @After
104         public void restoreStreams() throws IOException {
105             System.setOut(System.out);
106             System.setIn(System.in);
107         }
108
109         @Test
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));
114
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));
119         }
120
121         // @Test
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");
126         // }
127
128         @Test
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));
133         }
134
135         @Test
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"));
140         }
141
142         @Test
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));
147         }
148
149         @Test
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"));
154         }
155
156         @Test
157         public void md5Test() throws Exception {
158                 CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
159                 assertThat(outContent.toString(), is(quickBrownFoxMD5 + "\n"));
160         }
161
162         @Test
163         public void sha256Test() throws Exception {
164                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
165                 assertThat(outContent.toString(), is(quickBrownFoxSHA256 + "\n"));
166
167                 outContent.reset();
168                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
169                 String hash1 = outContent.toString();
170
171                 outContent.reset();
172                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
173                 String hash2 = outContent.toString();
174
175                 outContent.reset();
176                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
177                 String hash3 = outContent.toString();
178
179                 assertThat(hash1, is(hash2));
180                 assertThat(hash1, is(not(hash3)));
181         }
182
183         @Test
184         public void keygenTest() throws Exception {
185                 CmdLine.main(new String[]{"keygen"});
186                 assertThat(outContent.toString().length(), is(2074));
187
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)));
194                 keyfile.delete();
195         }
196
197         @Test
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"));
206
207                 int length = 10;
208                 outContent.reset();
209                 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
210                 output = outContent.toString().trim();
211                 assertThat(output.length(), is(length));
212                 
213                 length = 5;
214                 outContent.reset();
215                 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
216                 output = outContent.toString().trim();
217                 assertThat(output.length(), is(8));
218
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"));
226
227                 // Run this a bunch of times for coverage
228                 for (int i = 0; i < 1000; i++) {
229                         outContent.reset();
230                         CmdLine.main(new String[]{"passgen"});
231                         output = outContent.toString().trim();
232                         assertFalse(hasRepeats(output));
233                 }
234         }
235
236         @Test
237         public void urlgenTest() throws Exception {
238                 CmdLine.main(new String[]{"urlgen"});
239                 String output = outContent.toString().trim();
240                 assertThat(output.length(), is(24));
241
242                 int length = 5;
243                 outContent.reset();
244                 CmdLine.main(new String[]{"urlgen", String.valueOf(length)});
245                 output = outContent.toString().trim();
246                 assertThat(output.length(), is(5));
247         }
248
249         @Test
250         public void showHelpTest() {
251                 String expected = 
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";
266
267                 CmdLine.main(new String[]{});
268
269                 assertThat(outContent.toString(), is(expected));
270         }
271
272         private boolean containsAny(String str, String searchChars) {
273                 for (char c : searchChars.toCharArray()) {
274                         if (str.indexOf(c) >= 0) {
275                                 return true;
276                         }
277                 }
278                 return false;
279         }
280
281         private boolean hasRepeats(String str) {
282                 int c = -1;
283                 int last;
284                 for (int i = 0; i < str.length(); i++) {
285                         last = c;
286                         c = str.charAt(i);
287                         if (c == last) {
288                                 return true;
289                         }
290                 }
291                 return false;
292         }
293
294 }