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.openecomp.sdc.security;
23 import org.junit.Test;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 public class PasswordsTest {
31 public void hashPassword() throws Exception {
32 String hash = Passwords.hashPassword("hello1234");
33 assertTrue(Passwords.isExpectedPassword("hello1234", hash));
35 //test different salt-> result in different hash
36 String hash2 = Passwords.hashPassword("hello1234");
37 assertFalse(hash.equals(hash2));
39 String hash3 = Passwords.hashPassword("");
40 assertTrue(Passwords.isExpectedPassword("", hash3));
42 String hash4 = Passwords.hashPassword(null);
43 assertTrue(hash4 == null);
47 public void isExpectedPassword() throws Exception {
48 //region isExpectedPassword(String password, String salt, String hash)
49 assertTrue(Passwords.isExpectedPassword(null, null, null));
51 assertTrue(Passwords.isExpectedPassword("hello1234", "e0277df331f4ff8f74752ac4a8fbe03b", "6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
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"));
59 assertFalse(Passwords.isExpectedPassword("hello1234", null, "hello1234"));
62 //region isExpectedPassword(String password, String expectedHash)
63 assertTrue(Passwords.isExpectedPassword(null, null));
65 assertTrue(Passwords.isExpectedPassword("hello1234", "e0277df331f4ff8f74752ac4a8fbe03b:6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
67 assertFalse(Passwords.isExpectedPassword("hello1234", "c0000df331f4ff8f74752ac4a00be03c:6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0"));
68 //exacly 1 param uninitialized
69 assertFalse(Passwords.isExpectedPassword("hello1234", null));
70 assertFalse(Passwords.isExpectedPassword(null, "hello1234"));
72 assertFalse(Passwords.isExpectedPassword("hello1234", "hello1234"));
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));
88 hash = Passwords.hashPassword(password);
89 assertTrue(Passwords.isExpectedPassword(password, hash));
91 hash = Passwords.hashPassword(password);
92 assertTrue(Passwords.isExpectedPassword(password, hash));