Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 1 | /* 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 Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 26 | #include <osmocom/msc/debug.h> |
| 27 | #include <osmocom/msc/vlr.h> |
Neels Hofmeyr | 7b61ffe | 2018-11-30 02:46:53 +0100 | [diff] [blame] | 28 | #include <osmocom/msc/gsm_data.h> |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 29 | #include <osmocom/msc/gsm_04_11.h> |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 30 | #include <osmocom/msc/db.h> |
| 31 | #include <osmocom/msc/sms_queue.h> |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 32 | |
| 33 | static void *talloc_ctx = NULL; |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 34 | extern void *tall_gsms_ctx; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 35 | |
| 36 | struct gsm_sms *smsq_take_next_sms(struct gsm_network *net, |
| 37 | char *last_msisdn, |
| 38 | size_t last_msisdn_buflen); |
| 39 | |
| 40 | static 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 52 | struct { |
| 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' */ |
| 89 | struct gsm_sms *__real_db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net, |
| 90 | const char *last_msisdn, |
| 91 | unsigned int max_failed); |
| 92 | struct 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 Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 96 | static struct vlr_subscr arbitrary_vsub = {}; |
| 97 | static bool arbitrary_vsub_set_up = false; |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 98 | struct gsm_sms *sms; |
Vadim Yanitskiy | 678354f | 2019-05-11 03:10:22 +0700 | [diff] [blame] | 99 | int i, rc = 0; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 100 | printf(" hitting database: looking for MSISDN > '%s', failed_attempts <= %d\n", |
| 101 | last_msisdn, max_failed); |
| 102 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 103 | 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 109 | /* 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 Yanitskiy | 678354f | 2019-05-11 03:10:22 +0700 | [diff] [blame] | 113 | 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 115 | arbitrary_vsub.lu_complete = true; |
Vadim Yanitskiy | 678354f | 2019-05-11 03:10:22 +0700 | [diff] [blame] | 116 | OSMO_ASSERT(rc == 0); |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 117 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 118 | 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 125 | |
| 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 133 | if (fake_sms_db[i].vsub_attached) |
Max | 5e2e9bd | 2018-02-06 19:31:08 +0100 | [diff] [blame] | 134 | fake_sms_db[i].nr_of_sms--; |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 135 | return sms; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 136 | } |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 137 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | void 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 154 | /* sms_free() is not safe against NULL */ |
| 155 | #define sms_free_safe(sms) \ |
| 156 | if (sms != NULL) sms_free(sms) |
| 157 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 158 | static void test_next_sms() |
| 159 | { |
| 160 | int i; |
Vadim Yanitskiy | 8b0737f | 2019-05-25 19:27:17 +0700 | [diff] [blame] | 161 | char last_msisdn[GSM23003_MSISDN_MAX_DIGITS+1] = ""; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 162 | |
| 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 172 | sms_free_safe(sms); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 173 | } |
| 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 188 | sms_free_safe(sms); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 189 | } |
| 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 | |
| 221 | static struct log_info_cat sms_queue_test_categories[] = { |
| 222 | }; |
| 223 | |
| 224 | static struct log_info info = { |
| 225 | .cat = sms_queue_test_categories, |
| 226 | .num_cat = ARRAY_SIZE(sms_queue_test_categories), |
| 227 | }; |
| 228 | |
| 229 | int main(int argc, char **argv) |
| 230 | { |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 231 | void *msgb_ctx; |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 232 | void *logging_ctx; |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 233 | |
Vadim Yanitskiy | 6c766c6 | 2019-03-30 15:31:47 +0700 | [diff] [blame] | 234 | /* Track the use of talloc NULL memory contexts */ |
| 235 | talloc_enable_null_tracking(); |
| 236 | |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 237 | talloc_ctx = talloc_named_const(NULL, 0, "sms_queue_test"); |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 238 | 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 241 | |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 242 | /* 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 246 | OSMO_ASSERT(osmo_stderr_target); |
| 247 | log_set_use_color(osmo_stderr_target, 0); |
| 248 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 249 | log_set_print_filename(osmo_stderr_target, 0); |
| 250 | 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 Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 256 | 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 Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 265 | talloc_free(logging_ctx); |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 266 | |
| 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 Yanitskiy | 6c766c6 | 2019-03-30 15:31:47 +0700 | [diff] [blame] | 275 | talloc_report_full(NULL, stderr); |
| 276 | talloc_disable_null_tracking(); |
| 277 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 278 | return 0; |
| 279 | } |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 280 | |
| 281 | void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {} |
| 282 | struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; } |
| 283 | void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {} |
| 284 | struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link, |
| 285 | int fd, int (*cb)(struct osmo_stream_srv *conn), |
| 286 | int (*closed_cb)(struct osmo_stream_srv *conn), |
| 287 | void *data) { return NULL; } |
| 288 | void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {} |
| 289 | void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {} |
| 290 | struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; } |
| 291 | struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; } |
| 292 | void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; } |
| 293 | void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {} |
| 294 | void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb) |
| 295 | (struct osmo_stream_srv_link *link, int fd)) {} |
| 296 | int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; } |
| 297 | void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; } |
| 298 | void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {} |
| 299 | void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link, const char *addr) {} |
| 300 | int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; } |