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.sha;
18 import static org.junit.Assert.fail;
20 import org.junit.After;
21 import org.junit.AfterClass;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
28 import java.security.InvalidKeyException;
29 import java.security.MessageDigest;
30 import java.security.NoSuchAlgorithmException;
32 import javax.crypto.Mac;
33 import javax.crypto.spec.SecretKeySpec;
36 import mockit.NonStrictExpectations;
37 import mockit.integration.junit4.JMockit;
45 * @version 03-Jun-2016
47 @RunWith(JMockit.class)
48 public class Sha256Test {
53 * @throws java.lang.Exception
57 public static void setUpBeforeClass() throws Exception {
63 * @throws java.lang.Exception
67 public static void tearDownAfterClass() throws Exception {
73 * @throws java.lang.Exception
77 public void setUp() throws Exception {
83 * @throws java.lang.Exception
87 public void tearDown() throws Exception {
91 * Test method for {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#digest(java.lang.String)}
95 public void testDigest() {
97 String expected = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855";
98 Assert.assertEquals(expected, Sha256.digest(plain));
100 expected = "D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592";
101 plain = "The quick brown fox jumps over the lazy dog";
102 Assert.assertEquals(expected, Sha256.digest(plain));
106 public void testDigestException() throws Exception {
107 new NonStrictExpectations() {
113 md = MessageDigest.getInstance("SHA-256");
114 result = new NoSuchAlgorithmException();
117 final String plain = "";
118 final String expected = "";
119 Assert.assertEquals(expected, Sha256.digest(plain));
125 * {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#mac(java.lang.String, java.security.Key)}
128 * @throws InvalidKeyException
131 public void testMacStringKey() {
132 final String expected = "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8";
133 final String plain = "The quick brown fox jumps over the lazy dog";
135 Assert.assertEquals(expected, Sha256.mac(plain, new SecretKeySpec("key".getBytes(), "HmacSHA256")));
136 } catch(final InvalidKeyException e) {
138 fail("testMacStringKey failed" + e.getMessage());
141 Assert.assertEquals(expected, Sha256.mac(plain, new SecretKeySpec("key".getBytes(), "AES")));
142 } catch(final InvalidKeyException e) {
144 fail("testMacStringKey failed" + e.getMessage());
150 public void testMacStringKeyException() throws Exception {
151 new NonStrictExpectations() {
157 mac = Mac.getInstance("HmacSHA256");
158 result = new NoSuchAlgorithmException();
161 Sha256.mac("dummy", new SecretKeySpec("key".getBytes(), "AES"));
166 * {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#mac(java.lang.String, byte[])}.
169 public void testMacStringByteArray() {
170 final String expected = "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8";
171 final String plain = "The quick brown fox jumps over the lazy dog";
172 Assert.assertEquals(expected, Sha256.mac(plain, "key".getBytes()));
176 public void testMacStringByteArrayInvalidKeyException() throws Exception {
177 final String key = "key";
178 new NonStrictExpectations() {
184 mac = Mac.getInstance("HmacSHA256");
185 result = new InvalidKeyException();
188 final String expected = "";
189 final String plain = "The quick brown fox jumps over the lazy dog";
190 Assert.assertEquals(expected, Sha256.mac(plain, key.getBytes()));