blob: a42e6d0cbb5731d23a9708ab243d3300a6989e66 [file] [log] [blame]
Daniel Willmann37b2ebf2023-03-31 14:36:16 +02001/*
2 * (C) 2023 by sysmocom s.f.m.c
3 * Author: Daniel Willmann <daniel@sysmocom.de>
4 *
5 * All Rights Reserved
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <stdio.h>
22#include <stdint.h>
23#include <stdlib.h>
24#include <unistd.h>
25#include <string.h>
26#include <sys/socket.h>
27#include <errno.h>
28
29#include <osmocom/core/application.h>
30#include <osmocom/core/bits.h>
31#include <osmocom/core/logging.h>
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/osmo_io.h>
34#include <osmocom/core/select.h>
35#include <osmocom/core/utils.h>
36
37#include "config.h"
38
39#define TEST_START() printf("Running %s\n", __func__)
40
Daniel Willmann181eaa42023-06-16 15:17:33 +020041static uint8_t TESTDATA[] = {
42 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
43};
44
45
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020046static void *ctx = NULL;
47
48static void read_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg)
49{
50 printf("%s: read() msg with len=%d\n", osmo_iofd_get_name(iofd), rc);
51 if (msg)
52 printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
53
54 talloc_free(msg);
55}
56
Daniel Willmann3ed87722023-06-06 11:34:14 +020057static void write_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg)
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020058{
Daniel Willmann181eaa42023-06-16 15:17:33 +020059 uint8_t *buf;
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020060 printf("%s: write() returned rc=%d\n", osmo_iofd_get_name(iofd), rc);
Daniel Willmann181eaa42023-06-16 15:17:33 +020061 if (rc == 0) {
62 msg = msgb_alloc(1024, "Test data");
63 buf = msgb_put(msg, sizeof(TESTDATA));
64 memcpy(buf, TESTDATA, sizeof(TESTDATA));
65
66 osmo_iofd_write_msgb(iofd, msg);
67 }
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020068}
69
70struct osmo_io_ops ioops_conn_read_write = {
71 .read_cb = read_cb,
72 .write_cb = write_cb,
73};
74
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020075static void test_connected(void)
76{
77 int fds[2] = {0, 0}, rc;
78 struct osmo_io_fd *iofd1, *iofd2;
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020079
80 TEST_START();
81
82 rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
83 OSMO_ASSERT(rc == 0);
84
85 iofd1 = osmo_iofd_setup(ctx, fds[0], "ep1", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn_read_write, NULL);
86 osmo_iofd_register(iofd1, fds[0]);
87 iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn_read_write, NULL);
88 osmo_iofd_register(iofd2, fds[1]);
89
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020090 /* Allow enough cycles to handle the messages */
91 for (int i = 0; i < 128; i++)
92 osmo_select_main(1);
93
94 osmo_iofd_free(iofd1);
95 osmo_iofd_free(iofd2);
96}
97
98static void recvfrom_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg,
99 const struct osmo_sockaddr *saddr)
100{
101 printf("%s: recvfrom() msg with len=%d\n", osmo_iofd_get_name(iofd), rc);
102 if (msg)
103 printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
104
105 talloc_free(msg);
106}
107
Daniel Willmann3ed87722023-06-06 11:34:14 +0200108static void sendto_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg,
Daniel Willmann37b2ebf2023-03-31 14:36:16 +0200109 const struct osmo_sockaddr *daddr)
110{
111 printf("%s: sendto() returned rc=%d\n", osmo_iofd_get_name(iofd), rc);
112}
113
114struct osmo_io_ops ioops_conn_recvfrom_sendto = {
115 .sendto_cb = sendto_cb,
116 .recvfrom_cb = recvfrom_cb,
117};
118
119static void test_unconnected(void)
120{
121 int fds[2] = {0, 0}, rc;
122 struct osmo_io_fd *iofd1, *iofd2;
123 struct msgb *msg;
124 uint8_t *buf;
125
126 TEST_START();
127
128 rc = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds);
129 OSMO_ASSERT(rc == 0);
130
131 iofd1 = osmo_iofd_setup(ctx, fds[0], "ep1", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_conn_recvfrom_sendto, NULL);
132 osmo_iofd_register(iofd1, fds[0]);
133 iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_conn_recvfrom_sendto, NULL);
134 osmo_iofd_register(iofd2, fds[1]);
135
136 msg = msgb_alloc(1024, "Test data");
137 buf = msgb_put(msg, sizeof(TESTDATA));
138 memcpy(buf, TESTDATA, sizeof(TESTDATA));
139
140 osmo_iofd_sendto_msgb(iofd1, msg, 0, NULL);
Daniel Willmann37b2ebf2023-03-31 14:36:16 +0200141
142 /* Allow enough cycles to handle the messages */
143 for (int i = 0; i < 128; i++)
144 osmo_select_main(1);
145
146 osmo_iofd_free(iofd1);
147 osmo_iofd_free(iofd2);
148}
149static const struct log_info_cat default_categories[] = {
150};
151
152static struct log_info info = {
153 .cat = default_categories,
154 .num_cat = ARRAY_SIZE(default_categories),
155};
156
157int main(int argc, char *argv[])
158{
159 ctx = talloc_named_const(NULL, 0, "osmo_io_test");
160 osmo_init_logging2(ctx, &info);
161 log_set_use_color(osmo_stderr_target, 0);
162 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
163 log_set_print_category(osmo_stderr_target, 0);
164 log_set_print_category_hex(osmo_stderr_target, 0);
165
166 test_connected();
167 test_unconnected();
168
169 return EXIT_SUCCESS;
170}