|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Dominik Schadow, dominikschadow@gmail.com |
| 3 | + * |
| 4 | + * This file is part of the Java Security project. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package de.dominikschadow.javasecurity.hash; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | + |
| 24 | +class PasswordComparatorTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + void givenIdenticalHashesWhenComparingReturnsTrue() { |
| 28 | + byte[] originalHash = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 29 | + byte[] comparisonHash = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 30 | + |
| 31 | + boolean result = PasswordComparator.comparePasswords(originalHash, comparisonHash); |
| 32 | + |
| 33 | + assertTrue(result); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void givenDifferentHashesWhenComparingReturnsFalse() { |
| 38 | + byte[] originalHash = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 39 | + byte[] comparisonHash = {0x01, 0x02, 0x03, 0x04, 0x06}; |
| 40 | + |
| 41 | + boolean result = PasswordComparator.comparePasswords(originalHash, comparisonHash); |
| 42 | + |
| 43 | + assertFalse(result); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void givenDifferentLengthHashesWhenComparingReturnsFalse() { |
| 48 | + byte[] originalHash = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 49 | + byte[] comparisonHash = {0x01, 0x02, 0x03}; |
| 50 | + |
| 51 | + boolean result = PasswordComparator.comparePasswords(originalHash, comparisonHash); |
| 52 | + |
| 53 | + assertFalse(result); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void givenEmptyHashesWhenComparingReturnsTrue() { |
| 58 | + byte[] originalHash = {}; |
| 59 | + byte[] comparisonHash = {}; |
| 60 | + |
| 61 | + boolean result = PasswordComparator.comparePasswords(originalHash, comparisonHash); |
| 62 | + |
| 63 | + assertTrue(result); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void givenOneEmptyHashWhenComparingReturnsFalse() { |
| 68 | + byte[] originalHash = {0x01, 0x02, 0x03}; |
| 69 | + byte[] comparisonHash = {}; |
| 70 | + |
| 71 | + boolean result = PasswordComparator.comparePasswords(originalHash, comparisonHash); |
| 72 | + |
| 73 | + assertFalse(result); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void givenCompletelyDifferentHashesWhenComparingReturnsFalse() { |
| 78 | + byte[] originalHash = {0x00, 0x00, 0x00, 0x00}; |
| 79 | + byte[] comparisonHash = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; |
| 80 | + |
| 81 | + boolean result = PasswordComparator.comparePasswords(originalHash, comparisonHash); |
| 82 | + |
| 83 | + assertFalse(result); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void givenSingleByteIdenticalHashesWhenComparingReturnsTrue() { |
| 88 | + byte[] originalHash = {0x42}; |
| 89 | + byte[] comparisonHash = {0x42}; |
| 90 | + |
| 91 | + boolean result = PasswordComparator.comparePasswords(originalHash, comparisonHash); |
| 92 | + |
| 93 | + assertTrue(result); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void givenSingleByteDifferentHashesWhenComparingReturnsFalse() { |
| 98 | + byte[] originalHash = {0x42}; |
| 99 | + byte[] comparisonHash = {0x43}; |
| 100 | + |
| 101 | + boolean result = PasswordComparator.comparePasswords(originalHash, comparisonHash); |
| 102 | + |
| 103 | + assertFalse(result); |
| 104 | + } |
| 105 | +} |
0 commit comments