BUG-8252 Fix IP prefix length util 92/55692/2
authorKevin Wang <kevixw@gmail.com>
Thu, 20 Apr 2017 00:28:47 +0000 (17:28 -0700)
committerClaudio D. Gasparini <claudio.gasparini@pantheon.tech>
Thu, 20 Apr 2017 13:27:54 +0000 (13:27 +0000)
The byte representing IP prefix length is a unsigned byte.
The length should be converted to an unsigned int explicitly.

Change-Id: I0c1df171822858825da69f39c91de9ed332865f8
Signed-off-by: Kevin Wang <kevixw@gmail.com>
util/src/main/java/org/opendaylight/protocol/util/Ipv4Util.java
util/src/main/java/org/opendaylight/protocol/util/Ipv6Util.java
util/src/test/java/org/opendaylight/protocol/util/IPAddressesAndPrefixesTest.java

index 4eb674387feea4a05a4a1602af45f6282487b512..8561444afbba7440a126b278cb9534dc9ee6b8a5 100644 (file)
@@ -149,7 +149,7 @@ public final class Ipv4Util {
      * @return Ipv4Prefix object
      */
     public static Ipv4Prefix prefixForByteBuf(final ByteBuf buf) {
-        final int prefixLength = buf.readByte();
+        final int prefixLength = UnsignedBytes.toInt(buf.readByte());
         final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
         final int readable = buf.readableBytes();
         Preconditions.checkArgument(size <= readable, "Illegal length of IP prefix: %s/%s", size, readable);
index 3e4da1dfb3e94f5fdf2cc39dc8fb33a4998bcbe8..a6fefaf22592d3cdfd0aec27ac7ef41a73c60f6f 100644 (file)
@@ -125,7 +125,7 @@ public final class Ipv6Util {
      * @return Ipv6Prefix object
      */
     public static Ipv6Prefix prefixForByteBuf(final ByteBuf buf) {
-        final int prefixLength = buf.readByte();
+        final int prefixLength = UnsignedBytes.toInt(buf.readByte());
         final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
         final int readable = buf.readableBytes();
         Preconditions.checkArgument(size <= readable, "Illegal length of IP prefix: %s/%s", size, readable);
index 7c65fef99670e9c8271fce935886e3fd781c6556..7c8e0e342e9535f82c11e235d97fa4f4c6b7dcd5 100644 (file)
@@ -91,9 +91,19 @@ public class IPAddressesAndPrefixesTest {
 
     @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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } );
+        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