blob: ee2fee7b2b762f496d1fa0fc91260910eadfe2a5 [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 Freyther45222a72012-01-06 14:27:08 +0100121int main()
122{
Alexander Chemeris82a18582014-03-08 18:48:49 +0100123 char scratch_str[256];
124
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100125 printf("Testing subscriber database code.\n");
126 osmo_init_logging(&log_info);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000127
Holger Freytherc7b86f92009-06-06 13:54:20 +0000128 if (db_init("hlr.sqlite3")) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000129 printf("DB: Failed to init database. Please check the option settings.\n");
130 return 1;
131 }
132 printf("DB: Database initialized.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000133
Jan Luebbe5c15c852008-12-27 15:59:25 +0000134 if (db_prepare()) {
135 printf("DB: Failed to prepare database.\n");
136 return 1;
137 }
138 printf("DB: Database prepared.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +0000139
Jan Luebbe5c15c852008-12-27 15:59:25 +0000140 struct gsm_subscriber *alice = NULL;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000141 struct gsm_subscriber *alice_db;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000142
Holger Freyther12aa50d2009-01-01 18:02:05 +0000143 char *alice_imsi = "3243245432345";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200144 alice = db_create_subscriber(alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000145 db_sync_subscriber(alice);
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200146 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000147 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100148 SUBSCR_PUT(alice_db);
149 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000150
Holger Freyther12aa50d2009-01-01 18:02:05 +0000151 alice_imsi = "3693245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200152 alice = db_create_subscriber(alice_imsi);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000153 db_subscriber_assoc_imei(alice, "1234567890");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000154 db_subscriber_alloc_tmsi(alice);
155 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000156 db_sync_subscriber(alice);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100157 /* Get by TMSI */
158 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
159 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
160 COMPARE(alice, alice_db);
161 SUBSCR_PUT(alice_db);
162 /* Get by IMSI */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200163 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000164 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100165 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100166 /* Get by id */
167 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
168 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
169 COMPARE(alice, alice_db);
170 SUBSCR_PUT(alice_db);
171 /* Get by extension */
172 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
173 COMPARE(alice, alice_db);
174 SUBSCR_PUT(alice_db);
175 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000176
Holger Freyther12aa50d2009-01-01 18:02:05 +0000177 alice_imsi = "9993245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200178 alice = db_create_subscriber(alice_imsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000179 db_subscriber_alloc_tmsi(alice);
180 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000181 db_sync_subscriber(alice);
Jan Luebbefac25fc2008-12-27 18:04:34 +0000182 db_subscriber_assoc_imei(alice, "1234567890");
183 db_subscriber_assoc_imei(alice, "6543560920");
Alexander Chemeris82a18582014-03-08 18:48:49 +0100184 /* Get by TMSI */
185 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
186 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
187 COMPARE(alice, alice_db);
188 SUBSCR_PUT(alice_db);
189 /* Get by IMSI */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200190 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000191 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100192 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100193 /* Get by id */
194 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
195 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
196 COMPARE(alice, alice_db);
197 SUBSCR_PUT(alice_db);
198 /* Get by extension */
199 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
200 COMPARE(alice, alice_db);
201 SUBSCR_PUT(alice_db);
202 SUBSCR_PUT(alice);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000203
Holger Hans Peter Freyther73bc51d2014-03-07 19:08:03 +0100204 test_sms();
205
Jan Luebbe5c15c852008-12-27 15:59:25 +0000206 db_fini();
207
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100208 printf("Done\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000209 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000210}
Holger Freytherbab9cd92009-04-12 05:37:07 +0000211
212/* stubs */
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100213void vty_out() {}