CommonLibrary(util/rest-client) code upload.
[vfc/nfvo/wfengine.git] / common-util / src / test / java / org / openo / baseservice / encrypt / cbb / sha / Sha256Test.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.sha;
17
18 import static org.junit.Assert.fail;
19
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;
27
28 import java.security.InvalidKeyException;
29 import java.security.MessageDigest;
30 import java.security.NoSuchAlgorithmException;
31
32 import javax.crypto.Mac;
33 import javax.crypto.spec.SecretKeySpec;
34
35 import mockit.Mocked;
36 import mockit.NonStrictExpectations;
37 import mockit.integration.junit4.JMockit;
38
39 /**
40  * <br/>
41  * <p>
42  * </p>
43  * 
44  * @author
45  * @version SDNO 0.5 03-Jun-2016
46  */
47 @RunWith(JMockit.class)
48 public class Sha256Test {
49
50     /**
51      * <br/>
52      * 
53      * @throws java.lang.Exception
54      * @since SDNO 0.5
55      */
56     @BeforeClass
57     public static void setUpBeforeClass() throws Exception {
58     }
59
60     /**
61      * <br/>
62      * 
63      * @throws java.lang.Exception
64      * @since SDNO 0.5
65      */
66     @AfterClass
67     public static void tearDownAfterClass() throws Exception {
68     }
69
70     /**
71      * <br/>
72      * 
73      * @throws java.lang.Exception
74      * @since SDNO 0.5
75      */
76     @Before
77     public void setUp() throws Exception {
78     }
79
80     /**
81      * <br/>
82      * 
83      * @throws java.lang.Exception
84      * @since SDNO 0.5
85      */
86     @After
87     public void tearDown() throws Exception {
88     }
89
90     /**
91      * Test method for {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#digest(java.lang.String)}
92      * .
93      */
94     @Test
95     public void testDigest() {
96         String plain = "";
97         String expected = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855";
98         Assert.assertEquals(expected, Sha256.digest(plain));
99
100         expected = "D7A8FBB307D7809469CA9ABCB0082E4F8D5651E46D3CDB762D02D0BF37C9E592";
101         plain = "The quick brown fox jumps over the lazy dog";
102         Assert.assertEquals(expected, Sha256.digest(plain));
103     }
104
105     @Test
106     public void testDigestException() throws Exception {
107         new NonStrictExpectations() {
108
109             @Mocked
110             MessageDigest md;
111
112             {
113                 md = MessageDigest.getInstance("SHA-256");
114                 result = new NoSuchAlgorithmException();
115             }
116         };
117         final String plain = "";
118         final String expected = "";
119         Assert.assertEquals(expected, Sha256.digest(plain));
120
121     }
122
123     /**
124      * Test method for
125      * {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#mac(java.lang.String, java.security.Key)}
126      * .
127      * 
128      * @throws InvalidKeyException
129      */
130     @Test
131     public void testMacStringKey() {
132         final String expected = "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8";
133         final String plain = "The quick brown fox jumps over the lazy dog";
134         try {
135             Assert.assertEquals(expected, Sha256.mac(plain, new SecretKeySpec("key".getBytes(), "HmacSHA256")));
136         } catch(final InvalidKeyException e) {
137             e.printStackTrace();
138             fail("testMacStringKey failed" + e.getMessage());
139         }
140         try {
141             Assert.assertEquals(expected, Sha256.mac(plain, new SecretKeySpec("key".getBytes(), "AES")));
142         } catch(final InvalidKeyException e) {
143             e.printStackTrace();
144             fail("testMacStringKey failed" + e.getMessage());
145         }
146
147     }
148
149     @Test
150     public void testMacStringKeyException() throws Exception {
151         new NonStrictExpectations() {
152
153             @Mocked
154             Mac mac;
155
156             {
157                 mac = Mac.getInstance("HmacSHA256");
158                 result = new NoSuchAlgorithmException();
159             }
160         };
161         Sha256.mac("dummy", new SecretKeySpec("key".getBytes(), "AES"));
162     }
163
164     /**
165      * Test method for
166      * {@link org.openo.baseservice.encrypt.cbb.sha.Sha256#mac(java.lang.String, byte[])}.
167      */
168     @Test
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()));
173     }
174
175     @Test
176     public void testMacStringByteArrayInvalidKeyException() throws Exception {
177         final String key = "key";
178         new NonStrictExpectations() {
179
180             @Mocked
181             Mac mac;
182
183             {
184                 mac = Mac.getInstance("HmacSHA256");
185                 result = new InvalidKeyException();
186             }
187         };
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()));
191     }
192
193 }