blob: cff594b753803e6adf915ba38e514af754f0f3f5 [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]);
Daniel Willmanne2a8dc42023-06-30 10:51:53 +020089 // Explicitly check if ep1 is connected through write_cb
90 osmo_iofd_notify_connected(iofd1);
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020091
Daniel Willmann37b2ebf2023-03-31 14:36:16 +020092 /* Allow enough cycles to handle the messages */
93 for (int i = 0; i < 128; i++)
94 osmo_select_main(1);
95
96 osmo_iofd_free(iofd1);
97 osmo_iofd_free(iofd2);
98}
99
100static void recvfrom_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg,
101 const struct osmo_sockaddr *saddr)
102{
103 printf("%s: recvfrom() msg with len=%d\n", osmo_iofd_get_name(iofd), rc);
104 if (msg)
105 printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
106
107 talloc_free(msg);
108}
109
Daniel Willmann3ed87722023-06-06 11:34:14 +0200110static void sendto_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg,
Daniel Willmann37b2ebf2023-03-31 14:36:16 +0200111 const struct osmo_sockaddr *daddr)
112{
113 printf("%s: sendto() returned rc=%d\n", osmo_iofd_get_name(iofd), rc);
114}
115
116struct osmo_io_ops ioops_conn_recvfrom_sendto = {
117 .sendto_cb = sendto_cb,
118 .recvfrom_cb = recvfrom_cb,
119};
120
121static void test_unconnected(void)
122{
123 int fds[2] = {0, 0}, rc;
124 struct osmo_io_fd *iofd1, *iofd2;
125 struct msgb *msg;
126 uint8_t *buf;
127
128 TEST_START();
129
130 rc = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds);
131 OSMO_ASSERT(rc == 0);
132
133 iofd1 = osmo_iofd_setup(ctx, fds[0], "ep1", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_conn_recvfrom_sendto, NULL);
134 osmo_iofd_register(iofd1, fds[0]);
135 iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_conn_recvfrom_sendto, NULL);
136 osmo_iofd_register(iofd2, fds[1]);
137
138 msg = msgb_alloc(1024, "Test data");
139 buf = msgb_put(msg, sizeof(TESTDATA));
140 memcpy(buf, TESTDATA, sizeof(TESTDATA));
141
142 osmo_iofd_sendto_msgb(iofd1, msg, 0, NULL);
Daniel Willmann37b2ebf2023-03-31 14:36:16 +0200143
144 /* Allow enough cycles to handle the messages */
145 for (int i = 0; i < 128; i++)
146 osmo_select_main(1);
147
148 osmo_iofd_free(iofd1);
149 osmo_iofd_free(iofd2);
150}
151static const struct log_info_cat default_categories[] = {
152};
153
154static struct log_info info = {
155 .cat = default_categories,
156 .num_cat = ARRAY_SIZE(default_categories),
157};
158
159int main(int argc, char *argv[])
160{
161 ctx = talloc_named_const(NULL, 0, "osmo_io_test");
162 osmo_init_logging2(ctx, &info);
163 log_set_use_color(osmo_stderr_target, 0);
164 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
165 log_set_print_category(osmo_stderr_target, 0);
166 log_set_print_category_hex(osmo_stderr_target, 0);
167
168 test_connected();
169 test_unconnected();
170
171 return EXIT_SUCCESS;
172}