changeset 675:1002a35b0cdd

NcDataHelper bugfixes
author Devel 2
date Tue, 21 Nov 2017 09:29:19 +0100
parents b88d3359c1c2
children f1547b730293
files stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java stress-tester/src/test/java/com/passus/st/reader/nc/NcDataHelperTest.java
diffstat 2 files changed, 88 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java	Mon Nov 20 15:15:56 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java	Tue Nov 21 09:29:19 2017 +0100
@@ -1,6 +1,7 @@
 package com.passus.st.reader.nc;
 
 import com.passus.commons.AsciiUtils;
+import com.passus.commons.utils.FormatUtils;
 import com.passus.data.ByteBuff;
 import com.passus.data.ByteBuffUtils;
 import com.passus.data.ByteString;
@@ -76,7 +77,7 @@
     }
 
     public int getIntVLC(byte[] data, int startIndex, int endIndex, MutableInt length) {
-        int value = data[startIndex];
+        int value = data[startIndex] & 0xFF;
         int readed = 1;
         if ((value & VLC_INT_TRESHOLD) != 0) {
             startIndex++;
@@ -145,15 +146,15 @@
     }
 
     public int getLongVLC(byte[] data, int startIndex, int endIndex, MutableLong length) {
-        int b1 = data[startIndex++];
-        int b2 = data[startIndex++];
+        int b1 = data[startIndex++] & 0xFF;
+        int b2 = data[startIndex++] & 0xFF;
         long value = (b1 << 8) + b2;
         int readed = 2;
 
         if ((value & VLC_LONG_TRESHOLD) != 0) {
-            value &= VLC_LONG_TRESHOLD;
+            value &= ~VLC_LONG_TRESHOLD;
             if (value == 0 || value > 8) {
-                throw new IllegalArgumentException("Invalid length byte value.");
+                throw new IllegalArgumentException("Invalid length byte value " + value + ".");
             } else if (endIndex - startIndex < value) {
                 throw new IndexOutOfBoundsException();
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/reader/nc/NcDataHelperTest.java	Tue Nov 21 09:29:19 2017 +0100
@@ -0,0 +1,82 @@
+package com.passus.st.reader.nc;
+
+import com.passus.data.ByteBuff;
+import com.passus.data.HeapByteBuff;
+import java.nio.ByteBuffer;
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.commons.lang3.mutable.MutableLong;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class NcDataHelperTest {
+
+    private final NcDataHelper helper = new NcDataHelper();
+
+    @DataProvider(name = "validIntVLC")
+    public Object[][] validIntVLC() {
+        return new Object[][]{
+            {new byte[]{0x00}, 1, 0x00},
+            {new byte[]{0x7F}, 1, 0x7F},
+            {new byte[]{(byte) 0x81, (byte) 0x03}, 2, 0x03}
+        };
+    }
+
+    @DataProvider(name = "validLongVLC")
+    public Object[][] validLongVLC() {
+        return new Object[][]{
+            {new byte[]{0x00, 0x00}, 2, 0x00L},
+            {new byte[]{0x00, 0x7F}, 2, 0x7FL},
+            {new byte[]{(byte) 0x80, 0x01, 0x03}, 3, 0x03L}
+        };
+    }
+
+    @Test(dataProvider = "validIntVLC")
+    public void testReadIntVLC_ByteBuff(byte[] data, int expectedBytes, int expectedLength) {
+        ByteBuff buff = new HeapByteBuff();
+        buff.append(data);
+        assertEquals(expectedLength, helper.readIntVLC(buff));
+        assertEquals(0, buff.readableBytes());
+    }
+
+    @Test(dataProvider = "validIntVLC")
+    public void testReadIntVLC_ByteBuffer(byte[] data, int expectedBytes, int expectedLength) {
+        ByteBuffer buff = ByteBuffer.wrap(data);
+        assertEquals(expectedLength, helper.readIntVLC(buff));
+        assertEquals(0, buff.remaining());
+    }
+
+    @Test(dataProvider = "validIntVLC")
+    public void testGetIntVLC(byte[] data, int expectedBytes, int expectedLength) {
+        MutableInt length = new MutableInt();
+        assertEquals(expectedBytes, helper.getIntVLC(data, 0, data.length, length));
+        assertEquals(expectedLength, length.intValue());
+    }
+
+    @Test(dataProvider = "validLongVLC")
+    public void testReadLongVLC_ByteBuff(byte[] data, int expectedBytes, long expectedLength) {
+        ByteBuff buff = new HeapByteBuff();
+        buff.append(data);
+        assertEquals(expectedLength, helper.readLongVLC(buff));
+        assertEquals(0, buff.readableBytes());
+    }
+
+    @Test(dataProvider = "validLongVLC")
+    public void testReadLongVLC_ByteBuffer(byte[] data, int expectedBytes, long expectedLength) {
+        ByteBuffer buff = ByteBuffer.wrap(data);
+        assertEquals(expectedLength, helper.readLongVLC(buff));
+        assertEquals(0, buff.remaining());
+    }
+
+    @Test(dataProvider = "validLongVLC")
+    public void testGetLongVLC(byte[] data, int expectedBytes, long expectedLength) {
+        MutableLong length = new MutableLong();
+        assertEquals(expectedBytes, helper.getLongVLC(data, 0, data.length, length));
+        assertEquals(expectedLength, length.longValue());
+    }
+
+}