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