blob: f64f715dbc478be7d68ac38919118e9f5ae6f39a [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>
Harald Welte2483f1b2016-06-19 18:06:02 +020030
31static void *talloc_ctx = NULL;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +070032extern void *tall_gsms_ctx;
Harald Welte2483f1b2016-06-19 18:06:02 +020033
34struct gsm_sms *smsq_take_next_sms(struct gsm_network *net,
35 char *last_msisdn,
36 size_t last_msisdn_buflen);
37
38static void _test_take_next_sms_print(int i,
39 struct gsm_sms *sms,
40 const char *last_msisdn)
41{
42 printf("#%d: ", i);
43 if (sms)
44 printf("sending SMS to %s", sms->text);
45 else
46 printf("no SMS to send");
47 printf(" (last_msisdn='%s')\n", last_msisdn? last_msisdn : "NULL");
48}
49
Harald Welte2483f1b2016-06-19 18:06:02 +020050struct {
51 const char *msisdn;
52 int nr_of_sms;
53 int failed_attempts;
54 bool vsub_attached;
55} fake_sms_db[] = {
56 {
57 .msisdn = "1111",
58 .nr_of_sms = 0,
59 .vsub_attached = true,
60 },
61 {
62 .msisdn = "2222",
63 .nr_of_sms = 2,
64 .failed_attempts = 2,
65 .vsub_attached = true,
66 },
67 {
68 .msisdn = "3333",
69 .nr_of_sms = 2,
70 .failed_attempts = 3,
71 .vsub_attached = true,
72 },
73 {
74 .msisdn = "4444",
75 .nr_of_sms = 0,
76 .vsub_attached = true,
77 },
78 {
79 .msisdn = "5555",
80 .nr_of_sms = 2,
81 .failed_attempts = 5,
82 .vsub_attached = false,
83 },
84};
85
86/* override, requires '-Wl,--wrap=db_sms_get_next_unsent_rr_msisdn' */
87struct gsm_sms *__real_db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net,
88 const char *last_msisdn,
89 unsigned int max_failed);
90struct gsm_sms *__wrap_db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net,
91 const char *last_msisdn,
92 unsigned int max_failed)
93{
Vadim Yanitskiy96262a72019-03-28 21:25:14 +070094 static struct vlr_subscr arbitrary_vsub;
95 struct gsm_sms *sms;
Harald Welte2483f1b2016-06-19 18:06:02 +020096 int i;
97 printf(" hitting database: looking for MSISDN > '%s', failed_attempts <= %d\n",
98 last_msisdn, max_failed);
99
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700100 /* Every time we call sms_free(), the internal logic of libmsc
101 * may call vlr_subscr_put() on our arbitrary_vsub, what would
102 * lead to a segfault if its use_count <= 0. To prevent this,
103 * let's ensure a big enough initial value. */
104 arbitrary_vsub.use_count = 1000;
105 arbitrary_vsub.lu_complete = true;
106
Harald Welte2483f1b2016-06-19 18:06:02 +0200107 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
108 if (!fake_sms_db[i].nr_of_sms)
109 continue;
110 if (strcmp(fake_sms_db[i].msisdn, last_msisdn) <= 0)
111 continue;
112 if (fake_sms_db[i].failed_attempts > max_failed)
113 continue;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700114
115 sms = sms_alloc();
116 OSMO_ASSERT(sms);
117
118 osmo_strlcpy(sms->dst.addr, fake_sms_db[i].msisdn,
119 sizeof(sms->dst.addr));
120 sms->receiver = fake_sms_db[i].vsub_attached? &arbitrary_vsub : NULL;
121 osmo_strlcpy(sms->text, fake_sms_db[i].msisdn, sizeof(sms->text));
Harald Welte2483f1b2016-06-19 18:06:02 +0200122 if (fake_sms_db[i].vsub_attached)
Max5e2e9bd2018-02-06 19:31:08 +0100123 fake_sms_db[i].nr_of_sms--;
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700124 return sms;
Harald Welte2483f1b2016-06-19 18:06:02 +0200125 }
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700126
Harald Welte2483f1b2016-06-19 18:06:02 +0200127 return NULL;
128}
129
130void show_fake_sms_db()
131{
132 int i;
133 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
134 printf(" %s%s has %u SMS pending, %u failed attempts\n",
135 fake_sms_db[i].msisdn,
136 fake_sms_db[i].vsub_attached ? "" : " (NOT attached)",
137 fake_sms_db[i].nr_of_sms,
138 fake_sms_db[i].failed_attempts);
139 }
140 printf("-->\n");
141}
142
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700143/* sms_free() is not safe against NULL */
144#define sms_free_safe(sms) \
145 if (sms != NULL) sms_free(sms)
146
Harald Welte2483f1b2016-06-19 18:06:02 +0200147static void test_next_sms()
148{
149 int i;
Neels Hofmeyr7b61ffe2018-11-30 02:46:53 +0100150 char last_msisdn[VLR_MSISDN_LENGTH+1] = "";
Harald Welte2483f1b2016-06-19 18:06:02 +0200151
152 printf("Testing smsq_take_next_sms()\n");
153
154 printf("\n- vsub 2, 3 and 5 each have 2 SMS pending, but 5 is not attached\n");
155 last_msisdn[0] = '\0';
156 show_fake_sms_db();
157 for (i = 0; i < 7; i++) {
158 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
159 _test_take_next_sms_print(i, sms, last_msisdn);
160 OSMO_ASSERT(i >= 4 || sms);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700161 sms_free_safe(sms);
Harald Welte2483f1b2016-06-19 18:06:02 +0200162 }
163
164 printf("\n- SMS are pending at various nr failed attempts (cutoff at >= 10)\n");
165 last_msisdn[0] = '\0';
166 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
167 fake_sms_db[i].vsub_attached = true;
168 fake_sms_db[i].nr_of_sms = 1 + i;
169 fake_sms_db[i].failed_attempts = i*5;
170
171 }
172 show_fake_sms_db();
173 for (i = 0; i < 7; i++) {
174 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
175 _test_take_next_sms_print(i, sms, last_msisdn);
176 OSMO_ASSERT(i >= 2 || sms);
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700177 sms_free_safe(sms);
Harald Welte2483f1b2016-06-19 18:06:02 +0200178 }
179
180 printf("\n- iterate the SMS DB at most once\n");
181 osmo_strlcpy(last_msisdn, "2345", sizeof(last_msisdn));
182 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
183 fake_sms_db[i].vsub_attached = false;
184 fake_sms_db[i].nr_of_sms = 1;
185 fake_sms_db[i].failed_attempts = 0;
186 }
187 show_fake_sms_db();
188 for (i = 0; i < 3; i++) {
189 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
190 _test_take_next_sms_print(i, sms, last_msisdn);
191 OSMO_ASSERT(!sms);
192 }
193
194 printf("\n- there are no SMS in the DB\n");
195 last_msisdn[0] = '\0';
196 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
197 fake_sms_db[i].vsub_attached = true;
198 fake_sms_db[i].nr_of_sms = 0;
199 fake_sms_db[i].failed_attempts = 0;
200 }
201 show_fake_sms_db();
202 for (i = 0; i < 3; i++) {
203 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
204 _test_take_next_sms_print(i, sms, last_msisdn);
205 OSMO_ASSERT(!sms);
206 }
207}
208
209
210static struct log_info_cat sms_queue_test_categories[] = {
211};
212
213static struct log_info info = {
214 .cat = sms_queue_test_categories,
215 .num_cat = ARRAY_SIZE(sms_queue_test_categories),
216};
217
218int main(int argc, char **argv)
219{
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100220 void *msgb_ctx;
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200221 void *logging_ctx;
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100222
223 talloc_ctx = talloc_named_const(NULL, 0, "sms_queue_test");
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200224 msgb_ctx = msgb_talloc_ctx_init(talloc_ctx, 0);
225 logging_ctx = talloc_named_const(talloc_ctx, 0, "logging");
226 osmo_init_logging2(logging_ctx, &info);
Harald Welte2483f1b2016-06-19 18:06:02 +0200227
Vadim Yanitskiy96262a72019-03-28 21:25:14 +0700228 /* Share our talloc context with libmsc's GSM 04.11 code,
229 * so sms_alloc() would use it instead of NULL. */
230 tall_gsms_ctx = talloc_ctx;
231
Harald Welte2483f1b2016-06-19 18:06:02 +0200232 OSMO_ASSERT(osmo_stderr_target);
233 log_set_use_color(osmo_stderr_target, 0);
234 log_set_print_timestamp(osmo_stderr_target, 0);
235 log_set_print_filename(osmo_stderr_target, 0);
236 log_set_print_category(osmo_stderr_target, 1);
237 log_parse_category_mask(osmo_stderr_target, "DLOAP,1");
238
239 test_next_sms();
240 printf("Done\n");
241
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100242 if (talloc_total_blocks(msgb_ctx) != 1
243 || talloc_total_size(msgb_ctx) != 0) {
244 talloc_report_full(msgb_ctx, stderr);
245 fflush(stderr);
246 }
247
248 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
249 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
250 talloc_free(msgb_ctx);
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200251 talloc_free(logging_ctx);
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100252
253 if (talloc_total_blocks(talloc_ctx) != 1
254 || talloc_total_size(talloc_ctx) != 0)
255 talloc_report_full(talloc_ctx, stderr);
256
257 OSMO_ASSERT(talloc_total_blocks(talloc_ctx) == 1);
258 OSMO_ASSERT(talloc_total_size(talloc_ctx) == 0);
259 talloc_free(talloc_ctx);
260
Harald Welte2483f1b2016-06-19 18:06:02 +0200261 return 0;
262}
Harald Welte0df904d2018-12-03 11:00:04 +0100263
264void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
265struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
266void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
267struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
268 int fd, int (*cb)(struct osmo_stream_srv *conn),
269 int (*closed_cb)(struct osmo_stream_srv *conn),
270 void *data) { return NULL; }
271void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
272void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
273struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
274struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
275void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
276void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
277void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
278 (struct osmo_stream_srv_link *link, int fd)) {}
279int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
280void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
281void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
282void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link, const char *addr) {}
283int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }