blob: 07a7ad7305ee34deaa9b174a2e9387b8c9261c9a [file] [log] [blame]
Jacob Erlbeck36106ae2014-03-20 19:14:33 +01001/* IPA receive test */
2
3/*
4 * (C) 2014 by On-Waves
Harald Welte323d39d2017-11-13 01:09:21 +09005 * (C) 2014 by sysmocom - s.f.m.c. GmbH
Jacob Erlbeck36106ae2014-03-20 19:14:33 +01006 *
7 * All Rights Reserved
8 *
Harald Welte323d39d2017-11-13 01:09:21 +09009 * SPDX-License-Identifier: AGPL-3.0+
10 *
Jacob Erlbeck36106ae2014-03-20 19:14:33 +010011 * 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
43static 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
54static 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 Erlbeck98af3c32014-03-31 10:53:32 +020070static int receive_messages(int fd, struct msgb **pending_msg)
Jacob Erlbeck36106ae2014-03-20 19:14:33 +010071{
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 Erlbeck98af3c32014-03-31 10:53:32 +020081 rc = ipa_msg_recv_buffered(fd, &msg, pending_msg);
82
Jacob Erlbeck36106ae2014-03-20 19:14:33 +010083 fprintf(stderr,
Jacob Erlbeck98af3c32014-03-31 10:53:32 +020084 "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 Erlbeck36106ae2014-03-20 19:14:33 +010097 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 Erlbeck98af3c32014-03-31 10:53:32 +0200108 printf("ipa_msg_recv_buffered failed with: %s\n",
109 strerror(-rc));
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100110 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
120static 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 Erlbeck98af3c32014-03-31 10:53:32 +0200136static void test_complete_recv(int do_not_assemble)
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100137{
138 int sv[2];
139 struct msgb *msg_out = msgb_alloc(4096, "msg_out");
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200140 struct msgb *pending_msg = NULL;
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100141 int rc, i;
142
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200143 printf("Testing IPA recv with complete messages%s.\n",
144 do_not_assemble ? "" : " with assembling enabled");
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100145
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 Erlbeck98af3c32014-03-31 10:53:32 +0200162 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 Erlbeck36106ae2014-03-20 19:14:33 +0100167 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 Erlbeck98af3c32014-03-31 10:53:32 +0200181 msgb_free(pending_msg);
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100182}
183
184
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200185static void test_partial_recv(int do_not_assemble)
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100186{
187 int sv[2];
188 struct msgb *msg_out = msgb_alloc(4096, "msg_out");
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200189 struct msgb *pending_msg = NULL;
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100190 int rc, i;
191
Jacob Erlbeck98af3c32014-03-31 10:53:32 +0200192 printf("Testing IPA recv with partitioned messages%s.\n",
193 do_not_assemble ? "" : " with assembling enabled");
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100194
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 Erlbeck98af3c32014-03-31 10:53:32 +0200214 rc = receive_messages(sv[0],
215 do_not_assemble ? NULL : &pending_msg);
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100216
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 Erlbeck98af3c32014-03-31 10:53:32 +0200230 msgb_free(pending_msg);
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100231}
232
233static struct log_info info = {};
234
235int main(int argc, char **argv)
236{
Pau Espin Pedrolc04d8d22018-04-17 14:42:44 +0200237 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 Erlbeck36106ae2014-03-20 19:14:33 +0100240 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 Erlbeck98af3c32014-03-31 10:53:32 +0200246 test_complete_recv(1);
247 test_partial_recv(1);
248 test_complete_recv(0);
249 test_partial_recv(0);
Jacob Erlbeck36106ae2014-03-20 19:14:33 +0100250
251 printf("No crashes.\n");
252 return 0;
253}