31e5d32cb59c8428c29b06939abae82e5f6f259a
[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.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import org.junit.*;
27 import org.mockito.*;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.OutputStream;
35 import java.io.PrintStream;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.util.Properties;
39
40 import org.onap.aaf.cadi.CmdLine;
41 import org.onap.aaf.cadi.PropAccess;
42 import org.onap.aaf.cadi.Symm;
43
44 public class JU_CmdLine {
45
46         @Mock
47         private OutputStream thrower;
48
49         private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
50
51         private String password;
52         private String keyfile;
53         private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog";
54         private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
55         private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
56         private Symm symm;
57
58         @Before
59         public void setup() throws Exception {
60                 MockitoAnnotations.initMocks(this);
61
62             System.setOut(new PrintStream(outContent));
63
64                 Properties p = new Properties();
65                 p.setProperty("force_exit", "false");
66
67                 CmdLine.access = new PropAccess(p);
68                 keyfile = "src/test/resources/keyfile";
69                 password = "password";
70
71                 File keyF = new File("src/test/resources", "keyfile");
72                 FileInputStream fis = new FileInputStream(keyF);
73                 try {
74                         symm = Symm.obtain(fis);
75                 } finally {
76                         fis.close();
77                 }
78         }
79         
80         @After
81         public void restoreStreams() throws IOException {
82             System.setOut(System.out);
83             System.setIn(System.in);
84         }
85
86         @Test
87         public void digestTest() throws Exception {
88                 CmdLine.main(new String[]{"digest", password, keyfile});
89                 String decrypted = symm.depass(outContent.toString());
90                 assertThat(decrypted, is(password));
91
92                 System.setIn(new ByteArrayInputStream(password.getBytes()));
93                 CmdLine.main(new String[]{"digest", "-i", keyfile});
94                 decrypted = symm.depass(outContent.toString());
95                 assertThat(decrypted, is(password));
96         }
97
98         // @Test
99         // public void regurgitateTest() {
100         //      // TODO: We may still want to remove the regurgitate functionality
101         //      // from the CmdLine - Ian
102         //      fail("Tests not yet implemented");
103         // }
104
105         @Test
106         public void encode64Test() throws Exception {
107                 CmdLine.main(new String[]{"encode64", password});
108                 String decrypted = Symm.base64.decode(outContent.toString());
109                 assertThat(decrypted, is(password));
110         }
111
112         @Test
113         public void decode64Test() throws Exception {
114                 String encrypted = Symm.base64.encode(password);
115                 CmdLine.main(new String[]{"decode64", encrypted});
116                 assertThat(outContent.toString(), is(password + "\n"));
117         }
118
119         @Test
120         public void encode64urlTest() throws Exception {
121                 CmdLine.main(new String[]{"encode64url", password});
122                 String decrypted = Symm.base64url.decode(outContent.toString());
123                 assertThat(decrypted, is(password));
124         }
125
126         @Test
127         public void decode64urlTest() throws Exception {
128                 String encrypted = Symm.base64url.encode(password);
129                 CmdLine.main(new String[]{"decode64url", encrypted});
130                 assertThat(outContent.toString(), is(password + "\n"));
131         }
132
133         @Test
134         public void md5Test() throws Exception {
135                 CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
136                 assertThat(outContent.toString(), is(quickBrownFoxMD5 + "\n"));
137         }
138
139         @Test
140         public void sha256Test() throws Exception {
141                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
142                 assertThat(outContent.toString(), is(quickBrownFoxSHA256 + "\n"));
143
144                 outContent.reset();
145                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
146                 String hash1 = outContent.toString();
147
148                 outContent.reset();
149                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
150                 String hash2 = outContent.toString();
151
152                 outContent.reset();
153                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
154                 String hash3 = outContent.toString();
155
156                 assertThat(hash1, is(hash2));
157                 assertThat(hash1, is(not(hash3)));
158         }
159
160         @Test
161         public void keygenTest() throws Exception {
162                 CmdLine.main(new String[]{"keygen"});
163                 assertThat(outContent.toString().length(), is(2074));
164
165                 String filePath = "test/output_key";
166                 CmdLine.main(new String[]{"keygen", filePath});
167                 File keyfile = new File(filePath);
168                 assertTrue(Files.isReadable(Paths.get(filePath)));
169                 assertFalse(Files.isWritable(Paths.get(filePath)));
170                 assertFalse(Files.isExecutable(Paths.get(filePath)));
171                 keyfile.delete();
172         }
173
174         @Test
175         public void passgenTest() throws Exception {
176                 CmdLine.main(new String[]{"passgen"});
177                 String output = outContent.toString().trim();
178                 assertThat(output.length(), is(24));
179                 assertTrue(containsAny(output, "+!@#$%^&*(){}[]?:;,."));
180                 assertTrue(containsAny(output, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
181                 assertTrue(containsAny(output, "abcdefghijklmnopqrstuvwxyz"));
182                 assertTrue(containsAny(output, "0123456789"));
183
184                 int length = 10;
185                 outContent.reset();
186                 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
187                 output = outContent.toString().trim();
188                 assertThat(output.length(), is(length));
189                 
190                 length = 5;
191                 outContent.reset();
192                 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
193                 output = outContent.toString().trim();
194                 assertThat(output.length(), is(8));
195
196                 // Check that the custom hasRepeats method works
197                 assertTrue(hasRepeats("aa"));
198                 assertTrue(hasRepeats("baa"));
199                 assertTrue(hasRepeats("aab"));
200                 assertTrue(hasRepeats("baab"));
201                 assertFalse(hasRepeats("abc"));
202                 assertFalse(hasRepeats("aba"));
203
204                 // Run this a bunch of times for coverage
205                 for (int i = 0; i < 1000; i++) {
206                         outContent.reset();
207                         CmdLine.main(new String[]{"passgen"});
208                         output = outContent.toString().trim();
209                         assertFalse(hasRepeats(output));
210                 }
211         }
212
213         @Test
214         public void urlgenTest() throws Exception {
215                 CmdLine.main(new String[]{"urlgen"});
216                 String output = outContent.toString().trim();
217                 assertThat(output.length(), is(24));
218
219                 int length = 5;
220                 outContent.reset();
221                 CmdLine.main(new String[]{"urlgen", String.valueOf(length)});
222                 output = outContent.toString().trim();
223                 assertThat(output.length(), is(5));
224         }
225
226         @Test
227         public void showHelpTest() {
228                 String expected = 
229                         "Usage: java -jar <this jar> ...\n" +
230                         "  keygen [<keyfile>]                     (Generates Key on file, or Std Out)\n" +
231                         "  digest [<passwd>|-i|] <keyfile>        (Encrypts Password with \"keyfile\"\n" +
232                         "                                          if passwd = -i, will read StdIin\n" +
233                         "                                          if passwd is blank, will ask securely)\n" +
234                         "  passgen <digits>                       (Generate Password of given size)\n" +
235                         "  urlgen <digits>                        (Generate URL field of given size)\n" +
236                         "  csptest                                (Tests for CSP compatibility)\n" +
237                         "  encode64 <your text>                   (Encodes to Base64)\n" +
238                         "  decode64 <base64 encoded text>         (Decodes from Base64)\n" +
239                         "  encode64url <your text>                (Encodes to Base64 URL charset)\n" +
240                         "  decode64url <base64url encoded text>   (Decodes from Base64 URL charset)\n" +
241                         "  sha256 <text> <salts(s)>               (Digest String into SHA256 Hash)\n" +
242                         "  md5 <text>                             (Digest String into MD5 Hash)\n";
243
244                 CmdLine.main(new String[]{});
245
246                 assertThat(outContent.toString(), is(expected));
247         }
248
249         private boolean containsAny(String str, String searchChars) {
250                 for (char c : searchChars.toCharArray()) {
251                         if (str.indexOf(c) >= 0) {
252                                 return true;
253                         }
254                 }
255                 return false;
256         }
257
258         private boolean hasRepeats(String str) {
259                 int c = -1;
260                 int last;
261                 for (int i = 0; i < str.length(); i++) {
262                         last = c;
263                         c = str.charAt(i);
264                         if (c == last) {
265                                 return true;
266                         }
267                 }
268                 return false;
269         }
270
271 }