Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 1 | /* (C) 2008 by Jan Luebbe <jluebbe@debian.org> |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 2 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Alexander Chemeris | 82a1858 | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 3 | * (C) 2014 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 21 | #include <openbsc/debug.h> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 22 | #include <openbsc/db.h> |
Holger Hans Peter Freyther | 28dcbc5 | 2010-12-22 18:21:14 +0100 | [diff] [blame] | 23 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | 73bc51d | 2014-03-07 19:08:03 +0100 | [diff] [blame] | 24 | #include <openbsc/gsm_04_11.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 25 | |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 26 | #include <osmocom/core/application.h> |
| 27 | |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 29 | #include <string.h> |
Harald Welte | 12247c6 | 2009-05-21 07:23:02 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
Alexander Chemeris | 82a1858 | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 31 | #include <inttypes.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 32 | |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 33 | static struct gsm_network dummy_net; |
| 34 | |
| 35 | #define SUBSCR_PUT(sub) \ |
| 36 | sub->net = &dummy_net; \ |
| 37 | subscr_put(sub); |
| 38 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 39 | #define COMPARE(original, copy) \ |
| 40 | if (original->id != copy->id) \ |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 41 | printf("Ids do not match in %s:%d %llu %llu\n", \ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 42 | __FUNCTION__, __LINE__, original->id, copy->id); \ |
| 43 | if (original->lac != copy->lac) \ |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 44 | printf("LAC do not match in %s:%d %d %d\n", \ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 45 | __FUNCTION__, __LINE__, original->lac, copy->lac); \ |
| 46 | if (original->authorized != copy->authorized) \ |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 47 | printf("Authorize do not match in %s:%d %d %d\n", \ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 48 | __FUNCTION__, __LINE__, original->authorized, \ |
| 49 | copy->authorized); \ |
| 50 | if (strcmp(original->imsi, copy->imsi) != 0) \ |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 51 | printf("IMSIs do not match in %s:%d '%s' '%s'\n", \ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 52 | __FUNCTION__, __LINE__, original->imsi, copy->imsi); \ |
Holger Hans Peter Freyther | 2223025 | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 53 | if (original->tmsi != copy->tmsi) \ |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 54 | printf("TMSIs do not match in %s:%d '%u' '%u'\n", \ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 55 | __FUNCTION__, __LINE__, original->tmsi, copy->tmsi); \ |
| 56 | if (strcmp(original->name, copy->name) != 0) \ |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 57 | printf("names do not match in %s:%d '%s' '%s'\n", \ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 58 | __FUNCTION__, __LINE__, original->name, copy->name); \ |
| 59 | if (strcmp(original->extension, copy->extension) != 0) \ |
Alexander Chemeris | 86d46c5 | 2013-10-04 02:42:24 +0200 | [diff] [blame] | 60 | printf("Extensions do not match in %s:%d '%s' '%s'\n", \ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 61 | __FUNCTION__, __LINE__, original->extension, copy->extension); \ |
| 62 | |
Holger Hans Peter Freyther | 73bc51d | 2014-03-07 19:08:03 +0100 | [diff] [blame] | 63 | /* |
| 64 | * Create/Store a SMS and then try to load it. |
| 65 | */ |
| 66 | static void test_sms(void) |
| 67 | { |
| 68 | int rc; |
| 69 | struct gsm_sms *sms; |
| 70 | struct gsm_subscriber *subscr; |
| 71 | subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, "9993245423445"); |
| 72 | OSMO_ASSERT(subscr); |
| 73 | subscr->net = &dummy_net; |
| 74 | |
| 75 | sms = sms_alloc(); |
| 76 | sms->receiver = subscr_get(subscr); |
| 77 | |
| 78 | sms->src.ton = 0x23; |
| 79 | sms->src.npi = 0x24; |
| 80 | memcpy(sms->src.addr, "1234", strlen("1234") + 1); |
| 81 | |
| 82 | sms->dst.ton = 0x32; |
| 83 | sms->dst.npi = 0x42; |
| 84 | memcpy(sms->dst.addr, subscr->extension, sizeof(subscr->extension)); |
| 85 | |
| 86 | memcpy(sms->text, "Text123", strlen("Text123") + 1); |
| 87 | memcpy(sms->user_data, "UserData123", strlen("UserData123") + 1); |
| 88 | sms->user_data_len = strlen("UserData123"); |
| 89 | |
| 90 | /* random values */ |
| 91 | sms->reply_path_req = 1; |
| 92 | sms->status_rep_req = 2; |
| 93 | sms->ud_hdr_ind = 3; |
| 94 | sms->protocol_id = 4; |
| 95 | sms->data_coding_scheme = 5; |
| 96 | |
| 97 | rc = db_sms_store(sms); |
| 98 | sms_free(sms); |
| 99 | OSMO_ASSERT(rc == 0); |
| 100 | |
| 101 | /* now query */ |
| 102 | sms = db_sms_get_unsent_for_subscr(subscr); |
| 103 | OSMO_ASSERT(sms); |
| 104 | OSMO_ASSERT(sms->receiver == subscr); |
| 105 | OSMO_ASSERT(sms->reply_path_req == 1); |
| 106 | OSMO_ASSERT(sms->status_rep_req == 2); |
| 107 | OSMO_ASSERT(sms->ud_hdr_ind == 3); |
| 108 | OSMO_ASSERT(sms->protocol_id == 4); |
| 109 | OSMO_ASSERT(sms->data_coding_scheme == 5); |
| 110 | OSMO_ASSERT(sms->src.ton == 0x23); |
| 111 | OSMO_ASSERT(sms->src.npi == 0x24); |
| 112 | OSMO_ASSERT(sms->dst.ton == 0x32); |
| 113 | OSMO_ASSERT(sms->dst.npi == 0x42); |
| 114 | OSMO_ASSERT(strcmp((char *) sms->text, "Text123") == 0); |
| 115 | OSMO_ASSERT(sms->user_data_len == strlen("UserData123")); |
| 116 | OSMO_ASSERT(strcmp((char *) sms->user_data, "UserData123") == 0); |
| 117 | |
| 118 | subscr_put(subscr); |
| 119 | } |
| 120 | |
Holger Hans Peter Freyther | 6114401 | 2014-03-08 16:41:37 +0100 | [diff] [blame] | 121 | static void test_sms_migrate(void) |
| 122 | { |
| 123 | struct gsm_subscriber *rcv_subscr; |
| 124 | struct gsm_sms *sms; |
| 125 | static const uint8_t user_data_1[] = { |
| 126 | 0x41, 0xf1, 0xd8, 0x05, 0x22, 0x96, 0xcd, 0x2e, |
| 127 | 0x90, 0xf1, 0xfd, 0x06, 0x00 }; |
| 128 | static const uint8_t user_data_2[] = { |
| 129 | 0x41, 0xf1, 0xd8, 0x05, 0x22, 0x96, 0xcd, 0x2e, |
| 130 | 0xd0, 0xf1, 0xfd, 0x06, 0x00 }; |
| 131 | |
| 132 | rcv_subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, "901010000001111"); |
| 133 | rcv_subscr->net = &dummy_net; |
| 134 | |
| 135 | sms = db_sms_get(&dummy_net, 1); |
| 136 | OSMO_ASSERT(sms->id == 1); |
| 137 | OSMO_ASSERT(sms->receiver == rcv_subscr); |
| 138 | OSMO_ASSERT(strcmp(sms->text, "Abc. Def. Foo") == 0); |
| 139 | OSMO_ASSERT(sms->user_data_len == ARRAY_SIZE(user_data_1)); |
| 140 | OSMO_ASSERT(memcmp(sms->user_data, user_data_1, ARRAY_SIZE(user_data_1)) == 0); |
| 141 | sms_free(sms); |
| 142 | |
| 143 | sms = db_sms_get(&dummy_net, 2); |
| 144 | OSMO_ASSERT(sms->id == 2); |
| 145 | OSMO_ASSERT(sms->receiver == rcv_subscr); |
| 146 | OSMO_ASSERT(strcmp(sms->text, "Abc. Def. Goo") == 0); |
| 147 | OSMO_ASSERT(sms->user_data_len == ARRAY_SIZE(user_data_2)); |
| 148 | OSMO_ASSERT(memcmp(sms->user_data, user_data_2, ARRAY_SIZE(user_data_2)) == 0); |
| 149 | sms_free(sms); |
| 150 | |
| 151 | subscr_put(rcv_subscr); |
| 152 | } |
| 153 | |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 154 | int main() |
| 155 | { |
Alexander Chemeris | 82a1858 | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 156 | char scratch_str[256]; |
| 157 | |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 158 | printf("Testing subscriber database code.\n"); |
| 159 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | 3c9068f | 2014-05-01 07:53:42 +0200 | [diff] [blame] | 160 | log_set_print_filename(osmo_stderr_target, 0); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 161 | |
Holger Freyther | c7b86f9 | 2009-06-06 13:54:20 +0000 | [diff] [blame] | 162 | if (db_init("hlr.sqlite3")) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 163 | printf("DB: Failed to init database. Please check the option settings.\n"); |
| 164 | return 1; |
| 165 | } |
| 166 | printf("DB: Database initialized.\n"); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 167 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 168 | if (db_prepare()) { |
| 169 | printf("DB: Failed to prepare database.\n"); |
| 170 | return 1; |
| 171 | } |
| 172 | printf("DB: Database prepared.\n"); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 173 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 174 | struct gsm_subscriber *alice = NULL; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 175 | struct gsm_subscriber *alice_db; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 176 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 177 | char *alice_imsi = "3243245432345"; |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 178 | alice = db_create_subscriber(alice_imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 179 | db_sync_subscriber(alice); |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 180 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 181 | COMPARE(alice, alice_db); |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 182 | SUBSCR_PUT(alice_db); |
| 183 | SUBSCR_PUT(alice); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 184 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 185 | alice_imsi = "3693245423445"; |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 186 | alice = db_create_subscriber(alice_imsi); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 187 | db_subscriber_assoc_imei(alice, "1234567890"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 188 | db_subscriber_alloc_tmsi(alice); |
| 189 | alice->lac=42; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 190 | db_sync_subscriber(alice); |
Alexander Chemeris | 82a1858 | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 191 | /* Get by TMSI */ |
| 192 | snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi); |
| 193 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str); |
| 194 | COMPARE(alice, alice_db); |
| 195 | SUBSCR_PUT(alice_db); |
| 196 | /* Get by IMSI */ |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 197 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 198 | COMPARE(alice, alice_db); |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 199 | SUBSCR_PUT(alice_db); |
Alexander Chemeris | 82a1858 | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 200 | /* Get by id */ |
| 201 | snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id); |
| 202 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str); |
| 203 | COMPARE(alice, alice_db); |
| 204 | SUBSCR_PUT(alice_db); |
| 205 | /* Get by extension */ |
| 206 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension); |
| 207 | COMPARE(alice, alice_db); |
| 208 | SUBSCR_PUT(alice_db); |
| 209 | SUBSCR_PUT(alice); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 210 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 211 | alice_imsi = "9993245423445"; |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 212 | alice = db_create_subscriber(alice_imsi); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 213 | db_subscriber_alloc_tmsi(alice); |
| 214 | alice->lac=42; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 215 | db_sync_subscriber(alice); |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 216 | db_subscriber_assoc_imei(alice, "1234567890"); |
| 217 | db_subscriber_assoc_imei(alice, "6543560920"); |
Alexander Chemeris | 82a1858 | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 218 | /* Get by TMSI */ |
| 219 | snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi); |
| 220 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str); |
| 221 | COMPARE(alice, alice_db); |
| 222 | SUBSCR_PUT(alice_db); |
| 223 | /* Get by IMSI */ |
Holger Hans Peter Freyther | 7634ec1 | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 224 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 225 | COMPARE(alice, alice_db); |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 226 | SUBSCR_PUT(alice_db); |
Alexander Chemeris | 82a1858 | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 227 | /* Get by id */ |
| 228 | snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id); |
| 229 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str); |
| 230 | COMPARE(alice, alice_db); |
| 231 | SUBSCR_PUT(alice_db); |
| 232 | /* Get by extension */ |
| 233 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension); |
| 234 | COMPARE(alice, alice_db); |
| 235 | SUBSCR_PUT(alice_db); |
| 236 | SUBSCR_PUT(alice); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 237 | |
Holger Hans Peter Freyther | 73bc51d | 2014-03-07 19:08:03 +0100 | [diff] [blame] | 238 | test_sms(); |
Holger Hans Peter Freyther | 6114401 | 2014-03-08 16:41:37 +0100 | [diff] [blame] | 239 | test_sms_migrate(); |
Holger Hans Peter Freyther | 73bc51d | 2014-03-07 19:08:03 +0100 | [diff] [blame] | 240 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 241 | db_fini(); |
| 242 | |
Holger Hans Peter Freyther | 45222a7 | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 243 | printf("Done\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 244 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 245 | } |
Holger Freyther | bab9cd9 | 2009-04-12 05:37:07 +0000 | [diff] [blame] | 246 | |
| 247 | /* stubs */ |
Holger Hans Peter Freyther | 763b42a | 2010-12-29 11:07:22 +0100 | [diff] [blame] | 248 | void vty_out() {} |