|
| 1 | +package com.stubbornjava.common; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +public class UpdatableBCryptTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + public void testHash() { |
| 13 | + UpdatableBCrypt bcrypt = new UpdatableBCrypt(11); |
| 14 | + String hash = bcrypt.hash("password"); |
| 15 | + |
| 16 | + assertTrue(bcrypt.verifyHash("password", hash)); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testVerifyAndUpdateDoesNotUpdate() { |
| 21 | + UpdatableBCrypt bcrypt11 = new UpdatableBCrypt(10); |
| 22 | + String hash11 = bcrypt11.hash("password"); |
| 23 | + assertTrue(bcrypt11.verifyHash("password", hash11)); |
| 24 | + |
| 25 | + UpdatableBCrypt bcrypt12 = new UpdatableBCrypt(10); |
| 26 | + |
| 27 | + String[] hash12 = new String[1]; |
| 28 | + bcrypt12.verifyAndUpdateHash("password", hash11, newHash -> { |
| 29 | + hash12[0] = newHash; |
| 30 | + return true; |
| 31 | + }); |
| 32 | + assertTrue(null == hash12[0]); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testVerifyAndUpdateChangesHash() { |
| 37 | + UpdatableBCrypt bcrypt11 = new UpdatableBCrypt(10); |
| 38 | + String hash11 = bcrypt11.hash("password"); |
| 39 | + assertTrue(bcrypt11.verifyHash("password", hash11)); |
| 40 | + |
| 41 | + UpdatableBCrypt bcrypt12 = new UpdatableBCrypt(11); |
| 42 | + |
| 43 | + String[] hash12 = new String[1]; |
| 44 | + boolean result = bcrypt12.verifyAndUpdateHash("password", hash11, newHash -> { |
| 45 | + hash12[0] = newHash; |
| 46 | + return true; |
| 47 | + }); |
| 48 | + assertTrue(result); |
| 49 | + assertTrue(null != hash12[0]); |
| 50 | + assertTrue(bcrypt12.verifyHash("password", hash12[0])); |
| 51 | + |
| 52 | + // Hash shouldn't change again |
| 53 | + String finalHash = hash12[0]; |
| 54 | + result = bcrypt12.verifyAndUpdateHash("password", finalHash, newHash -> { |
| 55 | + hash12[0] = newHash; |
| 56 | + return true; |
| 57 | + }); |
| 58 | + assertTrue(result); |
| 59 | + assertTrue(null != finalHash); |
| 60 | + assertEquals(finalHash, hash12[0]); |
| 61 | + assertTrue(bcrypt12.verifyHash("password", finalHash)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testVerifyAndUpdateChangesHashSkippingRounds() { |
| 66 | + UpdatableBCrypt bcrypt11 = new UpdatableBCrypt(5); |
| 67 | + String hash11 = bcrypt11.hash("password"); |
| 68 | + assertTrue(bcrypt11.verifyHash("password", hash11)); |
| 69 | + |
| 70 | + UpdatableBCrypt bcrypt12 = new UpdatableBCrypt(13); |
| 71 | + |
| 72 | + String[] hash12 = new String[1]; |
| 73 | + boolean result = bcrypt12.verifyAndUpdateHash("password", hash11, newHash -> { |
| 74 | + hash12[0] = newHash; |
| 75 | + return true; |
| 76 | + }); |
| 77 | + assertTrue(result); |
| 78 | + assertTrue(null != hash12[0]); |
| 79 | + assertTrue(bcrypt12.verifyHash("password", hash12[0])); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testVerifyAndUpdateFailsOnBadPW() { |
| 84 | + UpdatableBCrypt bcrypt11 = new UpdatableBCrypt(10); |
| 85 | + String hash11 = bcrypt11.hash("password"); |
| 86 | + assertTrue(bcrypt11.verifyHash("password", hash11)); |
| 87 | + |
| 88 | + UpdatableBCrypt bcrypt12 = new UpdatableBCrypt(11); |
| 89 | + |
| 90 | + String[] hash12 = new String[1]; |
| 91 | + boolean result = bcrypt12.verifyAndUpdateHash("password1", hash11, newHash -> { |
| 92 | + hash12[0] = newHash; |
| 93 | + return true; |
| 94 | + }); |
| 95 | + assertFalse(result); |
| 96 | + assertTrue(null == hash12[0]); |
| 97 | + } |
| 98 | +} |
0 commit comments