Improve coverage of cadi-aaf
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / cm / test / JU_Factory.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.cm.test;
23
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
27 import org.junit.*;
28 import org.mockito.*;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import java.security.KeyPair;
34 import java.security.PublicKey;
35
36 import javax.crypto.Cipher;
37
38 import org.onap.aaf.cadi.cm.Factory;
39 import org.onap.aaf.cadi.cm.Factory.StripperInputStream;
40 import org.onap.aaf.misc.env.Env;
41 import org.onap.aaf.misc.env.LogTarget;
42 import org.onap.aaf.misc.env.TimeTaken;
43 import org.onap.aaf.misc.env.Trans;
44
45 public class JU_Factory {
46
47         @Mock
48         Trans transMock;
49
50         @Mock
51         TimeTaken timeTakenMock;
52
53         @Mock
54         LogTarget logTargetMock;
55
56         @Before
57         public void setup() {
58                 MockitoAnnotations.initMocks(this);
59
60                 when(transMock.start(anyString(), anyInt())).thenReturn(timeTakenMock);
61                 when(transMock.debug()).thenReturn(logTargetMock);
62         }
63
64         @Test
65         public void generateKeyPairTest() throws Exception {
66                 String message = "The quick brown fox jumps over the lazy dog.";
67
68                 Cipher encryptor = Cipher.getInstance(Factory.KEY_ALGO);
69                 Cipher decryptor = Cipher.getInstance(Factory.KEY_ALGO);
70
71                 KeyPair kp1 = Factory.generateKeyPair(transMock);
72                 encryptor.init(Cipher.ENCRYPT_MODE, kp1.getPublic());
73                 decryptor.init(Cipher.DECRYPT_MODE, kp1.getPrivate());
74                 byte[] encrypedMessage1 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8));
75                 String output1 = new String(decryptor.doFinal(encrypedMessage1));
76                 assertThat(output1, is(message));
77
78                 // coverage
79                 when(transMock.start("Generate KeyPair", Env.SUB)).thenReturn(null);
80                 KeyPair kp2 = Factory.generateKeyPair(transMock);
81                 encryptor.init(Cipher.ENCRYPT_MODE, kp2.getPublic());
82                 decryptor.init(Cipher.DECRYPT_MODE, kp2.getPrivate());
83                 byte[] encrypedMessage2 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8));
84                 String output2 = new String(decryptor.doFinal(encrypedMessage2));
85                 assertThat(output2, is(message));
86
87                 KeyPair kp3 = Factory.generateKeyPair(null);
88                 encryptor.init(Cipher.ENCRYPT_MODE, kp3.getPublic());
89                 decryptor.init(Cipher.DECRYPT_MODE, kp3.getPrivate());
90                 byte[] encrypedMessage3 = encryptor.doFinal(message.getBytes(StandardCharsets.UTF_8));
91                 String output3 = new String(decryptor.doFinal(encrypedMessage3));
92                 assertThat(output3, is(message));
93         }
94
95         @Test
96         public void keyToStringTest() throws IOException {
97                 KeyPair kp = Factory.generateKeyPair(transMock);
98
99                 String publicKeyString = Factory.toString(transMock, kp.getPublic());
100                 String privateKeyString = Factory.toString(transMock, kp.getPrivate());
101
102                 String[] publicKeyLines = publicKeyString.split("\n", 0);
103                 assertThat(publicKeyLines.length, is(9));
104                 assertThat(publicKeyLines[0], is("-----BEGIN PUBLIC KEY-----"));
105                 assertThat(publicKeyLines[8], is("-----END PUBLIC KEY-----"));
106
107                 String[] privateKeyLines = privateKeyString.split("\n", 0);
108                 assertThat(privateKeyLines.length, is(28));
109                 assertThat(privateKeyLines[0], is("-----BEGIN PRIVATE KEY-----"));
110                 assertThat(privateKeyLines[27], is("-----END PRIVATE KEY-----"));
111         }
112 }