You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by wi...@apache.org on 2015/03/04 11:06:30 UTC

[46/50] [abbrv] git commit: updated refs/heads/reporter to 178a938

CLOUDSTACK-4807: tests for NetUtils

Signed-off-by: Laszlo Hornyak <la...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/1f72548f
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/1f72548f
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/1f72548f

Branch: refs/heads/reporter
Commit: 1f72548f57fb76dca3b9542f232fc539bbfd1ea6
Parents: 57cd7f3
Author: Laszlo Hornyak <la...@gmail.com>
Authored: Tue Mar 3 20:42:46 2015 +0100
Committer: Laszlo Hornyak <la...@gmail.com>
Committed: Tue Mar 3 21:21:52 2015 +0100

----------------------------------------------------------------------
 utils/src/com/cloud/utils/net/NetUtils.java     | 24 ++++++------
 .../test/com/cloud/utils/net/NetUtilsTest.java  | 39 ++++++++++++++++++++
 2 files changed, 52 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1f72548f/utils/src/com/cloud/utils/net/NetUtils.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/net/NetUtils.java b/utils/src/com/cloud/utils/net/NetUtils.java
index 8875bb9..cc48ef1 100644
--- a/utils/src/com/cloud/utils/net/NetUtils.java
+++ b/utils/src/com/cloud/utils/net/NetUtils.java
@@ -373,7 +373,7 @@ public class NetUtils {
         }
     }
 
-    public static long ip2Long(String ip) {
+    public static long ip2Long(final String ip) {
         String[] tokens = ip.split("[.]");
         assert (tokens.length == 4);
         long result = 0;
@@ -388,8 +388,8 @@ public class NetUtils {
         return result;
     }
 
-    public static String long2Ip(long ip) {
-        StringBuilder result = new StringBuilder(15);
+    public static String long2Ip(final long ip) {
+        final StringBuilder result = new StringBuilder(15);
         result.append((ip >> 24 & 0xff)).append(".");
         result.append((ip >> 16 & 0xff)).append(".");
         result.append((ip >> 8 & 0xff)).append(".");
@@ -398,8 +398,8 @@ public class NetUtils {
         return result.toString();
     }
 
-    public static long mac2Long(String macAddress) {
-        String[] tokens = macAddress.split(":");
+    public static long mac2Long(final String macAddress) {
+        final String[] tokens = macAddress.split(":");
         assert (tokens.length == 6);
         long result = 0;
         for (int i = 0; i < tokens.length; i++) {
@@ -464,12 +464,14 @@ public class NetUtils {
         return result.toString();
     }
 
-    public static String long2Mac(long macAddress) {
-        StringBuilder result = new StringBuilder(17);
-        Formatter formatter = new Formatter(result);
-        formatter.format("%02x:%02x:%02x:%02x:%02x:%02x", (macAddress >> 40) & 0xff, (macAddress >> 32) & 0xff, (macAddress >> 24) & 0xff, (macAddress >> 16) & 0xff,
-                (macAddress >> 8) & 0xff, (macAddress & 0xff));
-        formatter.close();
+    public static String long2Mac(final long macAddress) {
+        final StringBuilder result = new StringBuilder(17);
+        try (Formatter formatter = new Formatter(result)) {
+            formatter.format("%02x:%02x:%02x:%02x:%02x:%02x",
+                    (macAddress >> 40) & 0xff, (macAddress >> 32) & 0xff,
+                    (macAddress >> 24) & 0xff, (macAddress >> 16) & 0xff,
+                    (macAddress >> 8) & 0xff, (macAddress & 0xff));
+        }
         return result.toString();
     }
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/1f72548f/utils/test/com/cloud/utils/net/NetUtilsTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/net/NetUtilsTest.java b/utils/test/com/cloud/utils/net/NetUtilsTest.java
index 62476ea..c4eceab 100644
--- a/utils/test/com/cloud/utils/net/NetUtilsTest.java
+++ b/utils/test/com/cloud/utils/net/NetUtilsTest.java
@@ -303,4 +303,43 @@ public class NetUtilsTest {
 
         assertTrue(NetUtils.validateGuestCidr(guestCidr));
     }
+
+    @Test
+    public void testMac2Long() {
+        assertEquals(0l, NetUtils.mac2Long("00:00:00:00:00:00"));
+        assertEquals(1l, NetUtils.mac2Long("00:00:00:00:00:01"));
+        assertEquals(0xFFl, NetUtils.mac2Long("00:00:00:00:00:FF"));
+        assertEquals(0xFFAAl, NetUtils.mac2Long("00:00:00:00:FF:AA"));
+        assertEquals(0x11FFAAl, NetUtils.mac2Long("00:00:00:11:FF:AA"));
+        assertEquals(0x12345678l, NetUtils.mac2Long("00:00:12:34:56:78"));
+        assertEquals(0x123456789Al, NetUtils.mac2Long("00:12:34:56:78:9A"));
+        assertEquals(0x123456789ABCl, NetUtils.mac2Long("12:34:56:78:9A:BC"));
+    }
+
+    @Test
+    public void testLong2Mac() {
+        assertEquals("00:00:00:00:00:00", NetUtils.long2Mac(0l));
+        assertEquals("00:00:00:00:00:01", NetUtils.long2Mac(1l));
+        assertEquals("00:00:00:00:00:ff", NetUtils.long2Mac(0xFFl));
+        assertEquals("00:00:00:00:ff:aa", NetUtils.long2Mac(0xFFAAl));
+        assertEquals("00:00:00:11:ff:aa", NetUtils.long2Mac(0x11FFAAl));
+        assertEquals("00:00:12:34:56:78", NetUtils.long2Mac(0x12345678l));
+        assertEquals("00:12:34:56:78:9a", NetUtils.long2Mac(0x123456789Al));
+        assertEquals("12:34:56:78:9a:bc", NetUtils.long2Mac(0x123456789ABCl));
+    }
+
+    @Test
+    public void testIp2Long() {
+        assertEquals(0x7f000001l, NetUtils.ip2Long("127.0.0.1"));
+        assertEquals(0xc0a80001l, NetUtils.ip2Long("192.168.0.1"));
+        assertEquals(0x08080808l, NetUtils.ip2Long("8.8.8.8"));
+    }
+
+    @Test
+    public void testLong2Ip() {
+        assertEquals("127.0.0.1", NetUtils.long2Ip(0x7f000001l));
+        assertEquals("192.168.0.1", NetUtils.long2Ip(0xc0a80001l));
+        assertEquals("8.8.8.8", NetUtils.long2Ip(0x08080808l));
+    }
+
 }