blob: e4263778aa1872df0b52667e3f18a94a3ec8fe90 [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>
Harald Welte2483f1b2016-06-19 18:06:02 +020029
30static void *talloc_ctx = NULL;
31
32struct gsm_sms *smsq_take_next_sms(struct gsm_network *net,
33 char *last_msisdn,
34 size_t last_msisdn_buflen);
35
36static void _test_take_next_sms_print(int i,
37 struct gsm_sms *sms,
38 const char *last_msisdn)
39{
40 printf("#%d: ", i);
41 if (sms)
42 printf("sending SMS to %s", sms->text);
43 else
44 printf("no SMS to send");
45 printf(" (last_msisdn='%s')\n", last_msisdn? last_msisdn : "NULL");
46}
47
48static struct gsm_sms fake_sms = { 0 };
49
50struct {
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{
94 static struct vlr_subscr arbitrary_vsub = { .lu_complete = true };
95 int i;
96 printf(" hitting database: looking for MSISDN > '%s', failed_attempts <= %d\n",
97 last_msisdn, max_failed);
98
99 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
100 if (!fake_sms_db[i].nr_of_sms)
101 continue;
102 if (strcmp(fake_sms_db[i].msisdn, last_msisdn) <= 0)
103 continue;
104 if (fake_sms_db[i].failed_attempts > max_failed)
105 continue;
106 osmo_strlcpy(fake_sms.dst.addr, fake_sms_db[i].msisdn,
107 sizeof(fake_sms.dst.addr));
108 fake_sms.receiver = fake_sms_db[i].vsub_attached? &arbitrary_vsub : NULL;
109 osmo_strlcpy(fake_sms.text, fake_sms_db[i].msisdn, sizeof(fake_sms.text));
110 if (fake_sms_db[i].vsub_attached)
Max5e2e9bd2018-02-06 19:31:08 +0100111 fake_sms_db[i].nr_of_sms--;
Harald Welte2483f1b2016-06-19 18:06:02 +0200112 return &fake_sms;
113 }
114 return NULL;
115}
116
117void show_fake_sms_db()
118{
119 int i;
120 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
121 printf(" %s%s has %u SMS pending, %u failed attempts\n",
122 fake_sms_db[i].msisdn,
123 fake_sms_db[i].vsub_attached ? "" : " (NOT attached)",
124 fake_sms_db[i].nr_of_sms,
125 fake_sms_db[i].failed_attempts);
126 }
127 printf("-->\n");
128}
129
130static void test_next_sms()
131{
132 int i;
Neels Hofmeyr7b61ffe2018-11-30 02:46:53 +0100133 char last_msisdn[VLR_MSISDN_LENGTH+1] = "";
Harald Welte2483f1b2016-06-19 18:06:02 +0200134
135 printf("Testing smsq_take_next_sms()\n");
136
137 printf("\n- vsub 2, 3 and 5 each have 2 SMS pending, but 5 is not attached\n");
138 last_msisdn[0] = '\0';
139 show_fake_sms_db();
140 for (i = 0; i < 7; i++) {
141 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
142 _test_take_next_sms_print(i, sms, last_msisdn);
143 OSMO_ASSERT(i >= 4 || sms);
144 }
145
146 printf("\n- SMS are pending at various nr failed attempts (cutoff at >= 10)\n");
147 last_msisdn[0] = '\0';
148 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
149 fake_sms_db[i].vsub_attached = true;
150 fake_sms_db[i].nr_of_sms = 1 + i;
151 fake_sms_db[i].failed_attempts = i*5;
152
153 }
154 show_fake_sms_db();
155 for (i = 0; i < 7; i++) {
156 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
157 _test_take_next_sms_print(i, sms, last_msisdn);
158 OSMO_ASSERT(i >= 2 || sms);
159 }
160
161 printf("\n- iterate the SMS DB at most once\n");
162 osmo_strlcpy(last_msisdn, "2345", sizeof(last_msisdn));
163 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
164 fake_sms_db[i].vsub_attached = false;
165 fake_sms_db[i].nr_of_sms = 1;
166 fake_sms_db[i].failed_attempts = 0;
167 }
168 show_fake_sms_db();
169 for (i = 0; i < 3; i++) {
170 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
171 _test_take_next_sms_print(i, sms, last_msisdn);
172 OSMO_ASSERT(!sms);
173 }
174
175 printf("\n- there are no SMS in the DB\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 = 0;
180 fake_sms_db[i].failed_attempts = 0;
181 }
182 show_fake_sms_db();
183 for (i = 0; i < 3; 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(!sms);
187 }
188}
189
190
191static struct log_info_cat sms_queue_test_categories[] = {
192};
193
194static struct log_info info = {
195 .cat = sms_queue_test_categories,
196 .num_cat = ARRAY_SIZE(sms_queue_test_categories),
197};
198
199int main(int argc, char **argv)
200{
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100201 void *msgb_ctx;
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200202 void *logging_ctx;
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100203
204 talloc_ctx = talloc_named_const(NULL, 0, "sms_queue_test");
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200205 msgb_ctx = msgb_talloc_ctx_init(talloc_ctx, 0);
206 logging_ctx = talloc_named_const(talloc_ctx, 0, "logging");
207 osmo_init_logging2(logging_ctx, &info);
Harald Welte2483f1b2016-06-19 18:06:02 +0200208
209 OSMO_ASSERT(osmo_stderr_target);
210 log_set_use_color(osmo_stderr_target, 0);
211 log_set_print_timestamp(osmo_stderr_target, 0);
212 log_set_print_filename(osmo_stderr_target, 0);
213 log_set_print_category(osmo_stderr_target, 1);
214 log_parse_category_mask(osmo_stderr_target, "DLOAP,1");
215
216 test_next_sms();
217 printf("Done\n");
218
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100219 if (talloc_total_blocks(msgb_ctx) != 1
220 || talloc_total_size(msgb_ctx) != 0) {
221 talloc_report_full(msgb_ctx, stderr);
222 fflush(stderr);
223 }
224
225 OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
226 OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
227 talloc_free(msgb_ctx);
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200228 talloc_free(logging_ctx);
Neels Hofmeyr0442ea22017-11-21 23:01:39 +0100229
230 if (talloc_total_blocks(talloc_ctx) != 1
231 || talloc_total_size(talloc_ctx) != 0)
232 talloc_report_full(talloc_ctx, stderr);
233
234 OSMO_ASSERT(talloc_total_blocks(talloc_ctx) == 1);
235 OSMO_ASSERT(talloc_total_size(talloc_ctx) == 0);
236 talloc_free(talloc_ctx);
237
Harald Welte2483f1b2016-06-19 18:06:02 +0200238 return 0;
239}
Harald Welte0df904d2018-12-03 11:00:04 +0100240
241void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {}
242struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; }
243void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {}
244struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link,
245 int fd, int (*cb)(struct osmo_stream_srv *conn),
246 int (*closed_cb)(struct osmo_stream_srv *conn),
247 void *data) { return NULL; }
248void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {}
249void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {}
250struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; }
251struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; }
252void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; }
253void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {}
254void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb)
255 (struct osmo_stream_srv_link *link, int fd)) {}
256int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; }
257void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; }
258void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {}
259void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link, const char *addr) {}
260int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; }