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