Support Multiple Realms for DefaultOrg
[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                 File testDir = new File("test");
167                 if(!testDir.exists()) {
168                         testDir.mkdirs();
169                 }
170                 CmdLine.main(new String[]{"keygen", filePath});
171                 File keyfile = new File(filePath);
172                 assertTrue(Files.isReadable(Paths.get(filePath)));
173                 assertFalse(Files.isWritable(Paths.get(filePath)));
174                 assertFalse(Files.isExecutable(Paths.get(filePath)));
175                 keyfile.delete();
176         }
177
178         @Test
179         public void passgenTest() throws Exception {
180                 CmdLine.main(new String[]{"passgen"});
181                 String output = outContent.toString().trim();
182                 assertThat(output.length(), is(24));
183                 assertTrue(containsAny(output, "+!@#$%^&*(){}[]?:;,."));
184                 assertTrue(containsAny(output, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
185                 assertTrue(containsAny(output, "abcdefghijklmnopqrstuvwxyz"));
186                 assertTrue(containsAny(output, "0123456789"));
187
188                 int length = 10;
189                 outContent.reset();
190                 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
191                 output = outContent.toString().trim();
192                 assertThat(output.length(), is(length));
193                 
194                 length = 5;
195                 outContent.reset();
196                 CmdLine.main(new String[]{"passgen", String.valueOf(length)});
197                 output = outContent.toString().trim();
198                 assertThat(output.length(), is(8));
199
200                 // Check that the custom hasRepeats method works
201                 assertTrue(hasRepeats("aa"));
202                 assertTrue(hasRepeats("baa"));
203                 assertTrue(hasRepeats("aab"));
204                 assertTrue(hasRepeats("baab"));
205                 assertFalse(hasRepeats("abc"));
206                 assertFalse(hasRepeats("aba"));
207
208                 // Run this a bunch of times for coverage
209                 for (int i = 0; i < 1000; i++) {
210                         outContent.reset();
211                         CmdLine.main(new String[]{"passgen"});
212                         output = outContent.toString().trim();
213                         assertFalse(hasRepeats(output));
214                 }
215         }
216
217         @Test
218         public void urlgenTest() throws Exception {
219                 CmdLine.main(new String[]{"urlgen"});
220                 String output = outContent.toString().trim();
221                 assertThat(output.length(), is(24));
222
223                 int length = 5;
224                 outContent.reset();
225                 CmdLine.main(new String[]{"urlgen", String.valueOf(length)});
226                 output = outContent.toString().trim();
227                 assertThat(output.length(), is(5));
228         }
229
230         @Test
231         public void showHelpTest() {
232                 String expected = 
233                         "Usage: java -jar <this jar> ...\n" +
234                         "  keygen [<keyfile>]                     (Generates Key on file, or Std Out)\n" +
235                         "  digest [<passwd>|-i|] <keyfile>        (Encrypts Password with \"keyfile\"\n" +
236                         "                                          if passwd = -i, will read StdIin\n" +
237                         "                                          if passwd is blank, will ask securely)\n" +
238                         "  passgen <digits>                       (Generate Password of given size)\n" +
239                         "  urlgen <digits>                        (Generate URL field of given size)\n" +
240                         "  csptest                                (Tests for CSP compatibility)\n" +
241                         "  encode64 <your text>                   (Encodes to Base64)\n" +
242                         "  decode64 <base64 encoded text>         (Decodes from Base64)\n" +
243                         "  encode64url <your text>                (Encodes to Base64 URL charset)\n" +
244                         "  decode64url <base64url encoded text>   (Decodes from Base64 URL charset)\n" +
245                         "  sha256 <text> <salts(s)>               (Digest String into SHA256 Hash)\n" +
246                         "  md5 <text>                             (Digest String into MD5 Hash)\n";
247
248                 CmdLine.main(new String[]{});
249
250                 assertThat(outContent.toString(), is(expected));
251         }
252
253         private boolean containsAny(String str, String searchChars) {
254                 for (char c : searchChars.toCharArray()) {
255                         if (str.indexOf(c) >= 0) {
256                                 return true;
257                         }
258                 }
259                 return false;
260         }
261
262         private boolean hasRepeats(String str) {
263                 int c = -1;
264                 int last;
265                 for (int i = 0; i < str.length(); i++) {
266                         last = c;
267                         c = str.charAt(i);
268                         if (c == last) {
269                                 return true;
270                         }
271                 }
272                 return false;
273         }
274
275 }