blob: cc78e14913debdb692a1b2d59b1a1b7290efbfa2 [file] [log] [blame]
Harald Welte2483f1b2016-06-19 18:06:02 +02001/* Test Osmocom SMS queue */
2
3/*
4 * (C) 2017 by sysmocom s.f.m.c. GmbH
5 * All Rights Reserved
6 *
7 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <osmocom/core/application.h>
25
Neels Hofmeyr90843962017-09-04 15:04:35 +020026#include <osmocom/msc/debug.h>
27#include <osmocom/msc/vlr.h>
Neels Hofmeyr7b61ffe2018-11-30 02:46:53 +010028#include <osmocom/msc/gsm_data.h>
Vadim Yanitskiy96262a72019-03-28 21:25:14 +070029#include <osmocom/msc/gsm_04_11.h>
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +010030#include <osmocom/msc/db.h>
31#include <osmocom/msc/sms_queue.h>
Harald Welte2483f1b2016-06-19 18:06:02 +020032
33static void *talloc_ctx = NULL;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +070034extern void *tall_gsms_ctx;
Harald Welte2483f1b2016-06-19 18:06:02 +020035
36struct gsm_sms *smsq_take_next_sms(struct gsm_network *net,
37 char *last_msisdn,
38 size_t last_msisdn_buflen);
39
40static void _test_take_next_sms_print(int i,
41 struct gsm_sms *sms,
42 const char *last_msisdn)
43{
44 printf("#%d: ", i);
45 if (sms)
46 printf("sending SMS to %s", sms->text);
47 else
48 printf("no SMS to send");
49 printf(" (last_msisdn='%s')\n", last_msisdn? last_msisdn : "NULL");
50}
51
Harald Welte2483f1b2016-06-19 18:06:02 +020052struct {
53 const char *msisdn;
54 int nr_of_sms;
55 int failed_attempts;
56 bool vsub_attached;
57} fake_sms_db[] = {
58 {
59 .msisdn = "1111",
60 .nr_of_sms = 0,
61 .vsub_attached = true,
62 },
63 {
64 .msisdn = "2222",
65 .nr_of_sms = 2,
66 .failed_attempts = 2,
67 .vsub_attached = true,
68 },
69 {
70 .msisdn = "3333",
71 .nr_of_sms = 2,
72 .failed_attempts = 3,
73 .vsub_attached = true,
74 },
75 {
76 .msisdn = "4444",
77 .nr_of_sms = 0,
78 .vsub_attached = true,
79 },
80 {
81 .msisdn = "5555",
82 .nr_of_sms = 2,
83 .failed_attempts = 5,
84 .vsub_attached = false,
85 },
86};
87
88/* override, requires '-Wl,--wrap=db_sms_get_next_unsent_rr_msisdn' */
89struct gsm_sms *__real_db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net,
90 const char *last_msisdn,
91 unsigned int max_failed);
92struct gsm_sms *__wrap_db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net,
93 const char *last_msisdn,
94 unsigned int max_failed)
95{
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +010096 static struct vlr_subscr arbitrary_vsub = {};
97 static bool arbitrary_vsub_set_up = false;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +070098 struct gsm_sms *sms;
Vadim Yanitskiy678354f2019-05-11 03:10:22 +070099 int i, rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +0200100 printf(" hitting database: looking for MSISDN > '%s', failed_attempts <= %d\n",
101 last_msisdn, max_failed);
102
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100103 if (!arbitrary_vsub_set_up) {
104 osmo_use_count_make_static_entries(&arbitrary_vsub.use_count, arbitrary_vsub.use_count_buf,
105 ARRAY_SIZE(arbitrary_vsub.use_count_buf));
106 arbitrary_vsub_set_up = true;
107 }
108
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700109 /* Every time we call sms_free(), the internal logic of libmsc
110 * may call vlr_subscr_put() on our arbitrary_vsub, what would
111 * lead to a segfault if its use_count <= 0. To prevent this,
112 * let's ensure a big enough initial value. */
Vadim Yanitskiy678354f2019-05-11 03:10:22 +0700113 rc += osmo_use_count_get_put(&arbitrary_vsub.use_count, VSUB_USE_SMS_RECEIVER, 1000);
114 rc += osmo_use_count_get_put(&arbitrary_vsub.use_count, VSUB_USE_SMS_PENDING, 1000);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700115 arbitrary_vsub.lu_complete = true;
Vadim Yanitskiy678354f2019-05-11 03:10:22 +0700116 OSMO_ASSERT(rc == 0);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700117
Harald Welte2483f1b2016-06-19 18:06:02 +0200118 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
119 if (!fake_sms_db[i].nr_of_sms)
120 continue;
121 if (strcmp(fake_sms_db[i].msisdn, last_msisdn) <= 0)
122 continue;
123 if (fake_sms_db[i].failed_attempts > max_failed)
124 continue;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700125
126 sms = sms_alloc();
127 OSMO_ASSERT(sms);
128
129 osmo_strlcpy(sms->dst.addr, fake_sms_db[i].msisdn,
130 sizeof(sms->dst.addr));
131 sms->receiver = fake_sms_db[i].vsub_attached? &arbitrary_vsub : NULL;
132 osmo_strlcpy(sms->text, fake_sms_db[i].msisdn, sizeof(sms->text));
Harald Welte2483f1b2016-06-19 18:06:02 +0200133 if (fake_sms_db[i].vsub_attached)
Max5e2e9bd2018-02-06 19:31:08 +0100134 fake_sms_db[i].nr_of_sms--;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700135 return sms;
Harald Welte2483f1b2016-06-19 18:06:02 +0200136 }
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700137
Harald Welte2483f1b2016-06-19 18:06:02 +0200138 return NULL;
139}
140
141void show_fake_sms_db()
142{
143 int i;
144 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
145 printf(" %s%s has %u SMS pending, %u failed attempts\n",
146 fake_sms_db[i].msisdn,
147 fake_sms_db[i].vsub_attached ? "" : " (NOT attached)",
148 fake_sms_db[i].nr_of_sms,
149 fake_sms_db[i].failed_attempts);
150 }
151 printf("-->\n");
152}
153
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700154/* sms_free() is not safe against NULL */
155#define sms_free_safe(sms) \
156 if (sms != NULL) sms_free(sms)
157
Harald Welte2483f1b2016-06-19 18:06:02 +0200158static void test_next_sms()
159{
160 int i;
Vadim Yanitskiy8b0737f2019-05-25 19:27:17 +0700161 char last_msisdn[GSM23003_MSISDN_MAX_DIGITS+1] = "";
Harald Welte2483f1b2016-06-19 18:06:02 +0200162
163 printf("Testing smsq_take_next_sms()\n");
164
165 printf("\n- vsub 2, 3 and 5 each have 2 SMS pending, but 5 is not attached\n");
166 last_msisdn[0] = '\0';
167 show_fake_sms_db();
168 for (i = 0; i < 7; i++) {
169 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
170 _test_take_next_sms_print(i, sms, last_msisdn);
171 OSMO_ASSERT(i >= 4 || sms);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700172 sms_free_safe(sms);
Harald Welte2483f1b2016-06-19 18:06:02 +0200173 }
174
175 printf("\n- SMS are pending at various nr failed attempts (cutoff at >= 10)\n");
176 last_msisdn[0] = '\0';
177 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
178 fake_sms_db[i].vsub_attached = true;
179 fake_sms_db[i].nr_of_sms = 1 + i;
180 fake_sms_db[i].failed_attempts = i*5;
181
182 }
183 show_fake_sms_db();
184 for (i = 0; i < 7; i++) {
185 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
186 _test_take_next_sms_print(i, sms, last_msisdn);
187 OSMO_ASSERT(i >= 2 || sms);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700188 sms_free_safe(sms);
Harald Welte2483f1b2016-06-19 18:06:02 +0200189 }
190
191 printf("\n- iterate the SMS DB at most once\n");
192 osmo_strlcpy(last_msisdn, "2345", sizeof(last_msisdn));
193 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
194 fake_sms_db[i].vsub_attached = false;
195 fake_sms_db[i].nr_of_sms = 1;
196 fake_sms_db[i].failed_attempts = 0;
197 }
198 show_fake_sms_db();
199 for (i = 0; i < 3; i++) {
200 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
201 _test_take_next_sms_print(i, sms, last_msisdn);
202 OSMO_ASSERT(!sms);
203 }
204
205 printf("\n- there are no SMS in the DB\n");
206 last_msisdn[0] = '\0';
207 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
208 fake_sms_db[i].vsub_attached = true;
209 fake_sms_db[i].nr_of_sms = 0;
210 fake_sms_db[i].failed_attempts = 0;
211 }
212 show_fake_sms_db();
213 for (i = 0; i < 3; i++) {
214 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
215 _test_take_next_sms_print(i, sms, last_msisdn);
216 OSMO_ASSERT(!sms);
217 }
218}
219
220
221static struct log_info_cat sms_queue_test_categories[] = {
222};
223
224static struct log_info info = {
225 .cat = sms_queue_test_categories,
226 .num_cat = ARRAY_SIZE(sms_queue_test_categories),
227};
228
229int main(int argc, char **argv)
230{
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100231 void *msgb_ctx;
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200232 void *logging_ctx;
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100233
Vadim Yanitskiy6c766c62019-03-30 15:31:47 +0700234 /* Track the use of talloc NULL memory contexts */
235 talloc_enable_null_tracking();
236
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100237 talloc_ctx = talloc_named_const(NULL, 0, "sms_queue_test");
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200238 msgb_ctx = msgb_talloc_ctx_init(talloc_ctx, 0);
239 logging_ctx = talloc_named_const(talloc_ctx, 0, "logging");
240 osmo_init_logging2(logging_ctx, &info);
Harald Welte2483f1b2016-06-19 18:06:02 +0200241
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700242 /* Share our talloc context with libmsc's GSM 04.11 code,
243 * so sms_alloc() would use it instead of NULL. */
244 tall_gsms_ctx = talloc_ctx;
245
Harald Welte2483f1b2016-06-19 18:06:02 +0200246 OSMO_ASSERT(osmo_stderr_target);
247 log_set_use_color(osmo_stderr_target, 0);
248 log_set_print_timestamp(osmo_stderr_target, 0);
Pau Espin Pedrolcad22fd2021-02-19 13:37:00 +0100249 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Harald Welte2483f1b2016-06-19 18:06:02 +0200250 log_set_print_category(osmo_stderr_target, 1);
251 log_parse_category_mask(osmo_stderr_target, "DLOAP,1");
252
253 test_next_sms();
254 printf("Done\n");
255
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100256 if (talloc_total_blocks(msgb_ctx) != 1
257 || talloc_total_size(msgb_ctx) != 0) {
258 talloc_report_full(msgb_ctx, stderr);
259 fflush(stderr);
260 }
261
262 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
263 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
264 talloc_free(msgb_ctx);
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200265 talloc_free(logging_ctx);
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100266
267 if (talloc_total_blocks(talloc_ctx) != 1
268 || talloc_total_size(talloc_ctx) != 0)
269 talloc_report_full(talloc_ctx, stderr);
270
271 OSMO_ASSERT(talloc_total_blocks(talloc_ctx) == 1);
272 OSMO_ASSERT(talloc_total_size(talloc_ctx) == 0);
273 talloc_free(talloc_ctx);
274
Vadim Yanitskiy6c766c62019-03-30 15:31:47 +0700275 talloc_report_full(NULL, stderr);
276 talloc_disable_null_tracking();
277
Harald Welte2483f1b2016-06-19 18:06:02 +0200278 return 0;
279}