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> |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 30 | |
| 31 | static void *talloc_ctx = NULL; |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 32 | extern void *tall_gsms_ctx; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 33 | |
| 34 | struct gsm_sms *smsq_take_next_sms(struct gsm_network *net, |
| 35 | char *last_msisdn, |
| 36 | size_t last_msisdn_buflen); |
| 37 | |
| 38 | static 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 50 | struct { |
| 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' */ |
| 87 | struct gsm_sms *__real_db_sms_get_next_unsent_rr_msisdn(struct gsm_network *net, |
| 88 | const char *last_msisdn, |
| 89 | unsigned int max_failed); |
| 90 | struct 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 94 | static struct vlr_subscr arbitrary_vsub; |
| 95 | struct gsm_sms *sms; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 96 | int i; |
| 97 | printf(" hitting database: looking for MSISDN > '%s', failed_attempts <= %d\n", |
| 98 | last_msisdn, max_failed); |
| 99 | |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 100 | /* 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 107 | 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 114 | |
| 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 122 | if (fake_sms_db[i].vsub_attached) |
Max | 5e2e9bd | 2018-02-06 19:31:08 +0100 | [diff] [blame] | 123 | fake_sms_db[i].nr_of_sms--; |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 124 | return sms; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 125 | } |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 126 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | void 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 143 | /* sms_free() is not safe against NULL */ |
| 144 | #define sms_free_safe(sms) \ |
| 145 | if (sms != NULL) sms_free(sms) |
| 146 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 147 | static void test_next_sms() |
| 148 | { |
| 149 | int i; |
Neels Hofmeyr | 7b61ffe | 2018-11-30 02:46:53 +0100 | [diff] [blame] | 150 | char last_msisdn[VLR_MSISDN_LENGTH+1] = ""; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 151 | |
| 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 161 | sms_free_safe(sms); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 162 | } |
| 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 Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 177 | sms_free_safe(sms); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 178 | } |
| 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 | |
| 210 | static struct log_info_cat sms_queue_test_categories[] = { |
| 211 | }; |
| 212 | |
| 213 | static struct log_info info = { |
| 214 | .cat = sms_queue_test_categories, |
| 215 | .num_cat = ARRAY_SIZE(sms_queue_test_categories), |
| 216 | }; |
| 217 | |
| 218 | int main(int argc, char **argv) |
| 219 | { |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 220 | void *msgb_ctx; |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 221 | void *logging_ctx; |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 222 | |
Vadim Yanitskiy | 6c766c6 | 2019-03-30 15:31:47 +0700 | [diff] [blame] | 223 | /* Track the use of talloc NULL memory contexts */ |
| 224 | talloc_enable_null_tracking(); |
| 225 | |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 226 | talloc_ctx = talloc_named_const(NULL, 0, "sms_queue_test"); |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 227 | msgb_ctx = msgb_talloc_ctx_init(talloc_ctx, 0); |
| 228 | logging_ctx = talloc_named_const(talloc_ctx, 0, "logging"); |
| 229 | osmo_init_logging2(logging_ctx, &info); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 230 | |
Vadim Yanitskiy | 96262a7 | 2019-03-28 21:25:14 +0700 | [diff] [blame] | 231 | /* Share our talloc context with libmsc's GSM 04.11 code, |
| 232 | * so sms_alloc() would use it instead of NULL. */ |
| 233 | tall_gsms_ctx = talloc_ctx; |
| 234 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 235 | OSMO_ASSERT(osmo_stderr_target); |
| 236 | log_set_use_color(osmo_stderr_target, 0); |
| 237 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 238 | log_set_print_filename(osmo_stderr_target, 0); |
| 239 | log_set_print_category(osmo_stderr_target, 1); |
| 240 | log_parse_category_mask(osmo_stderr_target, "DLOAP,1"); |
| 241 | |
| 242 | test_next_sms(); |
| 243 | printf("Done\n"); |
| 244 | |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 245 | if (talloc_total_blocks(msgb_ctx) != 1 |
| 246 | || talloc_total_size(msgb_ctx) != 0) { |
| 247 | talloc_report_full(msgb_ctx, stderr); |
| 248 | fflush(stderr); |
| 249 | } |
| 250 | |
| 251 | OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1); |
| 252 | OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0); |
| 253 | talloc_free(msgb_ctx); |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 254 | talloc_free(logging_ctx); |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 255 | |
| 256 | if (talloc_total_blocks(talloc_ctx) != 1 |
| 257 | || talloc_total_size(talloc_ctx) != 0) |
| 258 | talloc_report_full(talloc_ctx, stderr); |
| 259 | |
| 260 | OSMO_ASSERT(talloc_total_blocks(talloc_ctx) == 1); |
| 261 | OSMO_ASSERT(talloc_total_size(talloc_ctx) == 0); |
| 262 | talloc_free(talloc_ctx); |
| 263 | |
Vadim Yanitskiy | 6c766c6 | 2019-03-30 15:31:47 +0700 | [diff] [blame] | 264 | talloc_report_full(NULL, stderr); |
| 265 | talloc_disable_null_tracking(); |
| 266 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 267 | return 0; |
| 268 | } |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 269 | |
| 270 | void osmo_stream_srv_link_set_data(struct osmo_stream_srv_link *link, void *data) {} |
| 271 | struct osmo_fd *osmo_stream_srv_get_ofd(struct osmo_stream_srv *srv) { return NULL; } |
| 272 | void osmo_stream_srv_destroy(struct osmo_stream_srv *conn) {} |
| 273 | struct osmo_stream_srv *osmo_stream_srv_create(void *ctx, struct osmo_stream_srv_link *link, |
| 274 | int fd, int (*cb)(struct osmo_stream_srv *conn), |
| 275 | int (*closed_cb)(struct osmo_stream_srv *conn), |
| 276 | void *data) { return NULL; } |
| 277 | void osmo_stream_srv_send(struct osmo_stream_srv *conn, struct msgb *msg) {} |
| 278 | void osmo_stream_srv_link_set_proto(struct osmo_stream_srv_link *link, uint16_t proto) {} |
| 279 | struct osmo_fd *osmo_stream_srv_link_get_ofd(struct osmo_stream_srv_link *link) { return NULL; } |
| 280 | struct osmo_stream_srv_link *osmo_stream_srv_link_create(void *ctx) { return NULL; } |
| 281 | void *osmo_stream_srv_get_data(struct osmo_stream_srv *conn) { return NULL; } |
| 282 | void osmo_stream_srv_link_set_nodelay(struct osmo_stream_srv_link *link, bool nodelay) {} |
| 283 | void osmo_stream_srv_link_set_accept_cb(struct osmo_stream_srv_link *link, int (*accept_cb) |
| 284 | (struct osmo_stream_srv_link *link, int fd)) {} |
| 285 | int osmo_stream_srv_link_open(struct osmo_stream_srv_link *link) { return 0; } |
| 286 | void *osmo_stream_srv_link_get_data(struct osmo_stream_srv_link *link) { return NULL; } |
| 287 | void osmo_stream_srv_link_set_port(struct osmo_stream_srv_link *link, uint16_t port) {} |
| 288 | void osmo_stream_srv_link_set_addr(struct osmo_stream_srv_link *link, const char *addr) {} |
| 289 | int sctp_recvmsg(int sd, void *msg, size_t len, void *from, void *fromlen, void *info, int *msg_flags) { return 0; } |