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