BUG-730 : more unit tests to Util. 87/9987/1
authorDana Kutenicsova <dkutenic@cisco.com>
Thu, 14 Aug 2014 11:16:37 +0000 (13:16 +0200)
committerDana Kutenicsova <dkutenic@cisco.com>
Fri, 15 Aug 2014 18:45:53 +0000 (20:45 +0200)
Change-Id: Iee0db68ef7883bd7db391241d09ac6e9615cd2fb
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
util/src/main/java/org/opendaylight/protocol/util/Ipv6Util.java
util/src/main/java/org/opendaylight/protocol/util/PCEPHexDumpParser.java
util/src/test/java/org/opendaylight/protocol/util/IPAddressesAndPrefixesTest.java

index cc35b22f8538956aab25e55871059711a7592442..86968fb4e2d4fd2059c3d8cb451cc85c67565c47 100644 (file)
@@ -12,16 +12,13 @@ import com.google.common.collect.Lists;
 import com.google.common.net.InetAddresses;
 import com.google.common.primitives.Bytes;
 import com.google.common.primitives.UnsignedBytes;
-
 import io.netty.buffer.ByteBuf;
-
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
-
 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;
 
@@ -29,6 +26,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
  * Util class for creating generated Ipv6Address.
  */
 public final class Ipv6Util {
+
     private Ipv6Util() {
     }
 
@@ -82,10 +80,10 @@ public final class Ipv6Util {
     }
 
     /**
-     * Converts Ipv6Prefix to byte array.
+     * Converts Ipv6Prefix to byte array. Prefix length at the end.
      *
      * @param prefix Ipv6Prefix to be converted
-     * @return byte array
+     * @return byte array with prefix length at the end
      */
     public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
         final String p = prefix.getValue();
@@ -137,7 +135,6 @@ public final class Ipv6Util {
         if (bytes.length == 0) {
             return Collections.emptyList();
         }
-
         final List<Ipv6Prefix> list = Lists.newArrayList();
         int byteOffset = 0;
         while (byteOffset < bytes.length) {
index dd20b7ae6ecbac676546983da64a902190253067..7b6218edf6fd888ce221c13285899d3264a19a87 100644 (file)
@@ -46,8 +46,6 @@ public final class PCEPHexDumpParser {
         Preconditions.checkNotNull(is);
         try (InputStreamReader isr = new InputStreamReader(is)) {
             return parseMessages(CharStreams.toString(isr));
-        } finally {
-            is.close();
         }
     }
 
index b7106749d7aaf54c310664d1fff4e2d7092a6aca..5866c6c48d7b626ecd774a4dd8309a71d095e872 100644 (file)
@@ -13,6 +13,8 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 import com.google.common.collect.Lists;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import java.net.UnknownHostException;
 import java.util.List;
 import org.junit.Test;
@@ -54,6 +56,18 @@ public class IPAddressesAndPrefixesTest {
         assertArrayEquals(new byte[] { (byte) 255, (byte) 255, 0, 0, 16 }, Ipv4Util.bytesForPrefix(new Ipv4Prefix("255.255.0.0/16")));
     }
 
+    @Test
+    public void testBytesForPrefix4Begin() {
+        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 };
+        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")));
+    }
+
     @Test
     public void testAddress4ForBytes() {
         final byte[] bytes = new byte[] { (byte) 123, (byte) 122, (byte) 4, (byte) 5 };
@@ -80,12 +94,10 @@ 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 List<Ipv4Prefix> 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);
+    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 } );
+        assertEquals(new Ipv4Address("123.122.4.5"), Ipv4Util.addressForByteBuf(bb));
+        assertEquals(new Ipv6Address("2001::1"), Ipv6Util.addressForByteBuf(bb));
     }
 
     @Test
@@ -103,6 +115,28 @@ public class IPAddressesAndPrefixesTest {
             0x00, 0x00, 0x40 }, Ipv6Util.bytesForPrefix(new Ipv6Prefix("2001:db8:1:2::/64")));
     }
 
+    @Test
+    public void testBytesForPrefix6Begin() {
+        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[] { 0x40, 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02 }, Ipv6Util.bytesForPrefixBegin(new Ipv6Prefix("2001:db8:1:2::/64")));
+    }
+
+    @Test
+    public void testPrefixLength() {
+        assertEquals(8, Ipv4Util.getPrefixLengthBytes("2001:db8:1:2::/64"));
+        assertEquals(3, Ipv4Util.getPrefixLengthBytes("172.168.3.0/22"));
+    }
+
+    @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 List<Ipv4Prefix> 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);
+    }
+
     @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,
@@ -110,10 +144,4 @@ public class IPAddressesAndPrefixesTest {
         final List<Ipv6Prefix> prefs = Ipv6Util.prefixListForBytes(bytes);
         assertEquals(prefs, Lists.newArrayList(new Ipv6Prefix("2001:db8:1:2::/64"), new Ipv6Prefix("2001:db8:1:1::/64")));
     }
-
-    @Test
-    public void testPrefixLength() {
-        assertEquals(22, Ipv4Util.getPrefixLength(new IpPrefix(new Ipv4Prefix("172.168.3.0/22"))));
-        assertEquals(64, Ipv4Util.getPrefixLength(new IpPrefix(new Ipv6Prefix("2001:db8:1:2::/64"))));
-    }
 }