|
| 1 | +package me.confuser.banmanager.webenhancer.common.data; |
| 2 | + |
| 3 | +import me.confuser.banmanager.common.data.PlayerData; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.security.NoSuchAlgorithmException; |
| 7 | +import java.util.UUID; |
| 8 | + |
| 9 | +import static org.junit.Assert.*; |
| 10 | +import static org.mockito.Mockito.*; |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit tests for PlayerPinData. |
| 14 | + */ |
| 15 | +public class PlayerPinDataTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void shouldGenerateSixDigitPin() throws NoSuchAlgorithmException { |
| 19 | + PlayerData player = mock(PlayerData.class); |
| 20 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 21 | + |
| 22 | + PlayerPinData pinData = new PlayerPinData(player); |
| 23 | + int pin = pinData.getGeneratedPin(); |
| 24 | + |
| 25 | + // 6 digit pin: 100000-999999 |
| 26 | + assertTrue("Pin should be at least 100000", pin >= 100000); |
| 27 | + assertTrue("Pin should be at most 999999", pin <= 999999); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void shouldHashPinWithArgon2() throws NoSuchAlgorithmException { |
| 32 | + PlayerData player = mock(PlayerData.class); |
| 33 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 34 | + |
| 35 | + PlayerPinData pinData = new PlayerPinData(player); |
| 36 | + String hashedPin = pinData.getPin(); |
| 37 | + |
| 38 | + assertNotNull("Hashed pin should not be null", hashedPin); |
| 39 | + assertTrue("Pin should be hashed with Argon2i", hashedPin.startsWith("$argon2i$")); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void shouldSetExpiryFiveMinutesFromNow() throws NoSuchAlgorithmException { |
| 44 | + PlayerData player = mock(PlayerData.class); |
| 45 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 46 | + |
| 47 | + long beforeCreate = System.currentTimeMillis() / 1000L; |
| 48 | + PlayerPinData pinData = new PlayerPinData(player); |
| 49 | + long afterCreate = System.currentTimeMillis() / 1000L; |
| 50 | + |
| 51 | + // Expiry should be 300 seconds (5 minutes) from creation |
| 52 | + long expires = pinData.getExpires(); |
| 53 | + |
| 54 | + // Allow 1 second tolerance for test execution |
| 55 | + assertTrue("Expiry should be ~300 seconds from creation", |
| 56 | + expires >= beforeCreate + 299 && expires <= afterCreate + 301); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void shouldStoreGeneratedPinForDisplay() throws NoSuchAlgorithmException { |
| 61 | + PlayerData player = mock(PlayerData.class); |
| 62 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 63 | + |
| 64 | + PlayerPinData pinData = new PlayerPinData(player); |
| 65 | + |
| 66 | + int generatedPin = pinData.getGeneratedPin(); |
| 67 | + String hashedPin = pinData.getPin(); |
| 68 | + |
| 69 | + assertNotEquals("Generated pin should not be 0", 0, generatedPin); |
| 70 | + assertFalse("Hashed pin should not equal plaintext", |
| 71 | + hashedPin.equals(String.valueOf(generatedPin))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void shouldStorePlayerReference() throws NoSuchAlgorithmException { |
| 76 | + PlayerData player = mock(PlayerData.class); |
| 77 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 78 | + |
| 79 | + PlayerPinData pinData = new PlayerPinData(player); |
| 80 | + |
| 81 | + assertSame("Player reference should be stored", player, pinData.getPlayer()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void shouldHaveZeroIdBeforePersistence() throws NoSuchAlgorithmException { |
| 86 | + PlayerData player = mock(PlayerData.class); |
| 87 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 88 | + |
| 89 | + PlayerPinData pinData = new PlayerPinData(player); |
| 90 | + |
| 91 | + assertEquals("ID should be 0 before persistence", 0, pinData.getId()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void shouldGenerateRandomPins() throws NoSuchAlgorithmException { |
| 96 | + PlayerData player = mock(PlayerData.class); |
| 97 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 98 | + |
| 99 | + PlayerPinData pin1 = new PlayerPinData(player); |
| 100 | + PlayerPinData pin2 = new PlayerPinData(player); |
| 101 | + PlayerPinData pin3 = new PlayerPinData(player); |
| 102 | + |
| 103 | + boolean atLeastTwoDifferent = |
| 104 | + pin1.getGeneratedPin() != pin2.getGeneratedPin() || |
| 105 | + pin1.getGeneratedPin() != pin3.getGeneratedPin() || |
| 106 | + pin2.getGeneratedPin() != pin3.getGeneratedPin(); |
| 107 | + |
| 108 | + assertTrue("At least two of three pins should differ", atLeastTwoDifferent); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void shouldGenerateUniqueHashesForSamePin() throws NoSuchAlgorithmException { |
| 113 | + PlayerData player = mock(PlayerData.class); |
| 114 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 115 | + |
| 116 | + PlayerPinData pin1 = new PlayerPinData(player); |
| 117 | + PlayerPinData pin2 = new PlayerPinData(player); |
| 118 | + |
| 119 | + // Even if by chance the generated pins are the same, the hashes should differ |
| 120 | + // due to unique random salts in Argon2 |
| 121 | + assertNotEquals("Hashes should differ due to random salts", |
| 122 | + pin1.getPin(), pin2.getPin()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void generatedPinShouldBeSettable() throws NoSuchAlgorithmException { |
| 127 | + PlayerData player = mock(PlayerData.class); |
| 128 | + when(player.getUUID()).thenReturn(UUID.randomUUID()); |
| 129 | + |
| 130 | + PlayerPinData pinData = new PlayerPinData(player); |
| 131 | + pinData.setGeneratedPin(999999); |
| 132 | + |
| 133 | + assertEquals("Generated pin should be settable", 999999, pinData.getGeneratedPin()); |
| 134 | + } |
| 135 | +} |
0 commit comments