|
19 | 19 |
|
20 | 20 | import static org.apache.zookeeper.server.auth.IPAuthenticationProvider.USE_X_FORWARDED_FOR_KEY; |
21 | 21 | import static org.apache.zookeeper.server.auth.IPAuthenticationProvider.X_FORWARDED_FOR_HEADER_NAME; |
| 22 | +import static org.hamcrest.CoreMatchers.containsString; |
| 23 | +import static org.hamcrest.MatcherAssert.assertThat; |
22 | 24 | import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertNull; |
| 26 | +import static org.junit.Assert.fail; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
23 | 29 | import static org.mockito.Mockito.doReturn; |
24 | 30 | import static org.mockito.Mockito.mock; |
| 31 | +import java.util.stream.Stream; |
25 | 32 | import javax.servlet.http.HttpServletRequest; |
26 | 33 | import org.junit.After; |
27 | 34 | import org.junit.Before; |
28 | 35 | import org.junit.Test; |
| 36 | +import org.junit.jupiter.params.ParameterizedTest; |
| 37 | +import org.junit.jupiter.params.provider.Arguments; |
| 38 | +import org.junit.jupiter.params.provider.MethodSource; |
29 | 39 |
|
30 | 40 | public class IPAuthenticationProviderTest { |
31 | 41 |
|
@@ -96,4 +106,84 @@ public void testGetClientIPAddressMissingXForwardedFor() { |
96 | 106 | // Assert |
97 | 107 | assertEquals("192.168.1.1", clientIp); |
98 | 108 | } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testParsingOfIPv6Address() { |
| 112 | + //Full IPv6 address |
| 113 | + String ipv6Full = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"; |
| 114 | + byte[] expectedFull = { |
| 115 | + (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, |
| 116 | + (byte) 0x85, (byte) 0xa3, (byte) 0x00, (byte) 0x00, |
| 117 | + (byte) 0x00, (byte) 0x00, (byte) 0x8a, (byte) 0x2e, |
| 118 | + (byte) 0x03, (byte) 0x70, (byte) 0x73, (byte) 0x34 |
| 119 | + }; |
| 120 | + byte[] actualFull = IPAuthenticationProvider.v6addr2Bytes(ipv6Full); |
| 121 | + assertNotNull(actualFull, "Full IPv6 address should not return null"); |
| 122 | + assertArrayEquals(expectedFull, actualFull, "Full IPv6 address conversion mismatch"); |
| 123 | + |
| 124 | + //Compressed IPv6 address (double colon) |
| 125 | + String ipv6Compressed = "2001:db8::8a2e:370:7334"; |
| 126 | + byte[] expectedCompressed = { |
| 127 | + (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, |
| 128 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 129 | + (byte) 0x00, (byte) 0x00, (byte) 0x8a, (byte) 0x2e, |
| 130 | + (byte) 0x03, (byte) 0x70, (byte) 0x73, (byte) 0x34 |
| 131 | + }; |
| 132 | + byte[] actualCompressed = IPAuthenticationProvider.v6addr2Bytes(ipv6Compressed); |
| 133 | + assertNotNull(actualCompressed, "Compressed IPv6 address should not return null"); |
| 134 | + assertArrayEquals(expectedCompressed, actualCompressed, "Compressed IPv6 address conversion mismatch"); |
| 135 | + |
| 136 | + //Shortened IPv6 address |
| 137 | + String ipv6Shortened = "2001:db8::1"; |
| 138 | + byte[] expectedShortened = { |
| 139 | + (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, |
| 140 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 141 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 142 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 |
| 143 | + }; |
| 144 | + byte[] actualShortened = IPAuthenticationProvider.v6addr2Bytes(ipv6Shortened); |
| 145 | + assertNotNull(actualShortened, "Shortened IPv6 address should not return null"); |
| 146 | + assertArrayEquals(expectedShortened, actualShortened, "Shortened IPv6 address conversion mismatch"); |
| 147 | + |
| 148 | + //Loopback address |
| 149 | + String ipv6Loopback = "::1"; |
| 150 | + byte[] expectedLoopback = { |
| 151 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 152 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 153 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 154 | + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01 |
| 155 | + }; |
| 156 | + byte[] actualLoopback = IPAuthenticationProvider.v6addr2Bytes(ipv6Loopback); |
| 157 | + assertNotNull(actualLoopback, "Loopback IPv6 address should not return null"); |
| 158 | + assertArrayEquals(expectedLoopback, actualLoopback, "Loopback IPv6 address conversion mismatch"); |
| 159 | + } |
| 160 | + |
| 161 | + private static Stream<Arguments> invalidIPv6Addresses() { |
| 162 | + return Stream.of( |
| 163 | + Arguments.of("1", "wrong number of segments"), |
| 164 | + Arguments.of("1:2", "wrong number of segments"), |
| 165 | + Arguments.of("1::2:", "empty segment"), |
| 166 | + Arguments.of(":1::2:", "empty segment"), |
| 167 | + Arguments.of("1:2:3:4:5:6:7:8:", "wrong number of segments"), |
| 168 | + Arguments.of("1:2:3:4:5:6:7:8:9", "wrong number of segments"), |
| 169 | + Arguments.of("1:2::3:4:5:6:7:8", "too many segments"), |
| 170 | + Arguments.of("1::2::", "too many '::'"), |
| 171 | + Arguments.of("1:abcdf::", "segment too long"), |
| 172 | + Arguments.of("efgh::", "invalid hexadecimal characters in segment"), |
| 173 | + Arguments.of("1:: ", "invalid hexadecimal characters in segment"), |
| 174 | + Arguments.of(" 1::", "invalid hexadecimal characters in segment") |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + @ParameterizedTest(name = "address = \"{0}\"") |
| 179 | + @MethodSource("invalidIPv6Addresses") |
| 180 | + public void testParsingOfInvalidIPv6Address(String ipv6Address, String expectedMessage) { |
| 181 | + try { |
| 182 | + IPAuthenticationProvider.parseV6addr(ipv6Address); |
| 183 | + fail("expect failure"); |
| 184 | + } catch (IllegalArgumentException e) { |
| 185 | + assertThat(e.getMessage(), containsString(expectedMessage)); |
| 186 | + } |
| 187 | + assertNull(IPAuthenticationProvider.v6addr2Bytes(ipv6Address)); |
| 188 | + } |
99 | 189 | } |
0 commit comments