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. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 | */ |
| 17 | |
| 18 | #include <cstdlib> |
| 19 | #include <cstring> |
| 20 | #include <assert.h> |
| 21 | #include "gprs_rlcmac.h" |
| 22 | #include "bts.h" |
| 23 | |
| 24 | extern "C" { |
| 25 | #include <osmocom/vty/telnet_interface.h> |
| 26 | #include <osmocom/vty/logging.h> |
| 27 | #include <osmocom/core/utils.h> |
| 28 | #include <osmocom/core/msgb.h> |
| 29 | #include <osmocom/core/application.h> |
| 30 | } |
| 31 | |
| 32 | using namespace std; |
| 33 | gprs_rlcmac_dl_tbf *tbf1, *tbf2; |
| 34 | GprsMs *ms1, *ms2; |
| 35 | struct msgb *sched_app_info(struct gprs_rlcmac_tbf *tbf); |
| 36 | |
| 37 | /* globals used by the code */ |
| 38 | void *tall_pcu_ctx; |
| 39 | int16_t spoof_mnc = 0, spoof_mcc = 0; |
| 40 | bool spoof_mnc_3_digits = false; |
| 41 | |
| 42 | void test_enc_zero_len() { |
| 43 | struct gsm_pcu_if_app_info_req req = {0, 0, {0}}; |
| 44 | |
| 45 | fprintf(stderr, "--- %s ---\n", __func__); |
| 46 | assert(gprs_rlcmac_app_info_msg(&req) == NULL); |
| 47 | fprintf(stderr, "\n"); |
| 48 | } |
| 49 | |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 50 | void test_enc(const struct gsm_pcu_if_app_info_req *req) |
| 51 | { |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 52 | 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 */ |
| 53 | struct msgb *msg; |
| 54 | char *msg_dump; |
| 55 | |
| 56 | fprintf(stderr, "--- %s ---\n", __func__); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 57 | msg = gprs_rlcmac_app_info_msg(req); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 58 | msg_dump = msgb_hexdump_c(tall_pcu_ctx, msg); |
| 59 | |
| 60 | fprintf(stderr, "exp: %s\n", exp); |
| 61 | fprintf(stderr, "msg: %s\n", msg_dump); |
| 62 | assert(strcmp(msg_dump, exp) == 0); |
| 63 | |
| 64 | msgb_free(msg); |
| 65 | talloc_free(msg_dump); |
| 66 | fprintf(stderr, "\n"); |
| 67 | } |
| 68 | |
| 69 | void test_pcu_rx_no_subscr_with_active_tbf() |
| 70 | { |
| 71 | struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, }; |
| 72 | |
| 73 | fprintf(stderr, "--- %s ---\n", __func__); |
| 74 | pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim); |
| 75 | fprintf(stderr, "\n"); |
| 76 | } |
| 77 | |
| 78 | void prepare_bts_with_two_dl_tbf_subscr() |
| 79 | { |
| 80 | BTS *bts = BTS::main_bts(); |
| 81 | struct gprs_rlcmac_bts *bts_data; |
| 82 | struct gprs_rlcmac_trx *trx; |
| 83 | |
| 84 | fprintf(stderr, "--- %s ---\n", __func__); |
| 85 | |
| 86 | bts_data = bts->bts_data(); |
| 87 | bts_data->alloc_algorithm = alloc_algorithm_b; |
| 88 | |
| 89 | trx = bts_data->trx; |
| 90 | trx->pdch[4].enable(); |
| 91 | trx->pdch[5].enable(); |
| 92 | trx->pdch[6].enable(); |
| 93 | trx->pdch[7].enable(); |
| 94 | |
| 95 | ms1 = bts->ms_alloc(10, 11); |
| 96 | tbf1 = tbf_alloc_dl_tbf(bts_data, ms1, 0, 10, 11, false); |
| 97 | ms2 = bts->ms_alloc(12, 13); |
| 98 | tbf2 = tbf_alloc_dl_tbf(bts_data, ms2, 0, 12, 13, false); |
| 99 | |
| 100 | fprintf(stderr, "\n"); |
| 101 | } |
| 102 | |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 103 | 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] | 104 | { |
| 105 | struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, }; |
| 106 | struct msgb *msg; |
| 107 | |
| 108 | fprintf(stderr, "--- %s ---\n", __func__); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 109 | pcu_prim.u.app_info_req = *req; |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 110 | pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim); |
| 111 | |
| 112 | msg = sched_app_info(tbf1); |
| 113 | assert(msg); |
| 114 | msgb_free(msg); |
| 115 | |
| 116 | msg = sched_app_info(tbf2); |
| 117 | assert(msg); |
| 118 | msgb_free(msg); |
| 119 | |
| 120 | fprintf(stderr, "\n"); |
| 121 | } |
| 122 | |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 123 | 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] | 124 | { |
| 125 | struct gprs_rlcmac_bts *bts_data = BTS::main_bts()->bts_data(); |
| 126 | struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, }; |
| 127 | |
| 128 | fprintf(stderr, "--- %s ---\n", __func__); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 129 | pcu_prim.u.app_info_req = *req; |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 130 | pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim); |
| 131 | |
| 132 | msgb_free(bts_data->app_info); |
| 133 | bts_data->app_info = NULL; |
| 134 | |
| 135 | assert(sched_app_info(tbf1) == NULL); |
| 136 | |
| 137 | fprintf(stderr, "\n"); |
| 138 | } |
| 139 | |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 140 | 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] | 141 | { |
| 142 | struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, }; |
| 143 | |
| 144 | fprintf(stderr, "--- %s ---\n", __func__); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 145 | pcu_prim.u.app_info_req = *req; |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 146 | pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim); |
| 147 | pcu_rx(PCU_IF_MSG_APP_INFO_REQ, &pcu_prim); |
| 148 | fprintf(stderr, "\n"); |
| 149 | } |
| 150 | |
| 151 | void cleanup() |
| 152 | { |
| 153 | fprintf(stderr, "--- %s ---\n", __func__); |
| 154 | |
| 155 | BTS::main_bts()->cleanup(); |
| 156 | talloc_free(tbf1); |
| 157 | talloc_free(tbf2); |
| 158 | /* FIXME: talloc report disabled, because bts->ms_alloc() in prepare_bts_with_two_dl_tbf_subscr() causes leak */ |
| 159 | /* talloc_report_full(tall_pcu_ctx, stderr); */ |
| 160 | talloc_free(tall_pcu_ctx); |
| 161 | } |
| 162 | |
| 163 | int main(int argc, char *argv[]) |
| 164 | { |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 165 | struct gsm_pcu_if_app_info_req req = {0, 15, {0}}; |
| 166 | const uint8_t req_data[] = {0xff, 0x00, 0xff}; |
| 167 | memcpy(req.data, req_data, 3); |
| 168 | |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 169 | tall_pcu_ctx = talloc_named_const(NULL, 1, "AppInfoTest"); |
| 170 | osmo_init_logging2(tall_pcu_ctx, &gprs_log_info); |
| 171 | log_set_use_color(osmo_stderr_target, 0); |
| 172 | log_set_print_filename(osmo_stderr_target, 0); |
| 173 | log_parse_category_mask(osmo_stderr_target, "DL1IF,1:DRLCMAC,3:DRLCMACSCHED,1"); |
| 174 | |
| 175 | test_enc_zero_len(); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 176 | test_enc(&req); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 177 | test_pcu_rx_no_subscr_with_active_tbf(); |
| 178 | |
| 179 | prepare_bts_with_two_dl_tbf_subscr(); |
Oliver Smith | 2beb1b8 | 2019-09-18 13:47:25 +0200 | [diff] [blame] | 180 | test_sched_app_info_ok(&req); |
| 181 | test_sched_app_info_missing_app_info_in_bts(&req); |
| 182 | test_pcu_rx_overwrite_app_info(&req); |
Oliver Smith | cfb6321 | 2019-09-05 17:13:33 +0200 | [diff] [blame] | 183 | |
| 184 | cleanup(); |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * stubs that should not be reached |
| 189 | */ |
| 190 | extern "C" { |
| 191 | void l1if_pdch_req() { abort(); } |
| 192 | void l1if_connect_pdch() { abort(); } |
| 193 | void l1if_close_pdch() { abort(); } |
| 194 | void l1if_open_pdch() { abort(); } |
| 195 | } |