From 326aff85309d6ddbf7ccc74d4fc46dc0e0b4b002 Mon Sep 17 00:00:00 2001 From: Hideyuki Tai Date: Thu, 30 Jan 2014 02:17:25 -0800 Subject: [PATCH] Fix for bug 830. Fixed bugs in getUnsigned methods of NetUtils class. * Fixed bugs that getUnsignedByte(byte) and getUnsignedShort(short) handled 0 incorrectly. * Added tests for getUnsignedByte(byte) and getUnsignedShort(short). Change-Id: Ie67fb5c07bed31d16d4a52068dda7735fb7cd5e2 Signed-off-by: Hideyuki Tai (cherry picked from commit 37d7a25950e5132d7dabfe4a66d81becbc6ef4dd) --- .../controller/sal/utils/NetUtils.java | 7 +++--- .../controller/sal/utils/NetUtilsTest.java | 23 +++++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java index 6c3424c616..dc341625af 100644 --- a/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java +++ b/opendaylight/sal/api/src/main/java/org/opendaylight/controller/sal/utils/NetUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -482,7 +482,7 @@ public abstract class NetUtils { * @return the int variable containing the unsigned byte value */ public static int getUnsignedByte(byte b) { - return (b > 0) ? (int) b : (b & 0x7F | 0x80); + return b & 0xFF; } /** @@ -493,7 +493,7 @@ public abstract class NetUtils { * @return the int variable containing the unsigned short value */ public static int getUnsignedShort(short s) { - return (s > 0) ? (int) s : (s & 0x7FFF | 0x8000); + return s & 0xFFFF; } /** @@ -520,5 +520,4 @@ public abstract class NetUtils { public static byte[] getBroadcastMACAddr() { return Arrays.copyOf(BroadcastMACAddr, BroadcastMACAddr.length); } - } diff --git a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java index b8bc6fb447..a2b12782ac 100644 --- a/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java +++ b/opendaylight/sal/api/src/test/java/org/opendaylight/controller/sal/utils/NetUtilsTest.java @@ -1,6 +1,5 @@ - /* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2013-2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -449,4 +448,24 @@ public class NetUtilsTest { Assert.assertTrue(NetUtils .isIPAddressValid("2001:420:281:1004:407a:57f4:4d15:c355")); } + + @Test + public void testGetUnsignedByte() { + Assert.assertEquals(0, NetUtils.getUnsignedByte((byte) 0x00)); + Assert.assertEquals(1, NetUtils.getUnsignedByte((byte) 0x01)); + Assert.assertEquals(127, NetUtils.getUnsignedByte((byte) 0x7f)); + + Assert.assertEquals(128, NetUtils.getUnsignedByte((byte) 0x80)); + Assert.assertEquals(255, NetUtils.getUnsignedByte((byte) 0xff)); + } + + @Test + public void testGetUnsignedShort() { + Assert.assertEquals(0, NetUtils.getUnsignedShort((short) 0x0000)); + Assert.assertEquals(1, NetUtils.getUnsignedShort((short) 0x0001)); + Assert.assertEquals(32767, NetUtils.getUnsignedShort((short) 0x7fff)); + + Assert.assertEquals(32768, NetUtils.getUnsignedShort((short) 0x8000)); + Assert.assertEquals(65535, NetUtils.getUnsignedShort((short) 0xffff)); + } } -- 2.36.6