use of java.net.InetAddress in project guava by google.
the class InetAddressesTest method testDecrementIPv6.
public void testDecrementIPv6() throws UnknownHostException {
InetAddress addressV6660 = InetAddress.getByName("2001:db8::6600");
InetAddress addressV666ff = InetAddress.getByName("2001:db8::66ff");
InetAddress addressV6670 = InetAddress.getByName("2001:db8::6700");
InetAddress address = addressV6670;
address = InetAddresses.decrement(address);
assertEquals(addressV666ff, address);
for (int i = 0; i < 255; i++) {
address = InetAddresses.decrement(address);
}
assertEquals(addressV6660, address);
InetAddress addressV6000000 = InetAddress.getByName("0:0:0:0:0:0:0:0");
address = addressV6000000;
try {
address = InetAddresses.decrement(address);
fail();
} catch (IllegalArgumentException expected) {
}
}
use of java.net.InetAddress in project guava by google.
the class InetAddressesTest method testConvertDottedQuadToHex.
public void testConvertDottedQuadToHex() throws UnknownHostException {
String[] ipStrings = { "7::0.128.0.127", "7::0.128.0.128", "7::128.128.0.127", "7::0.128.128.127" };
for (String ipString : ipStrings) {
// Shouldn't hit DNS, because it's an IP string literal.
InetAddress ipv6Addr = InetAddress.getByName(ipString);
assertEquals(ipv6Addr, InetAddresses.forString(ipString));
assertTrue(InetAddresses.isInetAddress(ipString));
}
}
use of java.net.InetAddress in project guava by google.
the class InetAddressesTest method test6to4Addresses.
public void test6to4Addresses() {
String[] non6to4Addresses = { "::1.2.3.4", "3ffe::1", "::", "::1" };
for (int i = 0; i < non6to4Addresses.length; i++) {
InetAddress ip = InetAddresses.forString(non6to4Addresses[i]);
assertFalse(InetAddresses.is6to4Address((Inet6Address) ip));
try {
InetAddresses.get6to4IPv4Address((Inet6Address) ip);
fail("IllegalArgumentException expected for '" + non6to4Addresses[i] + "'");
} catch (IllegalArgumentException expected) {
// expected behavior
}
}
String valid6to4Address = "2002:0102:0304::1";
String ipv4Str = "1.2.3.4";
InetAddress ipv4 = InetAddresses.forString(ipv4Str);
InetAddress ip = InetAddresses.forString(valid6to4Address);
assertTrue(InetAddresses.is6to4Address((Inet6Address) ip));
assertEquals(ipv4, InetAddresses.get6to4IPv4Address((Inet6Address) ip));
}
use of java.net.InetAddress in project guava by google.
the class InetAddressesTest method testIncrementIPv6.
public void testIncrementIPv6() throws UnknownHostException {
InetAddress addressV6_66_0 = InetAddress.getByName("2001:db8::6600");
InetAddress addressV6_66_ff = InetAddress.getByName("2001:db8::66ff");
InetAddress addressV6_67_0 = InetAddress.getByName("2001:db8::6700");
InetAddress address = addressV6_66_0;
for (int i = 0; i < 255; i++) {
address = InetAddresses.increment(address);
}
assertEquals(addressV6_66_ff, address);
address = InetAddresses.increment(address);
assertEquals(addressV6_67_0, address);
InetAddress addressV6_ffffff = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
address = addressV6_ffffff;
try {
address = InetAddresses.increment(address);
fail();
} catch (IllegalArgumentException expected) {
}
}
use of java.net.InetAddress in project guava by google.
the class InetAddressesTest method testGetCoercedIPv4Address.
public void testGetCoercedIPv4Address() {
// Check that a coerced IPv4 address is unaltered.
InetAddress localHost4 = InetAddresses.forString("127.0.0.1");
assertEquals(localHost4, InetAddresses.getCoercedIPv4Address(localHost4));
// ::1 special case
assertEquals(localHost4, InetAddresses.getCoercedIPv4Address(InetAddresses.forString("::1")));
// :: special case
assertEquals(InetAddresses.forString("0.0.0.0"), InetAddresses.getCoercedIPv4Address(InetAddresses.forString("::")));
// test compat address (should be hashed)
assertTrue(InetAddresses.forString("1.2.3.4") != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("::1.2.3.4")));
// test 6to4 address (should be hashed)
assertTrue(InetAddresses.forString("1.2.3.4") != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0102:0304::1")));
// 2 6to4 addresses differing in the embedded IPv4 address should
// hash to the different values.
assertTrue(InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0102:0304::1")) != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0506:0708::1")));
// 2 6to4 addresses NOT differing in the embedded IPv4 address should
// hash to the same value.
assertTrue(InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0102:0304::1")) != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2002:0102:0304::2")));
// test Teredo address (should be hashed)
assertTrue(InetAddresses.forString("192.0.2.45") != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2001:0000:4136:e378:8000:63bf:3fff:fdd2")));
// 2 Teredo addresses differing in the embedded IPv4 address should
// hash to the different values.
assertTrue(InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2001:0000:4136:e378:8000:63bf:3fff:fdd2")) != InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2001:0000:4136:e379:8000:63bf:3fff:fdd2")));
// 2 Teredo addresses NOT differing in the embedded IPv4 address should
// hash to the same value.
assertEquals(InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2001:0000:4136:e378:8000:63bf:3fff:fdd2")), InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2001:0000:4136:e378:9000:63bf:3fff:fdd2")));
// Test that an address hashes in to the 224.0.0.0/3 number-space.
InetAddress coerced = InetAddresses.getCoercedIPv4Address(InetAddresses.forString("2001:4860::1"));
assertTrue(0xe0000000 <= InetAddresses.coerceToInteger(coerced));
assertTrue(InetAddresses.coerceToInteger(coerced) <= 0xfffffffe);
}
Aggregations