blob: e117b7933f554359fbfb821349e4c8752ed1b6ce [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>
Jan Luebbe7398eb92008-12-27 00:45:41 +000024
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010025#include <osmocom/core/application.h>
26
Jan Luebbe7398eb92008-12-27 00:45:41 +000027#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000028#include <string.h>
Harald Welte12247c62009-05-21 07:23:02 +000029#include <stdlib.h>
Alexander Chemeris82a18582014-03-08 18:48:49 +010030#include <inttypes.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000031
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010032static struct gsm_network dummy_net;
33
34#define SUBSCR_PUT(sub) \
35 sub->net = &dummy_net; \
36 subscr_put(sub);
37
Holger Freyther12aa50d2009-01-01 18:02:05 +000038#define COMPARE(original, copy) \
39 if (original->id != copy->id) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010040 printf("Ids do not match in %s:%d %llu %llu\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000041 __FUNCTION__, __LINE__, original->id, copy->id); \
42 if (original->lac != copy->lac) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010043 printf("LAC do not match in %s:%d %d %d\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000044 __FUNCTION__, __LINE__, original->lac, copy->lac); \
45 if (original->authorized != copy->authorized) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010046 printf("Authorize do not match in %s:%d %d %d\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000047 __FUNCTION__, __LINE__, original->authorized, \
48 copy->authorized); \
49 if (strcmp(original->imsi, copy->imsi) != 0) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010050 printf("IMSIs do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000051 __FUNCTION__, __LINE__, original->imsi, copy->imsi); \
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +020052 if (original->tmsi != copy->tmsi) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010053 printf("TMSIs do not match in %s:%d '%u' '%u'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000054 __FUNCTION__, __LINE__, original->tmsi, copy->tmsi); \
55 if (strcmp(original->name, copy->name) != 0) \
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010056 printf("names do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000057 __FUNCTION__, __LINE__, original->name, copy->name); \
58 if (strcmp(original->extension, copy->extension) != 0) \
Alexander Chemeris86d46c52013-10-04 02:42:24 +020059 printf("Extensions do not match in %s:%d '%s' '%s'\n", \
Holger Freyther12aa50d2009-01-01 18:02:05 +000060 __FUNCTION__, __LINE__, original->extension, copy->extension); \
61
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010062int main()
63{
Alexander Chemeris82a18582014-03-08 18:48:49 +010064 char scratch_str[256];
65
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010066 printf("Testing subscriber database code.\n");
67 osmo_init_logging(&log_info);
Jan Luebbe7398eb92008-12-27 00:45:41 +000068
Holger Freytherc7b86f92009-06-06 13:54:20 +000069 if (db_init("hlr.sqlite3")) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000070 printf("DB: Failed to init database. Please check the option settings.\n");
71 return 1;
72 }
73 printf("DB: Database initialized.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +000074
Jan Luebbe5c15c852008-12-27 15:59:25 +000075 if (db_prepare()) {
76 printf("DB: Failed to prepare database.\n");
77 return 1;
78 }
79 printf("DB: Database prepared.\n");
Jan Luebbe7398eb92008-12-27 00:45:41 +000080
Jan Luebbe5c15c852008-12-27 15:59:25 +000081 struct gsm_subscriber *alice = NULL;
Holger Freyther12aa50d2009-01-01 18:02:05 +000082 struct gsm_subscriber *alice_db;
Jan Luebbe7398eb92008-12-27 00:45:41 +000083
Holger Freyther12aa50d2009-01-01 18:02:05 +000084 char *alice_imsi = "3243245432345";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +020085 alice = db_create_subscriber(alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +000086 db_sync_subscriber(alice);
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +020087 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +000088 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +010089 SUBSCR_PUT(alice_db);
90 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +000091
Holger Freyther12aa50d2009-01-01 18:02:05 +000092 alice_imsi = "3693245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +020093 alice = db_create_subscriber(alice_imsi);
Jan Luebbe391d86e2008-12-27 22:33:34 +000094 db_subscriber_assoc_imei(alice, "1234567890");
Jan Luebbe5c15c852008-12-27 15:59:25 +000095 db_subscriber_alloc_tmsi(alice);
96 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +000097 db_sync_subscriber(alice);
Alexander Chemeris82a18582014-03-08 18:48:49 +010098 /* Get by TMSI */
99 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
100 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
101 COMPARE(alice, alice_db);
102 SUBSCR_PUT(alice_db);
103 /* Get by IMSI */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200104 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000105 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100106 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100107 /* Get by id */
108 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
109 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
110 COMPARE(alice, alice_db);
111 SUBSCR_PUT(alice_db);
112 /* Get by extension */
113 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
114 COMPARE(alice, alice_db);
115 SUBSCR_PUT(alice_db);
116 SUBSCR_PUT(alice);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000117
Holger Freyther12aa50d2009-01-01 18:02:05 +0000118 alice_imsi = "9993245423445";
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200119 alice = db_create_subscriber(alice_imsi);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000120 db_subscriber_alloc_tmsi(alice);
121 alice->lac=42;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000122 db_sync_subscriber(alice);
Jan Luebbefac25fc2008-12-27 18:04:34 +0000123 db_subscriber_assoc_imei(alice, "1234567890");
124 db_subscriber_assoc_imei(alice, "6543560920");
Alexander Chemeris82a18582014-03-08 18:48:49 +0100125 /* Get by TMSI */
126 snprintf(scratch_str, sizeof(scratch_str), "%"PRIu32, alice->tmsi);
127 alice_db = db_get_subscriber(GSM_SUBSCRIBER_TMSI, scratch_str);
128 COMPARE(alice, alice_db);
129 SUBSCR_PUT(alice_db);
130 /* Get by IMSI */
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200131 alice_db = db_get_subscriber(GSM_SUBSCRIBER_IMSI, alice_imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000132 COMPARE(alice, alice_db);
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100133 SUBSCR_PUT(alice_db);
Alexander Chemeris82a18582014-03-08 18:48:49 +0100134 /* Get by id */
135 snprintf(scratch_str, sizeof(scratch_str), "%llu", alice->id);
136 alice_db = db_get_subscriber(GSM_SUBSCRIBER_ID, scratch_str);
137 COMPARE(alice, alice_db);
138 SUBSCR_PUT(alice_db);
139 /* Get by extension */
140 alice_db = db_get_subscriber(GSM_SUBSCRIBER_EXTENSION, alice->extension);
141 COMPARE(alice, alice_db);
142 SUBSCR_PUT(alice_db);
143 SUBSCR_PUT(alice);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000144
145 db_fini();
146
Holger Hans Peter Freyther45222a72012-01-06 14:27:08 +0100147 printf("Done\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000148 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000149}
Holger Freytherbab9cd92009-04-12 05:37:07 +0000150
151/* stubs */
Holger Hans Peter Freyther763b42a2010-12-29 11:07:22 +0100152void vty_out() {}