mirror of https://github.com/apache/cloudstack.git
CLOUDSTACK-4807: tests for NetUtils
Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
This commit is contained in:
parent
57cd7f3db5
commit
1f72548f57
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue