blob: 755a6e9eb016e5128799f41c6edacd73b48d3c91 [file] [log] [blame]
Jan Luebbefaaa49c2008-12-27 01:07:07 +00001/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
Holger Hans Peter Freyther2826df52016-04-01 20:21:03 +02002 * (C) 2009-2016 by Holger Hans Peter Freyther <zecke@selfish.org>
Alexander Chemeris82a18582014-03-08 18:48:49 +01003 * (C) 2014 by Alexander Chemeris <Alexander.Chemeris@fairwaves.co>
Jan Luebbefaaa49c2008-12-27 01:07:07 +00004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * 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 Luebbefaaa49c2008-12-27 01:07:07 +00009 * (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 Welte9af6ddf2011-01-01 15:25:50 +010016 * 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 Luebbefaaa49c2008-12-27 01:07:07 +000018 *
19 */
20
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010021#include <openbsc/debug.h>
Jan Luebbefaaa49c2008-12-27 01:07:07 +000022#include <openbsc/db.h>
Holger Hans Peter Freyther28dcbc52010-12-22 18:21:14 +010023#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +010024#include <openbsc/gsm_04_11.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000025
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010026#include <osmocom/core/application.h>
27
Jan Luebbe7398eb92008-12-27 00:45:41 +000028#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000029#include <string.h>
Harald Welte12247c62009-05-21 07:23:02 +000030#include <stdlib.h>
Maxe6052c42016-06-30 10:25:49 +020031#include <stdbool.h>
Alexander Chemeris82a18582014-03-08 18:48:49 +010032#include <inttypes.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000033
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010034static struct gsm_network dummy_net;
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010035static struct gsm_subscriber_group dummy_sgrp;
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010036
37#define SUBSCR_PUT(sub) \
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010038 sub->group = &dummy_sgrp; \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010039 subscr_put(sub);
40
Holger Freyther12aa50d2009-01-01 18:02:05 +000041#define COMPARE(original, copy) \
42 if (original->id != copy->id) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010043 printf("Ids do not match in %s:%d %llu %llu\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000044 __FUNCTION__, __LINE__, original->id, copy->id); \
45 if (original->lac != copy->lac) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010046 printf("LAC do not match in %s:%d %d %d\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000047 __FUNCTION__, __LINE__, original->lac, copy->lac); \
48 if (original->authorized != copy->authorized) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010049 printf("Authorize do not match in %s:%d %d %d\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000050 __FUNCTION__, __LINE__, original->authorized, \
51 copy->authorized); \
52 if (strcmp(original->imsi, copy->imsi) != 0) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010053 printf("IMSIs do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000054 __FUNCTION__, __LINE__, original->imsi, copy->imsi); \
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +020055 if (original->tmsi != copy->tmsi) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010056 printf("TMSIs do not match in %s:%d '%u' '%u'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000057 __FUNCTION__, __LINE__, original->tmsi, copy->tmsi); \
58 if (strcmp(original->name, copy->name) != 0) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010059 printf("names do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000060 __FUNCTION__, __LINE__, original->name, copy->name); \
61 if (strcmp(original->extension, copy->extension) != 0) \
Alexander Chemeris86d46c52013-10-04 02:42:24 +020062 printf("Extensions do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000063 __FUNCTION__, __LINE__, original->extension, copy->extension); \
64
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +010065/*
66 * Create/Store a SMS and then try to load it.
67 */
68static void test_sms(void)
69{
70 int rc;
71 struct gsm_sms *sms;
72 struct gsm_subscriber *subscr;
73 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, "9993245423445");
74 OSMO_ASSERT(subscr);
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010075 subscr->group = &dummy_sgrp;
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +010076
77 sms = sms_alloc();
78 sms->receiver = subscr_get(subscr);
79
80 sms->src.ton = 0x23;
81 sms->src.npi = 0x24;
82 memcpy(sms->src.addr, "1234", strlen("1234") + 1);
83
84 sms->dst.ton = 0x32;
85 sms->dst.npi = 0x42;
86 memcpy(sms->dst.addr, subscr->extension, sizeof(subscr->extension));
87
88 memcpy(sms->text, "Text123", strlen("Text123") + 1);
89 memcpy(sms->user_data, "UserData123", strlen("UserData123") + 1);
90 sms->user_data_len = strlen("UserData123");
91
92 /* random values */
93 sms->reply_path_req = 1;
94 sms->status_rep_req = 2;
95 sms->ud_hdr_ind = 3;
96 sms->protocol_id = 4;
97 sms->data_coding_scheme = 5;
98
99 rc = db_sms_store(sms);
100 sms_free(sms);
101 OSMO_ASSERT(rc == 0);
102
103 /* now query */
104 sms = db_sms_get_unsent_for_subscr(subscr);
105 OSMO_ASSERT(sms);
106 OSMO_ASSERT(sms->receiver == subscr);
107 OSMO_ASSERT(sms->reply_path_req == 1);
108 OSMO_ASSERT(sms->status_rep_req == 2);
109 OSMO_ASSERT(sms->ud_hdr_ind == 3);
110 OSMO_ASSERT(sms->protocol_id == 4);
111 OSMO_ASSERT(sms->data_coding_scheme == 5);
112 OSMO_ASSERT(sms->src.ton == 0x23);
113 OSMO_ASSERT(sms->src.npi == 0x24);
114 OSMO_ASSERT(sms->dst.ton == 0x32);
115 OSMO_ASSERT(sms->dst.npi == 0x42);
116 OSMO_ASSERT(strcmp((char *) sms->text, "Text123") == 0);
117 OSMO_ASSERT(sms->user_data_len == strlen("UserData123"));
118 OSMO_ASSERT(strcmp((char *) sms->user_data, "UserData123") == 0);
119
Holger Hans Peter Freytherbe8e7752014-12-25 17:27:15 +0100120 /* Mark the SMS as delivered */
121 db_sms_mark_delivered(sms);
122 sms_free(sms);
123
124 sms = db_sms_get_unsent_for_subscr(subscr);
125 OSMO_ASSERT(!sms);
126
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100127 subscr_put(subscr);
128}
129
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100130static void test_sms_migrate(void)
131{
132 struct gsm_subscriber *rcv_subscr;
133 struct gsm_sms *sms;
134 static const uint8_t user_data_1[] = {
135 0x41, 0xf1, 0xd8, 0x05, 0x22, 0x96, 0xcd, 0x2e,
136 0x90, 0xf1, 0xfd, 0x06, 0x00 };
137 static const uint8_t user_data_2[] = {
138 0x41, 0xf1, 0xd8, 0x05, 0x22, 0x96, 0xcd, 0x2e,
139 0xd0, 0xf1, 0xfd, 0x06, 0x00 };
140
141 rcv_subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, "901010000001111");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100142 rcv_subscr->group = &dummy_sgrp;
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100143
144 sms = db_sms_get(&dummy_net, 1);
145 OSMO_ASSERT(sms->id == 1);
146 OSMO_ASSERT(sms->receiver == rcv_subscr);
147 OSMO_ASSERT(strcmp(sms->text, "Abc. Def. Foo") == 0);
148 OSMO_ASSERT(sms->user_data_len == ARRAY_SIZE(user_data_1));
149 OSMO_ASSERT(memcmp(sms->user_data, user_data_1, ARRAY_SIZE(user_data_1)) == 0);
150 sms_free(sms);
151
152 sms = db_sms_get(&dummy_net, 2);
153 OSMO_ASSERT(sms->id == 2);
154 OSMO_ASSERT(sms->receiver == rcv_subscr);
155 OSMO_ASSERT(strcmp(sms->text, "Abc. Def. Goo") == 0);
156 OSMO_ASSERT(sms->user_data_len == ARRAY_SIZE(user_data_2));
157 OSMO_ASSERT(memcmp(sms->user_data, user_data_2, ARRAY_SIZE(user_data_2)) == 0);
158 sms_free(sms);
159
160 subscr_put(rcv_subscr);
161}
162
Maxe6052c42016-06-30 10:25:49 +0200163static void test_subs(const char *imsi, char *imei1, char *imei2, bool make_ext)
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100164{
Max9a7e25b2016-05-11 13:05:13 +0200165 struct gsm_subscriber *alice = NULL, *alice_db;
Alexander Chemeris82a18582014-03-08 18:48:49 +0100166 char scratch_str[256];
167
Maxe6052c42016-06-30 10:25:49 +0200168 alice = db_create_subscriber(imsi, GSM_MIN_EXTEN, GSM_MAX_EXTEN,
169 make_ext);
Max9a7e25b2016-05-11 13:05:13 +0200170 db_subscriber_assoc_imei(alice, imei1);
171 if (imei2)
172 db_subscriber_assoc_imei(alice, imei2);
173 db_subscriber_alloc_tmsi(alice);
174 alice->lac=42;
175 db_sync_subscriber(alice);
176 /* Get by TMSI */
177 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
178 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
179 COMPARE(alice, alice_db);
180 SUBSCR_PUT(alice_db);
181 /* Get by IMSI */
Maxe6052c42016-06-30 10:25:49 +0200182 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
Max9a7e25b2016-05-11 13:05:13 +0200183 COMPARE(alice, alice_db);
184 SUBSCR_PUT(alice_db);
185 /* Get by id */
186 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
187 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
188 COMPARE(alice, alice_db);
189 SUBSCR_PUT(alice_db);
190 /* Get by extension */
191 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
Maxe6052c42016-06-30 10:25:49 +0200192 if (alice_db) {
193 if (!make_ext)
194 printf("FAIL: bogus extension created for IMSI %s\n",
195 imsi);
196 COMPARE(alice, alice_db);
197 SUBSCR_PUT(alice_db);
198 } else if (make_ext)
199 printf("FAIL: no subscriber extension for IMSI %s\n", imsi);
Max9a7e25b2016-05-11 13:05:13 +0200200 SUBSCR_PUT(alice);
201}
202
203int main()
204{
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100205 printf("Testing subscriber database code.\n");
206 osmo_init_logging(&log_info);
Holger Hans Peter Freyther3c9068f2014-05-01 07:53:42 +0200207 log_set_print_filename(osmo_stderr_target, 0);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000208
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100209 dummy_net.subscr_group = &dummy_sgrp;
210 dummy_sgrp.net = &dummy_net;
211
Holger Freytherc7b86f92009-06-06 13:54:20 +0000212 if (db_init("hlr.sqlite3")) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000213 printf("DB: Failed to init database. Please check the option settings.\n");
214 return 1;
215 }
216 printf("DB: Database initialized.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000217
Jan Luebbe5c15c852008-12-27 15:59:25 +0000218 if (db_prepare()) {
219 printf("DB: Failed to prepare database.\n");
220 return 1;
221 }
222 printf("DB: Database prepared.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000223
Jan Luebbe5c15c852008-12-27 15:59:25 +0000224 struct gsm_subscriber *alice = NULL;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000225 struct gsm_subscriber *alice_db;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000226
Holger Freyther12aa50d2009-01-01 18:02:05 +0000227 char *alice_imsi = "3243245432345";
Maxe6052c42016-06-30 10:25:49 +0200228 alice = db_create_subscriber(alice_imsi, GSM_MIN_EXTEN, GSM_MAX_EXTEN,
229 true);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000230 db_sync_subscriber(alice);
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200231 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000232 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100233 SUBSCR_PUT(alice_db);
234 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000235
Maxe6052c42016-06-30 10:25:49 +0200236 test_subs("3693245423445", "1234567890", NULL, true);
237 test_subs("9993245423445", "1234567890", "6543560920", true);
238 test_subs("3123122223445", "1234567890", NULL, false);
239 test_subs("9123121223445", "1234567890", "6543560920", false);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000240
Holger Hans Peter Freyther2826df52016-04-01 20:21:03 +0200241 /* create it again and see it fails */
Maxe6052c42016-06-30 10:25:49 +0200242 alice = db_create_subscriber(alice_imsi, GSM_MIN_EXTEN, GSM_MAX_EXTEN,
243 true);
Holger Hans Peter Freyther2826df52016-04-01 20:21:03 +0200244 OSMO_ASSERT(!alice);
245
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100246 test_sms();
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100247 test_sms_migrate();
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100248
Jan Luebbe5c15c852008-12-27 15:59:25 +0000249 db_fini();
250
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100251 printf("Done\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000252 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000253}
Holger Freytherbab9cd92009-04-12 05:37:07 +0000254
255/* stubs */
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100256void vty_out() {}