blob: 1494e77f74e2ea0619567a6cd3b476978f5a34bd [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);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000160
Holger Freytherc7b86f92009-06-06 13:54:20 +0000161 if (db_init("hlr.sqlite3")) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000162 printf("DB: Failed to init database. Please check the option settings.\n");
163 return 1;
164 }
165 printf("DB: Database initialized.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000166
Jan Luebbe5c15c852008-12-27 15:59:25 +0000167 if (db_prepare()) {
168 printf("DB: Failed to prepare database.\n");
169 return 1;
170 }
171 printf("DB: Database prepared.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000172
Jan Luebbe5c15c852008-12-27 15:59:25 +0000173 struct gsm_subscriber *alice = NULL;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000174 struct gsm_subscriber *alice_db;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000175
Holger Freyther12aa50d2009-01-01 18:02:05 +0000176 char *alice_imsi = "3243245432345";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200177 alice = db_create_subscriber(alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000178 db_sync_subscriber(alice);
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200179 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000180 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100181 SUBSCR_PUT(alice_db);
182 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000183
Holger Freyther12aa50d2009-01-01 18:02:05 +0000184 alice_imsi = "3693245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200185 alice = db_create_subscriber(alice_imsi);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000186 db_subscriber_assoc_imei(alice, "1234567890");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000187 db_subscriber_alloc_tmsi(alice);
188 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000189 db_sync_subscriber(alice);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100190 /* Get by TMSI */
191 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
192 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
193 COMPARE(alice, alice_db);
194 SUBSCR_PUT(alice_db);
195 /* Get by IMSI */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200196 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000197 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100198 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100199 /* Get by id */
200 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
201 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
202 COMPARE(alice, alice_db);
203 SUBSCR_PUT(alice_db);
204 /* Get by extension */
205 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
206 COMPARE(alice, alice_db);
207 SUBSCR_PUT(alice_db);
208 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000209
Holger Freyther12aa50d2009-01-01 18:02:05 +0000210 alice_imsi = "9993245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200211 alice = db_create_subscriber(alice_imsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000212 db_subscriber_alloc_tmsi(alice);
213 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000214 db_sync_subscriber(alice);
Jan Luebbefac25fc2008-12-27 18:04:34 +0000215 db_subscriber_assoc_imei(alice, "1234567890");
216 db_subscriber_assoc_imei(alice, "6543560920");
Alexander Chemeris82a18582014-03-08 18:48:49 +0100217 /* Get by TMSI */
218 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
219 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
220 COMPARE(alice, alice_db);
221 SUBSCR_PUT(alice_db);
222 /* Get by IMSI */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200223 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000224 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100225 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100226 /* Get by id */
227 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
228 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
229 COMPARE(alice, alice_db);
230 SUBSCR_PUT(alice_db);
231 /* Get by extension */
232 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
233 COMPARE(alice, alice_db);
234 SUBSCR_PUT(alice_db);
235 SUBSCR_PUT(alice);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000236
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100237 test_sms();
Holger Hans Peter Freyther61144012014-03-08 16:41:37 +0100238 test_sms_migrate();
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100239
Jan Luebbe5c15c852008-12-27 15:59:25 +0000240 db_fini();
241
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100242 printf("Done\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000243 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000244}
Holger Freytherbab9cd92009-04-12 05:37:07 +0000245
246/* stubs */
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100247void vty_out() {}