CommonLibrary(util/rest-client) code upload.
[vfc/nfvo/wfengine.git] / CommonLibrary / common-util / src / test / java / org / openo / baseservice / encrypt / cbb / impl / AesCipherTest.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.baseservice.encrypt.cbb.impl;
17
18 import java.security.NoSuchAlgorithmException;
19 import java.security.spec.InvalidKeySpecException;
20
21 import javax.crypto.Cipher;
22 import javax.crypto.SecretKeyFactory;
23
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.openo.baseservice.encrypt.cbb.CipherCreator;
30 import org.openo.baseservice.encrypt.cbb.inf.AbstractCipher;
31
32 import junit.framework.Assert;
33 import mockit.Mocked;
34 import mockit.NonStrictExpectations;
35
36 /**
37  * <br/>
38  * <p>
39  * </p>
40  * 
41  * @author
42  * @version SDNO 0.5 02-Jun-2016
43  */
44 public class AesCipherTest {
45
46     /**
47      * <br/>
48      * 
49      * @throws java.lang.Exception
50      * @since SDNO 0.5
51      */
52     @BeforeClass
53     public static void setUpBeforeClass() throws Exception {
54         CipherCreator.instance().setFactory(new AesCipherFactory());
55     }
56
57     /**
58      * <br/>
59      * 
60      * @throws java.lang.Exception
61      * @since SDNO 0.5
62      */
63     @AfterClass
64     public static void tearDownAfterClass() throws Exception {
65     }
66
67     /**
68      * <br/>
69      * 
70      * @throws java.lang.Exception
71      * @since SDNO 0.5
72      */
73     @Before
74     public void setUp() throws Exception {
75     }
76
77     /**
78      * <br/>
79      * 
80      * @throws java.lang.Exception
81      * @since SDNO 0.5
82      */
83     @After
84     public void tearDown() throws Exception {
85     }
86
87     /**
88      * Test method for
89      * {@link org.openo.baseservice.encrypt.cbb.impl.AesCipher#encrypt(java.lang.String)}.
90      */
91     @Test
92     public void testEncrypt() {
93         final AbstractCipher cipherManager = CipherCreator.instance().create();
94         final String encrypted = cipherManager.encrypt("test-encrypt");
95         final String decrypted = cipherManager.decrypt(encrypted);
96
97         Assert.assertEquals("test-encrypt", decrypted);
98     }
99
100     @Test
101     public void testEncryptException() throws Exception {
102         new NonStrictExpectations() {
103
104             @Mocked
105             Cipher cipher;
106
107             {
108                 cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
109                 result = new InvalidKeySpecException();
110             }
111         };
112         final AbstractCipher cipherManager = CipherCreator.instance().create();
113         final String encrypted = cipherManager.encrypt("test-encrypt");
114
115         Assert.assertEquals(null, encrypted);
116     }
117
118     /**
119      * Test method for
120      * {@link org.openo.baseservice.encrypt.cbb.impl.AesCipher#decrypt(java.lang.String)}.
121      */
122     @Test
123     public void testDecrypt() {
124         final AbstractCipher cipherManager = CipherCreator.instance().create();
125         final String encrypted = cipherManager.encrypt("test-encrypt");
126         final String decrypted = cipherManager.decrypt(encrypted);
127
128         Assert.assertEquals("test-encrypt", decrypted);
129     }
130
131     @Test
132     public void testDecryptNull() {
133         final AbstractCipher cipherManager = CipherCreator.instance().create();
134         String decrypted = cipherManager.decrypt(null);
135         Assert.assertEquals(null, decrypted);
136
137         decrypted = cipherManager.decrypt("");
138
139         Assert.assertEquals(null, decrypted);
140     }
141
142     /**
143      * Test method for
144      * {@link
145      * org.openo.baseservice.encrypt.cbb.impl.AesCipher#CipherManagerImpl(java.lang.String)}
146      * .
147      */
148     @Test
149     public void testCipherManagerImplString() {
150         final AbstractCipher cipherManager = CipherCreator.instance().create("secret-key");
151         final String encrypted = cipherManager.encrypt("test-encrypt");
152         final String decrypted = cipherManager.decrypt(encrypted);
153
154         Assert.assertEquals("test-encrypt", decrypted);
155     }
156
157     /**
158      * <br/>
159      * 
160      * @since SDNO 0.5
161      */
162     @Test
163     public void testCipherManagerImplStringDiffKey() {
164         final String encrypted = CipherCreator.instance().create("secret-key").encrypt("test-encrypt");
165         final String decrypted = CipherCreator.instance().create("wrong-key").decrypt(encrypted);
166
167         Assert.assertNotSame("test-encrypt", decrypted);
168
169         final String decrypt = CipherCreator.instance().create("secret-key").decrypt(encrypted);
170         Assert.assertEquals("test-encrypt", decrypt);
171     }
172     
173     @Test
174     public void testCreateSecretKeyNoSuchAlgorithmException() throws Exception {
175         new NonStrictExpectations() {
176
177             @Mocked
178             SecretKeyFactory keyFactory;
179
180             {
181                 keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
182                 result = new NoSuchAlgorithmException();
183             }
184         };
185
186         final AbstractCipher cipherManager = CipherCreator.instance().create("secret-key");
187         final String encrypted = cipherManager.encrypt("test-encrypt");
188         Assert.assertEquals(encrypted, encrypted);
189
190     }
191
192     @Test
193     public void testCreateSecretKeyInvalidKeySpecException() throws Exception {
194         new NonStrictExpectations() {
195
196             @Mocked
197             SecretKeyFactory keyFactory;
198
199             {
200                 keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
201                 result = new InvalidKeySpecException();
202             }
203         };
204
205         final AbstractCipher cipherManager = CipherCreator.instance().create("secret-key");
206         final String decrypted = cipherManager.decrypt("test-encrypt");
207         Assert.assertEquals(decrypted, null);
208
209     }
210 }