blob: 962ead485d61150496df8573b4c56f23f7cca087 [file] [log] [blame]
Oliver Smithcfb63212019-09-05 17:13:33 +02001/* 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
24extern "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
32using namespace std;
33gprs_rlcmac_dl_tbf *tbf1, *tbf2;
34GprsMs *ms1, *ms2;
35struct msgb *sched_app_info(struct gprs_rlcmac_tbf *tbf);
36
37/* globals used by the code */
38void *tall_pcu_ctx;
39int16_t spoof_mnc = 0, spoof_mcc = 0;
40bool spoof_mnc_3_digits = false;
41
42void 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 Smith2beb1b82019-09-18 13:47:25 +020050void test_enc(const struct gsm_pcu_if_app_info_req *req)
51{
Oliver Smithcfb63212019-09-05 17:13:33 +020052 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 Smith2beb1b82019-09-18 13:47:25 +020057 msg = gprs_rlcmac_app_info_msg(req);
Oliver Smithcfb63212019-09-05 17:13:33 +020058 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
69void 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
78void 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();
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +010087 the_pcu->alloc_algorithm = alloc_algorithm_b;
Oliver Smithcfb63212019-09-05 17:13:33 +020088
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);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020096 tbf1 = tbf_alloc_dl_tbf(bts_data, ms1, 0, false);
Oliver Smithcfb63212019-09-05 17:13:33 +020097 ms2 = bts->ms_alloc(12, 13);
Pau Espin Pedrol322456e2020-05-08 18:15:59 +020098 tbf2 = tbf_alloc_dl_tbf(bts_data, ms2, 0, false);
Oliver Smithcfb63212019-09-05 17:13:33 +020099
100 fprintf(stderr, "\n");
101}
102
Oliver Smith2beb1b82019-09-18 13:47:25 +0200103void test_sched_app_info_ok(const struct gsm_pcu_if_app_info_req *req)
Oliver Smithcfb63212019-09-05 17:13:33 +0200104{
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 Smith2beb1b82019-09-18 13:47:25 +0200109 pcu_prim.u.app_info_req = *req;
Oliver Smithcfb63212019-09-05 17:13:33 +0200110 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 Smith2beb1b82019-09-18 13:47:25 +0200123void test_sched_app_info_missing_app_info_in_bts(const struct gsm_pcu_if_app_info_req *req)
Oliver Smithcfb63212019-09-05 17:13:33 +0200124{
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 Smith2beb1b82019-09-18 13:47:25 +0200129 pcu_prim.u.app_info_req = *req;
Oliver Smithcfb63212019-09-05 17:13:33 +0200130 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 Smith2beb1b82019-09-18 13:47:25 +0200140void test_pcu_rx_overwrite_app_info(const struct gsm_pcu_if_app_info_req *req)
Oliver Smithcfb63212019-09-05 17:13:33 +0200141{
142 struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
143
144 fprintf(stderr, "--- %s ---\n", __func__);
Oliver Smith2beb1b82019-09-18 13:47:25 +0200145 pcu_prim.u.app_info_req = *req;
Oliver Smithcfb63212019-09-05 17:13:33 +0200146 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
151void cleanup()
152{
153 fprintf(stderr, "--- %s ---\n", __func__);
154
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100155 tbf_free(tbf1);
156 tbf_free(tbf2);
Oliver Smithcfb63212019-09-05 17:13:33 +0200157 BTS::main_bts()->cleanup();
Oliver Smithcfb63212019-09-05 17:13:33 +0200158 /* 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); */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100160 talloc_free(the_pcu);
Oliver Smithcfb63212019-09-05 17:13:33 +0200161 talloc_free(tall_pcu_ctx);
162}
163
164int main(int argc, char *argv[])
165{
Oliver Smith2beb1b82019-09-18 13:47:25 +0200166 struct gsm_pcu_if_app_info_req req = {0, 15, {0}};
167 const uint8_t req_data[] = {0xff, 0x00, 0xff};
168 memcpy(req.data, req_data, 3);
169
Oliver Smithcfb63212019-09-05 17:13:33 +0200170 tall_pcu_ctx = talloc_named_const(NULL, 1, "AppInfoTest");
171 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
172 log_set_use_color(osmo_stderr_target, 0);
173 log_set_print_filename(osmo_stderr_target, 0);
174 log_parse_category_mask(osmo_stderr_target, "DL1IF,1:DRLCMAC,3:DRLCMACSCHED,1");
175
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100176 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
177 the_pcu->bts = bts_alloc(the_pcu);
178
Oliver Smithcfb63212019-09-05 17:13:33 +0200179 test_enc_zero_len();
Oliver Smith2beb1b82019-09-18 13:47:25 +0200180 test_enc(&req);
Oliver Smithcfb63212019-09-05 17:13:33 +0200181 test_pcu_rx_no_subscr_with_active_tbf();
182
183 prepare_bts_with_two_dl_tbf_subscr();
Oliver Smith2beb1b82019-09-18 13:47:25 +0200184 test_sched_app_info_ok(&req);
185 test_sched_app_info_missing_app_info_in_bts(&req);
186 test_pcu_rx_overwrite_app_info(&req);
Oliver Smithcfb63212019-09-05 17:13:33 +0200187
188 cleanup();
189}
190
191/*
192 * stubs that should not be reached
193 */
194extern "C" {
195void l1if_pdch_req() { abort(); }
196void l1if_connect_pdch() { abort(); }
197void l1if_close_pdch() { abort(); }
198void l1if_open_pdch() { abort(); }
199}