Holger Hans Peter Freyther | 6058220 | 2013-11-21 21:30:23 +0100 | [diff] [blame] | 1 | /* |
| 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" |
Max | 20c7c46 | 2017-12-22 14:20:05 +0100 | [diff] [blame] | 24 | #include "pcu_utils.h" |
Holger Hans Peter Freyther | 6058220 | 2013-11-21 21:30:23 +0100 | [diff] [blame] | 25 | #include "gprs_debug.h" |
Daniel Willmann | 0f2b5fc | 2013-12-11 16:49:43 +0100 | [diff] [blame] | 26 | #include "encoding.h" |
Daniel Willmann | f1786a3 | 2013-12-11 18:44:49 +0100 | [diff] [blame] | 27 | #include "decoding.h" |
Holger Hans Peter Freyther | 6058220 | 2013-11-21 21:30:23 +0100 | [diff] [blame] | 28 | |
| 29 | extern "C" { |
| 30 | #include <osmocom/core/application.h> |
| 31 | #include <osmocom/core/msgb.h> |
| 32 | #include <osmocom/core/talloc.h> |
| 33 | #include <osmocom/core/utils.h> |
Max | 20c7c46 | 2017-12-22 14:20:05 +0100 | [diff] [blame] | 34 | #include <osmocom/core/bits.h> |
Holger Hans Peter Freyther | 6058220 | 2013-11-21 21:30:23 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Daniel Willmann | c3f4330 | 2013-12-11 16:47:19 +0100 | [diff] [blame] | 37 | #define OSMO_ASSERT_STR_EQ(a, b) \ |
| 38 | do { \ |
| 39 | if (strcmp(a, b)) { \ |
| 40 | printf("String mismatch:\nGot:\t%s\nWant:\t%s\n", a, b); \ |
| 41 | OSMO_ASSERT(false); \ |
| 42 | } \ |
| 43 | } while (0) |
| 44 | |
Holger Hans Peter Freyther | 6058220 | 2013-11-21 21:30:23 +0100 | [diff] [blame] | 45 | void *tall_pcu_ctx; |
| 46 | int16_t spoof_mnc = 0, spoof_mcc = 0; |
| 47 | |
| 48 | static void test_llc(void) |
| 49 | { |
| 50 | { |
| 51 | uint8_t data[LLC_MAX_LEN] = {1, 2, 3, 4, }; |
| 52 | uint8_t out; |
| 53 | gprs_llc llc; |
| 54 | llc.init(); |
| 55 | |
| 56 | OSMO_ASSERT(llc.chunk_size() == 0); |
| 57 | OSMO_ASSERT(llc.remaining_space() == LLC_MAX_LEN); |
| 58 | OSMO_ASSERT(llc.frame_length() == 0); |
| 59 | |
| 60 | llc.put_frame(data, 2); |
| 61 | OSMO_ASSERT(llc.remaining_space() == LLC_MAX_LEN - 2); |
| 62 | OSMO_ASSERT(llc.frame_length() == 2); |
| 63 | OSMO_ASSERT(llc.chunk_size() == 2); |
| 64 | OSMO_ASSERT(llc.frame[0] == 1); |
| 65 | OSMO_ASSERT(llc.frame[1] == 2); |
| 66 | |
| 67 | llc.append_frame(&data[3], 1); |
| 68 | OSMO_ASSERT(llc.remaining_space() == LLC_MAX_LEN - 3); |
| 69 | OSMO_ASSERT(llc.frame_length() == 3); |
| 70 | OSMO_ASSERT(llc.chunk_size() == 3); |
| 71 | |
| 72 | /* consume two bytes */ |
| 73 | llc.consume(&out, 1); |
| 74 | OSMO_ASSERT(llc.remaining_space() == LLC_MAX_LEN - 3); |
| 75 | OSMO_ASSERT(llc.frame_length() == 3); |
| 76 | OSMO_ASSERT(llc.chunk_size() == 2); |
| 77 | |
| 78 | /* check that the bytes are as we expected */ |
| 79 | OSMO_ASSERT(llc.frame[0] == 1); |
| 80 | OSMO_ASSERT(llc.frame[1] == 2); |
| 81 | OSMO_ASSERT(llc.frame[2] == 4); |
| 82 | |
| 83 | /* now fill the frame */ |
| 84 | llc.append_frame(data, llc.remaining_space() - 1); |
| 85 | OSMO_ASSERT(llc.fits_in_current_frame(1)); |
| 86 | OSMO_ASSERT(!llc.fits_in_current_frame(2)); |
| 87 | } |
| 88 | } |
| 89 | |
Holger Hans Peter Freyther | c6382dd | 2013-11-21 21:42:20 +0100 | [diff] [blame] | 90 | static void test_rlc() |
| 91 | { |
| 92 | { |
| 93 | struct gprs_rlc_data rlc = { 0, }; |
| 94 | memset(rlc.block, 0x23, RLC_MAX_LEN); |
| 95 | uint8_t *p = rlc.prepare(20); |
| 96 | OSMO_ASSERT(p == rlc.block); |
| 97 | for (int i = 0; i < 20; ++i) |
| 98 | OSMO_ASSERT(p[i] == 0x2B); |
| 99 | for (int i = 20; i < RLC_MAX_LEN; ++i) |
| 100 | OSMO_ASSERT(p[i] == 0x0); |
| 101 | } |
| 102 | } |
| 103 | |
Holger Hans Peter Freyther | 9525567 | 2013-11-23 16:18:18 +0100 | [diff] [blame] | 104 | static void test_rlc_v_b() |
| 105 | { |
| 106 | { |
| 107 | gprs_rlc_v_b vb; |
| 108 | vb.reset(); |
| 109 | |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 110 | for (size_t i = 0; i < RLC_MAX_SNS; ++i) |
Holger Hans Peter Freyther | 9525567 | 2013-11-23 16:18:18 +0100 | [diff] [blame] | 111 | OSMO_ASSERT(vb.is_invalid(i)); |
| 112 | |
| 113 | vb.mark_unacked(23); |
| 114 | OSMO_ASSERT(vb.is_unacked(23)); |
| 115 | |
| 116 | vb.mark_nacked(23); |
| 117 | OSMO_ASSERT(vb.is_nacked(23)); |
| 118 | |
| 119 | vb.mark_acked(23); |
| 120 | OSMO_ASSERT(vb.is_acked(23)); |
| 121 | |
| 122 | vb.mark_resend(23); |
| 123 | OSMO_ASSERT(vb.is_resend(23)); |
| 124 | |
| 125 | vb.mark_invalid(23); |
| 126 | OSMO_ASSERT(vb.is_invalid(23)); |
| 127 | } |
| 128 | } |
| 129 | |
Holger Hans Peter Freyther | e9b1ebb | 2013-11-24 22:00:43 +0100 | [diff] [blame] | 130 | static void test_rlc_v_n() |
| 131 | { |
| 132 | { |
| 133 | gprs_rlc_v_n vn; |
| 134 | vn.reset(); |
| 135 | |
| 136 | OSMO_ASSERT(!vn.is_received(0x23)); |
Daniel Willmann | d54d9f5 | 2013-12-28 21:16:13 +0100 | [diff] [blame] | 137 | OSMO_ASSERT(vn.state(0x23) == GPRS_RLC_UL_BSN_INVALID); |
Holger Hans Peter Freyther | e9b1ebb | 2013-11-24 22:00:43 +0100 | [diff] [blame] | 138 | |
| 139 | vn.mark_received(0x23); |
| 140 | OSMO_ASSERT(vn.is_received(0x23)); |
Daniel Willmann | d54d9f5 | 2013-12-28 21:16:13 +0100 | [diff] [blame] | 141 | OSMO_ASSERT(vn.state(0x23) == GPRS_RLC_UL_BSN_RECEIVED); |
Holger Hans Peter Freyther | e9b1ebb | 2013-11-24 22:00:43 +0100 | [diff] [blame] | 142 | |
| 143 | vn.mark_missing(0x23); |
| 144 | OSMO_ASSERT(!vn.is_received(0x23)); |
Daniel Willmann | d54d9f5 | 2013-12-28 21:16:13 +0100 | [diff] [blame] | 145 | OSMO_ASSERT(vn.state(0x23) == GPRS_RLC_UL_BSN_MISSING); |
Holger Hans Peter Freyther | e9b1ebb | 2013-11-24 22:00:43 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Holger Hans Peter Freyther | faf3ef4 | 2013-11-24 22:17:40 +0100 | [diff] [blame] | 149 | static void test_rlc_dl_ul_basic() |
| 150 | { |
| 151 | { |
Jacob Erlbeck | a3a567e | 2015-12-28 13:46:32 +0100 | [diff] [blame] | 152 | gprs_rlc_dl_window dl_win; |
Holger Hans Peter Freyther | faf3ef4 | 2013-11-24 22:17:40 +0100 | [diff] [blame] | 153 | OSMO_ASSERT(dl_win.window_empty()); |
| 154 | OSMO_ASSERT(!dl_win.window_stalled()); |
| 155 | OSMO_ASSERT(dl_win.distance() == 0); |
| 156 | |
| 157 | dl_win.increment_send(); |
| 158 | OSMO_ASSERT(!dl_win.window_empty()); |
| 159 | OSMO_ASSERT(!dl_win.window_stalled()); |
| 160 | OSMO_ASSERT(dl_win.distance() == 1); |
| 161 | |
| 162 | for (int i = 1; i < 64; ++i) { |
| 163 | dl_win.increment_send(); |
| 164 | OSMO_ASSERT(!dl_win.window_empty()); |
| 165 | OSMO_ASSERT(dl_win.distance() == i + 1); |
| 166 | } |
| 167 | |
| 168 | OSMO_ASSERT(dl_win.distance() == 64); |
| 169 | OSMO_ASSERT(dl_win.window_stalled()); |
| 170 | |
| 171 | dl_win.raise(1); |
| 172 | OSMO_ASSERT(dl_win.distance() == 63); |
| 173 | OSMO_ASSERT(!dl_win.window_stalled()); |
| 174 | for (int i = 62; i >= 0; --i) { |
| 175 | dl_win.raise(1); |
| 176 | OSMO_ASSERT(dl_win.distance() == i); |
| 177 | } |
| 178 | |
| 179 | OSMO_ASSERT(dl_win.distance() == 0); |
| 180 | OSMO_ASSERT(dl_win.window_empty()); |
| 181 | |
| 182 | dl_win.increment_send(); |
| 183 | dl_win.increment_send(); |
| 184 | dl_win.increment_send(); |
| 185 | dl_win.increment_send(); |
| 186 | OSMO_ASSERT(dl_win.distance() == 4); |
| 187 | |
| 188 | for (int i = 0; i < 128; ++i) { |
| 189 | dl_win.increment_send(); |
| 190 | dl_win.increment_send(); |
| 191 | dl_win.raise(2); |
| 192 | OSMO_ASSERT(dl_win.distance() == 4); |
| 193 | } |
| 194 | } |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 195 | |
| 196 | { |
Jacob Erlbeck | a3a567e | 2015-12-28 13:46:32 +0100 | [diff] [blame] | 197 | gprs_rlc_ul_window ul_win; |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 198 | int count; |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 199 | const char *rbb; |
Daniel Willmann | c3f4330 | 2013-12-11 16:47:19 +0100 | [diff] [blame] | 200 | char win_rbb[65]; |
Daniel Willmann | 0f2b5fc | 2013-12-11 16:49:43 +0100 | [diff] [blame] | 201 | uint8_t bin_rbb[8]; |
Daniel Willmann | c3f4330 | 2013-12-11 16:47:19 +0100 | [diff] [blame] | 202 | win_rbb[64] = '\0'; |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 203 | |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 204 | ul_win.m_v_n.reset(); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 205 | |
| 206 | OSMO_ASSERT(ul_win.is_in_window(0)); |
| 207 | OSMO_ASSERT(ul_win.is_in_window(63)); |
| 208 | OSMO_ASSERT(!ul_win.is_in_window(64)); |
| 209 | |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 210 | OSMO_ASSERT(!ul_win.m_v_n.is_received(0)); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 211 | |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 212 | rbb = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII"; |
| 213 | OSMO_ASSERT(ul_win.ssn() == 0); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 214 | ul_win.update_rbb(win_rbb); |
Daniel Willmann | c3f4330 | 2013-12-11 16:47:19 +0100 | [diff] [blame] | 215 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | 0f2b5fc | 2013-12-11 16:49:43 +0100 | [diff] [blame] | 216 | Encoding::encode_rbb(win_rbb, bin_rbb); |
| 217 | printf("rbb: %s\n", osmo_hexdump(bin_rbb, sizeof(bin_rbb))); |
Daniel Willmann | f1786a3 | 2013-12-11 18:44:49 +0100 | [diff] [blame] | 218 | Decoding::extract_rbb(bin_rbb, win_rbb); |
| 219 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 220 | |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 221 | /* simulate to have received 0, 1 and 5 */ |
| 222 | OSMO_ASSERT(ul_win.is_in_window(0)); |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 223 | ul_win.receive_bsn(0); |
| 224 | count = ul_win.raise_v_q(); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 225 | OSMO_ASSERT(ul_win.m_v_n.is_received(0)); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 226 | OSMO_ASSERT(ul_win.v_q() == 1); |
| 227 | OSMO_ASSERT(ul_win.v_r() == 1); |
| 228 | OSMO_ASSERT(count == 1); |
| 229 | |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 230 | rbb = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIR"; |
| 231 | OSMO_ASSERT(ul_win.ssn() == 1); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 232 | ul_win.update_rbb(win_rbb); |
Daniel Willmann | c3f4330 | 2013-12-11 16:47:19 +0100 | [diff] [blame] | 233 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | 0f2b5fc | 2013-12-11 16:49:43 +0100 | [diff] [blame] | 234 | Encoding::encode_rbb(win_rbb, bin_rbb); |
| 235 | printf("rbb: %s\n", osmo_hexdump(bin_rbb, sizeof(bin_rbb))); |
Daniel Willmann | f1786a3 | 2013-12-11 18:44:49 +0100 | [diff] [blame] | 236 | Decoding::extract_rbb(bin_rbb, win_rbb); |
| 237 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 238 | |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 239 | OSMO_ASSERT(ul_win.is_in_window(1)); |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 240 | ul_win.receive_bsn(1); |
| 241 | count = ul_win.raise_v_q(); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 242 | OSMO_ASSERT(ul_win.m_v_n.is_received(0)); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 243 | OSMO_ASSERT(ul_win.v_q() == 2); |
| 244 | OSMO_ASSERT(ul_win.v_r() == 2); |
| 245 | OSMO_ASSERT(count == 1); |
| 246 | |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 247 | rbb = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIRR"; |
| 248 | OSMO_ASSERT(ul_win.ssn() == 2); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 249 | ul_win.update_rbb(win_rbb); |
Daniel Willmann | c3f4330 | 2013-12-11 16:47:19 +0100 | [diff] [blame] | 250 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | 0f2b5fc | 2013-12-11 16:49:43 +0100 | [diff] [blame] | 251 | Encoding::encode_rbb(win_rbb, bin_rbb); |
| 252 | printf("rbb: %s\n", osmo_hexdump(bin_rbb, sizeof(bin_rbb))); |
Daniel Willmann | f1786a3 | 2013-12-11 18:44:49 +0100 | [diff] [blame] | 253 | Decoding::extract_rbb(bin_rbb, win_rbb); |
| 254 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 255 | |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 256 | OSMO_ASSERT(ul_win.is_in_window(5)); |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 257 | ul_win.receive_bsn(5); |
| 258 | count = ul_win.raise_v_q(); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 259 | OSMO_ASSERT(ul_win.m_v_n.is_received(0)); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 260 | OSMO_ASSERT(ul_win.v_q() == 2); |
| 261 | OSMO_ASSERT(ul_win.v_r() == 6); |
| 262 | OSMO_ASSERT(count == 0); |
| 263 | |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 264 | rbb = "IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIRRIIIR"; |
| 265 | OSMO_ASSERT(ul_win.ssn() == 6); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 266 | ul_win.update_rbb(win_rbb); |
Daniel Willmann | c3f4330 | 2013-12-11 16:47:19 +0100 | [diff] [blame] | 267 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | 0f2b5fc | 2013-12-11 16:49:43 +0100 | [diff] [blame] | 268 | Encoding::encode_rbb(win_rbb, bin_rbb); |
| 269 | printf("rbb: %s\n", osmo_hexdump(bin_rbb, sizeof(bin_rbb))); |
Daniel Willmann | f1786a3 | 2013-12-11 18:44:49 +0100 | [diff] [blame] | 270 | Decoding::extract_rbb(bin_rbb, win_rbb); |
| 271 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 272 | |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 273 | OSMO_ASSERT(ul_win.is_in_window(65)); |
| 274 | OSMO_ASSERT(ul_win.is_in_window(2)); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 275 | OSMO_ASSERT(ul_win.m_v_n.is_received(5)); |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 276 | ul_win.receive_bsn(65); |
| 277 | count = ul_win.raise_v_q(); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 278 | OSMO_ASSERT(count == 0); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 279 | OSMO_ASSERT(ul_win.m_v_n.is_received(5)); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 280 | OSMO_ASSERT(ul_win.v_q() == 2); |
| 281 | OSMO_ASSERT(ul_win.v_r() == 66); |
| 282 | |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 283 | rbb = "IIIRIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIR"; |
| 284 | OSMO_ASSERT(ul_win.ssn() == 66); |
Daniel Willmann | 7c3751b | 2013-12-28 13:59:24 +0100 | [diff] [blame] | 285 | ul_win.update_rbb(win_rbb); |
Daniel Willmann | c3f4330 | 2013-12-11 16:47:19 +0100 | [diff] [blame] | 286 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | 0f2b5fc | 2013-12-11 16:49:43 +0100 | [diff] [blame] | 287 | Encoding::encode_rbb(win_rbb, bin_rbb); |
| 288 | printf("rbb: %s\n", osmo_hexdump(bin_rbb, sizeof(bin_rbb))); |
Daniel Willmann | f1786a3 | 2013-12-11 18:44:49 +0100 | [diff] [blame] | 289 | Decoding::extract_rbb(bin_rbb, win_rbb); |
| 290 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
Daniel Willmann | f86fb7a | 2013-11-27 17:56:02 +0100 | [diff] [blame] | 291 | |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 292 | OSMO_ASSERT(ul_win.is_in_window(2)); |
| 293 | OSMO_ASSERT(!ul_win.is_in_window(66)); |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 294 | ul_win.receive_bsn(2); |
| 295 | count = ul_win.raise_v_q(); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 296 | OSMO_ASSERT(count == 1); |
| 297 | OSMO_ASSERT(ul_win.v_q() == 3); |
| 298 | OSMO_ASSERT(ul_win.v_r() == 66); |
| 299 | |
| 300 | OSMO_ASSERT(ul_win.is_in_window(66)); |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 301 | ul_win.receive_bsn(66); |
| 302 | count = ul_win.raise_v_q(); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 303 | OSMO_ASSERT(count == 0); |
| 304 | OSMO_ASSERT(ul_win.v_q() == 3); |
| 305 | OSMO_ASSERT(ul_win.v_r() == 67); |
| 306 | |
| 307 | for (int i = 3; i <= 67; ++i) { |
Daniel Willmann | 5584479 | 2013-12-28 14:41:00 +0100 | [diff] [blame] | 308 | ul_win.receive_bsn(i); |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 309 | ul_win.raise_v_q(); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | OSMO_ASSERT(ul_win.v_q() == 68); |
| 313 | OSMO_ASSERT(ul_win.v_r() == 68); |
| 314 | |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 315 | ul_win.receive_bsn(68); |
| 316 | count = ul_win.raise_v_q(); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 317 | OSMO_ASSERT(ul_win.v_q() == 69); |
| 318 | OSMO_ASSERT(ul_win.v_r() == 69); |
| 319 | OSMO_ASSERT(count == 1); |
| 320 | |
| 321 | /* now test the wrapping */ |
| 322 | OSMO_ASSERT(ul_win.is_in_window(4)); |
| 323 | OSMO_ASSERT(!ul_win.is_in_window(5)); |
Jacob Erlbeck | d87e1d6 | 2015-12-14 11:43:04 +0100 | [diff] [blame] | 324 | ul_win.receive_bsn(4); |
| 325 | count = ul_win.raise_v_q(); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 326 | OSMO_ASSERT(count == 0); |
Aravind Sirsikar | a35c911 | 2016-08-30 13:00:14 +0530 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * SSN wrap around case |
Aravind Sirsikar | 7c7a86c | 2016-08-30 13:08:28 +0530 | [diff] [blame] | 330 | * Should not expect any BSN as nacked. |
Aravind Sirsikar | a35c911 | 2016-08-30 13:00:14 +0530 | [diff] [blame] | 331 | */ |
Aravind Sirsikar | 7c7a86c | 2016-08-30 13:08:28 +0530 | [diff] [blame] | 332 | rbb = "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR"; |
Aravind Sirsikar | a35c911 | 2016-08-30 13:00:14 +0530 | [diff] [blame] | 333 | for (int i = 0; i < 128; ++i) { |
| 334 | ul_win.receive_bsn(i); |
| 335 | ul_win.raise_v_q(); |
| 336 | } |
| 337 | ul_win.receive_bsn(0); |
| 338 | ul_win.raise_v_q(); |
| 339 | ul_win.receive_bsn(1); |
| 340 | ul_win.raise_v_q(); |
| 341 | ul_win.update_rbb(win_rbb); |
| 342 | OSMO_ASSERT_STR_EQ(win_rbb, rbb); |
| 343 | OSMO_ASSERT(ul_win.ssn() == 2); |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 344 | } |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 345 | |
| 346 | { |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 347 | uint16_t lost = 0, recv = 0; |
| 348 | char show_rbb[65]; |
Jacob Erlbeck | 419b034 | 2016-01-14 13:40:01 +0100 | [diff] [blame] | 349 | uint8_t bits_data[8]; |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 350 | BTS dummy_bts; |
Jacob Erlbeck | a3a567e | 2015-12-28 13:46:32 +0100 | [diff] [blame] | 351 | gprs_rlc_dl_window dl_win; |
Jacob Erlbeck | 419b034 | 2016-01-14 13:40:01 +0100 | [diff] [blame] | 352 | bitvec bits; |
| 353 | int bsn_begin, bsn_end, num_blocks; |
| 354 | Ack_Nack_Description_t desc; |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 355 | |
Daniel Willmann | 146514e | 2013-12-28 18:24:42 +0100 | [diff] [blame] | 356 | dl_win.m_v_b.reset(); |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 357 | |
| 358 | OSMO_ASSERT(dl_win.window_empty()); |
| 359 | OSMO_ASSERT(!dl_win.window_stalled()); |
| 360 | OSMO_ASSERT(dl_win.distance() == 0); |
| 361 | |
| 362 | dl_win.increment_send(); |
| 363 | OSMO_ASSERT(!dl_win.window_empty()); |
| 364 | OSMO_ASSERT(!dl_win.window_stalled()); |
| 365 | OSMO_ASSERT(dl_win.distance() == 1); |
| 366 | |
| 367 | for (int i = 0; i < 35; ++i) { |
| 368 | dl_win.increment_send(); |
| 369 | OSMO_ASSERT(!dl_win.window_empty()); |
| 370 | OSMO_ASSERT(dl_win.distance() == i + 2); |
| 371 | } |
| 372 | |
| 373 | uint8_t rbb_cmp[8] = { 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff }; |
Jacob Erlbeck | 419b034 | 2016-01-14 13:40:01 +0100 | [diff] [blame] | 374 | bits.data = bits_data; |
| 375 | bits.data_len = sizeof(bits_data); |
| 376 | bits.cur_bit = 0; |
| 377 | |
| 378 | memcpy(desc.RECEIVED_BLOCK_BITMAP, rbb_cmp, |
| 379 | sizeof(desc.RECEIVED_BLOCK_BITMAP)); |
| 380 | desc.FINAL_ACK_INDICATION = 0; |
| 381 | desc.STARTING_SEQUENCE_NUMBER = 35; |
| 382 | |
| 383 | num_blocks = Decoding::decode_gprs_acknack_bits( |
| 384 | &desc, &bits, |
| 385 | &bsn_begin, &bsn_end, &dl_win); |
| 386 | Decoding::extract_rbb(&bits, show_rbb); |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 387 | printf("show_rbb: %s\n", show_rbb); |
| 388 | |
Jacob Erlbeck | 419b034 | 2016-01-14 13:40:01 +0100 | [diff] [blame] | 389 | dl_win.update(&dummy_bts, &bits, 0, &lost, &recv); |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 390 | OSMO_ASSERT(lost == 0); |
| 391 | OSMO_ASSERT(recv == 35); |
Jacob Erlbeck | 419b034 | 2016-01-14 13:40:01 +0100 | [diff] [blame] | 392 | OSMO_ASSERT(bsn_begin == 0); |
| 393 | OSMO_ASSERT(bsn_end == 35); |
| 394 | OSMO_ASSERT(num_blocks == 35); |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 395 | |
Jacob Erlbeck | 419b034 | 2016-01-14 13:40:01 +0100 | [diff] [blame] | 396 | dl_win.raise(dl_win.move_window()); |
| 397 | |
| 398 | for (int i = 0; i < 8; ++i) { |
| 399 | dl_win.increment_send(); |
| 400 | OSMO_ASSERT(!dl_win.window_empty()); |
| 401 | OSMO_ASSERT(dl_win.distance() == 2 + i); |
| 402 | } |
| 403 | |
| 404 | uint8_t rbb_cmp2[8] = { 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0x31 }; |
| 405 | bits.data = bits_data; |
| 406 | bits.data_len = sizeof(bits_data); |
| 407 | bits.cur_bit = 0; |
| 408 | |
| 409 | memcpy(desc.RECEIVED_BLOCK_BITMAP, rbb_cmp2, |
| 410 | sizeof(desc.RECEIVED_BLOCK_BITMAP)); |
| 411 | desc.FINAL_ACK_INDICATION = 0; |
| 412 | desc.STARTING_SEQUENCE_NUMBER = 35 + 8; |
| 413 | |
| 414 | num_blocks = Decoding::decode_gprs_acknack_bits( |
| 415 | &desc, &bits, |
| 416 | &bsn_begin, &bsn_end, &dl_win); |
| 417 | Decoding::extract_rbb(&bits, show_rbb); |
| 418 | printf("show_rbb: %s\n", show_rbb); |
| 419 | |
| 420 | lost = recv = 0; |
| 421 | dl_win.update(&dummy_bts, &bits, 0, &lost, &recv); |
| 422 | OSMO_ASSERT(lost == 5); |
| 423 | OSMO_ASSERT(recv == 3); |
| 424 | OSMO_ASSERT(bitvec_get_bit_pos(&bits, 0) == 0); |
| 425 | OSMO_ASSERT(bitvec_get_bit_pos(&bits, 7) == 1); |
| 426 | OSMO_ASSERT(bsn_begin == 35); |
| 427 | OSMO_ASSERT(bsn_end == 43); |
| 428 | OSMO_ASSERT(num_blocks == 8); |
Daniel Willmann | 48df40d | 2013-12-11 16:51:26 +0100 | [diff] [blame] | 429 | } |
Holger Hans Peter Freyther | faf3ef4 | 2013-11-24 22:17:40 +0100 | [diff] [blame] | 430 | } |
| 431 | |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 432 | void test_immediate_assign_rej() |
| 433 | { |
| 434 | uint8_t plen; |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 435 | bitvec *immediate_assignment_rej = bitvec_alloc(22, tall_pcu_ctx); |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 436 | |
| 437 | bitvec_unhex(immediate_assignment_rej, |
| 438 | "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); |
| 439 | plen = Encoding::write_immediate_assignment_reject( |
| 440 | immediate_assignment_rej, 112, 100, |
| 441 | GSM_L1_BURST_TYPE_ACCESS_1); |
| 442 | |
| 443 | printf("assignment reject: %s\n", |
| 444 | osmo_hexdump(immediate_assignment_rej->data, 22)); |
| 445 | |
| 446 | OSMO_ASSERT(plen == 19); |
| 447 | /* RA value */ |
| 448 | OSMO_ASSERT(immediate_assignment_rej->data[3] == 0x7f); |
| 449 | /* Extended RA value */ |
| 450 | OSMO_ASSERT(immediate_assignment_rej->data[19] == 0xc0); |
| 451 | |
| 452 | bitvec_unhex(immediate_assignment_rej, |
| 453 | "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b"); |
| 454 | |
| 455 | plen = Encoding::write_immediate_assignment_reject( |
| 456 | immediate_assignment_rej, 112, 100, |
| 457 | GSM_L1_BURST_TYPE_ACCESS_0); |
| 458 | |
| 459 | printf("assignment reject: %s\n", |
| 460 | osmo_hexdump(immediate_assignment_rej->data, 22)); |
| 461 | |
| 462 | OSMO_ASSERT(plen == 19); |
| 463 | /* RA value */ |
| 464 | OSMO_ASSERT(immediate_assignment_rej->data[3] == 0x70); |
| 465 | |
| 466 | } |
| 467 | |
Max | 20c7c46 | 2017-12-22 14:20:05 +0100 | [diff] [blame] | 468 | static void test_lsb() |
| 469 | { |
| 470 | uint8_t u = 0; |
| 471 | |
| 472 | printf("Testing LBS utility...\n"); |
| 473 | |
| 474 | do { |
| 475 | uint8_t x = pcu_lsb(u); /* equivalent of (1 << ffs(u)) / 2 */ |
| 476 | printf("%2X " OSMO_BIT_SPEC ": {%d} %3d\n", |
| 477 | u, OSMO_BIT_PRINT(u), pcu_bitcount(u), x); |
| 478 | u++; |
| 479 | } while (u); |
| 480 | } |
| 481 | |
Holger Hans Peter Freyther | 6058220 | 2013-11-21 21:30:23 +0100 | [diff] [blame] | 482 | int main(int argc, char **argv) |
| 483 | { |
Holger Hans Peter Freyther | 11f2d58 | 2013-11-26 23:32:49 +0100 | [diff] [blame] | 484 | osmo_init_logging(&gprs_log_info); |
| 485 | log_set_use_color(osmo_stderr_target, 0); |
| 486 | log_set_print_filename(osmo_stderr_target, 0); |
| 487 | |
Holger Hans Peter Freyther | 6058220 | 2013-11-21 21:30:23 +0100 | [diff] [blame] | 488 | printf("Making some basic type testing.\n"); |
| 489 | test_llc(); |
Holger Hans Peter Freyther | c6382dd | 2013-11-21 21:42:20 +0100 | [diff] [blame] | 490 | test_rlc(); |
Holger Hans Peter Freyther | 9525567 | 2013-11-23 16:18:18 +0100 | [diff] [blame] | 491 | test_rlc_v_b(); |
Holger Hans Peter Freyther | e9b1ebb | 2013-11-24 22:00:43 +0100 | [diff] [blame] | 492 | test_rlc_v_n(); |
Holger Hans Peter Freyther | faf3ef4 | 2013-11-24 22:17:40 +0100 | [diff] [blame] | 493 | test_rlc_dl_ul_basic(); |
aravind sirsikar | c0c3afd | 2016-11-09 16:27:00 +0530 | [diff] [blame] | 494 | test_immediate_assign_rej(); |
Max | 20c7c46 | 2017-12-22 14:20:05 +0100 | [diff] [blame] | 495 | test_lsb(); |
| 496 | |
Holger Hans Peter Freyther | 6058220 | 2013-11-21 21:30:23 +0100 | [diff] [blame] | 497 | return EXIT_SUCCESS; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * stubs that should not be reached |
| 502 | */ |
| 503 | extern "C" { |
| 504 | void l1if_pdch_req() { abort(); } |
| 505 | void l1if_connect_pdch() { abort(); } |
| 506 | void l1if_close_pdch() { abort(); } |
| 507 | void l1if_open_pdch() { abort(); } |
| 508 | } |