Jacob Erlbeck | e04e0b0 | 2015-05-06 18:30:48 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * MsTest.cpp |
| 3 | * |
| 4 | * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH |
| 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 | |
| 23 | #include "tbf.h" |
| 24 | #include "gprs_debug.h" |
| 25 | #include "gprs_ms.h" |
| 26 | |
| 27 | extern "C" { |
| 28 | #include "pcu_vty.h" |
| 29 | |
| 30 | #include <osmocom/core/application.h> |
| 31 | #include <osmocom/core/msgb.h> |
| 32 | #include <osmocom/core/talloc.h> |
| 33 | #include <osmocom/core/utils.h> |
| 34 | #include <osmocom/vty/vty.h> |
| 35 | } |
| 36 | |
| 37 | #include <errno.h> |
| 38 | |
| 39 | void *tall_pcu_ctx; |
| 40 | int16_t spoof_mnc = 0, spoof_mcc = 0; |
| 41 | |
| 42 | static void test_ms_state() |
| 43 | { |
| 44 | uint32_t tlli = 0xffeeddbb; |
| 45 | gprs_rlcmac_dl_tbf *dl_tbf; |
| 46 | gprs_rlcmac_ul_tbf *ul_tbf; |
| 47 | GprsMs *ms; |
| 48 | |
| 49 | printf("=== start %s ===\n", __func__); |
| 50 | |
| 51 | ms = new GprsMs(tlli); |
| 52 | OSMO_ASSERT(ms->is_idle()); |
| 53 | |
| 54 | dl_tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_dl_tbf); |
| 55 | dl_tbf->direction = GPRS_RLCMAC_DL_TBF; |
| 56 | ul_tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_ul_tbf); |
| 57 | ul_tbf->direction = GPRS_RLCMAC_UL_TBF; |
| 58 | |
| 59 | ms->attach_tbf(ul_tbf); |
| 60 | OSMO_ASSERT(!ms->is_idle()); |
| 61 | OSMO_ASSERT(ms->ul_tbf() == ul_tbf); |
| 62 | OSMO_ASSERT(ms->dl_tbf() == NULL); |
| 63 | |
| 64 | ms->attach_tbf(dl_tbf); |
| 65 | OSMO_ASSERT(!ms->is_idle()); |
| 66 | OSMO_ASSERT(ms->ul_tbf() == ul_tbf); |
| 67 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf); |
| 68 | |
| 69 | ms->detach_tbf(ul_tbf); |
| 70 | OSMO_ASSERT(!ms->is_idle()); |
| 71 | OSMO_ASSERT(ms->ul_tbf() == NULL); |
| 72 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf); |
| 73 | |
| 74 | ms->detach_tbf(dl_tbf); |
| 75 | /* The ms object is freed now */ |
| 76 | ms = NULL; |
| 77 | |
| 78 | talloc_free(dl_tbf); |
| 79 | talloc_free(ul_tbf); |
| 80 | |
| 81 | printf("=== end %s ===\n", __func__); |
| 82 | } |
| 83 | |
| 84 | static void test_ms_callback() |
| 85 | { |
| 86 | uint32_t tlli = 0xffeeddbb; |
| 87 | gprs_rlcmac_dl_tbf *dl_tbf; |
| 88 | gprs_rlcmac_ul_tbf *ul_tbf; |
| 89 | GprsMs *ms; |
| 90 | static enum {UNKNOWN, IS_IDLE, IS_ACTIVE} last_cb = UNKNOWN; |
| 91 | |
| 92 | struct MyCallback: public GprsMs::Callback { |
| 93 | virtual void ms_idle(class GprsMs *ms) { |
| 94 | OSMO_ASSERT(ms->is_idle()); |
| 95 | printf(" ms_idle() was called\n"); |
| 96 | last_cb = IS_IDLE; |
| 97 | } |
| 98 | virtual void ms_active(class GprsMs *ms) { |
| 99 | OSMO_ASSERT(!ms->is_idle()); |
| 100 | printf(" ms_active() was called\n"); |
| 101 | last_cb = IS_ACTIVE; |
| 102 | } |
| 103 | } cb; |
| 104 | |
| 105 | printf("=== start %s ===\n", __func__); |
| 106 | |
| 107 | ms = new GprsMs(tlli); |
| 108 | ms->set_callback(&cb); |
| 109 | |
| 110 | OSMO_ASSERT(ms->is_idle()); |
| 111 | |
| 112 | dl_tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_dl_tbf); |
| 113 | dl_tbf->direction = GPRS_RLCMAC_DL_TBF; |
| 114 | ul_tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_ul_tbf); |
| 115 | ul_tbf->direction = GPRS_RLCMAC_UL_TBF; |
| 116 | |
| 117 | OSMO_ASSERT(last_cb == UNKNOWN); |
| 118 | |
| 119 | ms->attach_tbf(ul_tbf); |
| 120 | OSMO_ASSERT(!ms->is_idle()); |
| 121 | OSMO_ASSERT(ms->ul_tbf() == ul_tbf); |
| 122 | OSMO_ASSERT(ms->dl_tbf() == NULL); |
| 123 | OSMO_ASSERT(last_cb == IS_ACTIVE); |
| 124 | |
| 125 | last_cb = UNKNOWN; |
| 126 | |
| 127 | ms->attach_tbf(dl_tbf); |
| 128 | OSMO_ASSERT(!ms->is_idle()); |
| 129 | OSMO_ASSERT(ms->ul_tbf() == ul_tbf); |
| 130 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf); |
| 131 | OSMO_ASSERT(last_cb == UNKNOWN); |
| 132 | |
| 133 | ms->detach_tbf(ul_tbf); |
| 134 | OSMO_ASSERT(!ms->is_idle()); |
| 135 | OSMO_ASSERT(ms->ul_tbf() == NULL); |
| 136 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf); |
| 137 | OSMO_ASSERT(last_cb == UNKNOWN); |
| 138 | |
| 139 | ms->detach_tbf(dl_tbf); |
| 140 | OSMO_ASSERT(ms->is_idle()); |
| 141 | OSMO_ASSERT(ms->ul_tbf() == NULL); |
| 142 | OSMO_ASSERT(ms->dl_tbf() == NULL); |
| 143 | OSMO_ASSERT(last_cb == IS_IDLE); |
| 144 | |
| 145 | last_cb = UNKNOWN; |
| 146 | delete ms; |
| 147 | |
| 148 | talloc_free(dl_tbf); |
| 149 | talloc_free(ul_tbf); |
| 150 | |
| 151 | printf("=== end %s ===\n", __func__); |
| 152 | } |
| 153 | |
| 154 | static void test_ms_replace_tbf() |
| 155 | { |
| 156 | uint32_t tlli = 0xffeeddbb; |
| 157 | gprs_rlcmac_dl_tbf *dl_tbf[2]; |
| 158 | gprs_rlcmac_ul_tbf *ul_tbf; |
| 159 | GprsMs *ms; |
| 160 | static bool was_idle; |
| 161 | |
| 162 | struct MyCallback: public GprsMs::Callback { |
| 163 | virtual void ms_idle(class GprsMs *ms) { |
| 164 | OSMO_ASSERT(ms->is_idle()); |
| 165 | printf(" ms_idle() was called\n"); |
| 166 | was_idle = true; |
| 167 | } |
| 168 | virtual void ms_active(class GprsMs *ms) { |
| 169 | OSMO_ASSERT(!ms->is_idle()); |
| 170 | printf(" ms_active() was called\n"); |
| 171 | } |
| 172 | } cb; |
| 173 | |
| 174 | printf("=== start %s ===\n", __func__); |
| 175 | |
| 176 | ms = new GprsMs(tlli); |
| 177 | ms->set_callback(&cb); |
| 178 | |
| 179 | OSMO_ASSERT(ms->is_idle()); |
| 180 | was_idle = false; |
| 181 | |
| 182 | dl_tbf[0] = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_dl_tbf); |
| 183 | dl_tbf[0]->direction = GPRS_RLCMAC_DL_TBF; |
| 184 | dl_tbf[1] = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_dl_tbf); |
| 185 | dl_tbf[1]->direction = GPRS_RLCMAC_DL_TBF; |
| 186 | ul_tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_ul_tbf); |
| 187 | ul_tbf->direction = GPRS_RLCMAC_UL_TBF; |
| 188 | |
| 189 | ms->attach_tbf(dl_tbf[0]); |
| 190 | OSMO_ASSERT(!ms->is_idle()); |
| 191 | OSMO_ASSERT(ms->ul_tbf() == NULL); |
| 192 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf[0]); |
| 193 | OSMO_ASSERT(!was_idle); |
| 194 | |
| 195 | ms->attach_tbf(dl_tbf[1]); |
| 196 | OSMO_ASSERT(!ms->is_idle()); |
| 197 | OSMO_ASSERT(ms->ul_tbf() == NULL); |
| 198 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf[1]); |
| 199 | OSMO_ASSERT(!was_idle); |
| 200 | |
| 201 | ms->attach_tbf(ul_tbf); |
| 202 | OSMO_ASSERT(!ms->is_idle()); |
| 203 | OSMO_ASSERT(ms->ul_tbf() == ul_tbf); |
| 204 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf[1]); |
| 205 | OSMO_ASSERT(!was_idle); |
| 206 | |
| 207 | ms->detach_tbf(ul_tbf); |
| 208 | OSMO_ASSERT(!ms->is_idle()); |
| 209 | OSMO_ASSERT(ms->ul_tbf() == NULL); |
| 210 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf[1]); |
| 211 | OSMO_ASSERT(!was_idle); |
| 212 | |
| 213 | ms->detach_tbf(dl_tbf[0]); |
| 214 | OSMO_ASSERT(!ms->is_idle()); |
| 215 | OSMO_ASSERT(ms->ul_tbf() == NULL); |
| 216 | OSMO_ASSERT(ms->dl_tbf() == dl_tbf[1]); |
| 217 | OSMO_ASSERT(!was_idle); |
| 218 | |
| 219 | ms->detach_tbf(dl_tbf[1]); |
| 220 | OSMO_ASSERT(ms->is_idle()); |
| 221 | OSMO_ASSERT(ms->ul_tbf() == NULL); |
| 222 | OSMO_ASSERT(ms->dl_tbf() == NULL); |
| 223 | OSMO_ASSERT(was_idle); |
| 224 | |
| 225 | delete ms; |
| 226 | |
| 227 | talloc_free(dl_tbf[0]); |
| 228 | talloc_free(dl_tbf[1]); |
| 229 | talloc_free(ul_tbf); |
| 230 | |
| 231 | printf("=== end %s ===\n", __func__); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static const struct log_info_cat default_categories[] = { |
| 236 | {"DPCU", "", "GPRS Packet Control Unit (PCU)", LOGL_INFO, 1}, |
| 237 | }; |
| 238 | |
| 239 | static int filter_fn(const struct log_context *ctx, |
| 240 | struct log_target *tar) |
| 241 | { |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | const struct log_info debug_log_info = { |
| 246 | filter_fn, |
| 247 | (struct log_info_cat*)default_categories, |
| 248 | ARRAY_SIZE(default_categories), |
| 249 | }; |
| 250 | |
| 251 | int main(int argc, char **argv) |
| 252 | { |
| 253 | struct vty_app_info pcu_vty_info = {0}; |
| 254 | |
| 255 | tall_pcu_ctx = talloc_named_const(NULL, 1, "MsTest context"); |
| 256 | if (!tall_pcu_ctx) |
| 257 | abort(); |
| 258 | |
| 259 | msgb_set_talloc_ctx(tall_pcu_ctx); |
| 260 | osmo_init_logging(&debug_log_info); |
| 261 | log_set_use_color(osmo_stderr_target, 0); |
| 262 | log_set_print_filename(osmo_stderr_target, 0); |
| 263 | log_set_log_level(osmo_stderr_target, LOGL_INFO); |
| 264 | |
| 265 | vty_init(&pcu_vty_info); |
| 266 | pcu_vty_init(&debug_log_info); |
| 267 | |
| 268 | test_ms_state(); |
| 269 | test_ms_callback(); |
| 270 | test_ms_replace_tbf(); |
| 271 | |
| 272 | if (getenv("TALLOC_REPORT_FULL")) |
| 273 | talloc_report_full(tall_pcu_ctx, stderr); |
| 274 | |
| 275 | return EXIT_SUCCESS; |
| 276 | } |
| 277 | |
| 278 | extern "C" { |
| 279 | void l1if_pdch_req() { abort(); } |
| 280 | void l1if_connect_pdch() { abort(); } |
| 281 | void l1if_close_pdch() { abort(); } |
| 282 | void l1if_open_pdch() { abort(); } |
| 283 | } |