blob: 75c92d309cf78f743571289f5d837ea0aa5a9176 [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;
34
35#define SUBSCR_PUT(sub) \
36 sub->net = &dummy_net; \
37 subscr_put(sub);
38
Holger Freyther12aa50d2009-01-01 18:02:05 +000039#define COMPARE(original, copy) \
40 if (original->id != copy->id) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010041 printf("Ids do not match in %s:%d %llu %llu\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000042 __FUNCTION__, __LINE__, original->id, copy->id); \
43 if (original->lac != copy->lac) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010044 printf("LAC do not match in %s:%d %d %d\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000045 __FUNCTION__, __LINE__, original->lac, copy->lac); \
46 if (original->authorized != copy->authorized) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010047 printf("Authorize do not match in %s:%d %d %d\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000048 __FUNCTION__, __LINE__, original->authorized, \
49 copy->authorized); \
50 if (strcmp(original->imsi, copy->imsi) != 0) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010051 printf("IMSIs do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000052 __FUNCTION__, __LINE__, original->imsi, copy->imsi); \
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +020053 if (original->tmsi != copy->tmsi) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010054 printf("TMSIs do not match in %s:%d '%u' '%u'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000055 __FUNCTION__, __LINE__, original->tmsi, copy->tmsi); \
56 if (strcmp(original->name, copy->name) != 0) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010057 printf("names do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000058 __FUNCTION__, __LINE__, original->name, copy->name); \
59 if (strcmp(original->extension, copy->extension) != 0) \
Alexander Chemeris86d46c52013-10-04 02:42:24 +020060 printf("Extensions do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000061 __FUNCTION__, __LINE__, original->extension, copy->extension); \
62
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +010063/*
64 * Create/Store a SMS and then try to load it.
65 */
66static 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 Freyther61144012014-03-08 16:41:37 +0100121static 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 Freyther45222a72012-01-06 14:27:08 +0100154int main()
155{
Alexander Chemeris82a18582014-03-08 18:48:49 +0100156 char scratch_str[256];
157
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100158 printf("Testing subscriber database code.\n");
159 osmo_init_logging(&log_info);
Holger Hans Peter Freyther3c9068f2014-05-01 07:53:42 +0200160 log_set_print_filename(osmo_stderr_target, 0);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000161
Holger Freytherc7b86f92009-06-06 13:54:20 +0000162 if (db_init("hlr.sqlite3")) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000163 printf("DB: Failed to init database. Please check the option settings.\n");
164 return 1;
165 }
166 printf("DB: Database initialized.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000167
Jan Luebbe5c15c852008-12-27 15:59:25 +0000168 if (db_prepare()) {
169 printf("DB: Failed to prepare database.\n");
170 return 1;
171 }
172 printf("DB: Database prepared.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000173
Jan Luebbe5c15c852008-12-27 15:59:25 +0000174 struct gsm_subscriber *alice = NULL;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000175 struct gsm_subscriber *alice_db;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000176
Holger Freyther12aa50d2009-01-01 18:02:05 +0000177 char *alice_imsi = "3243245432345";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200178 alice = db_create_subscriber(alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000179 db_sync_subscriber(alice);
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200180 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000181 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100182 SUBSCR_PUT(alice_db);
183 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000184
Holger Freyther12aa50d2009-01-01 18:02:05 +0000185 alice_imsi = "3693245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200186 alice = db_create_subscriber(alice_imsi);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000187 db_subscriber_assoc_imei(alice, "1234567890");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000188 db_subscriber_alloc_tmsi(alice);
189 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000190 db_sync_subscriber(alice);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100191 /* 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 Freyther7634ec12013-10-04 08:35:11 +0200197 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000198 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100199 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100200 /* 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 Luebbe7398eb92008-12-27 00:45:41 +0000210
Holger Freyther12aa50d2009-01-01 18:02:05 +0000211 alice_imsi = "9993245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200212 alice = db_create_subscriber(alice_imsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000213 db_subscriber_alloc_tmsi(alice);
214 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000215 db_sync_subscriber(alice);
Jan Luebbefac25fc2008-12-27 18:04:34 +0000216 db_subscriber_assoc_imei(alice, "1234567890");
217 db_subscriber_assoc_imei(alice, "6543560920");
Alexander Chemeris82a18582014-03-08 18:48:49 +0100218 /* 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 Freyther7634ec12013-10-04 08:35:11 +0200224 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000225 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100226 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100227 /* 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 Luebbe5c15c852008-12-27 15:59:25 +0000237
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100238 test_sms();
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100239 test_sms_migrate();
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100240
Jan Luebbe5c15c852008-12-27 15:59:25 +0000241 db_fini();
242
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100243 printf("Done\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000244 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000245}
Holger Freytherbab9cd92009-04-12 05:37:07 +0000246
247/* stubs */
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100248void vty_out() {}