Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 1 | /* IPA receive test */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2014 by On-Waves |
Harald Welte | 323d39d | 2017-11-13 01:09:21 +0900 | [diff] [blame] | 5 | * (C) 2014 by sysmocom - s.f.m.c. GmbH |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | 323d39d | 2017-11-13 01:09:21 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: AGPL-3.0+ |
| 10 | * |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU Affero General Public License as published by |
| 13 | * the Free Software Foundation; either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Affero General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Affero General Public License |
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <osmocom/abis/e1_input.h> |
| 27 | |
| 28 | #include <osmocom/abis/ipa.h> |
| 29 | #include <osmocom/core/utils.h> |
| 30 | #include <osmocom/core/msgb.h> |
| 31 | #include <osmocom/core/logging.h> |
| 32 | #include <osmocom/core/application.h> |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <unistd.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <errno.h> |
| 41 | #include <err.h> |
| 42 | |
| 43 | static const char *ipa_test_messages[] = { |
| 44 | "Hello IPA", |
| 45 | "A longer test message. ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz", |
| 46 | "Hello again IPA", |
| 47 | "", |
| 48 | "Next is empty", |
| 49 | NULL, |
| 50 | "Bye", |
| 51 | "Bye", |
| 52 | }; |
| 53 | |
| 54 | static void append_ipa_message(struct msgb *msg, int proto, const char *text) |
| 55 | { |
| 56 | int len = 0; |
| 57 | unsigned char *l2; |
| 58 | |
| 59 | if (text) |
| 60 | len = strlen(text) + 1; |
| 61 | |
| 62 | msgb_put_u16(msg, len); |
| 63 | msgb_put_u8(msg, proto); |
| 64 | |
| 65 | l2 = msgb_put(msg, len); |
| 66 | if (text) |
| 67 | strcpy((char *)l2, text); |
| 68 | } |
| 69 | |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 70 | static int receive_messages(int fd, struct msgb **pending_msg) |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 71 | { |
| 72 | struct msgb *msg; |
| 73 | char dummy; |
| 74 | int rc; |
| 75 | while (1) { |
| 76 | if (recv(fd, &dummy, 1, MSG_PEEK) < 1) { |
| 77 | rc = -EAGAIN; |
| 78 | break; |
| 79 | } |
| 80 | msg = NULL; |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 81 | rc = ipa_msg_recv_buffered(fd, &msg, pending_msg); |
| 82 | |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 83 | fprintf(stderr, |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 84 | "ipa_msg_recv_buffered: %d, msg %s NULL, " |
| 85 | "pending_msg %s NULL\n", |
| 86 | rc, msg ? "!=" : "==", |
| 87 | !pending_msg ? "??" : *pending_msg ? "!=" : "=="); |
| 88 | if (pending_msg && !!msg == !!*pending_msg) |
| 89 | printf( "got msg %s NULL, pending_msg %s NULL, " |
| 90 | "returned: %s\n", |
| 91 | msg ? "!=" : "==", |
| 92 | *pending_msg ? "!=" : "==", |
| 93 | rc == 0 ? "EOF" : |
| 94 | rc > 0 ? "OK" : |
| 95 | strerror(-rc)); |
| 96 | else if (!pending_msg && rc == -EAGAIN) |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 97 | printf( "got msg %s NULL, " |
| 98 | "returned: %s\n", |
| 99 | msg ? "!=" : "==", |
| 100 | rc == 0 ? "EOF" : |
| 101 | rc > 0 ? "OK" : |
| 102 | strerror(-rc)); |
| 103 | if (rc == 0) |
| 104 | return 0; |
| 105 | if (rc == -EAGAIN) |
| 106 | break; |
| 107 | if (rc < 0) { |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 108 | printf("ipa_msg_recv_buffered failed with: %s\n", |
| 109 | strerror(-rc)); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 110 | return rc; |
| 111 | } |
| 112 | printf("got IPA message, size=%d, proto=%d, text=\"%s\"\n", |
| 113 | rc, msg->data[2], msg->l2h); |
| 114 | msgb_free(msg); |
| 115 | }; |
| 116 | |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | static int slurp_data(int fd) { |
| 121 | int rc; |
| 122 | char buf[256]; |
| 123 | int count = 0; |
| 124 | |
| 125 | do { |
| 126 | rc = recv(fd, buf, sizeof(buf), 0); |
| 127 | if (rc <= 0) |
| 128 | break; |
| 129 | |
| 130 | count += rc; |
| 131 | } while (1); |
| 132 | |
| 133 | return count; |
| 134 | }; |
| 135 | |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 136 | static void test_complete_recv(int do_not_assemble) |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 137 | { |
| 138 | int sv[2]; |
| 139 | struct msgb *msg_out = msgb_alloc(4096, "msg_out"); |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 140 | struct msgb *pending_msg = NULL; |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 141 | int rc, i; |
| 142 | |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 143 | printf("Testing IPA recv with complete messages%s.\n", |
| 144 | do_not_assemble ? "" : " with assembling enabled"); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 145 | |
| 146 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) |
| 147 | err(1, "socketpair"); |
| 148 | |
| 149 | fcntl(sv[0], F_SETFL, O_NONBLOCK); |
| 150 | |
| 151 | for (i=0; i < ARRAY_SIZE(ipa_test_messages); i++) |
| 152 | append_ipa_message(msg_out, 200, ipa_test_messages[i]); |
| 153 | |
| 154 | while (msg_out->len > 0) { |
| 155 | rc = write(sv[1], msg_out->data, msg_out->len); |
| 156 | if (rc == -1) |
| 157 | err(1, "write"); |
| 158 | msgb_pull(msg_out, rc); |
| 159 | } |
| 160 | |
| 161 | for (i=0; i < ARRAY_SIZE(ipa_test_messages); i++) { |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 162 | rc = receive_messages(sv[0], |
| 163 | do_not_assemble ? NULL : &pending_msg); |
| 164 | if (pending_msg) |
| 165 | printf("Unexpected partial message: size=%d\n", |
| 166 | pending_msg->len); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 167 | if (rc == 0) |
| 168 | break; |
| 169 | |
| 170 | if (rc < 0 && rc != -EAGAIN) |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | rc = slurp_data(sv[0]); |
| 175 | printf("done: unread %d, unsent %d\n", rc, msg_out->len); |
| 176 | |
| 177 | close(sv[1]); |
| 178 | close(sv[0]); |
| 179 | |
| 180 | msgb_free(msg_out); |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 181 | msgb_free(pending_msg); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 185 | static void test_partial_recv(int do_not_assemble) |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 186 | { |
| 187 | int sv[2]; |
| 188 | struct msgb *msg_out = msgb_alloc(4096, "msg_out"); |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 189 | struct msgb *pending_msg = NULL; |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 190 | int rc, i; |
| 191 | |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 192 | printf("Testing IPA recv with partitioned messages%s.\n", |
| 193 | do_not_assemble ? "" : " with assembling enabled"); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 194 | |
| 195 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) |
| 196 | err(1, "socketpair"); |
| 197 | |
| 198 | fcntl(sv[0], F_SETFL, O_NONBLOCK); |
| 199 | |
| 200 | for (i=0; i < ARRAY_SIZE(ipa_test_messages); i++) |
| 201 | append_ipa_message(msg_out, 200, ipa_test_messages[i]); |
| 202 | |
| 203 | while (msg_out->len > 0) { |
| 204 | int len = 5; |
| 205 | if (len > msg_out->len) |
| 206 | len = msg_out->len; |
| 207 | if (write(sv[1], msg_out->data, len) == -1) |
| 208 | err(1, "write"); |
| 209 | msgb_pull(msg_out, len); |
| 210 | |
| 211 | if (msg_out->len == 0) |
| 212 | shutdown(sv[1], SHUT_WR); |
| 213 | |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 214 | rc = receive_messages(sv[0], |
| 215 | do_not_assemble ? NULL : &pending_msg); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 216 | |
| 217 | if (rc == 0) |
| 218 | break; |
| 219 | |
| 220 | if (rc < 0 && rc != -EAGAIN) |
| 221 | break; |
| 222 | } |
| 223 | rc = slurp_data(sv[0]); |
| 224 | printf("done: unread %d, unsent %d\n", rc, msg_out->len); |
| 225 | |
| 226 | close(sv[1]); |
| 227 | close(sv[0]); |
| 228 | |
| 229 | msgb_free(msg_out); |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 230 | msgb_free(pending_msg); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static struct log_info info = {}; |
| 234 | |
| 235 | int main(int argc, char **argv) |
| 236 | { |
Pau Espin Pedrol | c04d8d2 | 2018-04-17 14:42:44 +0200 | [diff] [blame] | 237 | void *tall_ctx = talloc_named_const(NULL, 1, "Root context"); |
| 238 | msgb_talloc_ctx_init(tall_ctx, 0); |
| 239 | osmo_init_logging2(tall_ctx, &info); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 240 | log_set_all_filter(osmo_stderr_target, 1); |
| 241 | log_set_log_level(osmo_stderr_target, LOGL_INFO); |
| 242 | |
| 243 | printf("Testing the IPA layer.\n"); |
| 244 | |
| 245 | /* run the tests */ |
Jacob Erlbeck | 98af3c3 | 2014-03-31 10:53:32 +0200 | [diff] [blame] | 246 | test_complete_recv(1); |
| 247 | test_partial_recv(1); |
| 248 | test_complete_recv(0); |
| 249 | test_partial_recv(0); |
Jacob Erlbeck | 36106ae | 2014-03-20 19:14:33 +0100 | [diff] [blame] | 250 | |
| 251 | printf("No crashes.\n"); |
| 252 | return 0; |
| 253 | } |