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> |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 29 | |
| 30 | static void *talloc_ctx = NULL; |
| 31 | |
| 32 | struct gsm_sms *smsq_take_next_sms(struct gsm_network *net, |
| 33 | char *last_msisdn, |
| 34 | size_t last_msisdn_buflen); |
| 35 | |
| 36 | static 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 | |
| 48 | static struct gsm_sms fake_sms = { 0 }; |
| 49 | |
| 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 | { |
| 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) |
Max | 5e2e9bd | 2018-02-06 19:31:08 +0100 | [diff] [blame] | 111 | fake_sms_db[i].nr_of_sms--; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 112 | return &fake_sms; |
| 113 | } |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | void 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 | |
| 130 | static void test_next_sms() |
| 131 | { |
| 132 | int i; |
Neels Hofmeyr | 7b61ffe | 2018-11-30 02:46:53 +0100 | [diff] [blame^] | 133 | char last_msisdn[VLR_MSISDN_LENGTH+1] = ""; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 134 | |
| 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 | |
| 191 | static struct log_info_cat sms_queue_test_categories[] = { |
| 192 | }; |
| 193 | |
| 194 | static struct log_info info = { |
| 195 | .cat = sms_queue_test_categories, |
| 196 | .num_cat = ARRAY_SIZE(sms_queue_test_categories), |
| 197 | }; |
| 198 | |
| 199 | int main(int argc, char **argv) |
| 200 | { |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 201 | void *msgb_ctx; |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 202 | void *logging_ctx; |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 203 | |
| 204 | talloc_ctx = talloc_named_const(NULL, 0, "sms_queue_test"); |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 205 | 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 208 | |
| 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 Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 219 | 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 Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 228 | talloc_free(logging_ctx); |
Neels Hofmeyr | 0442ea2 | 2017-11-21 23:01:39 +0100 | [diff] [blame] | 229 | |
| 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 Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 238 | return 0; |
| 239 | } |