blob: 1a4e660b22011c5c7b0a8278e61b8ded5ca76926 [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.
Oliver Smithcfb63212019-09-05 17:13:33 +020012 */
13
14#include <cstdlib>
15#include <cstring>
16#include <assert.h>
17#include "gprs_rlcmac.h"
18#include "bts.h"
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010019#include "tbf_dl.h"
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +020020#include "gprs_ms.h"
Oliver Smithcfb63212019-09-05 17:13:33 +020021
22extern "C" {
23#include <osmocom/vty/telnet_interface.h>
24#include <osmocom/vty/logging.h>
25#include <osmocom/core/utils.h>
26#include <osmocom/core/msgb.h>
27#include <osmocom/core/application.h>
Pau Espin Pedrolff7c5812022-12-14 18:49:06 +010028
29#include "alloc_algo.h"
Oliver Smithcfb63212019-09-05 17:13:33 +020030}
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__);
Pau Espin Pedrol1989a192021-06-23 19:46:07 +020074 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
Oliver Smithcfb63212019-09-05 17:13:33 +020075 fprintf(stderr, "\n");
76}
77
78void prepare_bts_with_two_dl_tbf_subscr()
79{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +010080 struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, 0);
Oliver Smithcfb63212019-09-05 17:13:33 +020081 struct gprs_rlcmac_trx *trx;
82
83 fprintf(stderr, "--- %s ---\n", __func__);
84
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +010085 the_pcu->alloc_algorithm = alloc_algorithm_b;
Oliver Smithcfb63212019-09-05 17:13:33 +020086
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010087 trx = bts->trx;
Oliver Smithcfb63212019-09-05 17:13:33 +020088 trx->pdch[4].enable();
89 trx->pdch[5].enable();
90 trx->pdch[6].enable();
91 trx->pdch[7].enable();
92
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +020093 ms1 = bts_alloc_ms(bts);
94 ms_set_ms_class(ms1, 10);
95 ms_set_egprs_ms_class(ms1, 11);
Pau Espin Pedrol87573842022-10-26 20:23:45 +020096 tbf1 = dl_tbf_alloc(bts, ms1, 0, false);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +020097 ms2 = bts_alloc_ms(bts);
98 ms_set_ms_class(ms2, 12);
99 ms_set_egprs_ms_class(ms2, 13);
Pau Espin Pedrol87573842022-10-26 20:23:45 +0200100 tbf2 = dl_tbf_alloc(bts, ms2, 0, false);
Oliver Smithcfb63212019-09-05 17:13:33 +0200101
102 fprintf(stderr, "\n");
103}
104
Oliver Smith2beb1b82019-09-18 13:47:25 +0200105void test_sched_app_info_ok(const struct gsm_pcu_if_app_info_req *req)
Oliver Smithcfb63212019-09-05 17:13:33 +0200106{
107 struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
108 struct msgb *msg;
109
110 fprintf(stderr, "--- %s ---\n", __func__);
Oliver Smith2beb1b82019-09-18 13:47:25 +0200111 pcu_prim.u.app_info_req = *req;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +0200112 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
Oliver Smithcfb63212019-09-05 17:13:33 +0200113
114 msg = sched_app_info(tbf1);
115 assert(msg);
116 msgb_free(msg);
117
118 msg = sched_app_info(tbf2);
119 assert(msg);
120 msgb_free(msg);
121
122 fprintf(stderr, "\n");
123}
124
Oliver Smith2beb1b82019-09-18 13:47:25 +0200125void 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 +0200126{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100127 struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, 0);
Oliver Smithcfb63212019-09-05 17:13:33 +0200128 struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
129
130 fprintf(stderr, "--- %s ---\n", __func__);
Oliver Smith2beb1b82019-09-18 13:47:25 +0200131 pcu_prim.u.app_info_req = *req;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +0200132 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
Oliver Smithcfb63212019-09-05 17:13:33 +0200133
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100134 msgb_free(bts->app_info);
135 bts->app_info = NULL;
Oliver Smithcfb63212019-09-05 17:13:33 +0200136
137 assert(sched_app_info(tbf1) == NULL);
138
139 fprintf(stderr, "\n");
140}
141
Oliver Smith2beb1b82019-09-18 13:47:25 +0200142void test_pcu_rx_overwrite_app_info(const struct gsm_pcu_if_app_info_req *req)
Oliver Smithcfb63212019-09-05 17:13:33 +0200143{
144 struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
145
146 fprintf(stderr, "--- %s ---\n", __func__);
Oliver Smith2beb1b82019-09-18 13:47:25 +0200147 pcu_prim.u.app_info_req = *req;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +0200148 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
149 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
Oliver Smithcfb63212019-09-05 17:13:33 +0200150 fprintf(stderr, "\n");
151}
152
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100153extern "C" void cleanup()
Oliver Smithcfb63212019-09-05 17:13:33 +0200154{
155 fprintf(stderr, "--- %s ---\n", __func__);
156
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100157 struct gprs_rlcmac_bts *bts;
158
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100159 tbf_free(tbf1);
160 tbf_free(tbf2);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100161
162 bts = gprs_pcu_get_bts_by_nr(the_pcu, 0);
163 talloc_free(bts);
164
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100165 /* FIXME: talloc report disabled, because bts_alloc_ms(bts, ) in prepare_bts_with_two_dl_tbf_subscr() causes leak */
Oliver Smithcfb63212019-09-05 17:13:33 +0200166 /* talloc_report_full(tall_pcu_ctx, stderr); */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100167 talloc_free(the_pcu);
Oliver Smithcfb63212019-09-05 17:13:33 +0200168 talloc_free(tall_pcu_ctx);
169}
170
171int main(int argc, char *argv[])
172{
Oliver Smith2beb1b82019-09-18 13:47:25 +0200173 struct gsm_pcu_if_app_info_req req = {0, 15, {0}};
174 const uint8_t req_data[] = {0xff, 0x00, 0xff};
175 memcpy(req.data, req_data, 3);
176
Oliver Smithcfb63212019-09-05 17:13:33 +0200177 tall_pcu_ctx = talloc_named_const(NULL, 1, "AppInfoTest");
178 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
179 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100180 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100181 log_set_print_category(osmo_stderr_target, 0);
182 log_set_print_category_hex(osmo_stderr_target, 0);
Oliver Smithcfb63212019-09-05 17:13:33 +0200183 log_parse_category_mask(osmo_stderr_target, "DL1IF,1:DRLCMAC,3:DRLCMACSCHED,1");
184
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100185 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100186 bts_alloc(the_pcu, 0);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100187
Oliver Smithcfb63212019-09-05 17:13:33 +0200188 test_enc_zero_len();
Oliver Smith2beb1b82019-09-18 13:47:25 +0200189 test_enc(&req);
Oliver Smithcfb63212019-09-05 17:13:33 +0200190 test_pcu_rx_no_subscr_with_active_tbf();
191
192 prepare_bts_with_two_dl_tbf_subscr();
Oliver Smith2beb1b82019-09-18 13:47:25 +0200193 test_sched_app_info_ok(&req);
194 test_sched_app_info_missing_app_info_in_bts(&req);
195 test_pcu_rx_overwrite_app_info(&req);
Oliver Smithcfb63212019-09-05 17:13:33 +0200196
197 cleanup();
198}
199
200/*
201 * stubs that should not be reached
202 */
203extern "C" {
204void l1if_pdch_req() { abort(); }
205void l1if_connect_pdch() { abort(); }
Philipp Maier72ed3332023-02-27 15:32:00 +0100206void l1if_disconnect_pdch() { abort(); }
Oliver Smithcfb63212019-09-05 17:13:33 +0200207void l1if_close_pdch() { abort(); }
208void l1if_open_pdch() { abort(); }
209}