blob: 4f928b3cee3976cdebe78e123e4919b52ac5d340 [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
Pau Espin Pedrol4d363912023-04-21 19:32:16 +020042/* override, requires '-Wl,--wrap=pcu_sock_send' */
43int __real_pcu_sock_send(struct msgb *msg);
44extern "C" int __wrap_pcu_sock_send(struct msgb *msg)
45{
46 msgb_free(msg);
47 return 0;
48}
49
Oliver Smithcfb63212019-09-05 17:13:33 +020050void test_enc_zero_len() {
51 struct gsm_pcu_if_app_info_req req = {0, 0, {0}};
52
53 fprintf(stderr, "--- %s ---\n", __func__);
54 assert(gprs_rlcmac_app_info_msg(&req) == NULL);
55 fprintf(stderr, "\n");
56}
57
Oliver Smith2beb1b82019-09-18 13:47:25 +020058void test_enc(const struct gsm_pcu_if_app_info_req *req)
59{
Oliver Smithcfb63212019-09-05 17:13:33 +020060 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 */
61 struct msgb *msg;
62 char *msg_dump;
63
64 fprintf(stderr, "--- %s ---\n", __func__);
Oliver Smith2beb1b82019-09-18 13:47:25 +020065 msg = gprs_rlcmac_app_info_msg(req);
Oliver Smithcfb63212019-09-05 17:13:33 +020066 msg_dump = msgb_hexdump_c(tall_pcu_ctx, msg);
67
68 fprintf(stderr, "exp: %s\n", exp);
69 fprintf(stderr, "msg: %s\n", msg_dump);
70 assert(strcmp(msg_dump, exp) == 0);
71
72 msgb_free(msg);
73 talloc_free(msg_dump);
74 fprintf(stderr, "\n");
75}
76
77void test_pcu_rx_no_subscr_with_active_tbf()
78{
79 struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
80
81 fprintf(stderr, "--- %s ---\n", __func__);
Pau Espin Pedrol1989a192021-06-23 19:46:07 +020082 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
Oliver Smithcfb63212019-09-05 17:13:33 +020083 fprintf(stderr, "\n");
84}
85
86void prepare_bts_with_two_dl_tbf_subscr()
87{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +010088 struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, 0);
Oliver Smithcfb63212019-09-05 17:13:33 +020089 struct gprs_rlcmac_trx *trx;
90
91 fprintf(stderr, "--- %s ---\n", __func__);
92
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +010093 the_pcu->alloc_algorithm = alloc_algorithm_b;
Oliver Smithcfb63212019-09-05 17:13:33 +020094
Pau Espin Pedrol2182e622021-01-14 16:48:38 +010095 trx = bts->trx;
Oliver Smithcfb63212019-09-05 17:13:33 +020096 trx->pdch[4].enable();
97 trx->pdch[5].enable();
98 trx->pdch[6].enable();
99 trx->pdch[7].enable();
100
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200101 ms1 = ms_alloc(bts, NULL);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200102 ms_set_ms_class(ms1, 10);
103 ms_set_egprs_ms_class(ms1, 11);
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200104 OSMO_ASSERT(ms_new_dl_tbf_assigned_on_pch(ms1) == 0);
105 tbf1 = ms_dl_tbf(ms1);
106 OSMO_ASSERT(tbf1);
107
Pau Espin Pedroleae91472023-04-19 19:42:05 +0200108 ms2 = ms_alloc(bts, NULL);
Pau Espin Pedrolbfc97562023-04-17 14:49:29 +0200109 ms_set_ms_class(ms2, 12);
110 ms_set_egprs_ms_class(ms2, 13);
Pau Espin Pedrol4d363912023-04-21 19:32:16 +0200111 OSMO_ASSERT(ms_new_dl_tbf_assigned_on_pch(ms2) == 0);
112 tbf2 = ms_dl_tbf(ms2);
113 OSMO_ASSERT(tbf2);
Oliver Smithcfb63212019-09-05 17:13:33 +0200114
115 fprintf(stderr, "\n");
116}
117
Oliver Smith2beb1b82019-09-18 13:47:25 +0200118void test_sched_app_info_ok(const struct gsm_pcu_if_app_info_req *req)
Oliver Smithcfb63212019-09-05 17:13:33 +0200119{
120 struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
121 struct msgb *msg;
122
123 fprintf(stderr, "--- %s ---\n", __func__);
Oliver Smith2beb1b82019-09-18 13:47:25 +0200124 pcu_prim.u.app_info_req = *req;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +0200125 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
Oliver Smithcfb63212019-09-05 17:13:33 +0200126
127 msg = sched_app_info(tbf1);
128 assert(msg);
129 msgb_free(msg);
130
131 msg = sched_app_info(tbf2);
132 assert(msg);
133 msgb_free(msg);
134
135 fprintf(stderr, "\n");
136}
137
Oliver Smith2beb1b82019-09-18 13:47:25 +0200138void 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 +0200139{
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100140 struct gprs_rlcmac_bts *bts = gprs_pcu_get_bts_by_nr(the_pcu, 0);
Oliver Smithcfb63212019-09-05 17:13:33 +0200141 struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
142
143 fprintf(stderr, "--- %s ---\n", __func__);
Oliver Smith2beb1b82019-09-18 13:47:25 +0200144 pcu_prim.u.app_info_req = *req;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +0200145 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
Oliver Smithcfb63212019-09-05 17:13:33 +0200146
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100147 msgb_free(bts->app_info);
148 bts->app_info = NULL;
Oliver Smithcfb63212019-09-05 17:13:33 +0200149
150 assert(sched_app_info(tbf1) == NULL);
151
152 fprintf(stderr, "\n");
153}
154
Oliver Smith2beb1b82019-09-18 13:47:25 +0200155void test_pcu_rx_overwrite_app_info(const struct gsm_pcu_if_app_info_req *req)
Oliver Smithcfb63212019-09-05 17:13:33 +0200156{
157 struct gsm_pcu_if pcu_prim = {PCU_IF_MSG_APP_INFO_REQ, };
158
159 fprintf(stderr, "--- %s ---\n", __func__);
Oliver Smith2beb1b82019-09-18 13:47:25 +0200160 pcu_prim.u.app_info_req = *req;
Pau Espin Pedrol1989a192021-06-23 19:46:07 +0200161 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
162 pcu_rx(&pcu_prim, sizeof(struct gsm_pcu_if));
Oliver Smithcfb63212019-09-05 17:13:33 +0200163 fprintf(stderr, "\n");
164}
165
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100166extern "C" void cleanup()
Oliver Smithcfb63212019-09-05 17:13:33 +0200167{
168 fprintf(stderr, "--- %s ---\n", __func__);
169
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100170 struct gprs_rlcmac_bts *bts;
171
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +0100172 tbf_free(tbf1);
173 tbf_free(tbf2);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100174
175 bts = gprs_pcu_get_bts_by_nr(the_pcu, 0);
176 talloc_free(bts);
177
Pau Espin Pedrol9da06862023-04-17 19:00:04 +0200178 /* FIXME: talloc report disabled, because ms_alloc(bts, ) in prepare_bts_with_two_dl_tbf_subscr() causes leak */
Oliver Smithcfb63212019-09-05 17:13:33 +0200179 /* talloc_report_full(tall_pcu_ctx, stderr); */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100180 talloc_free(the_pcu);
Oliver Smithcfb63212019-09-05 17:13:33 +0200181 talloc_free(tall_pcu_ctx);
182}
183
184int main(int argc, char *argv[])
185{
Oliver Smith2beb1b82019-09-18 13:47:25 +0200186 struct gsm_pcu_if_app_info_req req = {0, 15, {0}};
187 const uint8_t req_data[] = {0xff, 0x00, 0xff};
188 memcpy(req.data, req_data, 3);
189
Oliver Smithcfb63212019-09-05 17:13:33 +0200190 tall_pcu_ctx = talloc_named_const(NULL, 1, "AppInfoTest");
191 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
192 log_set_use_color(osmo_stderr_target, 0);
Pau Espin Pedrol00f52cc2021-02-19 14:01:52 +0100193 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Pau Espin Pedrolb18d2a52021-02-19 14:00:48 +0100194 log_set_print_category(osmo_stderr_target, 0);
195 log_set_print_category_hex(osmo_stderr_target, 0);
Pau Espin Pedrolfe4d2f72023-04-19 20:38:40 +0200196 log_parse_category_mask(osmo_stderr_target, "DL1IF,1:DRLCMAC,3:DRLCMACSCHED,1:DMS,3");
Oliver Smithcfb63212019-09-05 17:13:33 +0200197
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100198 the_pcu = gprs_pcu_alloc(tall_pcu_ctx);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100199 bts_alloc(the_pcu, 0);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100200
Oliver Smithcfb63212019-09-05 17:13:33 +0200201 test_enc_zero_len();
Oliver Smith2beb1b82019-09-18 13:47:25 +0200202 test_enc(&req);
Oliver Smithcfb63212019-09-05 17:13:33 +0200203 test_pcu_rx_no_subscr_with_active_tbf();
204
205 prepare_bts_with_two_dl_tbf_subscr();
Oliver Smith2beb1b82019-09-18 13:47:25 +0200206 test_sched_app_info_ok(&req);
207 test_sched_app_info_missing_app_info_in_bts(&req);
208 test_pcu_rx_overwrite_app_info(&req);
Oliver Smithcfb63212019-09-05 17:13:33 +0200209
210 cleanup();
211}
212
213/*
214 * stubs that should not be reached
215 */
216extern "C" {
217void l1if_pdch_req() { abort(); }
218void l1if_connect_pdch() { abort(); }
Philipp Maier72ed3332023-02-27 15:32:00 +0100219void l1if_disconnect_pdch() { abort(); }
Philipp Maier6ee8d132023-04-25 09:41:17 +0200220void l1if_close_trx() { abort(); }
221void l1if_open_trx() { abort(); }
Oliver Smithcfb63212019-09-05 17:13:33 +0200222}