blob: e6cb33ec9a15ad2e2ecd34b349a2549780c050e0 [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);
79 printf("put(4) -> data%+d\n", cptr - msg->data);
80 printf("Buffer: %s\n", msgb_hexdump(msg));
81 OSMO_ASSERT(msgb_test_invariant(msg));
82 cptr = msg->l2h = msgb_put(msg, 4);
83 printf("put(4) -> data%+d\n", cptr - msg->data);
84 printf("Buffer: %s\n", msgb_hexdump(msg));
85 OSMO_ASSERT(msgb_test_invariant(msg));
86 cptr = msg->l3h = msgb_put(msg, 4);
87 printf("put(4) -> data%+d\n", cptr - msg->data);
88 printf("Buffer: %s\n", msgb_hexdump(msg));
89 OSMO_ASSERT(msgb_test_invariant(msg));
90 cptr = msg->l4h = msgb_put(msg, 4);
91 printf("put(4) -> data%+d\n", cptr - msg->data);
92 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);
96 printf("push(4) -> data%+d\n", cptr - msg->data);
97 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);
108 printf("get(4) -> data%+d\n", cptr - msg->data);
109 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 Erlbeck0a053ec2015-11-27 13:26:18 +0100124static void test_msgb_copy()
125{
126 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
127 struct msgb *msg2;
128 int i;
129
130 printf("Testing msgb_copy\n");
131
132 msg->l1h = msgb_put(msg, 20);
133 msg->l2h = msgb_put(msg, 20);
134 msg->l3h = msgb_put(msg, 20);
135 msg->l4h = msgb_put(msg, 20);
136
137 OSMO_ASSERT(msgb_length(msg) == 80);
138 for (i = 0; i < msgb_length(msg); i++)
139 msg->data[i] = (uint8_t)i;
140
141 msg2 = msgb_copy(msg, "copy");
142
143 OSMO_ASSERT(msgb_length(msg) == msgb_length(msg2));
144 OSMO_ASSERT(msgb_l1len(msg) == msgb_l1len(msg2));
145 OSMO_ASSERT(msgb_l2len(msg) == msgb_l2len(msg2));
146 OSMO_ASSERT(msgb_l3len(msg) == msgb_l3len(msg2));
147 OSMO_ASSERT(msg->tail - msg->l4h == msg2->tail - msg2->l4h);
148
149 for (i = 0; i < msgb_length(msg2); i++)
150 OSMO_ASSERT(msg2->data[i] == (uint8_t)i);
151
152 printf("Src: %s\n", msgb_hexdump(msg));
153 printf("Dst: %s\n", msgb_hexdump(msg));
154
155 msgb_free(msg);
156 msgb_free(msg2);
157}
158
159static void test_msgb_resize_area()
160{
161 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
162 int rc;
163 volatile int e = 0;
164 int i, saved_i;
165 uint8_t *cptr, *old_l3h;
166
167 osmo_set_panic_handler(osmo_panic_raise);
168
169 rc = msgb_resize_area(msg, msg->data, 0, 0);
170 OSMO_ASSERT(rc >= 0);
171
172 if (OSMO_PANIC_TRY(&e))
173 msgb_resize_area(msg, NULL, 0, 0);
174 OSMO_ASSERT(e != 0);
175
176 if (OSMO_PANIC_TRY(&e))
177 msgb_resize_area(msg, NULL, (int)msg->data, 0);
178 OSMO_ASSERT(e != 0);
179
180 if (OSMO_PANIC_TRY(&e))
181 msgb_resize_area(msg, msg->data, 20, 0);
182 OSMO_ASSERT(e != 0);
183
184 if (OSMO_PANIC_TRY(&e))
185 msgb_resize_area(msg, msg->data, -1, 0);
186 OSMO_ASSERT(e != 0);
187
188 if (OSMO_PANIC_TRY(&e))
189 msgb_resize_area(msg, msg->data, 0, -1);
190 OSMO_ASSERT(e != 0);
191
192 printf("Testing msgb_resize_area\n");
193
194 msg->l1h = msgb_put(msg, 20);
195 msg->l2h = msgb_put(msg, 20);
196 msg->l3h = msgb_put(msg, 20);
197 msg->l4h = msgb_put(msg, 20);
198
199 for (i = 0; i < msgb_length(msg); i++)
200 msg->data[i] = (uint8_t)i;
201
202 printf("Original: %s\n", msgb_hexdump(msg));
203
204 /* Extend area */
205 saved_i = msg->l3h[0];
206 old_l3h = msg->l3h;
207
208 rc = msgb_resize_area(msg, msg->l2h, 20, 20 + 30);
209
210 /* Reset the undefined part to allow printing the buffer to stdout */
211 memset(old_l3h, 0, msg->l3h - old_l3h);
212
213 printf("Extended: %s\n", msgb_hexdump(msg));
214
215 OSMO_ASSERT(rc >= 0);
216 OSMO_ASSERT(msgb_length(msg) == 80 + 30);
217 OSMO_ASSERT(msgb_l1len(msg) == 80 + 30);
218 OSMO_ASSERT(msgb_l2len(msg) == 60 + 30);
219 OSMO_ASSERT(msgb_l3len(msg) == 40);
220 OSMO_ASSERT(msg->tail - msg->l4h == 20);
221
222 for (cptr = msgb_data(msg), i = 0; cptr < old_l3h; cptr++, i++)
223 OSMO_ASSERT(*cptr == (uint8_t)i);
224
225 for (cptr = msg->l3h, i = saved_i; cptr < msg->tail; cptr++, i++)
226 OSMO_ASSERT(*cptr == (uint8_t)i);
227
228 rc = msgb_resize_area(msg, msg->l2h, 50, 8000);
229 OSMO_ASSERT(rc == -1);
230
231 /* Shrink area */
232 saved_i = msg->l4h[0];
233 OSMO_ASSERT(saved_i == (uint8_t)(msg->l4h[-1] + 1));
234
235 rc = msgb_resize_area(msg, msg->l3h, 20, 10);
236
237 printf("Shrinked: %s\n", msgb_hexdump(msg));
238
239 OSMO_ASSERT(rc >= 0);
240 OSMO_ASSERT(msgb_length(msg) == 80 + 30 - 10);
241 OSMO_ASSERT(msgb_l1len(msg) == 80 + 30 - 10);
242 OSMO_ASSERT(msgb_l2len(msg) == 60 + 30 - 10);
243 OSMO_ASSERT(msgb_l3len(msg) == 40 - 10);
244 OSMO_ASSERT(msg->tail - msg->l4h == 20);
245
246 OSMO_ASSERT(msg->l4h[0] != msg->l4h[-1] - 1);
247
248 for (cptr = msg->l4h, i = saved_i; cptr < msg->tail; cptr++, i++)
249 OSMO_ASSERT(*cptr == (uint8_t)i);
250
251 rc = msgb_resize_area(msg, msg->l2h, 50, 8000);
252 OSMO_ASSERT(rc == -1);
253
254 msgb_free(msg);
255
256 osmo_set_panic_handler(NULL);
257}
258
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +0100259static struct log_info info = {};
260
261int main(int argc, char **argv)
262{
263 osmo_init_logging(&info);
264
265 test_msgb_api();
Jacob Erlbeck0a053ec2015-11-27 13:26:18 +0100266 test_msgb_copy();
267 test_msgb_resize_area();
Jacob Erlbeck7cd8a1b2015-11-27 13:26:16 +0100268
269 printf("Success.\n");
270
271 return 0;
272}