Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU General Public License |
| 5 | * as published by the Free Software Foundation; either version 2 |
| 6 | * of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <cstdlib> |
| 15 | #include <cstring> |
| 16 | #include <assert.h> |
| 17 | #include "gprs_rlcmac.h" |
| 18 | #include "bts.h" |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 19 | #include "tbf_dl.h" |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 20 | |
| 21 | extern "C" { |
| 22 | #include <osmocom/vty/telnet_interface.h> |
| 23 | #include <osmocom/vty/logging.h> |
| 24 | #include <osmocom/core/utils.h> |
| 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/application.h> |
Pau Espin Pedrol | ff7c581 | 2022-12-14 18:49:06 +0100 | [diff] [blame] | 27 | |
| 28 | #include "alloc_algo.h" |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | using namespace std; |
| 32 | gprs_rlcmac_dl_tbf *tbf1, *tbf2; |
| 33 | GprsMs *ms1, *ms2; |
| 34 | struct msgb *sched_app_info(struct gprs_rlcmac_tbf *tbf); |
| 35 | |
| 36 | /* globals used by the code */ |
| 37 | void *tall_pcu_ctx; |
| 38 | int16_t spoof_mnc = 0, spoof_mcc = 0; |
| 39 | bool spoof_mnc_3_digits = false; |
| 40 | |
| 41 | void test_enc_zero_len() { |
| 42 | struct gsm_pcu_if_app_info_req req = {0, 0, {0}}; |
| 43 | |
| 44 | fprintf(stderr, "--- %s ---\n", __func__); |
| 45 | assert(gprs_rlcmac_app_info_msg(&req) == NULL); |
| 46 | fprintf(stderr, "\n"); |
| 47 | } |
| 48 | |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 49 | void test_enc(const struct gsm_pcu_if_app_info_req *req) |
| 50 | { |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 51 | const char *exp = "03 fc 03 fc 00 00 00 00 00 00 00 00 00 00 00 00 "; /* shifted by two bits to the right */ |
| 52 | struct msgb *msg; |
| 53 | char *msg_dump; |
| 54 | |
| 55 | fprintf(stderr, "--- %s ---\n", __func__); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 56 | msg = gprs_rlcmac_app_info_msg(req); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 57 | msg_dump = msgb_hexdump_c(tall_pcu_ctx, msg); |
| 58 | |
| 59 | fprintf(stderr, "exp: %s\n", exp); |
| 60 | fprintf(stderr, "msg: %s\n", msg_dump); |
| 61 | assert(strcmp(msg_dump, exp) == 0); |
| 62 | |
| 63 | msgb_free(msg); |
| 64 | talloc_free(msg_dump); |
| 65 | fprintf(stderr, "\n"); |
| 66 | } |
| 67 | |
| 68 | void test_pcu_rx_no_subscr_with_active_tbf() |
| 69 | { |
| 70 | struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, }; |
| 71 | |
| 72 | fprintf(stderr, "--- %s ---\n", __func__); |
Pau Espin Pedrol | 1989a19 | 2021-06-23 19:46:07 +0200 | [diff] [blame] | 73 | pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if)); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 74 | fprintf(stderr, "\n"); |
| 75 | } |
| 76 | |
| 77 | void prepare_bts_with_two_dl_tbf_subscr() |
| 78 | { |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 79 | struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, 0); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 80 | struct gprs_rlcmac_trx *trx; |
| 81 | |
| 82 | fprintf(stderr, "--- %s ---\n", __func__); |
| 83 | |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 84 | the_pcu->alloc_algorithm = alloc_algorithm_b; |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 85 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 86 | trx = bts->trx; |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 87 | trx->pdch[4].enable(); |
| 88 | trx->pdch[5].enable(); |
| 89 | trx->pdch[6].enable(); |
| 90 | trx->pdch[7].enable(); |
| 91 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 92 | ms1 = bts_alloc_ms(bts, 10, 11); |
Pau Espin Pedrol | 8757384 | 2022-10-26 20:23:45 +0200 | [diff] [blame] | 93 | tbf1 = dl_tbf_alloc(bts, ms1, 0, false); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 94 | ms2 = bts_alloc_ms(bts, 12, 13); |
Pau Espin Pedrol | 8757384 | 2022-10-26 20:23:45 +0200 | [diff] [blame] | 95 | tbf2 = dl_tbf_alloc(bts, ms2, 0, false); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 96 | |
| 97 | fprintf(stderr, "\n"); |
| 98 | } |
| 99 | |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 100 | void test_sched_app_info_ok(const struct gsm_pcu_if_app_info_req *req) |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 101 | { |
| 102 | struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, }; |
| 103 | struct msgb *msg; |
| 104 | |
| 105 | fprintf(stderr, "--- %s ---\n", __func__); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 106 | pcu_prim.u.app_info_req = *req; |
Pau Espin Pedrol | 1989a19 | 2021-06-23 19:46:07 +0200 | [diff] [blame] | 107 | pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if)); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 108 | |
| 109 | msg = sched_app_info(tbf1); |
| 110 | assert(msg); |
| 111 | msgb_free(msg); |
| 112 | |
| 113 | msg = sched_app_info(tbf2); |
| 114 | assert(msg); |
| 115 | msgb_free(msg); |
| 116 | |
| 117 | fprintf(stderr, "\n"); |
| 118 | } |
| 119 | |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 120 | void test_sched_app_info_missing_app_info_in_bts(const struct gsm_pcu_if_app_info_req *req) |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 121 | { |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 122 | struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, 0); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 123 | struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, }; |
| 124 | |
| 125 | fprintf(stderr, "--- %s ---\n", __func__); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 126 | pcu_prim.u.app_info_req = *req; |
Pau Espin Pedrol | 1989a19 | 2021-06-23 19:46:07 +0200 | [diff] [blame] | 127 | pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if)); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 128 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 129 | msgb_free(bts->app_info); |
| 130 | bts->app_info = NULL; |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 131 | |
| 132 | assert(sched_app_info(tbf1) == NULL); |
| 133 | |
| 134 | fprintf(stderr, "\n"); |
| 135 | } |
| 136 | |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 137 | void test_pcu_rx_overwrite_app_info(const struct gsm_pcu_if_app_info_req *req) |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 138 | { |
| 139 | struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, }; |
| 140 | |
| 141 | fprintf(stderr, "--- %s ---\n", __func__); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 142 | pcu_prim.u.app_info_req = *req; |
Pau Espin Pedrol | 1989a19 | 2021-06-23 19:46:07 +0200 | [diff] [blame] | 143 | pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if)); |
| 144 | pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if)); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 145 | fprintf(stderr, "\n"); |
| 146 | } |
| 147 | |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 148 | extern "C" void cleanup() |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 149 | { |
| 150 | fprintf(stderr, "--- %s ---\n", __func__); |
| 151 | |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 152 | struct gprs_rlcmac_bts *bts; |
| 153 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 154 | tbf_free(tbf1); |
| 155 | tbf_free(tbf2); |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 156 | |
| 157 | bts = gprs_pcu_get_bts_by_nr(the_pcu, 0); |
| 158 | talloc_free(bts); |
| 159 | |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame] | 160 | /* FIXME: talloc report disabled, because bts_alloc_ms(bts, ) in prepare_bts_with_two_dl_tbf_subscr() causes leak */ |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 161 | /* talloc_report_full(tall_pcu_ctx, stderr); */ |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 162 | talloc_free(the_pcu); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 163 | talloc_free(tall_pcu_ctx); |
| 164 | } |
| 165 | |
| 166 | int main(int argc, char *argv[]) |
| 167 | { |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 168 | struct gsm_pcu_if_app_info_req req = {0, 15, {0}}; |
| 169 | const uint8_t req_data[] = {0xff, 0x00, 0xff}; |
| 170 | memcpy(req.data, req_data, 3); |
| 171 | |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 172 | tall_pcu_ctx = talloc_named_const(NULL, 1, "AppInfoTest"); |
| 173 | osmo_init_logging2(tall_pcu_ctx, &gprs_log_info); |
| 174 | log_set_use_color(osmo_stderr_target, 0); |
Pau Espin Pedrol | 00f52cc | 2021-02-19 14:01:52 +0100 | [diff] [blame] | 175 | log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); |
Pau Espin Pedrol | b18d2a5 | 2021-02-19 14:00:48 +0100 | [diff] [blame] | 176 | log_set_print_category(osmo_stderr_target, 0); |
| 177 | log_set_print_category_hex(osmo_stderr_target, 0); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 178 | log_parse_category_mask(osmo_stderr_target, "DL1IF,1:DRLCMAC,3:DRLCMACSCHED,1"); |
| 179 | |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 180 | the_pcu = gprs_pcu_alloc(tall_pcu_ctx); |
Pau Espin Pedrol | d1049dc | 2021-01-18 17:14:14 +0100 | [diff] [blame] | 181 | bts_alloc(the_pcu, 0); |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 182 | |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 183 | test_enc_zero_len(); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 184 | test_enc(&req); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 185 | test_pcu_rx_no_subscr_with_active_tbf(); |
| 186 | |
| 187 | prepare_bts_with_two_dl_tbf_subscr(); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 188 | test_sched_app_info_ok(&req); |
| 189 | test_sched_app_info_missing_app_info_in_bts(&req); |
| 190 | test_pcu_rx_overwrite_app_info(&req); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 191 | |
| 192 | cleanup(); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * stubs that should not be reached |
| 197 | */ |
| 198 | extern "C" { |
| 199 | void l1if_pdch_req() { abort(); } |
| 200 | void l1if_connect_pdch() { abort(); } |
Philipp Maier | 72ed333 | 2023-02-27 15:32:00 +0100 | [diff] [blame] | 201 | void l1if_disconnect_pdch() { abort(); } |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 202 | void l1if_close_pdch() { abort(); } |
| 203 | void l1if_open_pdch() { abort(); } |
| 204 | } |