X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=util%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fprotocol%2Futil%2FIPAddressesAndPrefixesTest.java;h=8716122453481655d7e5f9cd0343fbcc5598aa0e;hb=66b8611ab00a626167bf20556d7cbb5ec00ca124;hp=22412240a42451697a0fb75be60805166a665073;hpb=139f912b0fa4885db7c2d47b18a00141be5eceb3;p=bgpcep.git diff --git a/util/src/test/java/org/opendaylight/protocol/util/IPAddressesAndPrefixesTest.java b/util/src/test/java/org/opendaylight/protocol/util/IPAddressesAndPrefixesTest.java index 22412240a4..8716122453 100644 --- a/util/src/test/java/org/opendaylight/protocol/util/IPAddressesAndPrefixesTest.java +++ b/util/src/test/java/org/opendaylight/protocol/util/IPAddressesAndPrefixesTest.java @@ -15,18 +15,23 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.common.collect.Lists; +import com.google.common.net.InetAddresses; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; +import java.net.InetSocketAddress; import java.net.UnknownHostException; import java.util.List; import org.junit.Test; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber; +import org.opendaylight.yangtools.yang.common.Uint16; public class IPAddressesAndPrefixesTest { @@ -50,77 +55,105 @@ public class IPAddressesAndPrefixesTest { @Test public void testPrefix4ForBytes() { - byte[] bytes = new byte[] { 123, 122, 4, 5 }; + byte[] bytes = new byte[]{123, 122, 4, 5}; assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8)); - assertArrayEquals(new byte[] { 102, 0, 0, 0, 8 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8"))); + assertArrayEquals(new byte[]{102, 0, 0, 0, 8}, Ipv4Util.bytesForPrefix(new Ipv4Prefix("102.0.0.0/8"))); - bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 }; + bytes = new byte[]{(byte) 255, (byte) 255, 0, 0}; assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16)); - assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16"))); + assertArrayEquals(new byte[]{(byte) 255, (byte) 255, 0, 0, 16}, + Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16"))); try { Ipv4Util.prefixForBytes(bytes, 200); fail(); - } catch (final IllegalArgumentException e) { - assertNull(e.getMessage()); + } catch (final IllegalArgumentException exc) { + assertNull(exc.getMessage()); } } @Test public void testBytesForPrefix4Begin() { - byte[] bytes = new byte[] { 123, 122, 4, 5 }; + byte[] bytes = new byte[]{123, 122, 4, 5}; assertEquals(new Ipv4Prefix("123.122.4.5/8"), Ipv4Util.prefixForBytes(bytes, 8)); - assertArrayEquals(new byte[] { 8, 102 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("102.0.0.0/8"))); - bytes = new byte[] { (byte) 255, (byte) 255, 0, 0 }; + bytes = new byte[]{(byte) 255, (byte) 255, 0, 0}; assertEquals(new Ipv4Prefix("255.255.0.0/16"), Ipv4Util.prefixForBytes(bytes, 16)); - assertArrayEquals(new byte[] { 16, (byte) 255, (byte) 255 }, Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix("255.255.0.0/16"))); + bytes = new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 0}; + assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForBytes(bytes, 0)); + } + + @Test + public void testPrefixForByteBuf() { + final ByteBuf bb = Unpooled.wrappedBuffer(new byte[]{ + 0x0e, 123, 122, + 0x40, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, + 0x00, + (byte) 0x80, 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, + (byte) 0x88, (byte) 0x89 + }); + assertEquals(new Ipv4Prefix("123.122.0.0/14"), Ipv4Util.prefixForByteBuf(bb)); + assertEquals(new Ipv6Prefix("2001::/64"), Ipv6Util.prefixForByteBuf(bb)); + assertEquals(new Ipv4Prefix("0.0.0.0/0"), Ipv4Util.prefixForByteBuf(bb)); + assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForByteBuf(bb)); + // test prefix length 128, as its signed byte value is -128 + assertEquals(new Ipv6Prefix("2001:4860:4860:1:20::8889/128"), Ipv6Util.prefixForByteBuf(bb)); } @Test public void testAddressForByteBuf() { - final ByteBuf bb = Unpooled.wrappedBuffer(new byte[] { 123, 122, 4, 5, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } ); + final ByteBuf bb = Unpooled.wrappedBuffer(new byte[]{123, 122, 4, 5, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}); assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForByteBuf(bb)); assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForByteBuf(bb)); } @Test public void testByteBufForAddress() { - final ByteBuf bb4 = Unpooled.wrappedBuffer(new byte[] { 123, 122, 4, 5} ); - final ByteBuf bb6 = Unpooled.wrappedBuffer(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } ); - assertEquals(bb4, Ipv4Util.byteBufForAddress(new Ipv4Address("123.122.4.5"))); - assertEquals(bb6, Ipv6Util.byteBufForAddress(new Ipv6Address("2001::1"))); + final ByteBuf bb4 = Unpooled.wrappedBuffer(new byte[]{123, 122, 4, 5}); + final ByteBuf bb6 = Unpooled.wrappedBuffer(new byte[]{0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}); + assertEquals(bb4, Ipv4Util.byteBufForAddress(new Ipv4AddressNoZone("123.122.4.5"))); + assertEquals(bb6, Ipv6Util.byteBufForAddress(new Ipv6AddressNoZone("2001::1"))); } @Test public void testBytesForAddress() { - assertArrayEquals(new byte[] { 12, 58, (byte) 201, 99 }, Ipv4Util.bytesForAddress(new Ipv4Address("12.58.201.99"))); - assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x01 }, Ipv6Util.bytesForAddress(new Ipv6Address("2001::1"))); + assertArrayEquals(new byte[]{12, 58, (byte) 201, 99}, + Ipv4Util.bytesForAddress(new Ipv4AddressNoZone("12.58.201.99"))); + assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01}, Ipv6Util.bytesForAddress(new Ipv6AddressNoZone("2001::1"))); } @Test public void testPrefix6ForBytes() { - final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }; + final byte[] bytes = new byte[]{0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02}; assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64)); - assertArrayEquals(new byte[] { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64"))); + assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40}, + Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64"))); + assertArrayEquals(new byte[]{0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x80}, + Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/128"))); try { Ipv6Util.prefixForBytes(bytes, 200); fail(); - } catch (final IllegalArgumentException e) { - assertNull(e.getMessage()); + } catch (final IllegalArgumentException exc) { + assertNull(exc.getMessage()); } } @Test public void testBytesForPrefix6Begin() { - final byte[] bytes = new byte[] { 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }; + byte[] bytes = new byte[]{0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02}; assertEquals(new Ipv6Prefix("2001:db8:1:2::/64"), Ipv6Util.prefixForBytes(bytes, 64)); - assertArrayEquals(new byte[] { 0x40, 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("2001:db8:1:2::/64"))); + + bytes = new byte[]{(byte) 0}; + assertEquals(new Ipv6Prefix("::/0"), Ipv6Util.prefixForBytes(bytes, 0)); } @Test @@ -131,46 +164,54 @@ public class IPAddressesAndPrefixesTest { @Test public void testPrefixList4ForBytes() { - final byte[] bytes = new byte[] { 22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100 }; + final byte[] bytes = new byte[]{22, (byte) 172, (byte) 168, 3, 8, 12, 32, (byte) 192, (byte) 168, 35, 100}; List prefs = Ipv4Util.prefixListForBytes(bytes); assertEquals( - Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), new Ipv4Prefix("192.168.35.100/32")), - prefs); + Lists.newArrayList(new Ipv4Prefix("172.168.3.0/22"), new Ipv4Prefix("12.0.0.0/8"), + new Ipv4Prefix("192.168.35.100/32")), prefs); - prefs = Ipv4Util.prefixListForBytes(new byte[] {}); + prefs = Ipv4Util.prefixListForBytes(new byte[]{}); assertTrue(prefs.isEmpty()); } @Test public void testPrefixList6ForBytes() { - final byte[] bytes = new byte[] { 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, - 0x00, 0x01, 0x00, 0x01, }; + final byte[] bytes = new byte[]{0x40, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x40, 0x20, + 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x01,}; List prefs = Ipv6Util.prefixListForBytes(bytes); - assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64"))); + assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), + new Ipv6Prefix("2001:db8:1:1::/64"))); - prefs = Ipv6Util.prefixListForBytes(new byte[] {}); + prefs = Ipv6Util.prefixListForBytes(new byte[]{}); assertTrue(prefs.isEmpty()); } - @Test(expected=UnsupportedOperationException.class) - public void testIpv4UtilPrivateConstructor() throws Throwable { - final Constructor c = Ipv4Util.class.getDeclaredConstructor(); - c.setAccessible(true); - try { - c.newInstance(); - } catch (final InvocationTargetException e) { - throw e.getCause(); - } + @Test + public void testFullFormOfIpv6() { + assertEquals(new Ipv6Address("0:0:0:0:0:0:0:1"), Ipv6Util.getFullForm(new Ipv6AddressNoZone("::1"))); } - @Test(expected=UnsupportedOperationException.class) - public void testIpv6UtilPrivateConstructor() throws Throwable { - final Constructor c = Ipv6Util.class.getDeclaredConstructor(); - c.setAccessible(true); - try { - c.newInstance(); - } catch (final InvocationTargetException e) { - throw e.getCause(); - } + @Test + public void testInetAddressToIpAddress() { + final IpAddressNoZone ipAddress = Ipv4Util.getIpAddress(InetAddresses.forString("123.42.13.8")); + assertNotNull(ipAddress.getIpv4AddressNoZone()); + assertEquals(new Ipv4Address("123.42.13.8"), ipAddress.getIpv4AddressNoZone()); + + final IpAddressNoZone ipAddress2 = Ipv4Util.getIpAddress(InetAddresses.forString("2001:db8:1:2::")); + assertNotNull(ipAddress2.getIpv6AddressNoZone()); + assertEquals(new Ipv6Address("2001:db8:1:2::"), ipAddress2.getIpv6AddressNoZone()); + } + + @Test + public void testToInetSocketAddress() { + final InetSocketAddress isa = Ipv4Util.toInetSocketAddress( + new IpAddressNoZone(new Ipv4AddressNoZone("123.42.13.8")), new PortNumber(Uint16.TEN)); + assertEquals(10, isa.getPort()); + assertEquals("123.42.13.8", InetAddresses.toAddrString(isa.getAddress())); + + final InetSocketAddress isa2 = Ipv4Util.toInetSocketAddress( + new IpAddressNoZone(new Ipv6AddressNoZone("2001:db8:1:2::")), new PortNumber(Uint16.TEN)); + assertEquals(10, isa2.getPort()); + assertEquals("2001:db8:1:2::", InetAddresses.toAddrString(isa2.getAddress())); } }