Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 1 | /* (C) 2008 by Jan Luebbe <jluebbe@debian.org> |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 2 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Alexander Chemeris | 3b338fd | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 3 | * (C) 2014 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co> |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 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 |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +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 | 0e3e88e | 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/>. |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 21 | #include <openbsc/debug.h> |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 22 | #include <openbsc/db.h> |
Holger Hans Peter Freyther | ef3b59d | 2010-12-22 18:21:14 +0100 | [diff] [blame] | 23 | #include <openbsc/gsm_subscriber.h> |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 24 | |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 25 | #include <osmocom/core/application.h> |
| 26 | |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <string.h> |
Harald Welte | 5bc7c17 | 2009-05-21 07:23:02 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
Alexander Chemeris | 3b338fd | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 30 | #include <inttypes.h> |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 31 | |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 32 | static struct gsm_network dummy_net; |
| 33 | |
| 34 | #define SUBSCR_PUT(sub) \ |
| 35 | sub->net = &dummy_net; \ |
| 36 | subscr_put(sub); |
| 37 | |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 38 | #define COMPARE(original, copy) \ |
| 39 | if (original->id != copy->id) \ |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 40 | printf("Ids do not match in %s:%d %llu %llu\n", \ |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 41 | __FUNCTION__, __LINE__, original->id, copy->id); \ |
| 42 | if (original->lac != copy->lac) \ |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 43 | printf("LAC do not match in %s:%d %d %d\n", \ |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 44 | __FUNCTION__, __LINE__, original->lac, copy->lac); \ |
| 45 | if (original->authorized != copy->authorized) \ |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 46 | printf("Authorize do not match in %s:%d %d %d\n", \ |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 47 | __FUNCTION__, __LINE__, original->authorized, \ |
| 48 | copy->authorized); \ |
| 49 | if (strcmp(original->imsi, copy->imsi) != 0) \ |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 50 | printf("IMSIs do not match in %s:%d '%s' '%s'\n", \ |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 51 | __FUNCTION__, __LINE__, original->imsi, copy->imsi); \ |
Holger Hans Peter Freyther | cd8bacf | 2009-08-19 12:53:57 +0200 | [diff] [blame] | 52 | if (original->tmsi != copy->tmsi) \ |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 53 | printf("TMSIs do not match in %s:%d '%u' '%u'\n", \ |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 54 | __FUNCTION__, __LINE__, original->tmsi, copy->tmsi); \ |
| 55 | if (strcmp(original->name, copy->name) != 0) \ |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 56 | printf("names do not match in %s:%d '%s' '%s'\n", \ |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 57 | __FUNCTION__, __LINE__, original->name, copy->name); \ |
| 58 | if (strcmp(original->extension, copy->extension) != 0) \ |
Alexander Chemeris | 35f1290 | 2013-10-04 02:42:24 +0200 | [diff] [blame] | 59 | printf("Extensions do not match in %s:%d '%s' '%s'\n", \ |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 60 | __FUNCTION__, __LINE__, original->extension, copy->extension); \ |
| 61 | |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 62 | int main() |
| 63 | { |
Alexander Chemeris | 3b338fd | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 64 | char scratch_str[256]; |
| 65 | |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 66 | printf("Testing subscriber database code.\n"); |
| 67 | osmo_init_logging(&log_info); |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 68 | |
Holger Freyther | 761fdec | 2009-06-06 13:54:20 +0000 | [diff] [blame] | 69 | if (db_init("hlr.sqlite3")) { |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 70 | printf("DB: Failed to init database. Please check the option settings.\n"); |
| 71 | return 1; |
| 72 | } |
| 73 | printf("DB: Database initialized.\n"); |
| 74 | |
| 75 | if (db_prepare()) { |
| 76 | printf("DB: Failed to prepare database.\n"); |
| 77 | return 1; |
| 78 | } |
| 79 | printf("DB: Database prepared.\n"); |
| 80 | |
| 81 | struct gsm_subscriber *alice = NULL; |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 82 | struct gsm_subscriber *alice_db; |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 83 | |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 84 | char *alice_imsi = "3243245432345"; |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 85 | alice = db_create_subscriber(alice_imsi); |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 86 | db_sync_subscriber(alice); |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 87 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi); |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 88 | COMPARE(alice, alice_db); |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 89 | SUBSCR_PUT(alice_db); |
| 90 | SUBSCR_PUT(alice); |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 91 | |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 92 | alice_imsi = "3693245423445"; |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 93 | alice = db_create_subscriber(alice_imsi); |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 94 | db_subscriber_assoc_imei(alice, "1234567890"); |
| 95 | db_subscriber_alloc_tmsi(alice); |
| 96 | alice->lac=42; |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 97 | db_sync_subscriber(alice); |
Alexander Chemeris | 3b338fd | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 98 | /* Get by TMSI */ |
| 99 | snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi); |
| 100 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str); |
| 101 | COMPARE(alice, alice_db); |
| 102 | SUBSCR_PUT(alice_db); |
| 103 | /* Get by IMSI */ |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 104 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi); |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 105 | COMPARE(alice, alice_db); |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 106 | SUBSCR_PUT(alice_db); |
Alexander Chemeris | 3b338fd | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 107 | /* Get by id */ |
| 108 | snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id); |
| 109 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str); |
| 110 | COMPARE(alice, alice_db); |
| 111 | SUBSCR_PUT(alice_db); |
| 112 | /* Get by extension */ |
| 113 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension); |
| 114 | COMPARE(alice, alice_db); |
| 115 | SUBSCR_PUT(alice_db); |
| 116 | SUBSCR_PUT(alice); |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 117 | |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 118 | alice_imsi = "9993245423445"; |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 119 | alice = db_create_subscriber(alice_imsi); |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 120 | db_subscriber_alloc_tmsi(alice); |
| 121 | alice->lac=42; |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 122 | db_sync_subscriber(alice); |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 123 | db_subscriber_assoc_imei(alice, "1234567890"); |
| 124 | db_subscriber_assoc_imei(alice, "6543560920"); |
Alexander Chemeris | 3b338fd | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 125 | /* Get by TMSI */ |
| 126 | snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi); |
| 127 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str); |
| 128 | COMPARE(alice, alice_db); |
| 129 | SUBSCR_PUT(alice_db); |
| 130 | /* Get by IMSI */ |
Holger Hans Peter Freyther | abe587d | 2013-10-04 08:35:11 +0200 | [diff] [blame] | 131 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi); |
Holger Freyther | aa82f0d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 132 | COMPARE(alice, alice_db); |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 133 | SUBSCR_PUT(alice_db); |
Alexander Chemeris | 3b338fd | 2014-03-08 18:48:49 +0100 | [diff] [blame] | 134 | /* Get by id */ |
| 135 | snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id); |
| 136 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str); |
| 137 | COMPARE(alice, alice_db); |
| 138 | SUBSCR_PUT(alice_db); |
| 139 | /* Get by extension */ |
| 140 | alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension); |
| 141 | COMPARE(alice, alice_db); |
| 142 | SUBSCR_PUT(alice_db); |
| 143 | SUBSCR_PUT(alice); |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 144 | |
| 145 | db_fini(); |
| 146 | |
Holger Hans Peter Freyther | 99f47fb | 2012-01-06 14:27:08 +0100 | [diff] [blame] | 147 | printf("Done\n"); |
Holger Freyther | c1e1192 | 2008-12-31 23:25:05 +0000 | [diff] [blame] | 148 | return 0; |
| 149 | } |
Holger Freyther | 4b8eadf | 2009-04-12 05:37:07 +0000 | [diff] [blame] | 150 | |
| 151 | /* stubs */ |
Holger Hans Peter Freyther | 0fe0c8c | 2010-12-29 11:07:22 +0100 | [diff] [blame] | 152 | void vty_out() {} |