2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.baseservice.encrypt.cbb.impl;
18 import java.security.NoSuchAlgorithmException;
19 import java.security.spec.InvalidKeySpecException;
21 import javax.crypto.Cipher;
22 import javax.crypto.SecretKeyFactory;
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;
32 import junit.framework.Assert;
34 import mockit.NonStrictExpectations;
42 * @version 02-Jun-2016
44 public class AesCipherTest {
49 * @throws java.lang.Exception
53 public static void setUpBeforeClass() throws Exception {
54 CipherCreator.instance().setFactory(new AesCipherFactory());
60 * @throws java.lang.Exception
64 public static void tearDownAfterClass() throws Exception {
70 * @throws java.lang.Exception
74 public void setUp() throws Exception {
80 * @throws java.lang.Exception
84 public void tearDown() throws Exception {
89 * {@link org.openo.baseservice.encrypt.cbb.impl.AesCipher#encrypt(java.lang.String)}.
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);
97 Assert.assertEquals("test-encrypt", decrypted);
101 public void testEncryptException() throws Exception {
102 new NonStrictExpectations() {
108 cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
109 result = new InvalidKeySpecException();
112 final AbstractCipher cipherManager = CipherCreator.instance().create();
113 final String encrypted = cipherManager.encrypt("test-encrypt");
115 Assert.assertEquals(null, encrypted);
120 * {@link org.openo.baseservice.encrypt.cbb.impl.AesCipher#decrypt(java.lang.String)}.
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);
128 Assert.assertEquals("test-encrypt", decrypted);
132 public void testDecryptNull() {
133 final AbstractCipher cipherManager = CipherCreator.instance().create();
134 String decrypted = cipherManager.decrypt(null);
135 Assert.assertEquals(null, decrypted);
137 decrypted = cipherManager.decrypt("");
139 Assert.assertEquals(null, decrypted);
145 * org.openo.baseservice.encrypt.cbb.impl.AesCipher#CipherManagerImpl(java.lang.String)}
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);
154 Assert.assertEquals("test-encrypt", decrypted);
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);
167 Assert.assertNotSame("test-encrypt", decrypted);
169 final String decrypt = CipherCreator.instance().create("secret-key").decrypt(encrypted);
170 Assert.assertEquals("test-encrypt", decrypt);
174 public void testCreateSecretKeyNoSuchAlgorithmException() throws Exception {
175 new NonStrictExpectations() {
178 SecretKeyFactory keyFactory;
181 keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
182 result = new NoSuchAlgorithmException();
186 final AbstractCipher cipherManager = CipherCreator.instance().create("secret-key");
187 final String encrypted = cipherManager.encrypt("test-encrypt");
188 Assert.assertEquals(encrypted, null);
193 public void testCreateSecretKeyInvalidKeySpecException() throws Exception {
194 new NonStrictExpectations() {
197 SecretKeyFactory keyFactory;
200 keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
201 result = new InvalidKeySpecException();
205 final AbstractCipher cipherManager = CipherCreator.instance().create("secret-key");
206 final String decrypted = cipherManager.decrypt("test-encrypt");
207 Assert.assertEquals(decrypted, null);