blob: cb4f5b644e31961f42c019b8bac2fd858cbfd3e2 [file] [log] [blame]
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +01001/*
2 * TypesTest.cpp Test the primitive data types
3 *
4 * Copyright (C) 2013 by Holger Hans Peter Freyther
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22#include "bts.h"
23#include "tbf.h"
24#include "gprs_debug.h"
25
26extern "C" {
27#include <osmocom/core/application.h>
28#include <osmocom/core/msgb.h>
29#include <osmocom/core/talloc.h>
30#include <osmocom/core/utils.h>
31}
32
Daniel Willmannc3f43302013-12-11 16:47:19 +010033#define OSMO_ASSERT_STR_EQ(a, b) \
34 do { \
35 if (strcmp(a, b)) { \
36 printf("String mismatch:\nGot:\t%s\nWant:\t%s\n", a, b); \
37 OSMO_ASSERT(false); \
38 } \
39 } while (0)
40
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +010041void *tall_pcu_ctx;
42int16_t spoof_mnc = 0, spoof_mcc = 0;
43
44static void test_llc(void)
45{
46 {
47 uint8_t data[LLC_MAX_LEN] = {1, 2, 3, 4, };
48 uint8_t out;
49 gprs_llc llc;
50 llc.init();
51
52 OSMO_ASSERT(llc.chunk_size() == 0);
53 OSMO_ASSERT(llc.remaining_space() == LLC_MAX_LEN);
54 OSMO_ASSERT(llc.frame_length() == 0);
55
56 llc.put_frame(data, 2);
57 OSMO_ASSERT(llc.remaining_space() == LLC_MAX_LEN - 2);
58 OSMO_ASSERT(llc.frame_length() == 2);
59 OSMO_ASSERT(llc.chunk_size() == 2);
60 OSMO_ASSERT(llc.frame[0] == 1);
61 OSMO_ASSERT(llc.frame[1] == 2);
62
63 llc.append_frame(&data[3], 1);
64 OSMO_ASSERT(llc.remaining_space() == LLC_MAX_LEN - 3);
65 OSMO_ASSERT(llc.frame_length() == 3);
66 OSMO_ASSERT(llc.chunk_size() == 3);
67
68 /* consume two bytes */
69 llc.consume(&out, 1);
70 OSMO_ASSERT(llc.remaining_space() == LLC_MAX_LEN - 3);
71 OSMO_ASSERT(llc.frame_length() == 3);
72 OSMO_ASSERT(llc.chunk_size() == 2);
73
74 /* check that the bytes are as we expected */
75 OSMO_ASSERT(llc.frame[0] == 1);
76 OSMO_ASSERT(llc.frame[1] == 2);
77 OSMO_ASSERT(llc.frame[2] == 4);
78
79 /* now fill the frame */
80 llc.append_frame(data, llc.remaining_space() - 1);
81 OSMO_ASSERT(llc.fits_in_current_frame(1));
82 OSMO_ASSERT(!llc.fits_in_current_frame(2));
83 }
84}
85
Holger Hans Peter Freytherc6382dd2013-11-21 21:42:20 +010086static void test_rlc()
87{
88 {
89 struct gprs_rlc_data rlc = { 0, };
90 memset(rlc.block, 0x23, RLC_MAX_LEN);
91 uint8_t *p = rlc.prepare(20);
92 OSMO_ASSERT(p == rlc.block);
93 for (int i = 0; i < 20; ++i)
94 OSMO_ASSERT(p[i] == 0x2B);
95 for (int i = 20; i < RLC_MAX_LEN; ++i)
96 OSMO_ASSERT(p[i] == 0x0);
97 }
98}
99
Holger Hans Peter Freyther95255672013-11-23 16:18:18 +0100100static void test_rlc_v_b()
101{
102 {
103 gprs_rlc_v_b vb;
104 vb.reset();
105
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100106 for (size_t i = 0; i < RLC_MAX_SNS; ++i)
Holger Hans Peter Freyther95255672013-11-23 16:18:18 +0100107 OSMO_ASSERT(vb.is_invalid(i));
108
109 vb.mark_unacked(23);
110 OSMO_ASSERT(vb.is_unacked(23));
111
112 vb.mark_nacked(23);
113 OSMO_ASSERT(vb.is_nacked(23));
114
115 vb.mark_acked(23);
116 OSMO_ASSERT(vb.is_acked(23));
117
118 vb.mark_resend(23);
119 OSMO_ASSERT(vb.is_resend(23));
120
121 vb.mark_invalid(23);
122 OSMO_ASSERT(vb.is_invalid(23));
123 }
124}
125
Holger Hans Peter Freythere9b1ebb2013-11-24 22:00:43 +0100126static void test_rlc_v_n()
127{
128 {
129 gprs_rlc_v_n vn;
130 vn.reset();
131
132 OSMO_ASSERT(!vn.is_received(0x23));
133 OSMO_ASSERT(vn.state(0x23) == ' ');
134
135 vn.mark_received(0x23);
136 OSMO_ASSERT(vn.is_received(0x23));
137 OSMO_ASSERT(vn.state(0x23) == 'R');
138
139 vn.mark_missing(0x23);
140 OSMO_ASSERT(!vn.is_received(0x23));
141 OSMO_ASSERT(vn.state(0x23) == 'N');
142 }
143}
144
Holger Hans Peter Freytherfaf3ef42013-11-24 22:17:40 +0100145static void test_rlc_dl_ul_basic()
146{
147 {
148 gprs_rlc_dl_window dl_win = { 0, };
149 OSMO_ASSERT(dl_win.window_empty());
150 OSMO_ASSERT(!dl_win.window_stalled());
151 OSMO_ASSERT(dl_win.distance() == 0);
152
153 dl_win.increment_send();
154 OSMO_ASSERT(!dl_win.window_empty());
155 OSMO_ASSERT(!dl_win.window_stalled());
156 OSMO_ASSERT(dl_win.distance() == 1);
157
158 for (int i = 1; i < 64; ++i) {
159 dl_win.increment_send();
160 OSMO_ASSERT(!dl_win.window_empty());
161 OSMO_ASSERT(dl_win.distance() == i + 1);
162 }
163
164 OSMO_ASSERT(dl_win.distance() == 64);
165 OSMO_ASSERT(dl_win.window_stalled());
166
167 dl_win.raise(1);
168 OSMO_ASSERT(dl_win.distance() == 63);
169 OSMO_ASSERT(!dl_win.window_stalled());
170 for (int i = 62; i >= 0; --i) {
171 dl_win.raise(1);
172 OSMO_ASSERT(dl_win.distance() == i);
173 }
174
175 OSMO_ASSERT(dl_win.distance() == 0);
176 OSMO_ASSERT(dl_win.window_empty());
177
178 dl_win.increment_send();
179 dl_win.increment_send();
180 dl_win.increment_send();
181 dl_win.increment_send();
182 OSMO_ASSERT(dl_win.distance() == 4);
183
184 for (int i = 0; i < 128; ++i) {
185 dl_win.increment_send();
186 dl_win.increment_send();
187 dl_win.raise(2);
188 OSMO_ASSERT(dl_win.distance() == 4);
189 }
190 }
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100191
192 {
193 gprs_rlc_ul_window ul_win = { 0, };
194 gprs_rlc_v_n v_n;
195 int count;
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100196 const char *rbb;
Daniel Willmannc3f43302013-12-11 16:47:19 +0100197 char win_rbb[65];
198 win_rbb[64] = '\0';
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100199
200 v_n.reset();
201
202 OSMO_ASSERT(ul_win.is_in_window(0));
203 OSMO_ASSERT(ul_win.is_in_window(63));
204 OSMO_ASSERT(!ul_win.is_in_window(64));
205
206 OSMO_ASSERT(!v_n.is_received(0));
207
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100208 rbb = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";
209 OSMO_ASSERT(ul_win.ssn() == 0);
210 ul_win.update_rbb(&v_n, win_rbb);
Daniel Willmannc3f43302013-12-11 16:47:19 +0100211 OSMO_ASSERT_STR_EQ(win_rbb, rbb);
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100212
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100213 /* simulate to have received 0, 1 and 5 */
214 OSMO_ASSERT(ul_win.is_in_window(0));
215 v_n.mark_received(0);
216 ul_win.raise_v_r(0, &v_n);
217 count = ul_win.raise_v_q(&v_n);
218 OSMO_ASSERT(v_n.is_received(0));
219 OSMO_ASSERT(ul_win.v_q() == 1);
220 OSMO_ASSERT(ul_win.v_r() == 1);
221 OSMO_ASSERT(count == 1);
222
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100223 rbb = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIR";
224 OSMO_ASSERT(ul_win.ssn() == 1);
225 ul_win.update_rbb(&v_n, win_rbb);
Daniel Willmannc3f43302013-12-11 16:47:19 +0100226 OSMO_ASSERT_STR_EQ(win_rbb, rbb);
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100227
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100228 OSMO_ASSERT(ul_win.is_in_window(1));
229 v_n.mark_received(1);
230 ul_win.raise_v_r(1, &v_n);
231 count = ul_win.raise_v_q(&v_n);
232 OSMO_ASSERT(v_n.is_received(0));
233 OSMO_ASSERT(ul_win.v_q() == 2);
234 OSMO_ASSERT(ul_win.v_r() == 2);
235 OSMO_ASSERT(count == 1);
236
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100237 rbb = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIRR";
238 OSMO_ASSERT(ul_win.ssn() == 2);
239 ul_win.update_rbb(&v_n, win_rbb);
Daniel Willmannc3f43302013-12-11 16:47:19 +0100240 OSMO_ASSERT_STR_EQ(win_rbb, rbb);
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100241
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100242 OSMO_ASSERT(ul_win.is_in_window(5));
243 v_n.mark_received(5);
244 ul_win.raise_v_r(5, &v_n);
245 count = ul_win.raise_v_q(&v_n);
246 OSMO_ASSERT(v_n.is_received(0));
247 OSMO_ASSERT(ul_win.v_q() == 2);
248 OSMO_ASSERT(ul_win.v_r() == 6);
249 OSMO_ASSERT(count == 0);
250
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100251 rbb = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIRRIIIR";
252 OSMO_ASSERT(ul_win.ssn() == 6);
253 ul_win.update_rbb(&v_n, win_rbb);
Daniel Willmannc3f43302013-12-11 16:47:19 +0100254 OSMO_ASSERT_STR_EQ(win_rbb, rbb);
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100255
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100256 OSMO_ASSERT(ul_win.is_in_window(65));
257 OSMO_ASSERT(ul_win.is_in_window(2));
258 OSMO_ASSERT(v_n.is_received(5));
259 v_n.mark_received(65);
260 ul_win.raise_v_r(65, &v_n);
261 count = ul_win.raise_v_q(&v_n);
262 OSMO_ASSERT(count == 0);
263 OSMO_ASSERT(v_n.is_received(5));
264 OSMO_ASSERT(ul_win.v_q() == 2);
265 OSMO_ASSERT(ul_win.v_r() == 66);
266
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100267 rbb = "IIIRIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIR";
268 OSMO_ASSERT(ul_win.ssn() == 66);
269 ul_win.update_rbb(&v_n, win_rbb);
Daniel Willmannc3f43302013-12-11 16:47:19 +0100270 OSMO_ASSERT_STR_EQ(win_rbb, rbb);
Daniel Willmannf86fb7a2013-11-27 17:56:02 +0100271
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100272 OSMO_ASSERT(ul_win.is_in_window(2));
273 OSMO_ASSERT(!ul_win.is_in_window(66));
274 v_n.mark_received(2);
275 ul_win.raise_v_r(2, &v_n);
276 count = ul_win.raise_v_q(&v_n);
277 OSMO_ASSERT(count == 1);
278 OSMO_ASSERT(ul_win.v_q() == 3);
279 OSMO_ASSERT(ul_win.v_r() == 66);
280
281 OSMO_ASSERT(ul_win.is_in_window(66));
282 v_n.mark_received(66);
283 ul_win.raise_v_r(66, &v_n);
284 count = ul_win.raise_v_q(&v_n);
285 OSMO_ASSERT(count == 0);
286 OSMO_ASSERT(ul_win.v_q() == 3);
287 OSMO_ASSERT(ul_win.v_r() == 67);
288
289 for (int i = 3; i <= 67; ++i) {
290 v_n.mark_received(i);
291 ul_win.raise_v_r(i, &v_n);
292 ul_win.raise_v_q(&v_n);
293 }
294
295 OSMO_ASSERT(ul_win.v_q() == 68);
296 OSMO_ASSERT(ul_win.v_r() == 68);
297
298 v_n.mark_received(68);
299 ul_win.raise_v_r(68, &v_n);
300 count = ul_win.raise_v_q(&v_n);
301 OSMO_ASSERT(ul_win.v_q() == 69);
302 OSMO_ASSERT(ul_win.v_r() == 69);
303 OSMO_ASSERT(count == 1);
304
305 /* now test the wrapping */
306 OSMO_ASSERT(ul_win.is_in_window(4));
307 OSMO_ASSERT(!ul_win.is_in_window(5));
308 v_n.mark_received(4);
309 ul_win.raise_v_r(4, &v_n);
310 count = ul_win.raise_v_q(&v_n);
311 OSMO_ASSERT(count == 0);
312 }
Holger Hans Peter Freytherfaf3ef42013-11-24 22:17:40 +0100313}
314
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +0100315int main(int argc, char **argv)
316{
Holger Hans Peter Freyther11f2d582013-11-26 23:32:49 +0100317 osmo_init_logging(&gprs_log_info);
318 log_set_use_color(osmo_stderr_target, 0);
319 log_set_print_filename(osmo_stderr_target, 0);
320
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +0100321 printf("Making some basic type testing.\n");
322 test_llc();
Holger Hans Peter Freytherc6382dd2013-11-21 21:42:20 +0100323 test_rlc();
Holger Hans Peter Freyther95255672013-11-23 16:18:18 +0100324 test_rlc_v_b();
Holger Hans Peter Freythere9b1ebb2013-11-24 22:00:43 +0100325 test_rlc_v_n();
Holger Hans Peter Freytherfaf3ef42013-11-24 22:17:40 +0100326 test_rlc_dl_ul_basic();
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +0100327 return EXIT_SUCCESS;
328}
329
330/*
331 * stubs that should not be reached
332 */
333extern "C" {
334void l1if_pdch_req() { abort(); }
335void l1if_connect_pdch() { abort(); }
336void l1if_close_pdch() { abort(); }
337void l1if_open_pdch() { abort(); }
338}