Search in sources :

Example 61 with InetAddress

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) {
    }
}
Also used : InetAddress(java.net.InetAddress)

Example 62 with InetAddress

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));
    }
}
Also used : InetAddress(java.net.InetAddress)

Example 63 with InetAddress

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));
}
Also used : Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress)

Example 64 with InetAddress

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) {
    }
}
Also used : InetAddress(java.net.InetAddress)

Example 65 with InetAddress

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);
}
Also used : InetAddress(java.net.InetAddress)

Aggregations

InetAddress (java.net.InetAddress)2225 UnknownHostException (java.net.UnknownHostException)423 IOException (java.io.IOException)295 Test (org.junit.Test)289 InetSocketAddress (java.net.InetSocketAddress)251 NetworkInterface (java.net.NetworkInterface)192 ArrayList (java.util.ArrayList)174 SocketException (java.net.SocketException)154 HashMap (java.util.HashMap)104 Inet6Address (java.net.Inet6Address)103 Inet4Address (java.net.Inet4Address)96 Socket (java.net.Socket)78 DatagramPacket (java.net.DatagramPacket)75 LinkAddress (android.net.LinkAddress)70 DatagramSocket (java.net.DatagramSocket)67 Token (org.apache.cassandra.dht.Token)67 Map (java.util.Map)59 RouteInfo (android.net.RouteInfo)56 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)52