[ONOS-6661] MacAddress validation in not valid
Change-Id: Ib4e6a59ca4e158b79376b7fc0a671b7f8c33ba9a
diff --git a/utils/misc/src/main/java/org/onlab/packet/MacAddress.java b/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
index 1027708..7908d71 100644
--- a/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
+++ b/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
@@ -18,12 +18,15 @@
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* The class representing MAC address.
*/
public class MacAddress {
+ private static final Pattern MAC_PATTERN = Pattern.compile("^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$");
/**
* First MAC address in ONOS OUI range.
*/
@@ -77,13 +80,12 @@
* @throws IllegalArgumentException if the string cannot be parsed as a MAC address.
*/
public static MacAddress valueOf(final String address) {
- final String[] elements = address.split(":");
- if (elements.length != MacAddress.MAC_ADDRESS_LENGTH) {
+ if (!isValid(address)) {
throw new IllegalArgumentException(
"Specified MAC Address must contain 12 hex digits"
+ " separated pairwise by :'s.");
}
-
+ final String[] elements = address.split(":");
final byte[] addressInBytes = new byte[MacAddress.MAC_ADDRESS_LENGTH];
for (int i = 0; i < MacAddress.MAC_ADDRESS_LENGTH; i++) {
final String element = elements[i];
@@ -273,4 +275,9 @@
}
return builder.toString();
}
+
+ private static boolean isValid(final String mac) {
+ Matcher matcher = MAC_PATTERN.matcher(mac);
+ return matcher.matches();
+ }
}
diff --git a/utils/misc/src/test/java/org/onlab/packet/MacAddressTest.java b/utils/misc/src/test/java/org/onlab/packet/MacAddressTest.java
index 38a3b43..468107e 100644
--- a/utils/misc/src/test/java/org/onlab/packet/MacAddressTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/MacAddressTest.java
@@ -46,6 +46,10 @@
private static final String INVALID_STR = "invalid";
private static final byte[] INVALID_BYTE = {(byte) 0xaa};
+ private static final String INVALID_MAC_ADDITIONAL_COLON = "00:00:00:00:00:00:";
+ private static final String INVALID_MAC_OCTET_TOO_LONG = "000:000:000:000:000:00";
+ private static final String INVALID_MAC_OCTET_TOO_SHORT = "00:0:00:00:00:00";
+
@Test
public void testValueOfString() throws Exception {
@@ -57,6 +61,21 @@
MacAddress.valueOf(INVALID_STR);
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testValueOfInvalidStringWithAdditionalColon() throws Exception {
+ MacAddress.valueOf(INVALID_MAC_ADDITIONAL_COLON);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testValueOfInvalidStringWithTooLongOctet() throws Exception {
+ MacAddress.valueOf(INVALID_MAC_OCTET_TOO_LONG);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testValueOfInvalidStringWithTooShortOctet() throws Exception {
+ MacAddress.valueOf(INVALID_MAC_OCTET_TOO_SHORT);
+ }
+
@Test
public void testValueOfByte() throws Exception {
assertArrayEquals(MAC_ONOS_BYTE, MacAddress.valueOf(MAC_ONOS_BYTE).toBytes());