Added oparent to sdc main
[sdc.git] / openecomp-be / backend / openecomp-sdc-security-util / src / test / PasswordsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.sdc.security;
22
23 import org.junit.Test;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29 public class PasswordsTest {
30
31     @Test
32     public void hashPassword() throws Exception {
33         String hash = Passwords.hashPassword("hello1234");
34         assertTrue(Passwords.isExpectedPassword("hello1234", hash));
35
36         //test different salt-> result in different hash
37         String hash2 = Passwords.hashPassword("hello1234");
38         assertFalse(hash.equals(hash2));
39
40         String hash3  = Passwords.hashPassword("");
41         assertTrue(Passwords.isExpectedPassword("", hash3));
42
43         String hash4  = Passwords.hashPassword(null);
44         assertTrue(hash4 == null);
45     }
46
47     @Test
48     public void isExpectedPassword() throws Exception {
49         //region isExpectedPassword(String password, String salt, String hash)
50         assertTrue(Passwords.isExpectedPassword(null, null, null));
51         //valid hash
52         assertTrue(Passwords.isExpectedPassword("hello1234", "e0277df331f4ff8f74752ac4a8fbe03b", "6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
53         //invalid salt
54         assertFalse(Passwords.isExpectedPassword("hello1234", "c0000df331f4ff8f74752ac4a00be03c", "6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
55         assertFalse(Passwords.isExpectedPassword("hello1234", null, "6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
56         //exacly 1 param uninitialized
57         assertFalse(Passwords.isExpectedPassword("hello1234", "", null));
58         assertFalse(Passwords.isExpectedPassword(null, "", "hello1234"));
59         //no salt & no hash
60         assertFalse(Passwords.isExpectedPassword("hello1234", null, "hello1234"));
61         //endregion
62
63         //region isExpectedPassword(String password, String expectedHash)
64         assertTrue(Passwords.isExpectedPassword(null, null));
65         //valid hash
66         assertTrue(Passwords.isExpectedPassword("hello1234", "e0277df331f4ff8f74752ac4a8fbe03b:6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
67         //invalid salt
68         assertFalse(Passwords.isExpectedPassword("hello1234", "c0000df331f4ff8f74752ac4a00be03c:6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
69         //exacly 1 param uninitialized
70         assertFalse(Passwords.isExpectedPassword("hello1234", null));
71         assertFalse(Passwords.isExpectedPassword(null, "hello1234"));
72         //no salt & no hash
73         assertFalse(Passwords.isExpectedPassword("hello1234", "hello1234"));
74         //endregion
75     }
76
77     @Test
78     public void hashtest() {
79         String password = "123456";
80         String hash = Passwords.hashPassword(password);
81         assertTrue(Passwords.isExpectedPassword(password, hash));
82         password = "1sdfgsgd23456";
83         hash = Passwords.hashPassword(password);
84         assertTrue(Passwords.isExpectedPassword(password, hash));
85         password = "1sdfgsgd2345((*&%$%6";
86         hash = Passwords.hashPassword(password);
87         assertTrue(Passwords.isExpectedPassword(password, hash));
88         password = "";
89         hash = Passwords.hashPassword(password);
90         assertTrue(Passwords.isExpectedPassword(password, hash));
91         password = " ";
92         hash = Passwords.hashPassword(password);
93         assertTrue(Passwords.isExpectedPassword(password, hash));
94     }
95
96
97 }