Fix the remaining failing tests in Cadi
[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.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;
41
42 import org.onap.aaf.cadi.CmdLine;
43 import org.onap.aaf.cadi.PropAccess;
44 import org.onap.aaf.cadi.Symm;
45
46 public class JU_CmdLine {
47
48         @Mock
49         private OutputStream thrower;
50
51         private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
52
53         private String password;
54         private String keyfile;
55         private String quickBrownFoxPlain = "The quick brown fox jumps over the lazy dog";
56         private String quickBrownFoxMD5 = "0x9e107d9d372bb6826bd81d3542a419d6";
57         private String quickBrownFoxSHA256 = "0xd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592";
58         private Symm symm;
59
60         @Before
61         public void setup() throws Exception {
62                 MockitoAnnotations.initMocks(this);
63
64             System.setOut(new PrintStream(outContent));
65
66                 Properties p = new Properties();
67                 p.setProperty("force_exit", "false");
68
69                 CmdLine.access = new PropAccess(p);
70
71                 password = "password";
72                 keyfile = "test/keyfile";
73
74                 FileInputStream fis = new FileInputStream(keyfile);
75                 try {
76                         symm = Symm.obtain(fis);
77                 } finally {
78                         fis.close();
79                 }
80         }
81         
82         @After
83         public void restoreStreams() throws IOException {
84             System.setOut(System.out);
85             System.setIn(System.in);
86         }
87
88         @Test
89         public void digestTest() throws Exception {
90                 CmdLine.main(new String[]{"digest", password, keyfile});
91                 String decrypted = symm.depass(outContent.toString());
92                 assertThat(decrypted, is(password));
93
94                 System.setIn(new ByteArrayInputStream(password.getBytes()));
95                 CmdLine.main(new String[]{"digest", "-i", keyfile});
96                 decrypted = symm.depass(outContent.toString());
97                 assertThat(decrypted, is(password));
98         }
99
100         // @Test
101         // public void regurgitateTest() {
102         //      // TODO: We may still want to remove the regurgitate functionality
103         //      // from the CmdLine - Ian
104         //      fail("Tests not yet implemented");
105         // }
106
107         @Test
108         public void encode64Test() throws Exception {
109                 CmdLine.main(new String[]{"encode64", password});
110                 String decrypted = Symm.base64.decode(outContent.toString());
111                 assertThat(decrypted, is(password));
112         }
113
114         @Test
115         public void decode64Test() throws Exception {
116                 String encrypted = Symm.base64.encode(password);
117                 CmdLine.main(new String[]{"decode64", encrypted});
118                 assertThat(outContent.toString(), is(password + "\n"));
119         }
120
121         @Test
122         public void encode64urlTest() throws Exception {
123                 CmdLine.main(new String[]{"encode64url", password});
124                 String decrypted = Symm.base64url.decode(outContent.toString());
125                 assertThat(decrypted, is(password));
126         }
127
128         @Test
129         public void decode64urlTest() throws Exception {
130                 String encrypted = Symm.base64url.encode(password);
131                 CmdLine.main(new String[]{"decode64url", encrypted});
132                 assertThat(outContent.toString(), is(password + "\n"));
133         }
134
135         @Test
136         public void md5Test() throws Exception {
137                 CmdLine.main(new String[]{"md5", quickBrownFoxPlain});
138                 assertThat(outContent.toString(), is(quickBrownFoxMD5 + "\n"));
139         }
140
141         @Test
142         public void sha256Test() throws Exception {
143                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain});
144                 assertThat(outContent.toString(), is(quickBrownFoxSHA256 + "\n"));
145
146                 outContent.reset();
147                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
148                 String hash1 = outContent.toString();
149
150                 outContent.reset();
151                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "10"});
152                 String hash2 = outContent.toString();
153
154                 outContent.reset();
155                 CmdLine.main(new String[]{"sha256", quickBrownFoxPlain, "11"});
156                 String hash3 = outContent.toString();
157
158                 assertThat(hash1, is(hash2));
159                 assertThat(hash1, is(not(hash3)));
160         }
161
162         @Test
163         public void keygenTest() throws Exception {
164                 CmdLine.main(new String[]{"keygen"});
165                 assertThat(outContent.toString().length(), is(2074));
166
167                 String filePath = "test/output_key";
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 expected = 
231                         "Usage: java -jar <this jar> ...\n" +
232                         "  keygen [<keyfile>]                     (Generates Key on file, or Std Out)\n" +
233                         "  digest [<passwd>|-i|] <keyfile>        (Encrypts Password with \"keyfile\"\n" +
234                         "                                          if passwd = -i, will read StdIin\n" +
235                         "                                          if passwd is blank, will ask securely)\n" +
236                         "  passgen <digits>                       (Generate Password of given size)\n" +
237                         "  urlgen <digits>                        (Generate URL field of given size)\n" +
238                         "  csptest                                (Tests for CSP compatibility)\n" +
239                         "  encode64 <your text>                   (Encodes to Base64)\n" +
240                         "  decode64 <base64 encoded text>         (Decodes from Base64)\n" +
241                         "  encode64url <your text>                (Encodes to Base64 URL charset)\n" +
242                         "  decode64url <base64url encoded text>   (Decodes from Base64 URL charset)\n" +
243                         "  sha256 <text> <salts(s)>               (Digest String into SHA256 Hash)\n" +
244                         "  md5 <text>                             (Digest String into MD5 Hash)\n";
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 }