317 lines
6.9 KiB
C
317 lines
6.9 KiB
C
#include <check.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include "ucore/mbuf.h"
|
|
|
|
|
|
START_TEST (test_mbuf_new)
|
|
struct MBuf *mbuf;
|
|
|
|
mbuf = uc_mbuf_new(110);
|
|
|
|
fail_if(mbuf == NULL);
|
|
fail_if(uc_mbuf_len(mbuf) != 0);
|
|
fail_if(uc_mbuf_headroom(mbuf) != 0);
|
|
fail_if(uc_mbuf_tailroom(mbuf) != 110);
|
|
fail_if(mbuf->p1 != NULL);
|
|
fail_if(mbuf->p2 != NULL);
|
|
fail_if(mbuf->p3 != NULL);
|
|
fail_if(mbuf->p4 != NULL);
|
|
fail_if(mbuf->cookie != NULL);
|
|
fail_if(mbuf->head != mbuf->tail);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_new_inline)
|
|
struct MBuf *mbuf;
|
|
|
|
mbuf = uc_mbuf_new_inline(110);
|
|
|
|
fail_if(mbuf == NULL);
|
|
fail_if(uc_mbuf_len(mbuf) != 0);
|
|
fail_if(uc_mbuf_headroom(mbuf) != 0);
|
|
fail_if(uc_mbuf_tailroom(mbuf) != 110);
|
|
fail_if(mbuf->entry.next != NULL);
|
|
fail_if(mbuf->entry.prev != NULL);
|
|
fail_if(mbuf->p1 != NULL);
|
|
fail_if(mbuf->p2 != NULL);
|
|
fail_if(mbuf->p3 != NULL);
|
|
fail_if(mbuf->p4 != NULL);
|
|
fail_if(mbuf->cookie != NULL);
|
|
fail_if(mbuf->head != mbuf->tail);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_new_headroom)
|
|
struct MBuf *mbuf;
|
|
|
|
mbuf = uc_mbuf_new_hr(110,20);
|
|
|
|
fail_if(mbuf == NULL);
|
|
fail_if(uc_mbuf_len(mbuf) != 0);
|
|
fail_if(uc_mbuf_headroom(mbuf) != 20);
|
|
fail_if(uc_mbuf_tailroom(mbuf) != 110);
|
|
fail_if(mbuf->p1 != NULL);
|
|
fail_if(mbuf->p2 != NULL);
|
|
fail_if(mbuf->p3 != NULL);
|
|
fail_if(mbuf->p4 != NULL);
|
|
fail_if(mbuf->cookie != NULL);
|
|
fail_if(mbuf->head != mbuf->tail);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_put)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data;
|
|
|
|
mbuf = uc_mbuf_new_inline(111);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data = uc_mbuf_put(mbuf, 111);
|
|
fail_if(data == NULL);
|
|
fail_if(uc_mbuf_len(mbuf) != 111);
|
|
fail_if(uc_mbuf_tailroom(mbuf) != 0);
|
|
|
|
memset(data, 0, 111);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_put_fail)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data;
|
|
|
|
mbuf = uc_mbuf_new_inline(10);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data = uc_mbuf_put(mbuf, 11);
|
|
fail_if(data != NULL);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_push)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data;
|
|
|
|
mbuf = uc_mbuf_new_hr(10,1);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data = uc_mbuf_push(mbuf, 1);
|
|
fail_if(data == NULL);
|
|
fail_if(uc_mbuf_len(mbuf) != 1);
|
|
fail_if(uc_mbuf_headroom(mbuf) != 0);
|
|
|
|
*data = 0;
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
|
|
START_TEST (test_mbuf_push_fail)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data;
|
|
|
|
mbuf = uc_mbuf_new_hr(1,2);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data = uc_mbuf_push(mbuf, 3);
|
|
fail_if(data != NULL);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_pull)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data1;
|
|
uint8_t *data2;
|
|
|
|
mbuf = uc_mbuf_new_inline(10);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data1 = uc_mbuf_put(mbuf, 10);
|
|
fail_if(data1 == NULL);
|
|
fail_if(uc_mbuf_tailroom(mbuf) != 0);
|
|
fail_if(uc_mbuf_len(mbuf) != 10);
|
|
|
|
data2 = uc_mbuf_pull(mbuf,10);
|
|
fail_if(data2 == NULL);
|
|
fail_if(data1 != data2);
|
|
fail_if(uc_mbuf_len(mbuf) != 0);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_pull_fail)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data1;
|
|
uint8_t *data2;
|
|
|
|
mbuf = uc_mbuf_new_inline(10);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data1 = uc_mbuf_put(mbuf, 10);
|
|
fail_if(data1 == NULL);
|
|
fail_if(uc_mbuf_len(mbuf) != 10);
|
|
|
|
data2 = uc_mbuf_pull(mbuf,11);
|
|
fail_if(data2 != NULL);
|
|
fail_if(uc_mbuf_len(mbuf) != 10);
|
|
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_copy)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data1;
|
|
struct MBuf *copy;
|
|
|
|
mbuf = uc_mbuf_new_inline_hr(10,5);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data1 = uc_mbuf_put(mbuf, 10);
|
|
fail_if(data1 == NULL);
|
|
|
|
strcpy((char*)data1, "123456789");
|
|
|
|
copy = uc_mbuf_copy_data(mbuf);
|
|
fail_if(copy == NULL);
|
|
fail_if(uc_mbuf_len(copy) != 10);
|
|
fail_if(uc_mbuf_headroom(copy) != 5);
|
|
fail_if(memcmp(uc_mbuf_head(mbuf), uc_mbuf_head(copy), 10) != 0);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
uc_mbuf_free(copy);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_zero)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data1;
|
|
|
|
mbuf = uc_mbuf_new_inline(10);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data1 = uc_mbuf_put(mbuf, 10);
|
|
fail_if(data1 == NULL);
|
|
|
|
strcpy((char*)data1, "123456789");
|
|
uc_mbuf_zero(mbuf);
|
|
|
|
fail_if(memcmp(uc_mbuf_head(mbuf), "\0\0\0\0\0\0\0\0\0\0", 10) != 0);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_static)
|
|
|
|
uint8_t data[1024];
|
|
struct MBuf mbuf;
|
|
|
|
uc_mbuf_init(&mbuf, data, 1000, 24, UC_MBUF_FL_STATIC);
|
|
|
|
fail_if(uc_mbuf_len(&mbuf) != 0);
|
|
fail_if(uc_mbuf_headroom(&mbuf) != 24);
|
|
fail_if(uc_mbuf_tailroom(&mbuf) != 1000);
|
|
fail_if(mbuf.p1 != NULL);
|
|
fail_if(mbuf.p2 != NULL);
|
|
fail_if(mbuf.p3 != NULL);
|
|
fail_if(mbuf.p4 != NULL);
|
|
fail_if(mbuf.cookie != NULL);
|
|
fail_if(mbuf.head != mbuf.tail);
|
|
fail_if(mbuf.head != &data[24]);
|
|
|
|
uc_mbuf_free(&mbuf);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_extend_tailroom)
|
|
struct MBuf *mbuf;
|
|
uint8_t *data;
|
|
void *filler;
|
|
int rc;
|
|
|
|
mbuf = uc_mbuf_new_hr(5,2);
|
|
fail_if(mbuf == NULL);
|
|
|
|
data = uc_mbuf_put(mbuf, 5);
|
|
fail_if(data == NULL);
|
|
memcpy((char*)data, "hello", 5);
|
|
mbuf->p1 = mbuf->tail;
|
|
|
|
data = uc_mbuf_put(mbuf, 6);
|
|
fail_if(data != NULL);
|
|
|
|
filler = malloc(5); //just a hope, so realloc also moves..
|
|
rc = uc_mbuf_extend_tailroom(mbuf, 1024);
|
|
ck_assert_int_eq(rc, 0);
|
|
ck_assert_int_eq(uc_mbuf_tailroom(mbuf), 1024);
|
|
ck_assert_int_eq(uc_mbuf_headroom(mbuf), 2);
|
|
ck_assert_int_eq(uc_mbuf_len(mbuf), 5);
|
|
|
|
//should work now
|
|
data = uc_mbuf_put(mbuf, 6);
|
|
fail_if(data == NULL);
|
|
strcpy((char*)data, "world");
|
|
ck_assert_str_eq((const char*)mbuf->head, "helloworld");
|
|
ck_assert_str_eq((const char*)mbuf->p1, "world");
|
|
|
|
uc_mbuf_free(mbuf);
|
|
free(filler);
|
|
|
|
END_TEST
|
|
|
|
START_TEST (test_mbuf_extend_tailroom_wrong_type)
|
|
struct MBuf *mbuf;
|
|
int rc;
|
|
|
|
mbuf = uc_mbuf_new_inline(10);
|
|
fail_if(mbuf == NULL);
|
|
|
|
rc = uc_mbuf_extend_tailroom(mbuf, 1024);
|
|
ck_assert_int_ne(rc, 0);
|
|
|
|
uc_mbuf_free(mbuf);
|
|
|
|
END_TEST
|
|
|
|
|
|
Suite *mbuf_suite(void)
|
|
{
|
|
Suite *s = suite_create("mbuf");
|
|
TCase *tc = tcase_create("mbuf tests");
|
|
tcase_add_test(tc, test_mbuf_new);
|
|
tcase_add_test(tc, test_mbuf_new_inline);
|
|
tcase_add_test(tc, test_mbuf_new_headroom);
|
|
tcase_add_test(tc, test_mbuf_put);
|
|
tcase_add_test(tc, test_mbuf_put_fail);
|
|
tcase_add_test(tc, test_mbuf_push);
|
|
tcase_add_test(tc, test_mbuf_push_fail);
|
|
tcase_add_test(tc, test_mbuf_pull);
|
|
tcase_add_test(tc, test_mbuf_pull_fail);
|
|
tcase_add_test(tc, test_mbuf_copy);
|
|
tcase_add_test(tc, test_mbuf_zero);
|
|
tcase_add_test(tc, test_mbuf_static);
|
|
tcase_add_test(tc, test_mbuf_extend_tailroom);
|
|
tcase_add_test(tc, test_mbuf_extend_tailroom_wrong_type);
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
return s;
|
|
}
|
|
|