blob: 93beef4f941ce517d16847f8bc9c3c8bd4dfcaa0 [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);
Daniel Willmannf91d2aa2023-01-04 18:20:55 +010098
99 for (int i = 0; i < 128; i++)
100 osmo_select_main(1);
Daniel Willmann37b2ebf2023-03-31 14:36:16 +0200101}
102
103static void recvfrom_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg,
104 const struct osmo_sockaddr *saddr)
105{
106 printf("%s: recvfrom() msg with len=%d\n", osmo_iofd_get_name(iofd), rc);
107 if (msg)
108 printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
109
110 talloc_free(msg);
111}
112
Daniel Willmann3ed87722023-06-06 11:34:14 +0200113static void sendto_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg,
Daniel Willmann37b2ebf2023-03-31 14:36:16 +0200114 const struct osmo_sockaddr *daddr)
115{
116 printf("%s: sendto() returned rc=%d\n", osmo_iofd_get_name(iofd), rc);
117}
118
119struct osmo_io_ops ioops_conn_recvfrom_sendto = {
120 .sendto_cb = sendto_cb,
121 .recvfrom_cb = recvfrom_cb,
122};
123
124static void test_unconnected(void)
125{
126 int fds[2] = {0, 0}, rc;
127 struct osmo_io_fd *iofd1, *iofd2;
128 struct msgb *msg;
129 uint8_t *buf;
130
131 TEST_START();
132
133 rc = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds);
134 OSMO_ASSERT(rc == 0);
135
136 iofd1 = osmo_iofd_setup(ctx, fds[0], "ep1", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_conn_recvfrom_sendto, NULL);
137 osmo_iofd_register(iofd1, fds[0]);
138 iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_conn_recvfrom_sendto, NULL);
139 osmo_iofd_register(iofd2, fds[1]);
140
141 msg = msgb_alloc(1024, "Test data");
142 buf = msgb_put(msg, sizeof(TESTDATA));
143 memcpy(buf, TESTDATA, sizeof(TESTDATA));
144
145 osmo_iofd_sendto_msgb(iofd1, msg, 0, NULL);
Daniel Willmann37b2ebf2023-03-31 14:36:16 +0200146
147 /* Allow enough cycles to handle the messages */
148 for (int i = 0; i < 128; i++)
149 osmo_select_main(1);
150
151 osmo_iofd_free(iofd1);
152 osmo_iofd_free(iofd2);
Daniel Willmannf91d2aa2023-01-04 18:20:55 +0100153
154 for (int i = 0; i < 128; i++)
155 osmo_select_main(1);
Daniel Willmann37b2ebf2023-03-31 14:36:16 +0200156}
157static const struct log_info_cat default_categories[] = {
158};
159
160static struct log_info info = {
161 .cat = default_categories,
162 .num_cat = ARRAY_SIZE(default_categories),
163};
164
165int main(int argc, char *argv[])
166{
167 ctx = talloc_named_const(NULL, 0, "osmo_io_test");
168 osmo_init_logging2(ctx, &info);
169 log_set_use_color(osmo_stderr_target, 0);
170 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
171 log_set_print_category(osmo_stderr_target, 0);
172 log_set_print_category_hex(osmo_stderr_target, 0);
173
174 test_connected();
175 test_unconnected();
176
177 return EXIT_SUCCESS;
178}