Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_Symm.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.hamcrest.CoreMatchers.*;
25 import static org.junit.Assert.*;
26 import static org.mockito.Mockito.*;
27 import java.lang.reflect.*;
28 import org.junit.*;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.PrintStream;
33 import java.util.Arrays;
34
35 import org.onap.aaf.cadi.CadiException;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.Symm;
38
39 public class JU_Symm {
40     private Symm defaultSymm;
41
42     private ByteArrayOutputStream outStream;
43
44     @Before
45     public void setup() throws Exception {
46         defaultSymm = new Symm(
47                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()
48                 ,76, "Use default!" ,true);
49         outStream = new ByteArrayOutputStream();
50         System.setOut(new PrintStream(outStream));
51     }
52
53     @After
54     public void tearDown() {
55         System.setOut(System.out);
56     }
57
58     @Test
59     public void constructorTest() throws Exception {
60         Symm myCustomSymm = new Symm(
61             "ACEGIKMOQSUWYacegikmoqsuwy02468+/".toCharArray(), 76, "Default", true);
62         Field convert_field = Symm.class.getDeclaredField("convert");
63         convert_field.setAccessible(true);
64
65         Class<?> Unordered_class = Class.forName("org.onap.aaf.cadi.Symm$Unordered");
66         assertThat(convert_field.get(myCustomSymm), instanceOf(Unordered_class));
67     }
68
69     @SuppressWarnings("unused")
70     @Test
71     public void copyTest() throws Exception {
72         Symm copy = Symm.base64.copy(76);
73     }
74
75     @SuppressWarnings("deprecation")
76     @Test
77     public void deprecatedTest() {
78         assertEquals(Symm.base64(), Symm.base64);
79         assertEquals(Symm.base64noSplit(), Symm.base64noSplit);
80         assertEquals(Symm.base64url(), Symm.base64url);
81         assertEquals(Symm.baseCrypt(), Symm.encrypt);
82     }
83
84     @Test
85     public void encodeDecodeStringTest() throws Exception {
86         String orig = "hello";
87         String b64encrypted = Symm.base64.encode(orig);
88         assertEquals(Symm.base64.decode(b64encrypted), orig);
89
90         String defaultEnrypted = defaultSymm.encode(orig);
91         assertEquals(defaultSymm.decode(defaultEnrypted), orig);
92     }
93
94     @Test
95     public void encodeDecodeByteArrayTest() throws Exception {
96         String orig = "hello";
97         byte[] b64encrypted = Symm.base64.encode(orig.getBytes());
98         assertEquals(new String(Symm.base64.decode(b64encrypted)), orig);
99
100         byte[] empty = null;
101         assertTrue(Arrays.equals(Symm.base64.encode(empty), new byte[0]));
102     }
103
104     @Test
105     public void encodeDecodeStringToStreamTest() throws Exception {
106         String orig = "I'm a password, really";
107         String b64encrypted;
108         String output;
109         
110         ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
111         Symm.base64.encode(orig, baosEncrypt);
112         b64encrypted = new String(baosEncrypt.toByteArray());
113
114         ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
115         Symm.base64.decode(b64encrypted, baosDecrypt);
116         output = new String(baosDecrypt.toByteArray());
117
118         assertEquals(orig, output);
119     }
120
121     @Test
122     public void encryptDecryptStreamWithPrefixTest() throws Exception {
123         String orig = "I'm a password, really";
124         byte[] b64encrypted;
125         String output;
126
127         byte[] prefix = "enc:".getBytes();
128         
129         ByteArrayInputStream baisEncrypt = new ByteArrayInputStream(orig.getBytes());
130         ByteArrayOutputStream baosEncrypt = new ByteArrayOutputStream();
131         Symm.base64.encode(baisEncrypt, baosEncrypt, prefix);
132
133         b64encrypted = baosEncrypt.toByteArray();
134
135         ByteArrayInputStream baisDecrypt = new ByteArrayInputStream(b64encrypted);
136         ByteArrayOutputStream baosDecrypt = new ByteArrayOutputStream();
137         Symm.base64.decode(baisDecrypt, baosDecrypt, prefix.length);
138
139         output = new String(baosDecrypt.toByteArray());
140         assertEquals(orig, output);
141     }
142
143     @Test
144     public void randomGenTest() {
145         // Ian - There really isn't a great way to test for randomness...
146         String prev = null;
147         for (int i = 0; i < 10; i++) {
148             String current = Symm.randomGen(100);
149             if (current.equals(prev)) {
150                 fail("I don't know how, but you generated the exact same random string twice in a row");
151             }
152             prev = current;
153         }
154         assertTrue(true);
155     }
156
157     @Test
158     public void obtainTest() throws Exception {
159         Symm symm = Symm.base64.obtain();
160
161         String orig ="Another Password, please";
162         String encrypted = symm.enpass(orig);
163         String decrypted = symm.depass(encrypted);
164         assertEquals(orig, decrypted);
165     }
166
167     @Test
168     public void InputStreamObtainTest() throws Exception {
169         byte[] keygen = Symm.keygen();
170
171         Symm symm = Symm.obtain(new ByteArrayInputStream(keygen));
172
173         String orig ="Another Password, please";
174         String encrypted = symm.enpass(orig);
175         String decrypted = symm.depass(encrypted);
176         assertEquals(orig, decrypted);
177     }
178
179     @Test
180     public void StringObtainTest() throws Exception {
181         byte[] keygen = Symm.keygen();
182
183         Symm symm = Symm.obtain(new String(keygen));
184
185         String orig ="Another Password, please";
186         String encrypted = symm.enpass(orig);
187         String decrypted = symm.depass(encrypted);
188         assertEquals(orig, decrypted);
189     }
190
191     @Test
192     public void AccessObtainTest() throws Exception {
193         PropAccess pa = new PropAccess("cadi_keyfile=src/test/resources/keyfile");
194         Symm symm = Symm.obtain(pa);
195         String orig ="Another Password, please";
196         String encrypted = symm.enpass(orig);
197         String decrypted = symm.depass(encrypted);
198         assertEquals(orig, decrypted);
199
200         try {
201             PropAccess badPa = mock(PropAccess.class);
202             when(badPa.getProperty("cadi_keyfile", null)).thenReturn("not_a_real_file.txt");
203             symm = Symm.obtain(badPa);
204             fail("Should have thrown an exception");
205         } catch (CadiException e) {
206             assertTrue(e.getMessage().contains("ERROR: "));
207             assertTrue(e.getMessage().contains("not_a_real_file.txt"));
208             assertTrue(e.getMessage().contains(" does not exist!"));
209         }
210     }
211     
212 }