blob: ac1038297e6cdd1d1a8461f2f09b2906faa54ca4 [file] [log] [blame]
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +01001/*
2 * (C) 2014 by On-Waves
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <stdlib.h>
22#include <osmocom/core/application.h>
23#include <osmocom/core/logging.h>
24#include <osmocom/core/utils.h>
25#include <osmocom/core/msgb.h>
Jacob Erlbeckcbefa082015-11-27 13:26:17 +010026#include <setjmp.h>
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +010027
28#include <errno.h>
29
30#include <string.h>
31
32#define CHECK_RC(rc) \
33 if (rc != 0) { \
34 printf("Operation failed rc=%d on %s:%d\n", rc, __FILE__, __LINE__); \
35 abort(); \
36 }
37
Jacob Erlbeckcbefa082015-11-27 13:26:17 +010038static jmp_buf jmp_env;
39static int jmp_env_valid = 0;
40static void osmo_panic_raise(const char *fmt, va_list args)
41{
42 /*
43 * The args can include pointer values which are not suitable for
44 * regression testing. So just write the (hopefully constant) format
45 * string to stdout and write the full message to stderr.
46 */
47 printf("%s", fmt);
48 vfprintf(stderr, fmt, args);
49 if (!jmp_env_valid)
50 abort();
51 longjmp(jmp_env, 1);
52}
53
54/* Note that this does not nest */
55#define OSMO_PANIC_TRY(pE) (osmo_panic_try(pE, setjmp(jmp_env)))
56
57static int osmo_panic_try(volatile int *exception, int setjmp_result)
58{
59 jmp_env_valid = setjmp_result == 0;
60 *exception = setjmp_result;
61
62 if (setjmp_result)
63 fprintf(stderr, "Exception caught: %d\n", setjmp_result);
64
65 return *exception == 0;
66}
67
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +010068static void test_msgb_api()
69{
70 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
71 unsigned char *cptr = NULL;
72 int rc;
73
74 printf("Testing the msgb API\n");
75
76 printf("Buffer: %s\n", msgb_hexdump(msg));
77 OSMO_ASSERT(msgb_test_invariant(msg));
78 cptr = msg->l1h = msgb_put(msg, 4);
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +000079 printf("put(4) -> data%+td\n", cptr - msg->data);
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +010080 printf("Buffer: %s\n", msgb_hexdump(msg));
81 OSMO_ASSERT(msgb_test_invariant(msg));
82 cptr = msg->l2h = msgb_put(msg, 4);
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +000083 printf("put(4) -> data%+td\n", cptr - msg->data);
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +010084 printf("Buffer: %s\n", msgb_hexdump(msg));
85 OSMO_ASSERT(msgb_test_invariant(msg));
86 cptr = msg->l3h = msgb_put(msg, 4);
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +000087 printf("put(4) -> data%+td\n", cptr - msg->data);
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +010088 printf("Buffer: %s\n", msgb_hexdump(msg));
89 OSMO_ASSERT(msgb_test_invariant(msg));
90 cptr = msg->l4h = msgb_put(msg, 4);
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +000091 printf("put(4) -> data%+td\n", cptr - msg->data);
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +010092 printf("Buffer: %s\n", msgb_hexdump(msg));
93 OSMO_ASSERT(msgb_test_invariant(msg));
94 OSMO_ASSERT(msgb_length(msg) == 16);
95 cptr = msgb_push(msg, 4);
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +000096 printf("push(4) -> data%+td\n", cptr - msg->data);
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +010097 printf("Buffer: %s\n", msgb_hexdump(msg));
98 OSMO_ASSERT(msgb_test_invariant(msg));
99 OSMO_ASSERT(msgb_length(msg) == 20);
100 rc = msgb_trim(msg, 16);
101 printf("trim(16) -> %d\n", rc);
102 CHECK_RC(rc);
103 OSMO_ASSERT(msgb_test_invariant(msg));
104 printf("Buffer: %s\n", msgb_hexdump(msg));
105 OSMO_ASSERT(msgb_length(msg) == 16);
106
107 cptr = msgb_get(msg, 4);
Holger Hans Peter Freytherfdb46672015-11-09 16:32:43 +0000108 printf("get(4) -> data%+td\n", cptr - msg->data);
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +0100109 printf("Buffer: %s\n", msgb_hexdump(msg));
110 OSMO_ASSERT(msgb_test_invariant(msg));
111 OSMO_ASSERT(msgb_length(msg) == 12);
112
113 printf("Test msgb_hexdump\n");
114 msg->l1h = msg->head;
115 printf("Buffer: %s\n", msgb_hexdump(msg));
116 msg->l3h = msg->data;
117 printf("Buffer: %s\n", msgb_hexdump(msg));
118 msg->l3h = msg->head - 1;
119 printf("Buffer: %s\n", msgb_hexdump(msg));
120
121 msgb_free(msg);
122}
123
Jacob Erlbeck17b3c3a2015-11-27 13:26:20 +0100124static void test_msgb_api_errors()
125{
126 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
127 volatile int e = 0;
128 int rc;
129
130 printf("Testing the msgb API error handling\n");
131
132 osmo_set_panic_handler(osmo_panic_raise);
133
134 if (OSMO_PANIC_TRY(&e))
135 msgb_trim(msg, -1);
136 OSMO_ASSERT(e != 0);
137
138 rc = msgb_trim(msg, 4096 + 500);
139 OSMO_ASSERT(rc == -1);
140
141 msgb_free(msg);
142 osmo_set_panic_handler(NULL);
143}
144
Jacob Erlbeck0a053ec2015-11-27 13:26:18 +0100145static void test_msgb_copy()
146{
147 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
148 struct msgb *msg2;
149 int i;
150
151 printf("Testing msgb_copy\n");
152
153 msg->l1h = msgb_put(msg, 20);
154 msg->l2h = msgb_put(msg, 20);
155 msg->l3h = msgb_put(msg, 20);
156 msg->l4h = msgb_put(msg, 20);
157
158 OSMO_ASSERT(msgb_length(msg) == 80);
159 for (i = 0; i < msgb_length(msg); i++)
160 msg->data[i] = (uint8_t)i;
161
162 msg2 = msgb_copy(msg, "copy");
163
164 OSMO_ASSERT(msgb_length(msg) == msgb_length(msg2));
165 OSMO_ASSERT(msgb_l1len(msg) == msgb_l1len(msg2));
166 OSMO_ASSERT(msgb_l2len(msg) == msgb_l2len(msg2));
167 OSMO_ASSERT(msgb_l3len(msg) == msgb_l3len(msg2));
168 OSMO_ASSERT(msg->tail - msg->l4h == msg2->tail - msg2->l4h);
169
170 for (i = 0; i < msgb_length(msg2); i++)
171 OSMO_ASSERT(msg2->data[i] == (uint8_t)i);
172
173 printf("Src: %s\n", msgb_hexdump(msg));
174 printf("Dst: %s\n", msgb_hexdump(msg));
175
176 msgb_free(msg);
177 msgb_free(msg2);
178}
179
180static void test_msgb_resize_area()
181{
182 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
183 int rc;
184 volatile int e = 0;
185 int i, saved_i;
186 uint8_t *cptr, *old_l3h;
187
188 osmo_set_panic_handler(osmo_panic_raise);
189
190 rc = msgb_resize_area(msg, msg->data, 0, 0);
191 OSMO_ASSERT(rc >= 0);
192
193 if (OSMO_PANIC_TRY(&e))
194 msgb_resize_area(msg, NULL, 0, 0);
195 OSMO_ASSERT(e != 0);
196
197 if (OSMO_PANIC_TRY(&e))
Harald Weltea24be852016-12-09 17:58:17 +0100198 msgb_resize_area(msg, NULL, 0, 0);
Jacob Erlbeck0a053ec2015-11-27 13:26:18 +0100199 OSMO_ASSERT(e != 0);
200
201 if (OSMO_PANIC_TRY(&e))
202 msgb_resize_area(msg, msg->data, 20, 0);
203 OSMO_ASSERT(e != 0);
204
205 if (OSMO_PANIC_TRY(&e))
206 msgb_resize_area(msg, msg->data, -1, 0);
207 OSMO_ASSERT(e != 0);
208
209 if (OSMO_PANIC_TRY(&e))
210 msgb_resize_area(msg, msg->data, 0, -1);
211 OSMO_ASSERT(e != 0);
212
213 printf("Testing msgb_resize_area\n");
214
215 msg->l1h = msgb_put(msg, 20);
216 msg->l2h = msgb_put(msg, 20);
217 msg->l3h = msgb_put(msg, 20);
218 msg->l4h = msgb_put(msg, 20);
219
220 for (i = 0; i < msgb_length(msg); i++)
221 msg->data[i] = (uint8_t)i;
222
223 printf("Original: %s\n", msgb_hexdump(msg));
224
225 /* Extend area */
226 saved_i = msg->l3h[0];
227 old_l3h = msg->l3h;
228
229 rc = msgb_resize_area(msg, msg->l2h, 20, 20 + 30);
230
231 /* Reset the undefined part to allow printing the buffer to stdout */
232 memset(old_l3h, 0, msg->l3h - old_l3h);
233
234 printf("Extended: %s\n", msgb_hexdump(msg));
235
236 OSMO_ASSERT(rc >= 0);
237 OSMO_ASSERT(msgb_length(msg) == 80 + 30);
238 OSMO_ASSERT(msgb_l1len(msg) == 80 + 30);
239 OSMO_ASSERT(msgb_l2len(msg) == 60 + 30);
240 OSMO_ASSERT(msgb_l3len(msg) == 40);
241 OSMO_ASSERT(msg->tail - msg->l4h == 20);
242
243 for (cptr = msgb_data(msg), i = 0; cptr < old_l3h; cptr++, i++)
244 OSMO_ASSERT(*cptr == (uint8_t)i);
245
246 for (cptr = msg->l3h, i = saved_i; cptr < msg->tail; cptr++, i++)
247 OSMO_ASSERT(*cptr == (uint8_t)i);
248
249 rc = msgb_resize_area(msg, msg->l2h, 50, 8000);
250 OSMO_ASSERT(rc == -1);
251
252 /* Shrink area */
253 saved_i = msg->l4h[0];
254 OSMO_ASSERT(saved_i == (uint8_t)(msg->l4h[-1] + 1));
255
256 rc = msgb_resize_area(msg, msg->l3h, 20, 10);
257
258 printf("Shrinked: %s\n", msgb_hexdump(msg));
259
260 OSMO_ASSERT(rc >= 0);
261 OSMO_ASSERT(msgb_length(msg) == 80 + 30 - 10);
262 OSMO_ASSERT(msgb_l1len(msg) == 80 + 30 - 10);
263 OSMO_ASSERT(msgb_l2len(msg) == 60 + 30 - 10);
264 OSMO_ASSERT(msgb_l3len(msg) == 40 - 10);
265 OSMO_ASSERT(msg->tail - msg->l4h == 20);
266
267 OSMO_ASSERT(msg->l4h[0] != msg->l4h[-1] - 1);
268
269 for (cptr = msg->l4h, i = saved_i; cptr < msg->tail; cptr++, i++)
270 OSMO_ASSERT(*cptr == (uint8_t)i);
271
272 rc = msgb_resize_area(msg, msg->l2h, 50, 8000);
273 OSMO_ASSERT(rc == -1);
274
275 msgb_free(msg);
276
277 osmo_set_panic_handler(NULL);
278}
279
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +0100280static struct log_info info = {};
281
282int main(int argc, char **argv)
283{
284 osmo_init_logging(&info);
285
286 test_msgb_api();
Jacob Erlbeck17b3c3a2015-11-27 13:26:20 +0100287 test_msgb_api_errors();
Jacob Erlbeck0a053ec2015-11-27 13:26:18 +0100288 test_msgb_copy();
289 test_msgb_resize_area();
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +0100290
291 printf("Success.\n");
292
293 return 0;
294}