From 0bae8e3c33ef9115aef3d7edc5d3ca00df3f2efc Mon Sep 17 00:00:00 2001 From: Jacob Erlbeck Date: Tue, 18 Mar 2014 14:03:16 +0100 Subject: msgb/test: Add test for msgb message buffers This tests several API functions of the msgb by checking the invariant and by dumping resulting message buffers as hex. Sponsored-by: On-Waves ehf --- tests/Makefile.am | 8 +++- tests/msgb/msgb_test.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/msgb/msgb_test.ok | 21 +++++++++ tests/testsuite.at | 6 +++ 4 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 tests/msgb/msgb_test.c create mode 100644 tests/msgb/msgb_test.ok diff --git a/tests/Makefile.am b/tests/Makefile.am index c6216d50..9932fbe2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -7,7 +7,8 @@ check_PROGRAMS = timer/timer_test sms/sms_test ussd/ussd_test \ gb/bssgp_fc_test gb/gprs_ns_test \ logging/logging_test fr/fr_test \ loggingrb/loggingrb_test strrb/strrb_test \ - vty/vty_test comp128/comp128_test utils/utils_test + vty/vty_test comp128/comp128_test utils/utils_test \ + msgb/msgb_test if ENABLE_MSGFILE check_PROGRAMS += msgfile/msgfile_test @@ -40,6 +41,9 @@ gsm0408_gsm0408_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/ lapd_lapd_test_SOURCES = lapd/lapd_test.c lapd_lapd_test_LDADD = $(top_builddir)/src/libosmocore.la $(top_builddir)/src/gsm/libosmogsm.la +msgb_msgb_test_SOURCES = msgb/msgb_test.c +msgb_msgb_test_LDADD = $(top_builddir)/src/libosmocore.la + msgfile_msgfile_test_SOURCES = msgfile/msgfile_test.c msgfile_msgfile_test_LDADD = $(top_builddir)/src/libosmocore.la @@ -102,7 +106,7 @@ EXTRA_DIST = testsuite.at $(srcdir)/package.m4 $(TESTSUITE) \ lapd/lapd_test.ok gsm0408/gsm0408_test.ok \ gsm0808/gsm0808_test.ok gb/bssgp_fc_tests.err \ gb/bssgp_fc_tests.ok gb/bssgp_fc_tests.sh \ - gb/gprs_ns_test.ok \ + gb/gprs_ns_test.ok msgb/msgb_test.ok \ msgfile/msgfile_test.ok msgfile/msgconfig.cfg \ logging/logging_test.ok logging/logging_test.err \ fr/fr_test.ok loggingrb/logging_test.ok \ diff --git a/tests/msgb/msgb_test.c b/tests/msgb/msgb_test.c new file mode 100644 index 00000000..83c9dbb1 --- /dev/null +++ b/tests/msgb/msgb_test.c @@ -0,0 +1,121 @@ +/* + * (C) 2014 by On-Waves + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include +#include +#include +#include +#include + +#include + +#include + +#define CHECK_RC(rc) \ + if (rc != 0) { \ + printf("Operation failed rc=%d on %s:%d\n", rc, __FILE__, __LINE__); \ + abort(); \ + } + +static void test_msgb_api() +{ + struct msgb *msg = msgb_alloc_headroom(4096, 128, "data"); + unsigned char *cptr = NULL; + int rc; + + printf("Testing the msgb API\n"); + + printf("Buffer: %s\n", msgb_hexdump(msg)); + OSMO_ASSERT(msgb_test_invariant(msg)); + cptr = msg->l1h = msgb_put(msg, 4); + printf("put(4) -> data%+d\n", cptr - msg->data); + printf("Buffer: %s\n", msgb_hexdump(msg)); + OSMO_ASSERT(msgb_test_invariant(msg)); + cptr = msg->l2h = msgb_put(msg, 4); + printf("put(4) -> data%+d\n", cptr - msg->data); + printf("Buffer: %s\n", msgb_hexdump(msg)); + OSMO_ASSERT(msgb_test_invariant(msg)); + cptr = msg->l3h = msgb_put(msg, 4); + printf("put(4) -> data%+d\n", cptr - msg->data); + printf("Buffer: %s\n", msgb_hexdump(msg)); + OSMO_ASSERT(msgb_test_invariant(msg)); + cptr = msg->l4h = msgb_put(msg, 4); + printf("put(4) -> data%+d\n", cptr - msg->data); + printf("Buffer: %s\n", msgb_hexdump(msg)); + OSMO_ASSERT(msgb_test_invariant(msg)); + OSMO_ASSERT(msgb_length(msg) == 16); + cptr = msgb_push(msg, 4); + printf("push(4) -> data%+d\n", cptr - msg->data); + printf("Buffer: %s\n", msgb_hexdump(msg)); + OSMO_ASSERT(msgb_test_invariant(msg)); + OSMO_ASSERT(msgb_length(msg) == 20); + rc = msgb_trim(msg, 16); + printf("trim(16) -> %d\n", rc); + CHECK_RC(rc); + OSMO_ASSERT(msgb_test_invariant(msg)); + printf("Buffer: %s\n", msgb_hexdump(msg)); + OSMO_ASSERT(msgb_length(msg) == 16); + + + cptr = msgb_get(msg, 4); + printf("get(4) -> data%+d\n", cptr - msg->data); + printf("Buffer: %s\n", msgb_hexdump(msg)); + OSMO_ASSERT(msgb_test_invariant(msg)); + OSMO_ASSERT(msgb_length(msg) == 12); + + printf("Test msgb_hexdump\n"); + msg->l1h = msg->head; + printf("Buffer: %s\n", msgb_hexdump(msg)); + msg->l3h = msg->data; + printf("Buffer: %s\n", msgb_hexdump(msg)); + msg->l3h = msg->head - 1; + printf("Buffer: %s\n", msgb_hexdump(msg)); + +#if 0 +extern void msgb_reset(struct msgb *m); +#define msgb_l1(m) ((void *)(MSGB_CHECK2(m)->l1h)) +static inline unsigned int msgb_l1len(const struct msgb *msgb) + static inline int msgb_tailroom(const struct msgb *msgb) + static inline int msgb_headroom(const struct msgb *msgb) + static inline unsigned char *msgb_put(struct msgb *msgb, unsigned int len) + static inline unsigned char *msgb_get(struct msgb *msgb, unsigned int len) + static inline unsigned char *msgb_push(struct msgb *msgb, unsigned int len) + static inline unsigned char *msgb_pull(struct msgb *msgb, unsigned int len) + + static inline unsigned char *msgb_pull_to_l3(struct msgb *msg) + static inline int msgb_trim(struct msgb *msg, int len) + static inline int msgb_l3trim(struct msgb *msg, int l3len) + uint8_t *msgb_data(const struct msgb *msg); + return; +#endif +} + +static struct log_info info = {}; + +int main(int argc, char **argv) +{ + osmo_init_logging(&info); + + test_msgb_api(); + + printf("Success.\n"); + + return 0; +} diff --git a/tests/msgb/msgb_test.ok b/tests/msgb/msgb_test.ok new file mode 100644 index 00000000..33e9294a --- /dev/null +++ b/tests/msgb/msgb_test.ok @@ -0,0 +1,21 @@ +Testing the msgb API +Buffer: +put(4) -> data+0 +Buffer: [L1]> 00 00 00 00 +put(4) -> data+4 +Buffer: [L1]> 00 00 00 00 [L2]> 00 00 00 00 +put(4) -> data+8 +Buffer: [L1]> 00 00 00 00 [L2]> 00 00 00 00 [L3]> 00 00 00 00 +put(4) -> data+12 +Buffer: [L1]> 00 00 00 00 [L2]> 00 00 00 00 [L3]> 00 00 00 00 [L4]> 00 00 00 00 +push(4) -> data+0 +Buffer: 00 00 00 00 [L1]> 00 00 00 00 [L2]> 00 00 00 00 [L3]> 00 00 00 00 [L4]> 00 00 00 00 +trim(16) -> 0 +Buffer: 00 00 00 00 [L1]> 00 00 00 00 [L2]> 00 00 00 00 [L3]> 00 00 00 00 [L4]> +get(4) -> data-4 +Buffer: 00 00 00 00 [L1]> 00 00 00 00 [L2]> 00 00 00 00 [L3]> (L4=tail+4) +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) +Success. diff --git a/tests/testsuite.at b/tests/testsuite.at index 9124f251..92a748c1 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -21,6 +21,12 @@ cat $abs_srcdir/conv/conv_test.ok > expout AT_CHECK([$abs_top_builddir/tests/conv/conv_test], [0], [expout]) AT_CLEANUP +AT_SETUP([msgb]) +AT_KEYWORDS([msgb]) +cat $abs_srcdir/msgb/msgb_test.ok > expout +AT_CHECK([$abs_top_builddir/tests/msgb/msgb_test], [0], [expout]) +AT_CLEANUP + if ENABLE_MSGFILE AT_SETUP([msgfile]) AT_KEYWORDS([msgfile]) -- cgit v1.2.3