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