diff mbox

[6/8] msgb/test: Add tests for msgb_resize_area and msgb_copy

Message ID 1448627180-10603-7-git-send-email-jerlbeck@sysmocom.de
State Accepted
Headers show

Commit Message

Jacob Erlbeck Nov. 27, 2015, 12:26 p.m. UTC
Sponsored-by: On-Waves ehf
---
 tests/msgb/msgb_test.c  | 137 ++++++++++++++++++++++++++++++++++++++++++++++++
 tests/msgb/msgb_test.ok |  12 +++++
 2 files changed, 149 insertions(+)
diff mbox

Patch

diff --git a/tests/msgb/msgb_test.c b/tests/msgb/msgb_test.c
index 260aca5..e6cb33e 100644
--- a/tests/msgb/msgb_test.c
+++ b/tests/msgb/msgb_test.c
@@ -121,6 +121,141 @@  static void test_msgb_api()
 	msgb_free(msg);
 }
 
+static void test_msgb_copy()
+{
+	struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
+	struct msgb *msg2;
+	int i;
+
+	printf("Testing msgb_copy\n");
+
+	msg->l1h = msgb_put(msg, 20);
+	msg->l2h = msgb_put(msg, 20);
+	msg->l3h = msgb_put(msg, 20);
+	msg->l4h = msgb_put(msg, 20);
+
+	OSMO_ASSERT(msgb_length(msg) == 80);
+	for (i = 0; i < msgb_length(msg); i++)
+		msg->data[i] = (uint8_t)i;
+
+	msg2 = msgb_copy(msg, "copy");
+
+	OSMO_ASSERT(msgb_length(msg) == msgb_length(msg2));
+	OSMO_ASSERT(msgb_l1len(msg) == msgb_l1len(msg2));
+	OSMO_ASSERT(msgb_l2len(msg) == msgb_l2len(msg2));
+	OSMO_ASSERT(msgb_l3len(msg) == msgb_l3len(msg2));
+	OSMO_ASSERT(msg->tail - msg->l4h == msg2->tail - msg2->l4h);
+
+	for (i = 0; i < msgb_length(msg2); i++)
+		OSMO_ASSERT(msg2->data[i] == (uint8_t)i);
+
+	printf("Src: %s\n", msgb_hexdump(msg));
+	printf("Dst: %s\n", msgb_hexdump(msg));
+
+	msgb_free(msg);
+	msgb_free(msg2);
+}
+
+static void test_msgb_resize_area()
+{
+	struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
+	int rc;
+	volatile int e = 0;
+	int i, saved_i;
+	uint8_t *cptr, *old_l3h;
+
+	osmo_set_panic_handler(osmo_panic_raise);
+
+	rc = msgb_resize_area(msg, msg->data, 0, 0);
+	OSMO_ASSERT(rc >= 0);
+
+	if (OSMO_PANIC_TRY(&e))
+		msgb_resize_area(msg, NULL, 0, 0);
+	OSMO_ASSERT(e != 0);
+
+	if (OSMO_PANIC_TRY(&e))
+		msgb_resize_area(msg, NULL, (int)msg->data, 0);
+	OSMO_ASSERT(e != 0);
+
+	if (OSMO_PANIC_TRY(&e))
+		msgb_resize_area(msg, msg->data, 20, 0);
+	OSMO_ASSERT(e != 0);
+
+	if (OSMO_PANIC_TRY(&e))
+		msgb_resize_area(msg, msg->data, -1, 0);
+	OSMO_ASSERT(e != 0);
+
+	if (OSMO_PANIC_TRY(&e))
+		msgb_resize_area(msg, msg->data, 0, -1);
+	OSMO_ASSERT(e != 0);
+
+	printf("Testing msgb_resize_area\n");
+
+	msg->l1h = msgb_put(msg, 20);
+	msg->l2h = msgb_put(msg, 20);
+	msg->l3h = msgb_put(msg, 20);
+	msg->l4h = msgb_put(msg, 20);
+
+	for (i = 0; i < msgb_length(msg); i++)
+		msg->data[i] = (uint8_t)i;
+
+	printf("Original: %s\n", msgb_hexdump(msg));
+
+	/* Extend area */
+	saved_i = msg->l3h[0];
+	old_l3h = msg->l3h;
+
+	rc = msgb_resize_area(msg, msg->l2h, 20, 20 + 30);
+
+	/* Reset the undefined part to allow printing the buffer to stdout */
+	memset(old_l3h, 0, msg->l3h - old_l3h);
+
+	printf("Extended: %s\n", msgb_hexdump(msg));
+
+	OSMO_ASSERT(rc >= 0);
+	OSMO_ASSERT(msgb_length(msg) == 80 + 30);
+	OSMO_ASSERT(msgb_l1len(msg) == 80 + 30);
+	OSMO_ASSERT(msgb_l2len(msg) == 60 + 30);
+	OSMO_ASSERT(msgb_l3len(msg) == 40);
+	OSMO_ASSERT(msg->tail - msg->l4h == 20);
+
+	for (cptr = msgb_data(msg), i = 0; cptr < old_l3h; cptr++, i++)
+		OSMO_ASSERT(*cptr == (uint8_t)i);
+
+	for (cptr = msg->l3h, i = saved_i; cptr < msg->tail; cptr++, i++)
+		OSMO_ASSERT(*cptr == (uint8_t)i);
+
+	rc = msgb_resize_area(msg, msg->l2h, 50, 8000);
+	OSMO_ASSERT(rc == -1);
+
+	/* Shrink area */
+	saved_i = msg->l4h[0];
+	OSMO_ASSERT(saved_i == (uint8_t)(msg->l4h[-1] + 1));
+
+	rc = msgb_resize_area(msg, msg->l3h, 20, 10);
+
+	printf("Shrinked: %s\n", msgb_hexdump(msg));
+
+	OSMO_ASSERT(rc >= 0);
+	OSMO_ASSERT(msgb_length(msg) == 80 + 30 - 10);
+	OSMO_ASSERT(msgb_l1len(msg) == 80 + 30 - 10);
+	OSMO_ASSERT(msgb_l2len(msg) == 60 + 30 - 10);
+	OSMO_ASSERT(msgb_l3len(msg) == 40 - 10);
+	OSMO_ASSERT(msg->tail - msg->l4h == 20);
+
+	OSMO_ASSERT(msg->l4h[0] != msg->l4h[-1] - 1);
+
+	for (cptr = msg->l4h, i = saved_i; cptr < msg->tail; cptr++, i++)
+		OSMO_ASSERT(*cptr == (uint8_t)i);
+
+	rc = msgb_resize_area(msg, msg->l2h, 50, 8000);
+	OSMO_ASSERT(rc == -1);
+
+	msgb_free(msg);
+
+	osmo_set_panic_handler(NULL);
+}
+
 static struct log_info info = {};
 
 int main(int argc, char **argv)
@@ -128,6 +263,8 @@  int main(int argc, char **argv)
 	osmo_init_logging(&info);
 
 	test_msgb_api();
+	test_msgb_copy();
+	test_msgb_resize_area();
 
 	printf("Success.\n");
 
diff --git a/tests/msgb/msgb_test.ok b/tests/msgb/msgb_test.ok
index f8de0cd..4cb76a9 100644
--- a/tests/msgb/msgb_test.ok
+++ b/tests/msgb/msgb_test.ok
@@ -18,4 +18,16 @@  Test msgb_hexdump
 Buffer: (L1=data-124) 00 00 00 00 00 00 00 00 [L2]> 00 00 00 00 [L3]> (L4=tail+4) 
 Buffer: (L1=data-124) 00 00 00 00 00 00 00 00 [L2]> (L3+8) 00 00 00 00 (L4=tail+4) 
 Buffer: (L1=data-124) 00 00 00 00 00 00 00 00 [L2]> 00 00 00 00 (L3 out of range) (L4=tail+4) 
+Testing msgb_copy
+Src: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 
+Dst: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 
+msgb(%p): Sub area is not fully contained in the msg data
+msgb(%p): Sub area is not fully contained in the msg data
+msgb(%p): Sub area is not fully contained in the msg data
+msgb(%p): Negative sizes are not allowed
+msgb(%p): Negative sizes are not allowed
+Testing msgb_resize_area
+Original: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 
+Extended: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 
+Shrinked: [L1]> 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 [L2]> 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [L3]> 28 29 2a 2b 2c 2d 2e 2f 30 31 [L4]> 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 
 Success.