blob: af87c27b39c85d0d503a6e7b8f4a2c64bc900f9f [file] [log] [blame]
Jacob Erlbeck36106ae2014-03-20 19:14:33 +01001/* 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
41static 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
52static 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
68static int receive_messages(int fd)
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;
79 rc = ipa_msg_recv(fd, &msg);
80 if (rc == -1)
81 rc = -errno;
82 fprintf(stderr,
83 "ipa_msg_recv: %d, msg %s NULL\n",
84 rc, msg ? "!=" : "==");
85 if (rc == -EAGAIN)
86 printf( "got msg %s NULL, "
87 "returned: %s\n",
88 msg ? "!=" : "==",
89 rc == 0 ? "EOF" :
90 rc > 0 ? "OK" :
91 strerror(-rc));
92 if (rc == 0)
93 return 0;
94 if (rc == -EAGAIN)
95 break;
96 if (rc < 0) {
97 printf("ipa_msg_recv failed with: %s\n", strerror(-rc));
98 return rc;
99 }
100 printf("got IPA message, size=%d, proto=%d, text=\"%s\"\n",
101 rc, msg->data[2], msg->l2h);
102 msgb_free(msg);
103 };
104
105 return rc;
106}
107
108static int slurp_data(int fd) {
109 int rc;
110 char buf[256];
111 int count = 0;
112
113 do {
114 rc = recv(fd, buf, sizeof(buf), 0);
115 if (rc <= 0)
116 break;
117
118 count += rc;
119 } while (1);
120
121 return count;
122};
123
124static void test_complete_recv(void)
125{
126 int sv[2];
127 struct msgb *msg_out = msgb_alloc(4096, "msg_out");
128 int rc, i;
129
130 printf("Testing IPA recv with complete messages.\n");
131
132 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1)
133 err(1, "socketpair");
134
135 fcntl(sv[0], F_SETFL, O_NONBLOCK);
136
137 for (i=0; i < ARRAY_SIZE(ipa_test_messages); i++)
138 append_ipa_message(msg_out, 200, ipa_test_messages[i]);
139
140 while (msg_out->len > 0) {
141 rc = write(sv[1], msg_out->data, msg_out->len);
142 if (rc == -1)
143 err(1, "write");
144 msgb_pull(msg_out, rc);
145 }
146
147 for (i=0; i < ARRAY_SIZE(ipa_test_messages); i++) {
148 rc = receive_messages(sv[0]);
149 if (rc == 0)
150 break;
151
152 if (rc < 0 && rc != -EAGAIN)
153 break;
154 }
155
156 rc = slurp_data(sv[0]);
157 printf("done: unread %d, unsent %d\n", rc, msg_out->len);
158
159 close(sv[1]);
160 close(sv[0]);
161
162 msgb_free(msg_out);
163}
164
165
166static void test_partial_recv(void)
167{
168 int sv[2];
169 struct msgb *msg_out = msgb_alloc(4096, "msg_out");
170 int rc, i;
171
172 printf("Testing IPA recv with partitioned messages.\n");
173
174 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1)
175 err(1, "socketpair");
176
177 fcntl(sv[0], F_SETFL, O_NONBLOCK);
178
179 for (i=0; i < ARRAY_SIZE(ipa_test_messages); i++)
180 append_ipa_message(msg_out, 200, ipa_test_messages[i]);
181
182 while (msg_out->len > 0) {
183 int len = 5;
184 if (len > msg_out->len)
185 len = msg_out->len;
186 if (write(sv[1], msg_out->data, len) == -1)
187 err(1, "write");
188 msgb_pull(msg_out, len);
189
190 if (msg_out->len == 0)
191 shutdown(sv[1], SHUT_WR);
192
193 rc = receive_messages(sv[0]);
194
195 if (rc == 0)
196 break;
197
198 if (rc < 0 && rc != -EAGAIN)
199 break;
200 }
201 rc = slurp_data(sv[0]);
202 printf("done: unread %d, unsent %d\n", rc, msg_out->len);
203
204 close(sv[1]);
205 close(sv[0]);
206
207 msgb_free(msg_out);
208}
209
210static struct log_info info = {};
211
212int main(int argc, char **argv)
213{
214 osmo_init_logging(&info);
215 log_set_all_filter(osmo_stderr_target, 1);
216 log_set_log_level(osmo_stderr_target, LOGL_INFO);
217
218 printf("Testing the IPA layer.\n");
219
220 /* run the tests */
221 test_complete_recv();
222 test_partial_recv();
223
224 printf("No crashes.\n");
225 return 0;
226}