Daniel Willmann | 37b2ebf | 2023-03-31 14:36:16 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 41 | static void *ctx = NULL; |
| 42 | |
| 43 | static void read_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg) |
| 44 | { |
| 45 | printf("%s: read() msg with len=%d\n", osmo_iofd_get_name(iofd), rc); |
| 46 | if (msg) |
| 47 | printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg))); |
| 48 | |
| 49 | talloc_free(msg); |
| 50 | } |
| 51 | |
| 52 | static void write_cb(struct osmo_io_fd *iofd, int rc, const struct msgb *msg) |
| 53 | { |
| 54 | printf("%s: write() returned rc=%d\n", osmo_iofd_get_name(iofd), rc); |
| 55 | } |
| 56 | |
| 57 | struct osmo_io_ops ioops_conn_read_write = { |
| 58 | .read_cb = read_cb, |
| 59 | .write_cb = write_cb, |
| 60 | }; |
| 61 | |
| 62 | uint8_t TESTDATA[] = { |
| 63 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 |
| 64 | }; |
| 65 | |
| 66 | static void test_connected(void) |
| 67 | { |
| 68 | int fds[2] = {0, 0}, rc; |
| 69 | struct osmo_io_fd *iofd1, *iofd2; |
| 70 | struct msgb *msg; |
| 71 | uint8_t *buf; |
| 72 | |
| 73 | TEST_START(); |
| 74 | |
| 75 | rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); |
| 76 | OSMO_ASSERT(rc == 0); |
| 77 | |
| 78 | iofd1 = osmo_iofd_setup(ctx, fds[0], "ep1", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn_read_write, NULL); |
| 79 | osmo_iofd_register(iofd1, fds[0]); |
| 80 | iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_READ_WRITE, &ioops_conn_read_write, NULL); |
| 81 | osmo_iofd_register(iofd2, fds[1]); |
| 82 | |
| 83 | msg = msgb_alloc(1024, "Test data"); |
| 84 | buf = msgb_put(msg, sizeof(TESTDATA)); |
| 85 | memcpy(buf, TESTDATA, sizeof(TESTDATA)); |
| 86 | |
| 87 | osmo_iofd_write_msgb(iofd1, msg); |
| 88 | osmo_iofd_write_enable(iofd1); |
| 89 | osmo_iofd_write_enable(iofd2); |
| 90 | osmo_iofd_read_enable(iofd1); |
| 91 | osmo_iofd_read_enable(iofd2); |
| 92 | |
| 93 | |
| 94 | /* Allow enough cycles to handle the messages */ |
| 95 | for (int i = 0; i < 128; i++) |
| 96 | osmo_select_main(1); |
| 97 | |
| 98 | osmo_iofd_free(iofd1); |
| 99 | osmo_iofd_free(iofd2); |
| 100 | } |
| 101 | |
| 102 | static void recvfrom_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg, |
| 103 | const struct osmo_sockaddr *saddr) |
| 104 | { |
| 105 | printf("%s: recvfrom() msg with len=%d\n", osmo_iofd_get_name(iofd), rc); |
| 106 | if (msg) |
| 107 | printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg))); |
| 108 | |
| 109 | talloc_free(msg); |
| 110 | } |
| 111 | |
| 112 | static void sendto_cb(struct osmo_io_fd *iofd, int rc, const struct msgb *msg, |
| 113 | const struct osmo_sockaddr *daddr) |
| 114 | { |
| 115 | printf("%s: sendto() returned rc=%d\n", osmo_iofd_get_name(iofd), rc); |
| 116 | } |
| 117 | |
| 118 | struct osmo_io_ops ioops_conn_recvfrom_sendto = { |
| 119 | .sendto_cb = sendto_cb, |
| 120 | .recvfrom_cb = recvfrom_cb, |
| 121 | }; |
| 122 | |
| 123 | static void test_unconnected(void) |
| 124 | { |
| 125 | int fds[2] = {0, 0}, rc; |
| 126 | struct osmo_io_fd *iofd1, *iofd2; |
| 127 | struct msgb *msg; |
| 128 | uint8_t *buf; |
| 129 | |
| 130 | TEST_START(); |
| 131 | |
| 132 | rc = socketpair(AF_UNIX, SOCK_DGRAM, 0, fds); |
| 133 | OSMO_ASSERT(rc == 0); |
| 134 | |
| 135 | iofd1 = osmo_iofd_setup(ctx, fds[0], "ep1", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_conn_recvfrom_sendto, NULL); |
| 136 | osmo_iofd_register(iofd1, fds[0]); |
| 137 | iofd2 = osmo_iofd_setup(ctx, fds[1], "ep2", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops_conn_recvfrom_sendto, NULL); |
| 138 | osmo_iofd_register(iofd2, fds[1]); |
| 139 | |
| 140 | msg = msgb_alloc(1024, "Test data"); |
| 141 | buf = msgb_put(msg, sizeof(TESTDATA)); |
| 142 | memcpy(buf, TESTDATA, sizeof(TESTDATA)); |
| 143 | |
| 144 | osmo_iofd_sendto_msgb(iofd1, msg, 0, NULL); |
| 145 | osmo_iofd_write_enable(iofd1); |
| 146 | osmo_iofd_write_enable(iofd2); |
| 147 | osmo_iofd_read_enable(iofd1); |
| 148 | osmo_iofd_read_enable(iofd2); |
| 149 | |
| 150 | |
| 151 | /* Allow enough cycles to handle the messages */ |
| 152 | for (int i = 0; i < 128; i++) |
| 153 | osmo_select_main(1); |
| 154 | |
| 155 | osmo_iofd_free(iofd1); |
| 156 | osmo_iofd_free(iofd2); |
| 157 | } |
| 158 | static const struct log_info_cat default_categories[] = { |
| 159 | }; |
| 160 | |
| 161 | static struct log_info info = { |
| 162 | .cat = default_categories, |
| 163 | .num_cat = ARRAY_SIZE(default_categories), |
| 164 | }; |
| 165 | |
| 166 | int main(int argc, char *argv[]) |
| 167 | { |
| 168 | ctx = talloc_named_const(NULL, 0, "osmo_io_test"); |
| 169 | osmo_init_logging2(ctx, &info); |
| 170 | log_set_use_color(osmo_stderr_target, 0); |
| 171 | log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); |
| 172 | log_set_print_category(osmo_stderr_target, 0); |
| 173 | log_set_print_category_hex(osmo_stderr_target, 0); |
| 174 | |
| 175 | test_connected(); |
| 176 | test_unconnected(); |
| 177 | |
| 178 | return EXIT_SUCCESS; |
| 179 | } |