801259d4c11d2433caae96e79bf934bddddcc7e8
[aaf/authz.git] / cadi / core / src / test / java / org / onap / aaf / cadi / test / JU_Base64.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
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.security.SecureRandom;
31
32 import org.junit.Test;
33 import org.onap.aaf.cadi.Symm;
34 import org.onap.aaf.cadi.config.Config;
35
36 public class JU_Base64 {
37         private static final String encoding = "Man is distinguished, not only by his reason, but by this singular " +
38                         "passion from other animals, which is a lust of the mind, that by a " +
39                         "perseverance of delight in the continued and indefatigable generation of " +
40                         "knowledge, exceeds the short vehemence of any carnal pleasure.";
41
42         private static final String expected =
43                         "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n" +
44                         "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n" +
45                         "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" +
46                         "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n" +
47                         "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
48
49         @Test
50         public void test() throws Exception {
51                 // Test with different Padding
52                 assertEncoded("leas",     "bGVhcw==");
53                 assertEncoded("leasu",    "bGVhc3U=");
54                 assertEncoded("leasur",   "bGVhc3Vy");
55                 assertEncoded("leasure",  "bGVhc3VyZQ==");
56                 assertEncoded("leasure.", "bGVhc3VyZS4=");
57
58                 // Test with line ends
59                 assertEncoded(encoding, expected);
60         }
61
62         @Test
63         public void symmetric() throws IOException {
64                 String symmetric = new String(Symm.keygen());
65                 Symm bsym = Symm.obtain(symmetric);
66                 String result = bsym.encode(encoding);
67                 assertThat(bsym.decode(result), is(encoding));
68
69                 char[] manipulate = symmetric.toCharArray();
70                 int spot = new SecureRandom().nextInt(manipulate.length);
71                 manipulate[spot]|=0xFF;
72                 String newsymmetric = new String(manipulate);
73                 assertThat(symmetric, is(not(newsymmetric)));
74                 try {
75                         bsym = Symm.obtain(newsymmetric);
76                         result = bsym.decode(result);
77                         assertThat(result, is(encoding));
78                 } catch (IOException e) {
79                         // this is what we want to see if key wrong
80                 }
81         }
82
83         private void assertEncoded(String toEncode, String expected) throws IOException {
84                 String result = Symm.base64.encode(toEncode);
85                 assertThat(result, is(expected));
86                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
87                 Symm.base64.decode(new ByteArrayInputStream(result.getBytes()), baos);
88                 result = baos.toString(Config.UTF_8);
89                 assertThat(result, is(toEncode));
90         }
91
92 }