blob: ee0cbca5bd2f52cc84940820320c9a434a7da7b0 [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>
Alexander Chemeris82a18582014-03-08 18:48:49 +010031#include <inttypes.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000032
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010033static struct gsm_network dummy_net;
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010034static struct gsm_subscriber_group dummy_sgrp;
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010035
36#define SUBSCR_PUT(sub) \
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010037 sub->group = &dummy_sgrp; \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010038 subscr_put(sub);
39
Holger Freyther12aa50d2009-01-01 18:02:05 +000040#define COMPARE(original, copy) \
41 if (original->id != copy->id) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010042 printf("Ids do not match in %s:%d %llu %llu\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000043 __FUNCTION__, __LINE__, original->id, copy->id); \
44 if (original->lac != copy->lac) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010045 printf("LAC do not match in %s:%d %d %d\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000046 __FUNCTION__, __LINE__, original->lac, copy->lac); \
47 if (original->authorized != copy->authorized) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010048 printf("Authorize do not match in %s:%d %d %d\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000049 __FUNCTION__, __LINE__, original->authorized, \
50 copy->authorized); \
51 if (strcmp(original->imsi, copy->imsi) != 0) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010052 printf("IMSIs do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000053 __FUNCTION__, __LINE__, original->imsi, copy->imsi); \
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +020054 if (original->tmsi != copy->tmsi) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010055 printf("TMSIs do not match in %s:%d '%u' '%u'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000056 __FUNCTION__, __LINE__, original->tmsi, copy->tmsi); \
57 if (strcmp(original->name, copy->name) != 0) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010058 printf("names do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000059 __FUNCTION__, __LINE__, original->name, copy->name); \
60 if (strcmp(original->extension, copy->extension) != 0) \
Alexander Chemeris86d46c52013-10-04 02:42:24 +020061 printf("Extensions do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000062 __FUNCTION__, __LINE__, original->extension, copy->extension); \
63
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +010064/*
65 * Create/Store a SMS and then try to load it.
66 */
67static void test_sms(void)
68{
69 int rc;
70 struct gsm_sms *sms;
71 struct gsm_subscriber *subscr;
72 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, "9993245423445");
73 OSMO_ASSERT(subscr);
Jacob Erlbeck1e30a282014-12-03 09:28:24 +010074 subscr->group = &dummy_sgrp;
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +010075
76 sms = sms_alloc();
77 sms->receiver = subscr_get(subscr);
78
79 sms->src.ton = 0x23;
80 sms->src.npi = 0x24;
81 memcpy(sms->src.addr, "1234", strlen("1234") + 1);
82
83 sms->dst.ton = 0x32;
84 sms->dst.npi = 0x42;
85 memcpy(sms->dst.addr, subscr->extension, sizeof(subscr->extension));
86
87 memcpy(sms->text, "Text123", strlen("Text123") + 1);
88 memcpy(sms->user_data, "UserData123", strlen("UserData123") + 1);
89 sms->user_data_len = strlen("UserData123");
90
91 /* random values */
92 sms->reply_path_req = 1;
93 sms->status_rep_req = 2;
94 sms->ud_hdr_ind = 3;
95 sms->protocol_id = 4;
96 sms->data_coding_scheme = 5;
97
98 rc = db_sms_store(sms);
99 sms_free(sms);
100 OSMO_ASSERT(rc == 0);
101
102 /* now query */
103 sms = db_sms_get_unsent_for_subscr(subscr);
104 OSMO_ASSERT(sms);
105 OSMO_ASSERT(sms->receiver == subscr);
106 OSMO_ASSERT(sms->reply_path_req == 1);
107 OSMO_ASSERT(sms->status_rep_req == 2);
108 OSMO_ASSERT(sms->ud_hdr_ind == 3);
109 OSMO_ASSERT(sms->protocol_id == 4);
110 OSMO_ASSERT(sms->data_coding_scheme == 5);
111 OSMO_ASSERT(sms->src.ton == 0x23);
112 OSMO_ASSERT(sms->src.npi == 0x24);
113 OSMO_ASSERT(sms->dst.ton == 0x32);
114 OSMO_ASSERT(sms->dst.npi == 0x42);
115 OSMO_ASSERT(strcmp((char *) sms->text, "Text123") == 0);
116 OSMO_ASSERT(sms->user_data_len == strlen("UserData123"));
117 OSMO_ASSERT(strcmp((char *) sms->user_data, "UserData123") == 0);
118
Holger Hans Peter Freytherbe8e7752014-12-25 17:27:15 +0100119 /* Mark the SMS as delivered */
120 db_sms_mark_delivered(sms);
121 sms_free(sms);
122
123 sms = db_sms_get_unsent_for_subscr(subscr);
124 OSMO_ASSERT(!sms);
125
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100126 subscr_put(subscr);
127}
128
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100129static void test_sms_migrate(void)
130{
131 struct gsm_subscriber *rcv_subscr;
132 struct gsm_sms *sms;
133 static const uint8_t user_data_1[] = {
134 0x41, 0xf1, 0xd8, 0x05, 0x22, 0x96, 0xcd, 0x2e,
135 0x90, 0xf1, 0xfd, 0x06, 0x00 };
136 static const uint8_t user_data_2[] = {
137 0x41, 0xf1, 0xd8, 0x05, 0x22, 0x96, 0xcd, 0x2e,
138 0xd0, 0xf1, 0xfd, 0x06, 0x00 };
139
140 rcv_subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, "901010000001111");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100141 rcv_subscr->group = &dummy_sgrp;
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100142
143 sms = db_sms_get(&dummy_net, 1);
144 OSMO_ASSERT(sms->id == 1);
145 OSMO_ASSERT(sms->receiver == rcv_subscr);
146 OSMO_ASSERT(strcmp(sms->text, "Abc. Def. Foo") == 0);
147 OSMO_ASSERT(sms->user_data_len == ARRAY_SIZE(user_data_1));
148 OSMO_ASSERT(memcmp(sms->user_data, user_data_1, ARRAY_SIZE(user_data_1)) == 0);
149 sms_free(sms);
150
151 sms = db_sms_get(&dummy_net, 2);
152 OSMO_ASSERT(sms->id == 2);
153 OSMO_ASSERT(sms->receiver == rcv_subscr);
154 OSMO_ASSERT(strcmp(sms->text, "Abc. Def. Goo") == 0);
155 OSMO_ASSERT(sms->user_data_len == ARRAY_SIZE(user_data_2));
156 OSMO_ASSERT(memcmp(sms->user_data, user_data_2, ARRAY_SIZE(user_data_2)) == 0);
157 sms_free(sms);
158
159 subscr_put(rcv_subscr);
160}
161
Max9a7e25b2016-05-11 13:05:13 +0200162static void test_subs(const char *alice_imsi, char *imei1, char *imei2)
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100163{
Max9a7e25b2016-05-11 13:05:13 +0200164 struct gsm_subscriber *alice = NULL, *alice_db;
Alexander Chemeris82a18582014-03-08 18:48:49 +0100165 char scratch_str[256];
166
Max9a7e25b2016-05-11 13:05:13 +0200167 alice = db_create_subscriber(alice_imsi);
168 db_subscriber_assoc_imei(alice, imei1);
169 if (imei2)
170 db_subscriber_assoc_imei(alice, imei2);
171 db_subscriber_alloc_tmsi(alice);
172 alice->lac=42;
173 db_sync_subscriber(alice);
174 /* Get by TMSI */
175 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
176 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
177 COMPARE(alice, alice_db);
178 SUBSCR_PUT(alice_db);
179 /* Get by IMSI */
180 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
181 COMPARE(alice, alice_db);
182 SUBSCR_PUT(alice_db);
183 /* Get by id */
184 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
185 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
186 COMPARE(alice, alice_db);
187 SUBSCR_PUT(alice_db);
188 /* Get by extension */
189 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
190 COMPARE(alice, alice_db);
191 SUBSCR_PUT(alice_db);
192 SUBSCR_PUT(alice);
193}
194
195int main()
196{
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100197 printf("Testing subscriber database code.\n");
198 osmo_init_logging(&log_info);
Holger Hans Peter Freyther3c9068f2014-05-01 07:53:42 +0200199 log_set_print_filename(osmo_stderr_target, 0);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000200
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100201 dummy_net.subscr_group = &dummy_sgrp;
202 dummy_sgrp.net = &dummy_net;
203
Holger Freytherc7b86f92009-06-06 13:54:20 +0000204 if (db_init("hlr.sqlite3")) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000205 printf("DB: Failed to init database. Please check the option settings.\n");
206 return 1;
207 }
208 printf("DB: Database initialized.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000209
Jan Luebbe5c15c852008-12-27 15:59:25 +0000210 if (db_prepare()) {
211 printf("DB: Failed to prepare database.\n");
212 return 1;
213 }
214 printf("DB: Database prepared.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000215
Jan Luebbe5c15c852008-12-27 15:59:25 +0000216 struct gsm_subscriber *alice = NULL;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000217 struct gsm_subscriber *alice_db;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000218
Holger Freyther12aa50d2009-01-01 18:02:05 +0000219 char *alice_imsi = "3243245432345";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200220 alice = db_create_subscriber(alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000221 db_sync_subscriber(alice);
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200222 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000223 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100224 SUBSCR_PUT(alice_db);
225 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000226
Max9a7e25b2016-05-11 13:05:13 +0200227 test_subs("3693245423445", "1234567890", NULL);
228 test_subs("9993245423445", "1234567890", "6543560920");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000229
Holger Hans Peter Freyther2826df52016-04-01 20:21:03 +0200230 /* create it again and see it fails */
231 alice = db_create_subscriber(alice_imsi);
232 OSMO_ASSERT(!alice);
233
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100234 test_sms();
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100235 test_sms_migrate();
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100236
Jan Luebbe5c15c852008-12-27 15:59:25 +0000237 db_fini();
238
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100239 printf("Done\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000240 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000241}
Holger Freytherbab9cd92009-04-12 05:37:07 +0000242
243/* stubs */
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100244void vty_out() {}