2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.security;
23 import org.junit.Test;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
29 public class PasswordsTest {
32 public void hashPassword() throws Exception {
33 String hash = Passwords.hashPassword("hello1234");
34 assertTrue(Passwords.isExpectedPassword("hello1234", hash));
36 //test different salt-> result in different hash
37 String hash2 = Passwords.hashPassword("hello1234");
38 assertFalse(hash.equals(hash2));
40 String hash3 = Passwords.hashPassword("");
41 assertTrue(Passwords.isExpectedPassword("", hash3));
43 String hash4 = Passwords.hashPassword(null);
44 assertTrue(hash4 == null);
48 public void isExpectedPassword() throws Exception {
49 //region isExpectedPassword(String password, String salt, String hash)
50 assertTrue(Passwords.isExpectedPassword(null, null, null));
52 assertTrue(Passwords.isExpectedPassword("hello1234", "e0277df331f4ff8f74752ac4a8fbe03b", "6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
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"));
60 assertFalse(Passwords.isExpectedPassword("hello1234", null, "hello1234"));
63 //region isExpectedPassword(String password, String expectedHash)
64 assertTrue(Passwords.isExpectedPassword(null, null));
66 assertTrue(Passwords.isExpectedPassword("hello1234", "e0277df331f4ff8f74752ac4a8fbe03b:6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
68 assertFalse(Passwords.isExpectedPassword("hello1234", "c0000df331f4ff8f74752ac4a00be03c:6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
69 //exacly 1 param uninitialized
70 assertFalse(Passwords.isExpectedPassword("hello1234", null));
71 assertFalse(Passwords.isExpectedPassword(null, "hello1234"));
73 assertFalse(Passwords.isExpectedPassword("hello1234", "hello1234"));
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));
89 hash = Passwords.hashPassword(password);
90 assertTrue(Passwords.isExpectedPassword(password, hash));
92 hash = Passwords.hashPassword(password);
93 assertTrue(Passwords.isExpectedPassword(password, hash));