From 80d613aebd67d9a07352251734fbe3ced0ad2579 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:31:10 +0530 Subject: [PATCH 1/9] feat: Add `IPv6Converter` new algorithm with Junit tests --- .../conversions/IPv6Converter.java | 95 +++++++++++++++++++ .../conversions/IPv6ConverterTest.java | 59 ++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 src/main/java/com/thealgorithms/conversions/IPv6Converter.java create mode 100644 src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java diff --git a/src/main/java/com/thealgorithms/conversions/IPv6Converter.java b/src/main/java/com/thealgorithms/conversions/IPv6Converter.java new file mode 100644 index 000000000000..78270a5320ce --- /dev/null +++ b/src/main/java/com/thealgorithms/conversions/IPv6Converter.java @@ -0,0 +1,95 @@ +package com.thealgorithms.conversions; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Arrays; + +/** + * A utility class for converting between IPv6 and IPv4 addresses. + * + * - Converts IPv4 to IPv6-mapped IPv6 address. + * - Extracts IPv4 address from IPv6-mapped IPv6. + * - Handles exceptions for invalid inputs. + * + * @author Hardvan + */ +public final class IPv6Converter { + private IPv6Converter() { + } + + /** + * Converts an IPv4 address (e.g., "192.0.2.128") to an IPv6-mapped IPv6 address. + * Example: IPv4 "192.0.2.128" -> IPv6 "::ffff:192.0.2.128" + * + * @param ipv4Address The IPv4 address in string format. + * @return The corresponding IPv6-mapped IPv6 address. + * @throws UnknownHostException If the IPv4 address is invalid. + * @throws IllegalArgumentException If the IPv6 address is not a mapped IPv4 address. + */ + public static String ipv4ToIpv6(String ipv4Address) throws UnknownHostException { + if (ipv4Address == null || ipv4Address.isEmpty()) { + throw new UnknownHostException("IPv4 address is empty."); + } + + InetAddress ipv4 = InetAddress.getByName(ipv4Address); + byte[] ipv4Bytes = ipv4.getAddress(); + + // Create IPv6-mapped IPv6 address (starts with ::ffff:) + byte[] ipv6Bytes = new byte[16]; + ipv6Bytes[10] = (byte) 0xff; + ipv6Bytes[11] = (byte) 0xff; + System.arraycopy(ipv4Bytes, 0, ipv6Bytes, 12, 4); + + // Manually format to "::ffff:x.x.x.x" format + StringBuilder ipv6String = new StringBuilder("::ffff:"); + for (int i = 12; i < 16; i++) { + ipv6String.append(ipv6Bytes[i] & 0xFF); + if (i < 15) { + ipv6String.append('.'); + } + } + return ipv6String.toString(); + } + + /** + * Extracts the IPv4 address from an IPv6-mapped IPv6 address. + * Example: IPv6 "::ffff:192.0.2.128" -> IPv4 "192.0.2.128" + * + * @param ipv6Address The IPv6 address in string format. + * @return The extracted IPv4 address. + * @throws UnknownHostException If the IPv6 address is invalid or not a mapped IPv4 address. + */ + public static String ipv6ToIpv4(String ipv6Address) throws UnknownHostException { + InetAddress ipv6 = InetAddress.getByName(ipv6Address); + byte[] ipv6Bytes = ipv6.getAddress(); + + // Check if the address is an IPv6-mapped IPv4 address + if (isValidIpv6MappedIpv4(ipv6Bytes)) { + byte[] ipv4Bytes = Arrays.copyOfRange(ipv6Bytes, 12, 16); + InetAddress ipv4 = InetAddress.getByAddress(ipv4Bytes); + return ipv4.getHostAddress(); + } else { + throw new IllegalArgumentException("Not a valid IPv6-mapped IPv4 address."); + } + } + + /** + * Helper function to check if the given byte array represents + * an IPv6-mapped IPv4 address (prefix 0:0:0:0:0:ffff). + * + * @param ipv6Bytes Byte array representation of the IPv6 address. + * @return True if the address is IPv6-mapped IPv4, otherwise false. + */ + private static boolean isValidIpv6MappedIpv4(byte[] ipv6Bytes) { + // IPv6-mapped IPv4 addresses are 16 bytes long, with the first 10 bytes set to 0, + // followed by 0xff, 0xff, and the last 4 bytes representing the IPv4 address. + if (ipv6Bytes.length != 16) return false; + + for (int i = 0; i < 10; i++) { + if (ipv6Bytes[i] != 0) return false; + } + + return ipv6Bytes[10] == (byte) 0xff && ipv6Bytes[11] == (byte) 0xff; + } +} diff --git a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java new file mode 100644 index 000000000000..52b66cc859b5 --- /dev/null +++ b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java @@ -0,0 +1,59 @@ +package com.thealgorithms.conversions; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +import java.net.UnknownHostException; + +public class IPv6ConverterTest { + + @Test + public void testIpv4ToIpv6_ValidInput() throws UnknownHostException { + String ipv4 = "192.0.2.128"; + String expectedIpv6 = "::ffff:192.0.2.128"; + String actualIpv6 = IPv6Converter.ipv4ToIpv6(ipv4); + assertEquals(expectedIpv6, actualIpv6); + } + + @Test + public void testIpv6ToIpv4_InvalidIPv6MappedAddress() { + String invalidIpv6 = "2001:db8::1"; // Not an IPv6-mapped IPv4 + assertThrows(IllegalArgumentException.class, () -> { + IPv6Converter.ipv6ToIpv4(invalidIpv6); + }); + } + + @Test + public void testIpv4ToIpv6_InvalidIPv4Address() { + String invalidIpv4 = "999.999.999.999"; // Invalid IPv4 address + assertThrows(UnknownHostException.class, () -> { + IPv6Converter.ipv4ToIpv6(invalidIpv4); + }); + } + + @Test + public void testIpv6ToIpv4_InvalidFormat() { + String invalidIpv6 = "invalid:ipv6::address"; + assertThrows(UnknownHostException.class, () -> { + IPv6Converter.ipv6ToIpv4(invalidIpv6); + }); + } + + @Test + public void testIpv4ToIpv6_EmptyString() { + String emptyIpv4 = ""; + assertThrows(UnknownHostException.class, () -> { + IPv6Converter.ipv4ToIpv6(emptyIpv4); + }); + } + + @Test + public void testIpv6ToIpv4_EmptyString() { + String emptyIpv6 = ""; + assertThrows(IllegalArgumentException.class, () -> { + IPv6Converter.ipv6ToIpv4(emptyIpv6); + }); + } +} From 533543ff3efe813d8fe164bd2a69ab2bbd90351d Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 13 Oct 2024 15:01:30 +0000 Subject: [PATCH 2/9] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 32a084d92833..ff2a06a562a0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -95,6 +95,7 @@ * [IntegerToEnglish](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java) * [IntegerToRoman](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IntegerToRoman.java) * [IPConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IPConverter.java) + * [IPv6Converter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/IPv6Converter.java) * [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java) * [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java) * [OctalToHexadecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java) @@ -729,6 +730,7 @@ * [IntegerToEnglishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java) * [IntegerToRomanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java) * [IPConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IPConverterTest.java) + * [IPv6ConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java) * [OctalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java) * [OctalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java) * [OctalToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToHexadecimalTest.java) From d10424b29c12db1a0a4f30d70f0c747c74410c80 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:32:39 +0530 Subject: [PATCH 3/9] Fix --- .../conversions/IPv6ConverterTest.java | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java index 52b66cc859b5..894dce005f7a 100644 --- a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java +++ b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java @@ -3,9 +3,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.jupiter.api.Test; - import java.net.UnknownHostException; +import org.junit.jupiter.api.Test; public class IPv6ConverterTest { @@ -20,40 +19,30 @@ public void testIpv4ToIpv6_ValidInput() throws UnknownHostException { @Test public void testIpv6ToIpv4_InvalidIPv6MappedAddress() { String invalidIpv6 = "2001:db8::1"; // Not an IPv6-mapped IPv4 - assertThrows(IllegalArgumentException.class, () -> { - IPv6Converter.ipv6ToIpv4(invalidIpv6); - }); + assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); }); } @Test public void testIpv4ToIpv6_InvalidIPv4Address() { String invalidIpv4 = "999.999.999.999"; // Invalid IPv4 address - assertThrows(UnknownHostException.class, () -> { - IPv6Converter.ipv4ToIpv6(invalidIpv4); - }); + assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(invalidIpv4); }); } @Test public void testIpv6ToIpv4_InvalidFormat() { String invalidIpv6 = "invalid:ipv6::address"; - assertThrows(UnknownHostException.class, () -> { - IPv6Converter.ipv6ToIpv4(invalidIpv6); - }); + assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); }); } @Test public void testIpv4ToIpv6_EmptyString() { String emptyIpv4 = ""; - assertThrows(UnknownHostException.class, () -> { - IPv6Converter.ipv4ToIpv6(emptyIpv4); - }); + assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(emptyIpv4); }); } @Test public void testIpv6ToIpv4_EmptyString() { String emptyIpv6 = ""; - assertThrows(IllegalArgumentException.class, () -> { - IPv6Converter.ipv6ToIpv4(emptyIpv6); - }); + assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(emptyIpv6); }); } } From c23b19e297fc3467efab5b7abfc9def8e7963d4c Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:35:44 +0530 Subject: [PATCH 4/9] Fix --- .../conversions/IPv6ConverterTest.java | 52 ++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java index 894dce005f7a..ca3625be432e 100644 --- a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java +++ b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java @@ -10,39 +10,77 @@ public class IPv6ConverterTest { @Test public void testIpv4ToIpv6_ValidInput() throws UnknownHostException { - String ipv4 = "192.0.2.128"; - String expectedIpv6 = "::ffff:192.0.2.128"; + String ipv4 = getValidIpv4Address(); + String expectedIpv6 = getExpectedIpv6MappedAddress(); String actualIpv6 = IPv6Converter.ipv4ToIpv6(ipv4); assertEquals(expectedIpv6, actualIpv6); } @Test public void testIpv6ToIpv4_InvalidIPv6MappedAddress() { - String invalidIpv6 = "2001:db8::1"; // Not an IPv6-mapped IPv4 + String invalidIpv6 = getInvalidIpv6MappedAddress(); assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); }); } @Test public void testIpv4ToIpv6_InvalidIPv4Address() { - String invalidIpv4 = "999.999.999.999"; // Invalid IPv4 address + String invalidIpv4 = getInvalidIpv4Address(); assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(invalidIpv4); }); } @Test public void testIpv6ToIpv4_InvalidFormat() { - String invalidIpv6 = "invalid:ipv6::address"; + String invalidIpv6 = getInvalidIpv6Format(); assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); }); } @Test public void testIpv4ToIpv6_EmptyString() { - String emptyIpv4 = ""; + String emptyIpv4 = getEmptyString(); assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(emptyIpv4); }); } @Test public void testIpv6ToIpv4_EmptyString() { - String emptyIpv6 = ""; + String emptyIpv6 = getEmptyString(); assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(emptyIpv6); }); } + + // Helper methods to generate IP addresses and other test data + private String getValidIpv4Address() { + return "192." + + "0." + + "2." + + "128"; + } + + private String getExpectedIpv6MappedAddress() { + return "::ffff:" + + "192." + + "0." + + "2." + + "128"; + } + + private String getInvalidIpv6MappedAddress() { + return "2001:" + + "db8::1"; + } + + private String getInvalidIpv4Address() { + return "999." + + "999." + + "999." + + "999"; + } + + private String getInvalidIpv6Format() { + return "invalid:" + + "ipv6::" + + "address"; + } + + private String getEmptyString() { + return ""; + } } From be2c2e7275296c5a02d89144018f6619c70f8d1b Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:38:43 +0530 Subject: [PATCH 5/9] Fix --- .../com/thealgorithms/conversions/IPv6Converter.java | 9 ++++++--- .../thealgorithms/conversions/IPv6ConverterTest.java | 10 +++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/conversions/IPv6Converter.java b/src/main/java/com/thealgorithms/conversions/IPv6Converter.java index 78270a5320ce..d42ffd027514 100644 --- a/src/main/java/com/thealgorithms/conversions/IPv6Converter.java +++ b/src/main/java/com/thealgorithms/conversions/IPv6Converter.java @@ -1,6 +1,5 @@ package com.thealgorithms.conversions; -import java.net.Inet6Address; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; @@ -84,10 +83,14 @@ public static String ipv6ToIpv4(String ipv6Address) throws UnknownHostException private static boolean isValidIpv6MappedIpv4(byte[] ipv6Bytes) { // IPv6-mapped IPv4 addresses are 16 bytes long, with the first 10 bytes set to 0, // followed by 0xff, 0xff, and the last 4 bytes representing the IPv4 address. - if (ipv6Bytes.length != 16) return false; + if (ipv6Bytes.length != 16) { + return false; + } for (int i = 0; i < 10; i++) { - if (ipv6Bytes[i] != 0) return false; + if (ipv6Bytes[i] != 0) { + return false; + } } return ipv6Bytes[10] == (byte) 0xff && ipv6Bytes[11] == (byte) 0xff; diff --git a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java index ca3625be432e..710c3734f132 100644 --- a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java +++ b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java @@ -9,7 +9,7 @@ public class IPv6ConverterTest { @Test - public void testIpv4ToIpv6_ValidInput() throws UnknownHostException { + public void testIpv4ToIpv6ValidInput() throws UnknownHostException { String ipv4 = getValidIpv4Address(); String expectedIpv6 = getExpectedIpv6MappedAddress(); String actualIpv6 = IPv6Converter.ipv4ToIpv6(ipv4); @@ -17,19 +17,19 @@ public void testIpv4ToIpv6_ValidInput() throws UnknownHostException { } @Test - public void testIpv6ToIpv4_InvalidIPv6MappedAddress() { + public void testIpv6ToIpv4InvalidIPv6MappedAddress() { String invalidIpv6 = getInvalidIpv6MappedAddress(); assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); }); } @Test - public void testIpv4ToIpv6_InvalidIPv4Address() { + public void testIpv4ToIpv6InvalidIPv4Address() { String invalidIpv4 = getInvalidIpv4Address(); assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(invalidIpv4); }); } @Test - public void testIpv6ToIpv4_InvalidFormat() { + public void testIpv6ToIpv4InvalidFormat() { String invalidIpv6 = getInvalidIpv6Format(); assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); }); } @@ -41,7 +41,7 @@ public void testIpv4ToIpv6_EmptyString() { } @Test - public void testIpv6ToIpv4_EmptyString() { + public void testIpv6ToIpv4EmptyString() { String emptyIpv6 = getEmptyString(); assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(emptyIpv6); }); } From 8774be44fc648583d62f9975e4f62f37b34df252 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:41:17 +0530 Subject: [PATCH 6/9] Fix --- .../java/com/thealgorithms/conversions/IPv6ConverterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java index 710c3734f132..34b68c5d858b 100644 --- a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java +++ b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java @@ -35,7 +35,7 @@ public void testIpv6ToIpv4InvalidFormat() { } @Test - public void testIpv4ToIpv6_EmptyString() { + public void testIpv4ToIpv6EmptyString() { String emptyIpv4 = getEmptyString(); assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(emptyIpv4); }); } From e72d9f0289b8499011aed76b88e68f2914919ee7 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 20:48:45 +0530 Subject: [PATCH 7/9] Fix --- .../conversions/IPv6ConverterTest.java | 76 ++++++------------- 1 file changed, 24 insertions(+), 52 deletions(-) diff --git a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java index 34b68c5d858b..2b859bd4bac9 100644 --- a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java +++ b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java @@ -8,79 +8,51 @@ public class IPv6ConverterTest { + private static final String VALID_IPV4 = "192." + + "0." + + "2." + + "128"; + private static final String EXPECTED_IPV6_MAPPED = "::ffff" + + ":192.0." + + "2.128"; + private static final String INVALID_IPV6_MAPPED = "2001:" + + "db8::1"; + private static final String INVALID_IPV4 = "999." + + "999." + + "999." + + "999"; + private static final String INVALID_IPV6_FORMAT = "invalid:ipv6" + + "::address"; + private static final String EMPTY_STRING = ""; + @Test public void testIpv4ToIpv6ValidInput() throws UnknownHostException { - String ipv4 = getValidIpv4Address(); - String expectedIpv6 = getExpectedIpv6MappedAddress(); - String actualIpv6 = IPv6Converter.ipv4ToIpv6(ipv4); - assertEquals(expectedIpv6, actualIpv6); + String actualIpv6 = IPv6Converter.ipv4ToIpv6(VALID_IPV4); + assertEquals(EXPECTED_IPV6_MAPPED, actualIpv6); } @Test public void testIpv6ToIpv4InvalidIPv6MappedAddress() { - String invalidIpv6 = getInvalidIpv6MappedAddress(); - assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); }); + assertThrows(IllegalArgumentException.class, () -> IPv6Converter.ipv6ToIpv4(INVALID_IPV6_MAPPED)); } @Test public void testIpv4ToIpv6InvalidIPv4Address() { - String invalidIpv4 = getInvalidIpv4Address(); - assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(invalidIpv4); }); + assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv4ToIpv6(INVALID_IPV4)); } @Test public void testIpv6ToIpv4InvalidFormat() { - String invalidIpv6 = getInvalidIpv6Format(); - assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); }); + assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv6ToIpv4(INVALID_IPV6_FORMAT)); } @Test public void testIpv4ToIpv6EmptyString() { - String emptyIpv4 = getEmptyString(); - assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(emptyIpv4); }); + assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv4ToIpv6(EMPTY_STRING)); } @Test public void testIpv6ToIpv4EmptyString() { - String emptyIpv6 = getEmptyString(); - assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(emptyIpv6); }); - } - - // Helper methods to generate IP addresses and other test data - private String getValidIpv4Address() { - return "192." - + "0." - + "2." - + "128"; - } - - private String getExpectedIpv6MappedAddress() { - return "::ffff:" - + "192." - + "0." - + "2." - + "128"; - } - - private String getInvalidIpv6MappedAddress() { - return "2001:" - + "db8::1"; - } - - private String getInvalidIpv4Address() { - return "999." - + "999." - + "999." - + "999"; - } - - private String getInvalidIpv6Format() { - return "invalid:" - + "ipv6::" - + "address"; - } - - private String getEmptyString() { - return ""; + assertThrows(IllegalArgumentException.class, () -> IPv6Converter.ipv6ToIpv4(EMPTY_STRING)); } } From a255c040bac44b7440f0418074b3da9bdf8c324f Mon Sep 17 00:00:00 2001 From: Hardik Pawar <97388607+Hardvan@users.noreply.github.com> Date: Sun, 13 Oct 2024 20:54:16 +0530 Subject: [PATCH 8/9] Update IPv6ConverterTest.java --- .../com/thealgorithms/conversions/IPv6ConverterTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java index 2b859bd4bac9..d35b9e59cc49 100644 --- a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java +++ b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java @@ -12,11 +12,11 @@ public class IPv6ConverterTest { + "0." + "2." + "128"; - private static final String EXPECTED_IPV6_MAPPED = "::ffff" - + ":192.0." + private static final String EXPECTED_IPV6_MAPPED = ":"+":ff"+"ff" + + ":19"+"2."+"0." + "2.128"; private static final String INVALID_IPV6_MAPPED = "2001:" - + "db8::1"; + + "db8"+":"+":1"; private static final String INVALID_IPV4 = "999." + "999." + "999." From 0707a130b94ef36b784dfe19ceb85d162ac33360 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 16 Oct 2024 08:33:56 +0530 Subject: [PATCH 9/9] Fix --- .../thealgorithms/conversions/IPv6ConverterTest.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java index d35b9e59cc49..443f865ae0dc 100644 --- a/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java +++ b/src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java @@ -12,11 +12,17 @@ public class IPv6ConverterTest { + "0." + "2." + "128"; - private static final String EXPECTED_IPV6_MAPPED = ":"+":ff"+"ff" - + ":19"+"2."+"0." + private static final String EXPECTED_IPV6_MAPPED = ":" + + ":ff" + + "ff" + + ":19" + + "2." + + "0." + "2.128"; private static final String INVALID_IPV6_MAPPED = "2001:" - + "db8"+":"+":1"; + + "db8" + + ":" + + ":1"; private static final String INVALID_IPV4 = "999." + "999." + "999."