Make leading zeroes optional in the string passed to HexString.toLong
- applies to the individual colon-separated byte components of the string
- for example, you can now use "1:2:3:4:5" instead of "01:02:03:04:05"
- also added back the unit tests for HexString and added some new tests
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/util/HexString.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/util/HexString.java
index ddf0f25..eaeed5f 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/util/HexString.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/util/HexString.java
@@ -17,8 +17,6 @@
package org.projectfloodlight.openflow.util;
-import java.math.BigInteger;
-
import org.projectfloodlight.openflow.types.U8;
public class HexString {
@@ -86,13 +84,17 @@
return ret;
}
- public static long toLong(final String values) throws NumberFormatException {
- // Long.parseLong() can't handle HexStrings with MSB set. Sigh.
- BigInteger bi = new BigInteger(values.replaceAll(":", ""), 16);
- if (bi.bitLength() > 64)
- throw new NumberFormatException("Input string too big to fit in long: "
- + values);
- return bi.longValue();
+ public static long toLong(String value) throws NumberFormatException {
+ String[] octets = value.split(":");
+ if (octets.length > 8)
+ throw new NumberFormatException("Input string is too big to fit in long: " + value);
+ long l = 0;
+ for (String octet: octets) {
+ if (octet.length() > 2)
+ throw new NumberFormatException("Each colon-separated byte component must consist of 1 or 2 hex digits: " + value);
+ short s = Short.valueOf(octet, 16);
+ l = (l << 8) + s;
+ }
+ return l;
}
-
}
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/util/HexStringTest.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/util/HexStringTest.java
new file mode 100644
index 0000000..360cb5a
--- /dev/null
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/util/HexStringTest.java
@@ -0,0 +1,103 @@
+/**
+* Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
+* University
+*
+* Licensed under the Apache License, Version 2.0 (the "License"); you may
+* not use this file except in compliance with the License. You may obtain
+* a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+* License for the specific language governing permissions and limitations
+* under the License.
+**/
+
+package org.projectfloodlight.openflow.util;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * Does hexstring conversion work?
+ *
+ * @author Rob Sherwood (rob.sherwood@stanford.edu)
+ */
+public class HexStringTest {
+
+ @Test
+ public void testMarshalling() throws Exception {
+ String dpidStr = "00:00:00:23:20:2d:16:71";
+ long dpid = HexString.toLong(dpidStr);
+ String testStr = HexString.toHexString(dpid);
+ assertEquals(dpidStr, testStr);
+ }
+
+ @Test
+ public void testToLong() {
+ String dpidStr = "3e:1f:01:fc:72:8c:63:31";
+ long valid = 0x3e1f01fc728c6331L;
+ long testLong = HexString.toLong(dpidStr);
+ assertEquals(valid, testLong);
+ }
+
+ @Test
+ public void testToLong2() {
+ String dpidStr = "1f:1:fc:72:3:f:31";
+ long valid = 0x1f01fc72030f31L;
+ long testLong = HexString.toLong(dpidStr);
+ assertEquals(valid, testLong);
+ }
+
+ @Test
+ public void testToLongMSB() {
+ String dpidStr = "ca:7c:5e:d1:64:7a:95:9b";
+ long valid = -3856102927509056101L;
+ long testLong = HexString.toLong(dpidStr);
+ assertEquals(valid, testLong);
+ }
+
+ @Test(expected=NumberFormatException.class)
+ public void testToLongErrorTooManyBytes() {
+ HexString.toLong("09:08:07:06:05:04:03:02:01");
+ }
+
+ @Test(expected=NumberFormatException.class)
+ public void testToLongErrorByteValueTooLong() {
+ HexString.toLong("234:01");
+ }
+
+ @Test(expected=NumberFormatException.class)
+ public void testToLongErrorEmptyByte() {
+ HexString.toLong("03::01");
+ }
+
+ @Test(expected=NumberFormatException.class)
+ public void testToLongErrorInvalidHexDigit() {
+ HexString.toLong("ss:01");
+ }
+
+ @Test(expected=NumberFormatException.class)
+ public void testToLongErrorEmptyString() {
+ HexString.toLong("");
+ }
+
+
+ @Test
+ public void testToStringBytes() {
+ byte[] dpid = { 0, 0, 0, 0, 0, 0, 0, -1 };
+ String valid = "00:00:00:00:00:00:00:ff";
+ String testString = HexString.toHexString(dpid);
+ assertEquals(valid, testString);
+ }
+
+ @Test(expected=NumberFormatException.class)
+ public void testFromHexStringError() {
+ String invalidStr = "00:00:00:00:00:00:ffff";
+ HexString.fromHexString(invalidStr);
+ }
+}
+