Replaced bitwise operator usage with java lang APIs for robustness. Had found that... 05/22805/2
authorSteven Pisarski <s.pisarski@cablelabs.com>
Wed, 17 Jun 2015 15:41:36 +0000 (09:41 -0600)
committerSteve Pisarski <s.pisarski@cablelabs.com>
Tue, 23 Jun 2015 19:51:57 +0000 (19:51 +0000)
Change-Id: I6a0d62da87acd3fa448245a990a739276fc3b7c9
Signed-off-by: Steven Pisarski <s.pisarski@cablelabs.com>
packetcable-driver/src/main/java/org/umu/cops/stack/COPSMsgParser.java
packetcable-driver/src/test/java/org/umu/cops/stack/COPSMsgParserTest.java

index 84279912d9ee4b888db90f82c1f944ed09d836e3..e2b4c1fda8039e8eb0208fc090ab567aa5042cd4 100644 (file)
@@ -6,6 +6,8 @@
 
 package org.umu.cops.stack;
 
+import com.google.common.primitives.Ints;
+import com.google.common.primitives.Shorts;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.umu.cops.stack.COPSHeader.Flag;
@@ -74,10 +76,7 @@ public class COPSMsgParser {
      * @return - a 2 byte array
      */
     public static byte[] shortToBytes(final short val) {
-        final byte[] out = new byte[2];
-        out[0] = (byte) (val >> 8);
-        out[1] = (byte) val;
-        return out;
+        return Shorts.toByteArray(val);
     }
 
     /**
@@ -87,10 +86,15 @@ public class COPSMsgParser {
      * @return - the short value
      */
     public static short bytesToShort(final byte byte1, final byte byte2) {
-        short out = 0;
-        out |= ((short) byte1) << 8;
-        out |= ((short) byte2) & 0xFF;
-        return out;
+        return Shorts.fromBytes(byte1, byte2);
+    }
+
+    public static byte[] intToBytes(final int value) {
+        return Ints.toByteArray(value);
+    }
+
+    public static int bytesToInt(final byte byte1, final byte byte2, final byte byte3, final byte byte4) {
+        return Ints.fromBytes(byte1, byte2, byte3, byte4);
     }
 
     private static COPSMsg readBody(final Socket socket, final COPSHeaderData hdrData) throws IOException, COPSException {
@@ -98,7 +102,6 @@ public class COPSMsgParser {
         final int expectedBytes = hdrData.msgByteCount - hdrData.header.getHdrLength();
         final byte[] buffer = new byte[expectedBytes];
         final int nread = readData(socket, buffer, expectedBytes);
-//        buffer[expectedBytes] = (byte) '\0';
         if (nread != expectedBytes) {
             throw new COPSException("Bad COPS message");
         }
index 847d1855c16becf489e7310d353993137463ea8a..9615d11301e6bdc2ef076278c9b1345dadcaacb4 100644 (file)
@@ -1,9 +1,15 @@
+/*
+ * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
+ */
+
 package org.umu.cops.stack;
 
 import org.junit.Assert;
 import org.junit.Test;
 import org.pcmm.rcd.IPCMMClient;
 
+import java.util.Random;
+
 /**
  * Tests the public static COPSMsgParser methods
  */
@@ -57,6 +63,51 @@ public class COPSMsgParserTest {
         Assert.assertEquals(byte2, outBytes[1]);
     }
 
+    @Test
+    public void bytesToShortAndBack() {
+        final Random rnd = new Random();
+        final short val = (short)rnd.nextInt();
+        final byte[] bytes = COPSMsgParser.shortToBytes(val);
+        final short parsed = COPSMsgParser.bytesToShort(bytes[0], bytes[1]);
+        Assert.assertEquals(val, parsed);
+    }
+
+    @Test
+    public void testBytesToIntMin() {
+        final byte byte1 = (byte)0;
+        final byte byte2 = (byte)0;
+        final byte byte3 = (byte)0;
+        final byte byte4 = (byte)0;
+        final int val = COPSMsgParser.bytesToInt(byte1, byte2, byte3, byte4);
+        final byte[] outBytes = COPSMsgParser.intToBytes(val);
+        Assert.assertEquals(byte1, outBytes[0]);
+        Assert.assertEquals(byte2, outBytes[1]);
+        Assert.assertEquals(byte3, outBytes[2]);
+        Assert.assertEquals(byte4, outBytes[3]);
+        Assert.assertEquals(0, val);
+    }
+
+    @Test
+    public void intToBytesAndBack() {
+        final int val = 100001;
+        final byte[] bytes = COPSMsgParser.intToBytes(val);
+        final int parsed = COPSMsgParser.bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
+        Assert.assertEquals(val, parsed);
+        System.out.println("Sucessfully converted value - " + val);
+    }
+
+    @Test
+    public void randomIntToBytesAndBack() {
+        for (int i = 0; i < 5; i++) {
+            final Random rnd = new Random();
+            final int val = rnd.nextInt();
+            final byte[] bytes = COPSMsgParser.intToBytes(val);
+            final int parsed = COPSMsgParser.bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
+            Assert.assertEquals(val, parsed);
+            System.out.println("Sucessfully converted value - " + val);
+        }
+    }
+
     @Test
     public void testBytesToShortPCMMClientType() {
         final byte[] outBytes = COPSMsgParser.shortToBytes(IPCMMClient.CLIENT_TYPE);