blob: 495e078a3d8169113117ee5eb772ea0460aab97e [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;
Harald Welte2483f1b2016-06-19 18:06:02 +020099 int i;
100 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. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100113 osmo_use_count_get_put(&arbitrary_vsub.use_count, VSUB_USE_SMS_RECEIVER, 1000);
114 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;
116
Harald Welte2483f1b2016-06-19 18:06:02 +0200117 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
118 if (!fake_sms_db[i].nr_of_sms)
119 continue;
120 if (strcmp(fake_sms_db[i].msisdn, last_msisdn) <= 0)
121 continue;
122 if (fake_sms_db[i].failed_attempts > max_failed)
123 continue;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700124
125 sms = sms_alloc();
126 OSMO_ASSERT(sms);
127
128 osmo_strlcpy(sms->dst.addr, fake_sms_db[i].msisdn,
129 sizeof(sms->dst.addr));
130 sms->receiver = fake_sms_db[i].vsub_attached? &arbitrary_vsub : NULL;
131 osmo_strlcpy(sms->text, fake_sms_db[i].msisdn, sizeof(sms->text));
Harald Welte2483f1b2016-06-19 18:06:02 +0200132 if (fake_sms_db[i].vsub_attached)
Max5e2e9bd2018-02-06 19:31:08 +0100133 fake_sms_db[i].nr_of_sms--;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700134 return sms;
Harald Welte2483f1b2016-06-19 18:06:02 +0200135 }
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700136
Harald Welte2483f1b2016-06-19 18:06:02 +0200137 return NULL;
138}
139
140void show_fake_sms_db()
141{
142 int i;
143 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
144 printf(" %s%s has %u SMS pending, %u failed attempts\n",
145 fake_sms_db[i].msisdn,
146 fake_sms_db[i].vsub_attached ? "" : " (NOT attached)",
147 fake_sms_db[i].nr_of_sms,
148 fake_sms_db[i].failed_attempts);
149 }
150 printf("-->\n");
151}
152
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700153/* sms_free() is not safe against NULL */
154#define sms_free_safe(sms) \
155 if (sms != NULL) sms_free(sms)
156
Harald Welte2483f1b2016-06-19 18:06:02 +0200157static void test_next_sms()
158{
159 int i;
Neels Hofmeyr7b61ffe2018-11-30 02:46:53 +0100160 char last_msisdn[VLR_MSISDN_LENGTH+1] = "";
Harald Welte2483f1b2016-06-19 18:06:02 +0200161
162 printf("Testing smsq_take_next_sms()\n");
163
164 printf("\n- vsub 2, 3 and 5 each have 2 SMS pending, but 5 is not attached\n");
165 last_msisdn[0] = '\0';
166 show_fake_sms_db();
167 for (i = 0; i < 7; i++) {
168 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
169 _test_take_next_sms_print(i, sms, last_msisdn);
170 OSMO_ASSERT(i >= 4 || sms);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700171 sms_free_safe(sms);
Harald Welte2483f1b2016-06-19 18:06:02 +0200172 }
173
174 printf("\n- SMS are pending at various nr failed attempts (cutoff at >= 10)\n");
175 last_msisdn[0] = '\0';
176 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
177 fake_sms_db[i].vsub_attached = true;
178 fake_sms_db[i].nr_of_sms = 1 + i;
179 fake_sms_db[i].failed_attempts = i*5;
180
181 }
182 show_fake_sms_db();
183 for (i = 0; i < 7; i++) {
184 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
185 _test_take_next_sms_print(i, sms, last_msisdn);
186 OSMO_ASSERT(i >= 2 || sms);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700187 sms_free_safe(sms);
Harald Welte2483f1b2016-06-19 18:06:02 +0200188 }
189
190 printf("\n- iterate the SMS DB at most once\n");
191 osmo_strlcpy(last_msisdn, "2345", sizeof(last_msisdn));
192 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
193 fake_sms_db[i].vsub_attached = false;
194 fake_sms_db[i].nr_of_sms = 1;
195 fake_sms_db[i].failed_attempts = 0;
196 }
197 show_fake_sms_db();
198 for (i = 0; i < 3; i++) {
199 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
200 _test_take_next_sms_print(i, sms, last_msisdn);
201 OSMO_ASSERT(!sms);
202 }
203
204 printf("\n- there are no SMS in the DB\n");
205 last_msisdn[0] = '\0';
206 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
207 fake_sms_db[i].vsub_attached = true;
208 fake_sms_db[i].nr_of_sms = 0;
209 fake_sms_db[i].failed_attempts = 0;
210 }
211 show_fake_sms_db();
212 for (i = 0; i < 3; i++) {
213 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
214 _test_take_next_sms_print(i, sms, last_msisdn);
215 OSMO_ASSERT(!sms);
216 }
217}
218
219
220static struct log_info_cat sms_queue_test_categories[] = {
221};
222
223static struct log_info info = {
224 .cat = sms_queue_test_categories,
225 .num_cat = ARRAY_SIZE(sms_queue_test_categories),
226};
227
228int main(int argc, char **argv)
229{
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100230 void *msgb_ctx;
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200231 void *logging_ctx;
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100232
Vadim Yanitskiy6c766c62019-03-30 15:31:47 +0700233 /* Track the use of talloc NULL memory contexts */
234 talloc_enable_null_tracking();
235
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100236 talloc_ctx = talloc_named_const(NULL, 0, "sms_queue_test");
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200237 msgb_ctx = msgb_talloc_ctx_init(talloc_ctx, 0);
238 logging_ctx = talloc_named_const(talloc_ctx, 0, "logging");
239 osmo_init_logging2(logging_ctx, &info);
Harald Welte2483f1b2016-06-19 18:06:02 +0200240
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700241 /* Share our talloc context with libmsc's GSM 04.11 code,
242 * so sms_alloc() would use it instead of NULL. */
243 tall_gsms_ctx = talloc_ctx;
244
Harald Welte2483f1b2016-06-19 18:06:02 +0200245 OSMO_ASSERT(osmo_stderr_target);
246 log_set_use_color(osmo_stderr_target, 0);
247 log_set_print_timestamp(osmo_stderr_target, 0);
248 log_set_print_filename(osmo_stderr_target, 0);
249 log_set_print_category(osmo_stderr_target, 1);
250 log_parse_category_mask(osmo_stderr_target, "DLOAP,1");
251
252 test_next_sms();
253 printf("Done\n");
254
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100255 if (talloc_total_blocks(msgb_ctx) != 1
256 || talloc_total_size(msgb_ctx) != 0) {
257 talloc_report_full(msgb_ctx, stderr);
258 fflush(stderr);
259 }
260
261 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
262 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
263 talloc_free(msgb_ctx);
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200264 talloc_free(logging_ctx);
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100265
266 if (talloc_total_blocks(talloc_ctx) != 1
267 || talloc_total_size(talloc_ctx) != 0)
268 talloc_report_full(talloc_ctx, stderr);
269
270 OSMO_ASSERT(talloc_total_blocks(talloc_ctx) == 1);
271 OSMO_ASSERT(talloc_total_size(talloc_ctx) == 0);
272 talloc_free(talloc_ctx);
273
Vadim Yanitskiy6c766c62019-03-30 15:31:47 +0700274 talloc_report_full(NULL, stderr);
275 talloc_disable_null_tracking();
276
Harald Welte2483f1b2016-06-19 18:06:02 +0200277 return 0;
278}
Harald Welte0df904d2018-12-03 11:00:04 +0100279
280void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
281struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
282void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
283struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
284 int fd, int (*cb)(struct osmo_stream_srv *conn),
285 int (*closed_cb)(struct osmo_stream_srv *conn),
286 void *data) { return NULL; }
287void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
288void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
289struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
290struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
291void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
292void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
293void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
294 (struct osmo_stream_srv_link *link, int fd)) {}
295int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
296void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
297void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
298void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link, const char *addr) {}
299int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }