blob: af25b0645ba286164d136ac18eeaedca3ef6c350 [file] [log] [blame]
Harald Welte7b423ed2016-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
26#include <openbsc/debug.h>
27#include <openbsc/vlr.h>
28
29static void *talloc_ctx = NULL;
30
31struct gsm_sms *smsq_take_next_sms(struct gsm_network *net,
32 char *last_msisdn,
33 size_t last_msisdn_buflen);
34
35static void _test_take_next_sms_print(int i,
36 struct gsm_sms *sms,
37 const char *last_msisdn)
38{
39 printf("#%d: ", i);
40 if (sms)
41 printf("sending SMS to %s", sms->text);
42 else
43 printf("no SMS to send");
44 printf(" (last_msisdn='%s')\n", last_msisdn? last_msisdn : "NULL");
45}
46
47static struct gsm_sms fake_sms = { 0 };
48
49struct {
50 const char *msisdn;
51 int nr_of_sms;
52 int failed_attempts;
53 bool vsub_attached;
54} fake_sms_db[] = {
55 {
56 .msisdn = "1111",
57 .nr_of_sms = 0,
58 .vsub_attached = true,
59 },
60 {
61 .msisdn = "2222",
62 .nr_of_sms = 2,
63 .failed_attempts = 2,
64 .vsub_attached = true,
65 },
66 {
67 .msisdn = "3333",
68 .nr_of_sms = 2,
69 .failed_attempts = 3,
70 .vsub_attached = true,
71 },
72 {
73 .msisdn = "4444",
74 .nr_of_sms = 0,
75 .vsub_attached = true,
76 },
77 {
78 .msisdn = "5555",
79 .nr_of_sms = 2,
80 .failed_attempts = 5,
81 .vsub_attached = false,
82 },
83};
84
85/* override, requires '-Wl,--wrap=db_sms_get_next_unsent_rr_msisdn' */
86struct gsm_sms *__real_db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net,
87 const char *last_msisdn,
88 unsigned int max_failed);
89struct gsm_sms *__wrap_db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net,
90 const char *last_msisdn,
91 unsigned int max_failed)
92{
93 static struct vlr_subscr arbitrary_vsub = { .lu_complete = true };
94 int i;
95 printf(" hitting database: looking for MSISDN > '%s', failed_attempts <= %d\n",
96 last_msisdn, max_failed);
97
98 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
99 if (!fake_sms_db[i].nr_of_sms)
100 continue;
101 if (strcmp(fake_sms_db[i].msisdn, last_msisdn) <= 0)
102 continue;
103 if (fake_sms_db[i].failed_attempts > max_failed)
104 continue;
105 osmo_strlcpy(fake_sms.dst.addr, fake_sms_db[i].msisdn,
106 sizeof(fake_sms.dst.addr));
107 fake_sms.receiver = fake_sms_db[i].vsub_attached? &arbitrary_vsub : NULL;
108 osmo_strlcpy(fake_sms.text, fake_sms_db[i].msisdn, sizeof(fake_sms.text));
109 if (fake_sms_db[i].vsub_attached)
110 fake_sms_db[i].nr_of_sms --;
111 return &fake_sms;
112 }
113 return NULL;
114}
115
116void show_fake_sms_db()
117{
118 int i;
119 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
120 printf(" %s%s has %u SMS pending, %u failed attempts\n",
121 fake_sms_db[i].msisdn,
122 fake_sms_db[i].vsub_attached ? "" : " (NOT attached)",
123 fake_sms_db[i].nr_of_sms,
124 fake_sms_db[i].failed_attempts);
125 }
126 printf("-->\n");
127}
128
129static void test_next_sms()
130{
131 int i;
132 char last_msisdn[GSM_EXTENSION_LENGTH+1] = "";
133
134 printf("Testing smsq_take_next_sms()\n");
135
136 printf("\n- vsub 2, 3 and 5 each have 2 SMS pending, but 5 is not attached\n");
137 last_msisdn[0] = '\0';
138 show_fake_sms_db();
139 for (i = 0; i < 7; i++) {
140 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
141 _test_take_next_sms_print(i, sms, last_msisdn);
142 OSMO_ASSERT(i >= 4 || sms);
143 }
144
145 printf("\n- SMS are pending at various nr failed attempts (cutoff at >= 10)\n");
146 last_msisdn[0] = '\0';
147 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
148 fake_sms_db[i].vsub_attached = true;
149 fake_sms_db[i].nr_of_sms = 1 + i;
150 fake_sms_db[i].failed_attempts = i*5;
151
152 }
153 show_fake_sms_db();
154 for (i = 0; i < 7; i++) {
155 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
156 _test_take_next_sms_print(i, sms, last_msisdn);
157 OSMO_ASSERT(i >= 2 || sms);
158 }
159
160 printf("\n- iterate the SMS DB at most once\n");
161 osmo_strlcpy(last_msisdn, "2345", sizeof(last_msisdn));
162 for (i = 0; i < ARRAY_SIZE(fake_sms_db); i++) {
163 fake_sms_db[i].vsub_attached = false;
164 fake_sms_db[i].nr_of_sms = 1;
165 fake_sms_db[i].failed_attempts = 0;
166 }
167 show_fake_sms_db();
168 for (i = 0; i < 3; 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(!sms);
172 }
173
174 printf("\n- there are no SMS in the DB\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 = 0;
179 fake_sms_db[i].failed_attempts = 0;
180 }
181 show_fake_sms_db();
182 for (i = 0; i < 3; i++) {
183 struct gsm_sms *sms = smsq_take_next_sms(NULL, last_msisdn, sizeof(last_msisdn));
184 _test_take_next_sms_print(i, sms, last_msisdn);
185 OSMO_ASSERT(!sms);
186 }
187}
188
189
190static struct log_info_cat sms_queue_test_categories[] = {
191};
192
193static struct log_info info = {
194 .cat = sms_queue_test_categories,
195 .num_cat = ARRAY_SIZE(sms_queue_test_categories),
196};
197
198int main(int argc, char **argv)
199{
200 talloc_ctx = talloc_named_const(NULL, 1, "sms_queue_test");
201 msgb_talloc_ctx_init(talloc_ctx, 0);
202 osmo_init_logging(&info);
203
204 OSMO_ASSERT(osmo_stderr_target);
205 log_set_use_color(osmo_stderr_target, 0);
206 log_set_print_timestamp(osmo_stderr_target, 0);
207 log_set_print_filename(osmo_stderr_target, 0);
208 log_set_print_category(osmo_stderr_target, 1);
209 log_parse_category_mask(osmo_stderr_target, "DLOAP,1");
210
211 test_next_sms();
212 printf("Done\n");
213
214 return 0;
215}