aboutsummaryrefslogtreecommitdiffstats
path: root/tests/db/db_test.c
blob: 30de51d95a55ceab4996b5332611780ae9cd9037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>

#include <osmocom/core/application.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/logging.h>

#include "db.h"
#include "logging.h"

#define comment_start() fprintf(stderr, "\n===== %s\n", __func__);
#define comment(fmt, args...) fprintf(stderr, "\n--- " fmt "\n\n", ## args);
#define comment_end() fprintf(stderr, "===== %s: SUCCESS\n\n", __func__);

/* Perform a function call and verbosely assert that its return value is as expected.
 * The return code is then available in g_rc. */
#define ASSERT_RC(call, expect_rc) \
	do { \
		fprintf(stderr, #call " --> " #expect_rc "\n"); \
		g_rc = call; \
		if (g_rc != (expect_rc)) \
			fprintf(stderr, " MISMATCH: got rc = %d, expected: " \
                                        #expect_rc " = %d\n", g_rc, expect_rc); \
		OSMO_ASSERT(g_rc == (expect_rc)); \
		fprintf(stderr, "\n"); \
	} while (0)

/* Do db_subscr_get_by_xxxx and verbosely assert that its return value is as expected.
 * Print the subscriber struct to stderr to be validated by db_test.err.
 * The result is then available in g_subscr. */
#define ASSERT_SEL(by, val, expect_rc) \
	do { \
		int rc; \
		g_subscr = (struct hlr_subscriber){}; \
		fprintf(stderr, "db_subscr_get_by_" #by "(dbc, " #val ", &g_subscr) --> " \
                                #expect_rc "\n"); \
		rc = db_subscr_get_by_##by(dbc, val, &g_subscr); \
		if (rc != (expect_rc)) \
			fprintf(stderr, " MISMATCH: got rc = %d, expected: " \
                                        #expect_rc " = %d\n", rc, expect_rc); \
		OSMO_ASSERT(rc == (expect_rc)); \
		if (!rc) \
			dump_subscr(&g_subscr); \
		fprintf(stderr, "\n"); \
	} while (0)

static struct db_context *dbc = NULL;
static void *ctx = NULL;
static struct hlr_subscriber g_subscr;
static int g_rc;
static int64_t g_id;

#define Pfv(name, fmt, val) \
	fprintf(stderr, "  ." #name " = " fmt ",\n", val)
#define Pfo(name, fmt, obj) \
	Pfv(name, fmt, obj->name)

/* Print a subscriber struct to stderr to be validated by db_test.err. */
void dump_subscr(struct hlr_subscriber *subscr)
{
#define Ps(name) \
	if (*subscr->name) \
		Pfo(name, "'%s'", subscr)
#define Pd(name) \
	Pfv(name, "%"PRId64, (int64_t)subscr->name)
#define Pd_nonzero(name) \
	if (subscr->name) \
		Pd(name)
#define Pb(if_val, name) \
	if (subscr->name == (if_val)) \
		Pfv(name, "%s", subscr->name ? "true" : "false")

	fprintf(stderr, "struct hlr_subscriber {\n");
	Pd(id);
	Ps(imsi);
	Ps(msisdn);
	Ps(vlr_number);
	Ps(sgsn_number);
	Ps(sgsn_address);
	Pd_nonzero(periodic_lu_timer);
	Pd_nonzero(periodic_rau_tau_timer);
	Pb(false, nam_cs);
	Pb(false, nam_ps);
	if (subscr->lmsi)
		Pfo(lmsi, "0x%x", subscr);
	Pb(true, ms_purged_cs);
	Pb(true, ms_purged_ps);
	fprintf(stderr, "}\n");
#undef Ps
#undef Pd
#undef Pd_nonzero
#undef Pb
}

void dump_aud(const char *label, struct osmo_sub_auth_data *aud)
{
	if (aud->type == OSMO_AUTH_TYPE_NONE) {
		fprintf(stderr, "%s: none\n", label);
		return;
	}

	fprintf(stderr, "%s: struct osmo_sub_auth_data {\n", label);
#define Pf(name, fmt) \
	Pfo(name, fmt, aud)
#define Phex(name) \
	Pfv(name, "'%s'", osmo_hexdump_nospc(aud->name, sizeof(aud->name)))

	Pfv(type, "%s", osmo_sub_auth_type_name(aud->type));
	Pfv(algo, "%s", osmo_auth_alg_name(aud->algo));
	switch (aud->type) {
	case OSMO_AUTH_TYPE_GSM:
		Phex(u.gsm.ki);
		break;
	case OSMO_AUTH_TYPE_UMTS:
		Phex(u.umts.opc);
		Pf(u.umts.opc_is_op, "%u");
		Phex(u.umts.k);
		Phex(u.umts.amf);
		if (aud->u.umts.sqn) {
			Pf(u.umts.sqn, "%"PRIu64);
			Pf(u.umts.sqn, "0x%"PRIx64);
		}
		if (aud->u.umts.ind_bitlen)
			Pf(u.umts.ind_bitlen, "%u");
		break;
	default:
		OSMO_ASSERT(false);
	}

	fprintf(stderr, "}\n");

#undef Pf
#undef Phex
}

static const char *imsi0 = "123456789000000";
static const char *imsi1 = "123456789000001";
static const char *imsi2 = "123456789000002";
static const char *short_imsi = "123456";
static const char *unknown_imsi = "999999999";

static void test_subscr_create_update_sel_delete()
{
	int64_t id0, id1, id2, id_short;
	comment_start();

	comment("Create with valid / invalid IMSI");

	ASSERT_RC(db_subscr_create(dbc, imsi0), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	id0 = g_subscr.id;
	ASSERT_RC(db_subscr_create(dbc, imsi1), 0);
	ASSERT_SEL(imsi, imsi1, 0);
	id1 = g_subscr.id;
	ASSERT_RC(db_subscr_create(dbc, imsi2), 0);
	ASSERT_SEL(imsi, imsi2, 0);
	id2 = g_subscr.id;
	ASSERT_RC(db_subscr_create(dbc, imsi0), -EIO);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_create(dbc, imsi1), -EIO);
	ASSERT_RC(db_subscr_create(dbc, imsi1), -EIO);
	ASSERT_SEL(imsi, imsi1, 0);
	ASSERT_RC(db_subscr_create(dbc, imsi2), -EIO);
	ASSERT_RC(db_subscr_create(dbc, imsi2), -EIO);
	ASSERT_SEL(imsi, imsi2, 0);

	ASSERT_RC(db_subscr_create(dbc, "123456789 000003"), -EINVAL);
	ASSERT_SEL(imsi, "123456789000003", -ENOENT);

	ASSERT_RC(db_subscr_create(dbc, "123456789000002123456"), -EINVAL);
	ASSERT_SEL(imsi, "123456789000002123456", -ENOENT);

	ASSERT_RC(db_subscr_create(dbc, "foobar123"), -EINVAL);
	ASSERT_SEL(imsi, "foobar123", -ENOENT);

	ASSERT_RC(db_subscr_create(dbc, "123"), -EINVAL);
	ASSERT_SEL(imsi, "123", -ENOENT);

	ASSERT_RC(db_subscr_create(dbc, short_imsi), 0);
	ASSERT_SEL(imsi, short_imsi, 0);
	id_short = g_subscr.id;


	comment("Set valid / invalid MSISDN");

	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, imsi0, "54321"), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_SEL(msisdn, "54321", 0);
	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, imsi0,
					  "54321012345678912345678"), -EINVAL);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_SEL(msisdn, "54321", 0);
	ASSERT_SEL(msisdn, "54321012345678912345678", -ENOENT);
	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, imsi0,
					  "543 21"), -EINVAL);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_SEL(msisdn, "543 21", -ENOENT);
	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, imsi0,
					  "foobar123"), -EINVAL);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_SEL(msisdn, "foobar123", -ENOENT);
	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, imsi0,
					  "5"), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_SEL(msisdn, "5", 0);
	ASSERT_SEL(msisdn, "54321", -ENOENT);
	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, imsi0,
					  "543210123456789"), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_SEL(msisdn, "543210123456789", 0);
	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, imsi0,
					  "5432101234567891"), -EINVAL);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_SEL(msisdn, "5432101234567891", -ENOENT);

	comment("Set MSISDN on non-existent / invalid IMSI");

	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, unknown_imsi, "99"), -ENOENT);
	ASSERT_SEL(msisdn, "99", -ENOENT);

	ASSERT_RC(db_subscr_update_msisdn_by_imsi(dbc, "foobar", "99"), -ENOENT);
	ASSERT_SEL(msisdn, "99", -ENOENT);

	comment("Set / unset nam_cs and nam_ps");

	/*                                nam_val, is_ps */
	ASSERT_RC(db_subscr_nam(dbc, imsi0, false, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, false, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, true, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, true, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);

	comment("Set / unset nam_cs and nam_ps *again*");
	ASSERT_RC(db_subscr_nam(dbc, imsi0, false, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, false, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, false, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, false, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, true, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, true, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, true, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_nam(dbc, imsi0, true, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);

	comment("Set nam_cs and nam_ps on non-existent / invalid IMSI");

	ASSERT_RC(db_subscr_nam(dbc, unknown_imsi, false, true), -ENOENT);
	ASSERT_RC(db_subscr_nam(dbc, unknown_imsi, false, false), -ENOENT);
	ASSERT_SEL(imsi, unknown_imsi, -ENOENT);

	ASSERT_RC(db_subscr_nam(dbc, "foobar", false, true), -ENOENT);
	ASSERT_RC(db_subscr_nam(dbc, "foobar", false, false), -ENOENT);

	comment("Delete non-existent / invalid IDs");

	ASSERT_RC(db_subscr_delete_by_id(dbc, 999), -ENOENT);
	ASSERT_RC(db_subscr_delete_by_id(dbc, -10), -ENOENT);

	comment("Delete subscribers");

	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_delete_by_id(dbc, id0), 0);
	ASSERT_SEL(imsi, imsi0, -ENOENT);
	ASSERT_RC(db_subscr_delete_by_id(dbc, id0), -ENOENT);

	ASSERT_SEL(imsi, imsi1, 0);
	ASSERT_RC(db_subscr_delete_by_id(dbc, id1), 0);
	ASSERT_SEL(imsi, imsi1, -ENOENT);

	ASSERT_SEL(imsi, imsi2, 0);
	ASSERT_RC(db_subscr_delete_by_id(dbc, id2), 0);
	ASSERT_SEL(imsi, imsi2, -ENOENT);

	ASSERT_SEL(imsi, short_imsi, 0);
	ASSERT_RC(db_subscr_delete_by_id(dbc, id_short), 0);
	ASSERT_SEL(imsi, short_imsi, -ENOENT);

	comment_end();
}

static struct {
	bool verbose;
} cmdline_opts = {
	.verbose = false,
};

static void print_help(const char *program)
{
	printf("Usage:\n"
	       "  %s [-v] [N [N...]]\n"
	       "Options:\n"
	       "  -h --help      show this text.\n"
	       "  -v --verbose   print source file and line numbers\n",
	       program
	       );
}

static void handle_options(int argc, char **argv)
{
	while (1) {
		int option_index = 0, c;
		static struct option long_options[] = {
			{"help", 0, 0, 'h'},
			{"verbose", 1, 0, 'v'},
			{0, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "hv",
				long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'h':
			print_help(argv[0]);
			exit(0);
		case 'v':
			cmdline_opts.verbose = true;
			break;
		default:
			/* catch unknown options *as well as* missing arguments. */
			fprintf(stderr, "Error in command line options. Exiting.\n");
			exit(-1);
			break;
		}
	}

	if (optind < argc) {
		fprintf(stderr, "too many args\n");
		exit(-1);
	}
}

int main(int argc, char **argv)
{
	printf("db_test.c\n");

	ctx = talloc_named_const(NULL, 1, "db_test");

	handle_options(argc, argv);

	osmo_init_logging(&hlr_log_info);
	log_set_print_filename(osmo_stderr_target, cmdline_opts.verbose);
	log_set_print_timestamp(osmo_stderr_target, 0);
	log_set_use_color(osmo_stderr_target, 0);
	log_set_print_category(osmo_stderr_target, 1);

	/* omit the SQLite version and compilation flags from test output */
	log_set_log_level(osmo_stderr_target, LOGL_ERROR);
	dbc = db_open(ctx, "db_test.db");
	log_set_log_level(osmo_stderr_target, 0);
	OSMO_ASSERT(dbc);

	test_subscr_create_update_sel_delete();

	printf("Done\n");
	return 0;
}

/* stubs */
int auc_compute_vectors(struct osmo_auth_vector *vec, unsigned int num_vec,
			struct osmo_sub_auth_data *aud2g,
			struct osmo_sub_auth_data *aud3g,
			const uint8_t *rand_auts, const uint8_t *auts)
{ OSMO_ASSERT(false); return -1; }