daniel has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/32181 )
Change subject: tests: Add initial osmo_io tests ......................................................................
tests: Add initial osmo_io tests
Change-Id: Ia67629e53f4d2e5784177250d58e268fdfcaa0c2 --- M tests/Makefile.am A tests/osmo_io/osmo_io_test.c A tests/osmo_io/osmo_io_test.err A tests/osmo_io/osmo_io_test.ok M tests/testsuite.at 5 files changed, 206 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/81/32181/1
diff --git a/tests/Makefile.am b/tests/Makefile.am index 6876df3..025d294 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -53,6 +53,7 @@ v110/test_frame \ v110/test_ra1 \ gsm44021/test_frame_csd \ + osmo_io/osmo_io_test \ $(NULL)
if ENABLE_MSGFILE @@ -321,6 +322,7 @@ tdef_tdef_vty_dynamic_test_LDADD = $(top_builddir)/src/vty/libosmovty.la $(LDADD)
sockaddr_str_sockaddr_str_test_SOURCES = sockaddr_str/sockaddr_str_test.c + osmo_io/osmo_io_test \ sockaddr_str_sockaddr_str_test_LDADD = $(LDADD)
use_count_use_count_test_SOURCES = use_count/use_count_test.c @@ -367,6 +369,8 @@ $(top_builddir)/src/gsm/libosmogsm.la \ $(LDADD)
+osmo_io_osmo_io_test_SOURCES = osmo_io/osmo_io_test.c +
# The `:;' works around a Bash 3.2 bug when the output is not writeable. $(srcdir)/package.m4: $(top_srcdir)/configure.ac @@ -469,6 +473,7 @@ v110/test_frame.ok \ v110/test_ra1.ok \ gsm44021/test_frame_csd.ok \ + osmo_io/osmo_io_test.ok osmo_io/osmo_io_test.err \ $(NULL)
if ENABLE_LIBSCTP @@ -678,6 +683,10 @@ >$(srcdir)/v110/test_ra1.ok gsm44021/test_frame_csd \ >$(srcdir)/gsm44021/test_frame_csd.ok + osmo_io/osmo_io_test \ + >$(srcdir)/osmo_io/osmo_io_test.ok \ + 2>$(srcdir)/osmo_io/osmo_io_test.err +
check-local: atconfig $(TESTSUITE) [ -e /proc/cpuinfo ] && cat /proc/cpuinfo diff --git a/tests/osmo_io/osmo_io_test.c b/tests/osmo_io/osmo_io_test.c new file mode 100644 index 0000000..9c3440d --- /dev/null +++ b/tests/osmo_io/osmo_io_test.c @@ -0,0 +1,172 @@ +/* + * (C) 2023 by sysmocom s.f.m.c + * Author: Daniel Willmann daniel@sysmocom.de + * + * All Rights Reserved + * + * SPDX-License-Identifier: GPL-2.0+ + * + * 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. + * + */ + +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/socket.h> +#include <errno.h> + +#include <osmocom/core/application.h> +#include <osmocom/core/osmo_io.h> +#include <osmocom/core/utils.h> +#include <osmocom/core/logging.h> +#include <osmocom/core/select.h> +#include <osmocom/core/bits.h> + +#include "config.h" +#include "osmocom/core/msgb.h" + +#define TEST_START() printf("Running %s\n", __func__) + +void *ctx = NULL; + +static void read_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg) +{ + printf("%s: Got msg with len=%d\n", osmo_iofd_get_name(iofd), rc); + if (msg) + printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg))); + + talloc_free(msg); +} + +static void write_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg) +{ + printf("%s: Write returned rc=%d\n",osmo_iofd_get_name(iofd), rc); +} + +struct osmo_io_ops ioops_conn = { + .read_cb = read_cb, + .write_cb = write_cb, +}; + +uint8_t TESTDATA[] = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 +}; + +static void test_connected() { + int fds[2] = {0, 0}, rc; + struct osmo_io_fd *iofd1, *iofd2; + struct msgb *msg; + uint8_t *buf; + + TEST_START(); + + rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); + OSMO_ASSERT(rc == 0); + + iofd1 = osmo_iofd_setup(ctx, fds[0], "ep1", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn, NULL); + osmo_iofd_register(iofd1, fds[0]); + iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn, NULL); + osmo_iofd_register(iofd2, fds[1]); + + msg = msgb_alloc(1024, "Test data"); + buf = msgb_put(msg, sizeof(TESTDATA)); + memcpy(buf, TESTDATA, sizeof(TESTDATA)); + + osmo_iofd_write_msgb(iofd1, msg); + osmo_iofd_write_enable(iofd1); + osmo_iofd_write_enable(iofd2); + osmo_iofd_read_enable(iofd1); + osmo_iofd_read_enable(iofd2); + + + /* Allow enough cycles to handle the messages */ + for (int i = 0; i < 128; i++) { + osmo_select_main(1); + } + + osmo_iofd_free(iofd1); + osmo_iofd_free(iofd2); +} + +static void recvmsg_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg, struct osmo_sockaddr *addr) +{ + printf("%s: Got msg with len=%d\n", osmo_iofd_get_name(iofd), rc); + if (msg) + printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg))); + + talloc_free(msg); +} + +struct osmo_io_ops ioops_unconn = { + .sendmsg_cb = write_cb, + .recvmsg_cb = recvmsg_cb, +}; + +static void test_unconnected() { + int fds[2] = {0, 0}, rc; + struct osmo_io_fd *iofd1, *iofd2; + struct msgb *msg; + uint8_t *buf; + + TEST_START(); + + rc = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds); + OSMO_ASSERT(rc == 0); + + iofd1 = osmo_iofd_setup(ctx, fds[0], "ep1", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_unconn, NULL); + osmo_iofd_register(iofd1, fds[0]); + iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_unconn, NULL); + osmo_iofd_register(iofd2, fds[1]); + + msg = msgb_alloc(1024, "Test data"); + buf = msgb_put(msg, sizeof(TESTDATA)); + memcpy(buf, TESTDATA, sizeof(TESTDATA)); + + osmo_iofd_sendto_msgb(iofd1, msg, 0, NULL); + osmo_iofd_write_enable(iofd1); + osmo_iofd_write_enable(iofd2); + osmo_iofd_read_enable(iofd1); + osmo_iofd_read_enable(iofd2); + + + /* Allow enough cycles to handle the messages */ + for (int i = 0; i < 128; i++) { + osmo_select_main(1); + } + + osmo_iofd_free(iofd1); + osmo_iofd_free(iofd2); +} +static const struct log_info_cat default_categories[] = { +}; + +static struct log_info info = { + .cat = default_categories, + .num_cat = ARRAY_SIZE(default_categories), +}; + +int main(int argc, char *argv[]) +{ + ctx = talloc_named_const(NULL, 0, "osmo_io_test"); + osmo_init_logging2(ctx, &info); + log_set_use_color(osmo_stderr_target, 0); + log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); + log_set_print_category(osmo_stderr_target, 0); + log_set_print_category_hex(osmo_stderr_target, 0); + + test_connected(); + test_unconnected(); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/tests/osmo_io/osmo_io_test.err b/tests/osmo_io/osmo_io_test.err new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/osmo_io/osmo_io_test.err diff --git a/tests/osmo_io/osmo_io_test.ok b/tests/osmo_io/osmo_io_test.ok new file mode 100644 index 0000000..5556d4a --- /dev/null +++ b/tests/osmo_io/osmo_io_test.ok @@ -0,0 +1,8 @@ +Running test_connected +ep1: Write returned rc=16 +ep2: Got msg with len=16 +01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 +Running test_unconnected +ep1: Write returned rc=16 +ep2: Got msg with len=16 +01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 diff --git a/tests/testsuite.at b/tests/testsuite.at index 60aa74d..a2c8845 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -508,3 +508,11 @@ cat $abs_srcdir/gsm44021/test_frame_csd.ok > expout AT_CHECK([$abs_top_builddir/tests/gsm44021/test_frame_csd], [], [expout],[]) AT_CLEANUP + +AT_SETUP([osmo_io]) +AT_KEYWORDS([osmo_io]) +cat $abs_srcdir/osmo_io/osmo_io_test.ok > expout +cat $abs_srcdir/osmo_io/osmo_io_test.err > experr +touch experr +AT_CHECK([$abs_top_builddir/tests/osmo_io/osmo_io_test], [0], [expout], [experr]) +AT_CLEANUP