aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVadim Yanitskiy <axilirator@gmail.com>2020-02-19 05:55:49 +0700
committerVadim Yanitskiy <axilirator@gmail.com>2020-02-19 18:23:40 +0700
commit6e270e2530fc095c7fe96296aa0d0fd6c0c64796 (patch)
tree3b08381d6d3296dce6375fde538de2a9351383f5
parent053bebc7ce2fa277fbb0839e4e6e4178f9320369 (diff)
tests/bitvec: add a unit test for bitvec_read_field()
The aim of this unit test is to demonstrate the problem described in OS#4388: bitvec_read_field() can never return negative value on error (e.g. out of bounds access). Change-Id: I340ab5799fa53d5345edb02f3e2a3655527705c0 Related: OS#4388
-rw-r--r--tests/bitvec/bitvec_test.c42
-rw-r--r--tests/bitvec/bitvec_test.ok18
2 files changed, 60 insertions, 0 deletions
diff --git a/tests/bitvec/bitvec_test.c b/tests/bitvec/bitvec_test.c
index fbf5c5dd..b4764e68 100644
--- a/tests/bitvec/bitvec_test.c
+++ b/tests/bitvec/bitvec_test.c
@@ -222,6 +222,45 @@ static void test_tailroom()
}
}
+static void test_bitvec_read_field(void)
+{
+ uint8_t data[8] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xeb, 0xda, 0xed };
+ struct bitvec bv = {
+ .data_len = sizeof(data),
+ .data = data,
+ .cur_bit = 0,
+ };
+
+ unsigned int readIndex;
+ uint64_t field;
+
+#define _bitvec_read_field(idx, len) \
+ readIndex = idx; \
+ field = bitvec_read_field(&bv, &readIndex, len); \
+ printf("bitvec_read_field(idx=%u, len=%u) => %" PRIx64 "\n", idx, len, field);
+
+ _bitvec_read_field(0, 64);
+ _bitvec_read_field(0, 32);
+ _bitvec_read_field(0, 16);
+ _bitvec_read_field(0, 8);
+ _bitvec_read_field(0, 0);
+
+ _bitvec_read_field(8, 8);
+ _bitvec_read_field(8, 4);
+ _bitvec_read_field(8, 0);
+
+ _bitvec_read_field(10, 9);
+ _bitvec_read_field(10, 7);
+ _bitvec_read_field(10, 5);
+ _bitvec_read_field(10, 3);
+ _bitvec_read_field(10, 1);
+
+ /* Out of bounds (see OS#4388) */
+ _bitvec_read_field(8 * 8 * 8, 16); /* index too far */
+ _bitvec_read_field(0, 8 * 8 + 1); /* too many bits */
+ _bitvec_read_field(8 * 8, 16); /* 16 bits past */
+}
+
int main(int argc, char **argv)
{
struct bitvec bv;
@@ -331,6 +370,9 @@ int main(int argc, char **argv)
test_used_bytes();
test_tailroom();
+ printf("\ntest bitvec_read_field():\n");
+ test_bitvec_read_field();
+
printf("\nbitvec ok.\n");
return 0;
}
diff --git a/tests/bitvec/bitvec_test.ok b/tests/bitvec/bitvec_test.ok
index a48912d5..29745545 100644
--- a/tests/bitvec/bitvec_test.ok
+++ b/tests/bitvec/bitvec_test.ok
@@ -170,4 +170,22 @@ bitvec_runlength....
bitvec bytes used.
+test bitvec_read_field():
+bitvec_read_field(idx=0, len=64) => deadbeeffeebdaed
+bitvec_read_field(idx=0, len=32) => deadbeef
+bitvec_read_field(idx=0, len=16) => dead
+bitvec_read_field(idx=0, len=8) => de
+bitvec_read_field(idx=0, len=0) => 0
+bitvec_read_field(idx=8, len=8) => ad
+bitvec_read_field(idx=8, len=4) => a
+bitvec_read_field(idx=8, len=0) => 0
+bitvec_read_field(idx=10, len=9) => 16d
+bitvec_read_field(idx=10, len=7) => 5b
+bitvec_read_field(idx=10, len=5) => 16
+bitvec_read_field(idx=10, len=3) => 5
+bitvec_read_field(idx=10, len=1) => 1
+bitvec_read_field(idx=512, len=16) => ffffffffffffffea
+bitvec_read_field(idx=0, len=65) => ffffffffffffffea
+bitvec_read_field(idx=64, len=16) => ffffffffffffffea
+
bitvec ok.