1 /*******************************************************************************
2 * ============LICENSE_START====================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 ******************************************************************************/
22 package org.onap.aaf.cadi.cm.test;
24 import static org.junit.Assert.*;
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.mockito.Mockito.*;
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;
36 import javax.crypto.Cipher;
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;
45 public class JU_Factory {
51 TimeTaken timeTakenMock;
54 LogTarget logTargetMock;
58 MockitoAnnotations.initMocks(this);
60 when(transMock.start(anyString(), anyInt())).thenReturn(timeTakenMock);
61 when(transMock.debug()).thenReturn(logTargetMock);
65 public void generateKeyPairTest() throws Exception {
66 String message = "The quick brown fox jumps over the lazy dog.";
68 Cipher encryptor = Cipher.getInstance(Factory.KEY_ALGO);
69 Cipher decryptor = Cipher.getInstance(Factory.KEY_ALGO);
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));
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));
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));
96 public void keyToStringTest() throws IOException {
97 KeyPair kp = Factory.generateKeyPair(transMock);
99 String publicKeyString = Factory.toString(transMock, kp.getPublic());
100 String privateKeyString = Factory.toString(transMock, kp.getPrivate());
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-----"));
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-----"));