Yuli Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame^] | 1 | package org.onap.sdc.security; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import static org.junit.Assert.assertEquals; |
| 6 | import static org.junit.Assert.assertFalse; |
| 7 | import static org.junit.Assert.assertTrue; |
| 8 | |
| 9 | public class PasswordsTest { |
| 10 | |
| 11 | @Test |
| 12 | public void hashPassword() throws Exception { |
| 13 | String hash = Passwords.hashPassword("hello1234"); |
| 14 | assertTrue(Passwords.isExpectedPassword("hello1234", hash)); |
| 15 | |
| 16 | //test different salt-> result in different hash |
| 17 | String hash2 = Passwords.hashPassword("hello1234"); |
| 18 | assertFalse(hash.equals(hash2)); |
| 19 | |
| 20 | String hash3 = Passwords.hashPassword(""); |
| 21 | assertTrue(Passwords.isExpectedPassword("", hash3)); |
| 22 | |
| 23 | String hash4 = Passwords.hashPassword(null); |
| 24 | assertTrue(hash4 == null); |
| 25 | } |
| 26 | |
| 27 | @Test |
| 28 | public void isExpectedPassword() throws Exception { |
| 29 | //region isExpectedPassword(String password, String salt, String hash) |
| 30 | assertTrue(Passwords.isExpectedPassword(null, null, null)); |
| 31 | //valid hash |
| 32 | assertTrue(Passwords.isExpectedPassword("hello1234", "e0277df331f4ff8f74752ac4a8fbe03b", "6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0")); |
| 33 | //invalid salt |
| 34 | assertFalse(Passwords.isExpectedPassword("hello1234", "c0000df331f4ff8f74752ac4a00be03c", "6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0")); |
| 35 | assertFalse(Passwords.isExpectedPassword("hello1234", null, "6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0")); |
| 36 | //exacly 1 param uninitialized |
| 37 | assertFalse(Passwords.isExpectedPassword("hello1234", "", null)); |
| 38 | assertFalse(Passwords.isExpectedPassword(null, "", "hello1234")); |
| 39 | //no salt & no hash |
| 40 | assertFalse(Passwords.isExpectedPassword("hello1234", null, "hello1234")); |
| 41 | //endregion |
| 42 | |
| 43 | //region isExpectedPassword(String password, String expectedHash) |
| 44 | assertTrue(Passwords.isExpectedPassword(null, null)); |
| 45 | //valid hash |
| 46 | assertTrue(Passwords.isExpectedPassword("hello1234", "e0277df331f4ff8f74752ac4a8fbe03b:6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0")); |
| 47 | //invalid salt |
| 48 | assertFalse(Passwords.isExpectedPassword("hello1234", "c0000df331f4ff8f74752ac4a00be03c:6dfbad308cdf53c9ff2ee2dca811ee92f1b359586b33027580e2ff92578edbd0")); |
| 49 | //exacly 1 param uninitialized |
| 50 | assertFalse(Passwords.isExpectedPassword("hello1234", null)); |
| 51 | assertFalse(Passwords.isExpectedPassword(null, "hello1234")); |
| 52 | //no salt & no hash |
| 53 | assertFalse(Passwords.isExpectedPassword("hello1234", "hello1234")); |
| 54 | //endregion |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void hashtest() { |
| 59 | String password = "123456"; |
| 60 | String hash = Passwords.hashPassword(password); |
| 61 | assertTrue(Passwords.isExpectedPassword(password, hash)); |
| 62 | password = "1sdfgsgd23456"; |
| 63 | hash = Passwords.hashPassword(password); |
| 64 | assertTrue(Passwords.isExpectedPassword(password, hash)); |
| 65 | password = "1sdfgsgd2345((*&%$%6"; |
| 66 | hash = Passwords.hashPassword(password); |
| 67 | assertTrue(Passwords.isExpectedPassword(password, hash)); |
| 68 | password = ""; |
| 69 | hash = Passwords.hashPassword(password); |
| 70 | assertTrue(Passwords.isExpectedPassword(password, hash)); |
| 71 | password = " "; |
| 72 | hash = Passwords.hashPassword(password); |
| 73 | assertTrue(Passwords.isExpectedPassword(password, hash)); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | } |