blob: 62fbb732e28547cb45b9689925337a16794baf9a [file] [log] [blame]
Jan Luebbefaaa49c2008-12-27 01:07:07 +00001/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00002 * (C) 2009 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
119 subscr_put(subscr);
120}
121
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100122static void test_sms_migrate(void)
123{
124 struct gsm_subscriber *rcv_subscr;
125 struct gsm_sms *sms;
126 static const uint8_t user_data_1[] = {
127 0x41, 0xf1, 0xd8, 0x05, 0x22, 0x96, 0xcd, 0x2e,
128 0x90, 0xf1, 0xfd, 0x06, 0x00 };
129 static const uint8_t user_data_2[] = {
130 0x41, 0xf1, 0xd8, 0x05, 0x22, 0x96, 0xcd, 0x2e,
131 0xd0, 0xf1, 0xfd, 0x06, 0x00 };
132
133 rcv_subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, "901010000001111");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100134 rcv_subscr->group = &dummy_sgrp;
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100135
136 sms = db_sms_get(&dummy_net, 1);
137 OSMO_ASSERT(sms->id == 1);
138 OSMO_ASSERT(sms->receiver == rcv_subscr);
139 OSMO_ASSERT(strcmp(sms->text, "Abc. Def. Foo") == 0);
140 OSMO_ASSERT(sms->user_data_len == ARRAY_SIZE(user_data_1));
141 OSMO_ASSERT(memcmp(sms->user_data, user_data_1, ARRAY_SIZE(user_data_1)) == 0);
142 sms_free(sms);
143
144 sms = db_sms_get(&dummy_net, 2);
145 OSMO_ASSERT(sms->id == 2);
146 OSMO_ASSERT(sms->receiver == rcv_subscr);
147 OSMO_ASSERT(strcmp(sms->text, "Abc. Def. Goo") == 0);
148 OSMO_ASSERT(sms->user_data_len == ARRAY_SIZE(user_data_2));
149 OSMO_ASSERT(memcmp(sms->user_data, user_data_2, ARRAY_SIZE(user_data_2)) == 0);
150 sms_free(sms);
151
152 subscr_put(rcv_subscr);
153}
154
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100155int main()
156{
Alexander Chemeris82a18582014-03-08 18:48:49 +0100157 char scratch_str[256];
158
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100159 printf("Testing subscriber database code.\n");
160 osmo_init_logging(&log_info);
Holger Hans Peter Freyther3c9068f2014-05-01 07:53:42 +0200161 log_set_print_filename(osmo_stderr_target, 0);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000162
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100163 dummy_net.subscr_group = &dummy_sgrp;
164 dummy_sgrp.net = &dummy_net;
165
Holger Freytherc7b86f92009-06-06 13:54:20 +0000166 if (db_init("hlr.sqlite3")) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000167 printf("DB: Failed to init database. Please check the option settings.\n");
168 return 1;
169 }
170 printf("DB: Database initialized.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000171
Jan Luebbe5c15c852008-12-27 15:59:25 +0000172 if (db_prepare()) {
173 printf("DB: Failed to prepare database.\n");
174 return 1;
175 }
176 printf("DB: Database prepared.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000177
Jan Luebbe5c15c852008-12-27 15:59:25 +0000178 struct gsm_subscriber *alice = NULL;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000179 struct gsm_subscriber *alice_db;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000180
Holger Freyther12aa50d2009-01-01 18:02:05 +0000181 char *alice_imsi = "3243245432345";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200182 alice = db_create_subscriber(alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000183 db_sync_subscriber(alice);
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200184 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000185 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100186 SUBSCR_PUT(alice_db);
187 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000188
Holger Freyther12aa50d2009-01-01 18:02:05 +0000189 alice_imsi = "3693245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200190 alice = db_create_subscriber(alice_imsi);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000191 db_subscriber_assoc_imei(alice, "1234567890");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000192 db_subscriber_alloc_tmsi(alice);
193 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000194 db_sync_subscriber(alice);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100195 /* Get by TMSI */
196 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
197 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
198 COMPARE(alice, alice_db);
199 SUBSCR_PUT(alice_db);
200 /* Get by IMSI */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200201 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000202 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100203 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100204 /* Get by id */
205 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
206 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
207 COMPARE(alice, alice_db);
208 SUBSCR_PUT(alice_db);
209 /* Get by extension */
210 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
211 COMPARE(alice, alice_db);
212 SUBSCR_PUT(alice_db);
213 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000214
Holger Freyther12aa50d2009-01-01 18:02:05 +0000215 alice_imsi = "9993245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200216 alice = db_create_subscriber(alice_imsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000217 db_subscriber_alloc_tmsi(alice);
218 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000219 db_sync_subscriber(alice);
Jan Luebbefac25fc2008-12-27 18:04:34 +0000220 db_subscriber_assoc_imei(alice, "1234567890");
221 db_subscriber_assoc_imei(alice, "6543560920");
Alexander Chemeris82a18582014-03-08 18:48:49 +0100222 /* Get by TMSI */
223 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
224 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
225 COMPARE(alice, alice_db);
226 SUBSCR_PUT(alice_db);
227 /* Get by IMSI */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200228 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000229 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100230 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100231 /* Get by id */
232 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
233 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
234 COMPARE(alice, alice_db);
235 SUBSCR_PUT(alice_db);
236 /* Get by extension */
237 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
238 COMPARE(alice, alice_db);
239 SUBSCR_PUT(alice_db);
240 SUBSCR_PUT(alice);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000241
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100242 test_sms();
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100243 test_sms_migrate();
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100244
Jan Luebbe5c15c852008-12-27 15:59:25 +0000245 db_fini();
246
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100247 printf("Done\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000248 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000249}
Holger Freytherbab9cd92009-04-12 05:37:07 +0000250
251/* stubs */
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100252void vty_out() {}