aboutsummaryrefslogtreecommitdiffstats
path: root/tests/db/db_test.c
blob: 1bdabdb42ac4bb3ae2c742e726a494383c5d9edf (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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/* (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)

/* Do db_get_auth_data() and verbosely assert that its return value is as expected.
 * Print the subscriber struct to stderr to be validated by db_test.err.
 * The results are then available in g_aud2g and g_aud3g. */
#define ASSERT_SEL_AUD(imsi, expect_rc, expect_id) \
	do { \
		g_aud2g = (struct osmo_sub_auth_data){}; \
		g_aud3g = (struct osmo_sub_auth_data){}; \
		g_id = 0; \
		ASSERT_RC(db_get_auth_data(dbc, imsi, &g_aud2g, &g_aud3g, &g_id), expect_rc); \
		if (!g_rc) { \
			dump_aud("2G", &g_aud2g); \
			dump_aud("3G", &g_aud3g); \
		}\
		if (g_id != expect_id) {\
			fprintf(stderr, "MISMATCH: got subscriber id %"PRId64 \
				", expected %"PRId64"\n", g_id, (int64_t)(expect_id)); \
			OSMO_ASSERT(g_id == expect_id); \
		} \
		fprintf(stderr, "\n"); \
	} while (0)

static struct db_context *dbc = NULL;
static void *ctx = NULL;
static struct hlr_subscriber g_subscr;
static struct osmo_sub_auth_data g_aud2g;
static struct osmo_sub_auth_data g_aud3g;
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("Record LU for PS and CS (SGSN and VLR names)");

	ASSERT_RC(db_subscr_lu(dbc, id0, "5952", true), 0);
	ASSERT_SEL(id, id0, 0);
	ASSERT_RC(db_subscr_lu(dbc, id0, "712", false), 0);
	ASSERT_SEL(id, id0, 0);

	comment("Record LU for PS and CS (SGSN and VLR names) *again*");

	ASSERT_RC(db_subscr_lu(dbc, id0, "111", true), 0);
	ASSERT_SEL(id, id0, 0);
	ASSERT_RC(db_subscr_lu(dbc, id0, "111", true), 0);
	ASSERT_SEL(id, id0, 0);
	ASSERT_RC(db_subscr_lu(dbc, id0, "222", false), 0);
	ASSERT_SEL(id, id0, 0);
	ASSERT_RC(db_subscr_lu(dbc, id0, "222", false), 0);
	ASSERT_SEL(id, id0, 0);

	comment("Unset LU info for PS and CS (SGSN and VLR names)");
	ASSERT_RC(db_subscr_lu(dbc, id0, "", true), 0);
	ASSERT_SEL(id, id0, 0);
	ASSERT_RC(db_subscr_lu(dbc, id0, "", false), 0);
	ASSERT_SEL(id, id0, 0);

	ASSERT_RC(db_subscr_lu(dbc, id0, "111", true), 0);
	ASSERT_RC(db_subscr_lu(dbc, id0, "222", false), 0);
	ASSERT_SEL(id, id0, 0);
	ASSERT_RC(db_subscr_lu(dbc, id0, NULL, true), 0);
	ASSERT_SEL(id, id0, 0);
	ASSERT_RC(db_subscr_lu(dbc, id0, NULL, false), 0);
	ASSERT_SEL(id, id0, 0);

	comment("Record LU for non-existent ID");
	ASSERT_RC(db_subscr_lu(dbc, 99999, "5952", true), -ENOENT);
	ASSERT_RC(db_subscr_lu(dbc, 99999, "712", false), -ENOENT);
	ASSERT_SEL(id, 99999, -ENOENT);

	comment("Purge and un-purge PS and CS");

	/*                               purge_val, is_ps */
	ASSERT_RC(db_subscr_purge(dbc, imsi0, true, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, true, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, false, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, false, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);

	comment("Purge PS and CS *again*");

	ASSERT_RC(db_subscr_purge(dbc, imsi0, true, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, true, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, false, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, false, true), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, true, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, true, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, false, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);
	ASSERT_RC(db_subscr_purge(dbc, imsi0, false, false), 0);
	ASSERT_SEL(imsi, imsi0, 0);

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

	ASSERT_RC(db_subscr_purge(dbc, unknown_imsi, true, true), -ENOENT);
	ASSERT_SEL(imsi, unknown_imsi, -ENOENT);
	ASSERT_RC(db_subscr_purge(dbc, unknown_imsi, true, false), -ENOENT);
	ASSERT_SEL(imsi, unknown_imsi, -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 const struct sub_auth_data_str *mk_aud_2g(enum osmo_auth_algo algo,
						 const char *ki)
{
	static struct sub_auth_data_str aud;
	aud = (struct sub_auth_data_str){
		.type = OSMO_AUTH_TYPE_GSM,
		.algo = algo,
		.u.gsm.ki = ki,
	};
	return &aud;
}

static const struct sub_auth_data_str *mk_aud_3g(enum osmo_auth_algo algo,
						 const char *opc, bool opc_is_op,
						 const char *k, unsigned int ind_bitlen)
{
	static struct sub_auth_data_str aud;
	aud = (struct sub_auth_data_str){
		.type = OSMO_AUTH_TYPE_UMTS,
		.algo = algo,
		.u.umts.k = k,
		.u.umts.opc = opc,
		.u.umts.opc_is_op = opc_is_op ? 1 : 0,
		.u.umts.ind_bitlen = ind_bitlen,
	};
	return &aud;
}

static void test_subscr_aud()
{
	int64_t id;

	comment_start();

	comment("Get auth data for non-existent subscriber");
	ASSERT_SEL_AUD(unknown_imsi, -ENOENT, 0);

	comment("Create subscriber");

	ASSERT_RC(db_subscr_create(dbc, imsi0), 0);
	ASSERT_SEL(imsi, imsi0, 0);

	id = g_subscr.id;
	ASSERT_SEL_AUD(imsi0, -ENOENT, id);


	comment("Set auth data, 2G only");

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_COMP128v1, "0123456789abcdef0123456789abcdef")),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	/* same again */
	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_COMP128v1, "0123456789abcdef0123456789abcdef")),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_COMP128v2, "BeadedBeeAced1EbbedDefacedFacade")),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_COMP128v3, "DeafBeddedBabeAcceededFadedDecaf")),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_XOR, "CededEffacedAceFacedBadFadedBeef")),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	comment("Remove 2G auth data");

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_NONE, NULL)),
		0);
	ASSERT_SEL_AUD(imsi0, -ENOENT, id);

	/* Removing nothing results in -ENOENT */
	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_NONE, NULL)),
		-ENOENT);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_XOR, "CededEffacedAceFacedBadFadedBeef")),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_NONE, "f000000000000f00000000000f000000")),
		0);
	ASSERT_SEL_AUD(imsi0, -ENOENT, id);


	comment("Set auth data, 3G only");

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "BeefedCafeFaceAcedAddedDecadeFee", true,
			  "C01ffedC1cadaeAc1d1f1edAcac1aB0a", 5)),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	/* same again */
	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "BeefedCafeFaceAcedAddedDecadeFee", true,
			  "C01ffedC1cadaeAc1d1f1edAcac1aB0a", 5)),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "Deaf0ff1ceD0d0DabbedD1ced1ceF00d", true,
			  "F1bbed0afD0eF0bD0ffed0ddF1fe0b0e", 0)),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "BeefedCafeFaceAcedAddedDecadeFee", false,
			  "DeafBeddedBabeAcceededFadedDecaf",
			  OSMO_MILENAGE_IND_BITLEN_MAX)),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "CededEffacedAceFacedBadFadedBeef", false,
			  "BeefedCafeFaceAcedAddedDecadeFee", 5)),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	comment("Remove 3G auth data");

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_NONE, NULL, false, NULL, 0)),
		0);
	ASSERT_SEL_AUD(imsi0, -ENOENT, id);

	/* Removing nothing results in -ENOENT */
	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_NONE, NULL, false, NULL, 0)),
		-ENOENT);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "CededEffacedAceFacedBadFadedBeef", false,
			  "BeefedCafeFaceAcedAddedDecadeFee", 5)),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_NONE,
			  "asdfasdfasd", false,
			  "asdfasdfasdf", 99999)),
		0);
	ASSERT_SEL_AUD(imsi0, -ENOENT, id);


	comment("Set auth data, 2G and 3G");

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_COMP128v3, "CededEffacedAceFacedBadFadedBeef")),
		0);
	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "BeefedCafeFaceAcedAddedDecadeFee", false,
			  "DeafBeddedBabeAcceededFadedDecaf", 5)),
		0);
	ASSERT_SEL_AUD(imsi0, 0, id);


	comment("Set invalid auth data");

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(99999, "f000000000000f00000000000f000000")),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_XOR, "f000000000000f00000000000f000000f00000000")),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_XOR, "f00")),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_2g(OSMO_AUTH_ALG_MILENAGE, "0123456789abcdef0123456789abcdef")),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "0f000000000000f00000000000f000000", false,
			  "f000000000000f00000000000f000000", 5)),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "f000000000000f00000000000f000000", false,
			  "000000000000f00000000000f000000", 5)),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "f000000000000f00000000000f000000", false,
			  "f000000000000f00000000000f000000",
			  OSMO_MILENAGE_IND_BITLEN_MAX + 1)),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "X000000000000f00000000000f000000", false,
			  "f000000000000f00000000000f000000", 5)),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	ASSERT_RC(db_subscr_update_aud_by_id(dbc, id,
		mk_aud_3g(OSMO_AUTH_ALG_MILENAGE,
			  "f000000000000f00000000000f000000", false,
			  "f000000000000 f00000000000 f000000", 5)),
		-EINVAL);
	ASSERT_SEL_AUD(imsi0, 0, id);

	comment("Delete subscriber");

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

	comment("Re-add subscriber and verify auth data didn't come back");

	ASSERT_RC(db_subscr_create(dbc, imsi0), 0);
	ASSERT_SEL(imsi, imsi0, 0);

	/* For this test to work, we want to get the same subscriber ID back,
	 * and make sure there are no auth data leftovers for this ID. */
	OSMO_ASSERT(id == g_subscr.id);
	ASSERT_SEL_AUD(imsi0, -ENOENT, id);

	ASSERT_RC(db_subscr_delete_by_id(dbc, id), 0);
	ASSERT_SEL(imsi, imsi0, -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();
	test_subscr_aud();

	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; }