From 008e53baaeba1aaf126b11b5c211a1110e9dc087 Mon Sep 17 00:00:00 2001 From: Katerina Barone-Adesi Date: Sun, 3 Mar 2013 10:36:52 +0000 Subject: utils: Introduce OSMO_ASSERT in the utils and use it in the unit tests The tests should unconditionally assert, regardless of debug settings. This uses the OSMO_ prefix as it's in the global namespace. --- tests/lapd/lapd_test.c | 62 +++++++++---------- tests/loggingrb/loggingrb_test.c | 3 +- tests/strrb/strrb_test.c | 126 +++++++++++++++++++-------------------- 3 files changed, 93 insertions(+), 98 deletions(-) (limited to 'tests') diff --git a/tests/lapd/lapd_test.c b/tests/lapd/lapd_test.c index acd3cadb..a60c45d4 100644 --- a/tests/lapd/lapd_test.c +++ b/tests/lapd/lapd_test.c @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -34,13 +35,6 @@ abort(); \ } -#define ASSERT(exp) \ - if (!(exp)) { \ - printf("Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ - abort(); \ - } - - static struct log_info info = {}; struct lapdm_polling_state { @@ -101,9 +95,9 @@ static struct msgb *create_mm_id_req(void) msg = msgb_from_array(mm, sizeof(mm)); msg->l2h = msg->data + 3; - ASSERT(msgb_l2len(msg) == 12); + OSMO_ASSERT(msgb_l2len(msg) == 12); msg->l3h = msg->l2h + 6; - ASSERT(msgb_l3len(msg) == 6); + OSMO_ASSERT(msgb_l3len(msg) == 6); return msg; } @@ -113,7 +107,7 @@ static struct msgb *create_empty_msg(void) struct msgb *msg; msg = msgb_from_array(NULL, 0); - ASSERT(msgb_l3len(msg) == 0); + OSMO_ASSERT(msgb_l3len(msg) == 0); rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1); return msg; } @@ -155,7 +149,7 @@ static int send(struct msgb *in_msg, struct lapdm_channel *chan) pp.u.data.link_id = 0; /* feed into the LAPDm code of libosmogsm */ rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch); - ASSERT(rc == 0 || rc == -EBUSY); + OSMO_ASSERT(rc == 0 || rc == -EBUSY); return 0; } @@ -172,12 +166,14 @@ static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ if (state->bts_read == 0) { printf("BTS: Verifying CM request.\n"); - ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm_padded)); - ASSERT(memcmp(in_msg->l3h, cm_padded, ARRAY_SIZE(cm_padded)) == 0); + OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm_padded)); + OSMO_ASSERT(memcmp(in_msg->l3h, cm_padded, + ARRAY_SIZE(cm_padded)) == 0); } else if (state->bts_read == 1) { printf("BTS: Verifying dummy message.\n"); - ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1)); - ASSERT(memcmp(in_msg->l3h, dummy1, ARRAY_SIZE(dummy1)) == 0); + OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1)); + OSMO_ASSERT(memcmp(in_msg->l3h, dummy1, + ARRAY_SIZE(dummy1)) == 0); } else { printf("BTS: Do not know to verify: %d\n", state->bts_read); } @@ -210,23 +206,23 @@ static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx struct abis_rsl_rll_hdr hdr; printf("MS: Verifying incoming primitive.\n"); - ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3); + OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3); /* verify the header */ memset(&hdr, 0, sizeof(hdr)); rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF); hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP; - ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0); + OSMO_ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0); /* Verify the added RSL_IE_L3_INFO but we have a bug here */ - ASSERT(msg->data[6] == RSL_IE_L3_INFO); + OSMO_ASSERT(msg->data[6] == RSL_IE_L3_INFO); #warning "RSL_IE_L3_INFO 16 bit length is wrong" /* ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x9c); */ /* this should be 0x0 and 0x0... but we have a bug */ } else if (state->ms_read == 1) { printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg)); - ASSERT(msgb_l3len(msg) == 3); - ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0); + OSMO_ASSERT(msgb_l3len(msg) == 3); + OSMO_ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0); } else { printf("MS: Do not know to verify: %d\n", state->ms_read); } @@ -274,13 +270,13 @@ static void test_lapdm_polling() /* 2. Poll on the BTS for sending out a confirmation */ printf("\nConfirming\n"); - ASSERT(test_state.bts_read == 1) + OSMO_ASSERT(test_state.bts_read == 1); rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); CHECK_RC(rc); - ASSERT(pp.oph.msg->data == pp.oph.msg->l2h); + OSMO_ASSERT(pp.oph.msg->data == pp.oph.msg->l2h); send(pp.oph.msg, &ms_to_bts_channel); msgb_free(pp.oph.msg); - ASSERT(test_state.ms_read == 1); + OSMO_ASSERT(test_state.ms_read == 1); /* 3. Send some data to the MS */ printf("\nSending back to MS\n"); @@ -289,35 +285,35 @@ static void test_lapdm_polling() CHECK_RC(rc); send(pp.oph.msg, &ms_to_bts_channel); msgb_free(pp.oph.msg); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); /* verify that there is nothing more to poll */ rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); - ASSERT(rc < 0); + OSMO_ASSERT(rc < 0); /* 3. And back to the BTS */ printf("\nSending back to BTS\n"); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel); /* 4. And back to the MS, but let's move data/l2h apart */ - ASSERT(test_state.bts_read == 2) - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.bts_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); CHECK_RC(rc); send(pp.oph.msg, &ms_to_bts_channel); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(test_state.ms_read == 2); msgb_free(pp.oph.msg); /* verify that there is nothing more to poll */ rc = lapdm_phsap_dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp); - ASSERT(rc < 0); + OSMO_ASSERT(rc < 0); /* check sending an empty L3 message fails */ rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel); - ASSERT(rc == -1); - ASSERT(test_state.ms_read == 2); + OSMO_ASSERT(rc == -1); + OSMO_ASSERT(test_state.ms_read == 2); /* clean up */ lapdm_channel_exit(&bts_to_ms_channel); @@ -346,7 +342,7 @@ static void test_lapdm_early_release() /* Send the release request */ rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel); - ASSERT(rc == -EINVAL); + OSMO_ASSERT(rc == -EINVAL); /* clean up */ lapdm_channel_exit(&bts_to_ms_channel); diff --git a/tests/loggingrb/loggingrb_test.c b/tests/loggingrb/loggingrb_test.c index 1ab5212d..9957b536 100644 --- a/tests/loggingrb/loggingrb_test.c +++ b/tests/loggingrb/loggingrb_test.c @@ -18,7 +18,6 @@ * along with this program. If not, see . * */ -#include #include #include @@ -77,7 +76,7 @@ int main(int argc, char **argv) DEBUGP(DMM, "You should not see this\n"); fprintf(stderr, ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 0)); fprintf(stderr, ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 1)); - assert(!ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 2)); + OSMO_ASSERT(!ringbuffer_get_nth(ringbuf_target->tgt_rbvty.rb, 2)); return 0; } diff --git a/tests/strrb/strrb_test.c b/tests/strrb/strrb_test.c index abe649f5..6140ac9b 100644 --- a/tests/strrb/strrb_test.c +++ b/tests/strrb/strrb_test.c @@ -18,12 +18,12 @@ */ #include -#include #include #include #include #include +#include struct osmo_strrb *rb0, *rb1, *rb2, *rb3, *rb4, *rb5; @@ -77,98 +77,98 @@ void free_rbs(void) void test_offset_valid(void) { - assert(_osmo_strrb_is_bufindex_valid(rb1, 0)); - assert(!_osmo_strrb_is_bufindex_valid(rb1, 1)); - assert(!_osmo_strrb_is_bufindex_valid(rb1, 2)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb1, 0)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb1, 1)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb1, 2)); - assert(!_osmo_strrb_is_bufindex_valid(rb3, 0)); - assert(_osmo_strrb_is_bufindex_valid(rb3, 1)); - assert(_osmo_strrb_is_bufindex_valid(rb3, 2)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb3, 0)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb3, 1)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb3, 2)); - assert(_osmo_strrb_is_bufindex_valid(rb4, 0)); - assert(!_osmo_strrb_is_bufindex_valid(rb4, 1)); - assert(_osmo_strrb_is_bufindex_valid(rb4, 2)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb4, 0)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb4, 1)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb4, 2)); - assert(_osmo_strrb_is_bufindex_valid(rb5, 0)); - assert(_osmo_strrb_is_bufindex_valid(rb5, 1)); - assert(!_osmo_strrb_is_bufindex_valid(rb5, 2)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 0)); + OSMO_ASSERT(_osmo_strrb_is_bufindex_valid(rb5, 1)); + OSMO_ASSERT(!_osmo_strrb_is_bufindex_valid(rb5, 2)); } void test_elems(void) { - assert(osmo_strrb_elements(rb0) == 0); - assert(osmo_strrb_elements(rb1) == 1); - assert(osmo_strrb_elements(rb2) == 2); - assert(osmo_strrb_elements(rb3) == 2); + OSMO_ASSERT(osmo_strrb_elements(rb0) == 0); + OSMO_ASSERT(osmo_strrb_elements(rb1) == 1); + OSMO_ASSERT(osmo_strrb_elements(rb2) == 2); + OSMO_ASSERT(osmo_strrb_elements(rb3) == 2); } void test_getn(void) { - assert(!osmo_strrb_get_nth(rb0, 0)); - assert(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0))); - assert(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1))); - assert(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0))); - assert(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1))); - assert(!osmo_strrb_get_nth(rb3, 2)); + OSMO_ASSERT(!osmo_strrb_get_nth(rb0, 0)); + OSMO_ASSERT(!strcmp(STR0, osmo_strrb_get_nth(rb2, 0))); + OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb2, 1))); + OSMO_ASSERT(!strcmp(STR1, osmo_strrb_get_nth(rb3, 0))); + OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb3, 1))); + OSMO_ASSERT(!osmo_strrb_get_nth(rb3, 2)); } void test_getn_wrap(void) { - assert(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0))); - assert(!strcmp(STR3, osmo_strrb_get_nth(rb4, 1))); + OSMO_ASSERT(!strcmp(STR2, osmo_strrb_get_nth(rb4, 0))); + OSMO_ASSERT(!strcmp(STR3, osmo_strrb_get_nth(rb4, 1))); - assert(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0))); - assert(!strcmp(STR4, osmo_strrb_get_nth(rb5, 1))); + OSMO_ASSERT(!strcmp(STR3, osmo_strrb_get_nth(rb5, 0))); + OSMO_ASSERT(!strcmp(STR4, osmo_strrb_get_nth(rb5, 1))); } void test_add(void) { struct osmo_strrb *rb = osmo_strrb_create(NULL, 4); - assert(rb->start == 0); - assert(rb->end == 0); + OSMO_ASSERT(rb->start == 0); + OSMO_ASSERT(rb->end == 0); osmo_strrb_add(rb, "a"); osmo_strrb_add(rb, "b"); osmo_strrb_add(rb, "c"); - assert(rb->start == 0); - assert(rb->end == 3); - assert(osmo_strrb_elements(rb) == 3); + OSMO_ASSERT(rb->start == 0); + OSMO_ASSERT(rb->end == 3); + OSMO_ASSERT(osmo_strrb_elements(rb) == 3); osmo_strrb_add(rb, "d"); - assert(rb->start == 1); - assert(rb->end == 0); - assert(osmo_strrb_elements(rb) == 3); - assert(!strcmp("b", osmo_strrb_get_nth(rb, 0))); - assert(!strcmp("c", osmo_strrb_get_nth(rb, 1))); - assert(!strcmp("d", osmo_strrb_get_nth(rb, 2))); + OSMO_ASSERT(rb->start == 1); + OSMO_ASSERT(rb->end == 0); + OSMO_ASSERT(osmo_strrb_elements(rb) == 3); + OSMO_ASSERT(!strcmp("b", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 1))); + OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 2))); osmo_strrb_add(rb, "e"); - assert(rb->start == 2); - assert(rb->end == 1); - assert(!strcmp("c", osmo_strrb_get_nth(rb, 0))); - assert(!strcmp("d", osmo_strrb_get_nth(rb, 1))); - assert(!strcmp("e", osmo_strrb_get_nth(rb, 2))); + OSMO_ASSERT(rb->start == 2); + OSMO_ASSERT(rb->end == 1); + OSMO_ASSERT(!strcmp("c", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 1))); + OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 2))); osmo_strrb_add(rb, "f"); - assert(rb->start == 3); - assert(rb->end == 2); - assert(!strcmp("d", osmo_strrb_get_nth(rb, 0))); - assert(!strcmp("e", osmo_strrb_get_nth(rb, 1))); - assert(!strcmp("f", osmo_strrb_get_nth(rb, 2))); + OSMO_ASSERT(rb->start == 3); + OSMO_ASSERT(rb->end == 2); + OSMO_ASSERT(!strcmp("d", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 1))); + OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 2))); osmo_strrb_add(rb, "g"); - assert(rb->start == 0); - assert(rb->end == 3); - assert(!strcmp("e", osmo_strrb_get_nth(rb, 0))); - assert(!strcmp("f", osmo_strrb_get_nth(rb, 1))); - assert(!strcmp("g", osmo_strrb_get_nth(rb, 2))); + OSMO_ASSERT(rb->start == 0); + OSMO_ASSERT(rb->end == 3); + OSMO_ASSERT(!strcmp("e", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 1))); + OSMO_ASSERT(!strcmp("g", osmo_strrb_get_nth(rb, 2))); osmo_strrb_add(rb, "h"); - assert(rb->start == 1); - assert(rb->end == 0); - assert(!strcmp("f", osmo_strrb_get_nth(rb, 0))); - assert(!strcmp("g", osmo_strrb_get_nth(rb, 1))); - assert(!strcmp("h", osmo_strrb_get_nth(rb, 2))); + OSMO_ASSERT(rb->start == 1); + OSMO_ASSERT(rb->end == 0); + OSMO_ASSERT(!strcmp("f", osmo_strrb_get_nth(rb, 0))); + OSMO_ASSERT(!strcmp("g", osmo_strrb_get_nth(rb, 1))); + OSMO_ASSERT(!strcmp("h", osmo_strrb_get_nth(rb, 2))); talloc_free(rb); } @@ -184,8 +184,8 @@ void test_long_msg(void) tests1 = malloc(test_size); tests2 = malloc(test_size); /* Be certain allocating memory worked before continuing */ - assert(tests1); - assert(tests2); + OSMO_ASSERT(tests1); + OSMO_ASSERT(tests2); for (i = 0; i < RB_MAX_MESSAGE_SIZE; i += 2) { tests1[i] = 'a'; @@ -201,9 +201,9 @@ void test_long_msg(void) free(tests1); rb_content = osmo_strrb_get_nth(rb, 0); - assert(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1)); - assert(!rb_content[RB_MAX_MESSAGE_SIZE - 1]); - assert(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1); + OSMO_ASSERT(!strncmp(tests2, rb_content, RB_MAX_MESSAGE_SIZE - 1)); + OSMO_ASSERT(!rb_content[RB_MAX_MESSAGE_SIZE - 1]); + OSMO_ASSERT(strlen(rb_content) == RB_MAX_MESSAGE_SIZE - 1); free(tests2); talloc_free(rb); -- cgit v1.2.3