summaryrefslogtreecommitdiffstats
path: root/openbsc/tests/bsc-nat/bsc_nat_test.c
blob: a4b313c91e9fcd4b15c00593b90841745f4e8770 (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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
/*
 * BSC NAT Message filtering
 *
 * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2010-2013 by On-Waves
 *
 * All Rights Reserved
 *
 * 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 <openbsc/debug.h>
#include <openbsc/gsm_data.h>
#include <openbsc/bsc_nat.h>
#include <openbsc/bsc_nat_sccp.h>
#include <openbsc/bsc_msg_filter.h>
#include <openbsc/nat_rewrite_trie.h>

#include <osmocom/core/application.h>
#include <osmocom/core/backtrace.h>
#include <osmocom/core/talloc.h>

#include <osmocom/sccp/sccp.h>
#include <osmocom/gsm/protocol/gsm_08_08.h>

#include <stdio.h>

/* test messages for ipa */
static uint8_t ipa_id[] = {
	0x00, 0x01, 0xfe, 0x06,
};

/* SCCP messages are below */
static uint8_t gsm_reset[] = {
	0x00, 0x12, 0xfd,
	0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
	0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
	0x01, 0x20,
};

static const uint8_t gsm_reset_ack[] = {
	0x00, 0x13, 0xfd,
	0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
	0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
	0x00, 0x01, 0x31,
};

static const uint8_t gsm_paging[] = {
	0x00, 0x20, 0xfd,
	0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
	0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10,
	0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10,
	0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06,
};

/* BSC -> MSC connection open */
static const uint8_t bssmap_cr[] = {
	0x00, 0x2c, 0xfd,
	0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02,
	0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05,
	0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3,
	0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33,
	0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01,
	0x31, 0x97, 0x61, 0x00
};

/* MSC -> BSC connection confirm */
static const uint8_t bssmap_cc[] = {
	0x00, 0x0a, 0xfd,
	0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
};

/* MSC -> BSC released */
static const uint8_t bssmap_released[] = {
	0x00, 0x0e, 0xfd,
	0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f,
	0x02, 0x23, 0x42, 0x00,
};

/* BSC -> MSC released */
static const uint8_t bssmap_release_complete[] = {
	0x00, 0x07, 0xfd,
	0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03
};

/* both directions IT timer */
static const uint8_t connnection_it[] = {
	0x00, 0x0b, 0xfd,
	0x10, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03,
	0x00, 0x00, 0x00, 0x00,
};

/* error in both directions */
static const uint8_t proto_error[] = {
	0x00, 0x05, 0xfd,
	0x0f, 0x22, 0x33, 0x44, 0x00,
};

/* MGCP wrap... */
static const uint8_t mgcp_msg[] = {
	0x00, 0x03, 0xfc,
	0x20, 0x20, 0x20,
};

/* location updating request */
static const uint8_t bss_lu[] = {
	0x00, 0x2e, 0xfd,
	0x01, 0x91, 0x45, 0x14, 0x02, 0x02, 0x04, 0x02,
	0x42, 0xfe, 0x0f, 0x21, 0x00, 0x1f, 0x57, 0x05,
	0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x14, 0xc3,
	0x50, 0x17, 0x12, 0x05, 0x08, 0x70, 0x72, 0xf4,
	0x80, 0xff, 0xfe, 0x30, 0x08, 0x29, 0x44, 0x50,
	0x12, 0x03, 0x24, 0x01, 0x95, 0x00
};

/* paging response */
static const uint8_t pag_resp[] = {
	0x00, 0x2c, 0xfd, 0x01, 0xe5, 0x68,
	0x14, 0x02, 0x02, 0x04, 0x02, 0x42, 0xfe, 0x0f,
	0x1f, 0x00, 0x1d, 0x57, 0x05, 0x08, 0x00, 0x72,
	0xf4, 0x80, 0x20, 0x16, 0xc3, 0x50, 0x17, 0x10,
	0x06, 0x27, 0x01, 0x03, 0x30, 0x18, 0x96, 0x08,
	0x29, 0x26, 0x30, 0x32, 0x11, 0x42, 0x01, 0x19,
	0x00
};

struct filter_result {
	const uint8_t *data;
	const uint16_t length;
	const int dir;
	const int result;
};

static const struct filter_result results[] = {
	{
		.data = ipa_id,
		.length = ARRAY_SIZE(ipa_id),
		.dir = DIR_MSC,
		.result = 1,
	},
	{
		.data = gsm_reset,
		.length = ARRAY_SIZE(gsm_reset),
		.dir = DIR_MSC,
		.result = 1,
	},
	{
		.data = gsm_reset_ack,
		.length = ARRAY_SIZE(gsm_reset_ack),
		.dir = DIR_BSC,
		.result = 1,
	},
	{
		.data = gsm_paging,
		.length = ARRAY_SIZE(gsm_paging),
		.dir = DIR_BSC,
		.result = 0,
	},
	{
		.data = bssmap_cr,
		.length = ARRAY_SIZE(bssmap_cr),
		.dir = DIR_MSC,
		.result = 0,
	},
	{
		.data = bssmap_cc,
		.length = ARRAY_SIZE(bssmap_cc),
		.dir = DIR_BSC,
		.result = 0,
	},
	{
		.data = bssmap_released,
		.length = ARRAY_SIZE(bssmap_released),
		.dir = DIR_MSC,
		.result = 0,
	},
	{
		.data = bssmap_release_complete,
		.length = ARRAY_SIZE(bssmap_release_complete),
		.dir = DIR_BSC,
		.result = 0,
	},
	{
		.data = mgcp_msg,
		.length = ARRAY_SIZE(mgcp_msg),
		.dir = DIR_MSC,
		.result = 0,
	},
	{
		.data = connnection_it,
		.length = ARRAY_SIZE(connnection_it),
		.dir = DIR_BSC,
		.result = 0,
	},
	{
		.data = connnection_it,
		.length = ARRAY_SIZE(connnection_it),
		.dir = DIR_MSC,
		.result = 0,
	},
	{
		.data = proto_error,
		.length = ARRAY_SIZE(proto_error),
		.dir = DIR_BSC,
		.result = 0,
	},
	{
		.data = proto_error,
		.length = ARRAY_SIZE(proto_error),
		.dir = DIR_MSC,
		.result = 0,
	},

};

static void test_filter(void)
{
	int i;


	/* start testinh with proper messages */
	printf("Testing BSS Filtering.\n");
	for (i = 0; i < ARRAY_SIZE(results); ++i) {
		int result;
		struct bsc_nat_parsed *parsed;
		struct msgb *msg = msgb_alloc(4096, "test-message");

		printf("Going to test item: %d\n", i);
		memcpy(msg->data, results[i].data, results[i].length);
		msg->l2h = msgb_put(msg, results[i].length);

		parsed = bsc_nat_parse(msg);
		if (!parsed) {
			printf("FAIL: Failed to parse the message\n");
			continue;
		}

		result = bsc_nat_filter_ipa(results[i].dir, msg, parsed);
		if (result != results[i].result) {
			printf("FAIL: Not the expected result got: %d wanted: %d\n",
				result, results[i].result);
		}

		msgb_free(msg);
	}
}

#include "bsc_data.c"

static void copy_to_msg(struct msgb *msg, const uint8_t *data, unsigned int length)
{
	msgb_reset(msg);
	msg->l2h = msgb_put(msg, length);
	memcpy(msg->l2h, data, msgb_l2len(msg));
}

static void verify_msg(struct msgb *out, const uint8_t *ref, int ref_len)
{
	if (out->len != ref_len) {
		printf("FAIL: The size should match: %d vs. %d\n",
			out->len, ref_len);
		printf("%s\n", osmo_hexdump(out->data, out->len));
		printf("Wanted\n");
		printf("%s\n", osmo_hexdump(ref, ref_len));
		abort();
	}

	if (memcmp(out->data, ref, out->len) != 0) {
		printf("FAIL: the data should be changed.\n");
		printf("%s\n", osmo_hexdump(out->data, out->len));
		printf("Wanted\n");
		printf("%s\n", osmo_hexdump(ref, ref_len));
		abort();
	}
}


#define VERIFY(con_found, con, msg, ver, str) \
	if (!con_found) {						\
		printf("Failed to find connection.\n");			\
		abort();						\
	}								\
	if (con_found->bsc != con) {					\
		printf("Got connection of the wrong BSC: %d\n",		\
			con_found->bsc->cfg->nr);			\
		abort();						\
	}								\
	if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \
		printf("Failed to patch the %s msg.\n", str); 		\
		abort(); \
	}

/* test conn tracking once */
static void test_contrack()
{
	struct bsc_nat *nat;
	struct bsc_connection *con;
	struct nat_sccp_connection *con_found;
	struct nat_sccp_connection *rc_con;
	struct bsc_nat_parsed *parsed;
	struct msgb *msg;

	printf("Testing connection tracking.\n");
	nat = bsc_nat_alloc();
	con = bsc_connection_alloc(nat);
	con->cfg = bsc_config_alloc(nat, "foo");
	bsc_config_add_lac(con->cfg, 23);
	bsc_config_add_lac(con->cfg, 49);
	bsc_config_add_lac(con->cfg, 42);
	bsc_config_del_lac(con->cfg, 49);
	bsc_config_add_lac(con->cfg, 1111);
	msg = msgb_alloc(4096, "test");

	/* 1.) create a connection */
	copy_to_msg(msg, bsc_cr, sizeof(bsc_cr));
	parsed = bsc_nat_parse(msg);
	con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
	if (con_found != NULL) {
		printf("Con should not exist realref(%u)\n",
		       sccp_src_ref_to_int(&con_found->real_ref));
		abort();
	}
	rc_con = create_sccp_src_ref(con, parsed);
	if (!rc_con) {
		printf("Failed to create a ref\n");
		abort();
	}
	con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
	if (!con_found) {
		printf("Failed to find connection.\n");
		abort();
	}
	if (con_found->bsc != con) {
		printf("Got connection of the wrong BSC: %d\n",
			con_found->bsc->cfg->nr);
		abort();
	}
	if (con_found != rc_con) {
		printf("Failed to find the right connection.\n");
		abort();
	}
	if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) {
		printf("Failed to patch the BSC CR msg.\n");
		abort();
	}
	talloc_free(parsed);

	/* 2.) get the cc */
	copy_to_msg(msg, msc_cc, sizeof(msc_cc));
	parsed = bsc_nat_parse(msg);
	con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
	VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC");
	if (update_sccp_src_ref(con_found, parsed) != 0) {
		printf("Failed to update the SCCP con.\n");
		abort();
	}

	/* 3.) send some data */
	copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap));
	parsed = bsc_nat_parse(msg);
	con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
	VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP");

	/* 4.) receive some data */
	copy_to_msg(msg, msc_dtap, sizeof(msc_dtap));
	parsed = bsc_nat_parse(msg);
	con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
	VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP");

	/* 5.) close the connection */
	copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd));
	parsed = bsc_nat_parse(msg);
	con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
	VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD");

	/* 6.) confirm the connection close */
	copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
	parsed = bsc_nat_parse(msg);
	con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
	if (!con_found) {
		printf("Failed to find connection.\n");
		abort();
	}
	if (con_found->bsc != con) {
		printf("Got connection of the wrong BSC: %d\n",
			con_found->bsc->cfg->nr);
		abort();
	}
	if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) {
		printf("Failed to patch the BSC CR msg.\n");
		abort();
	}
	remove_sccp_src_ref(con, msg, parsed);
	talloc_free(parsed);

	copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
	parsed = bsc_nat_parse(msg);
	con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);

	/* verify that it is gone */
	if (con_found != NULL) {
		printf("Con should not exist real_ref(%u)\n",
		       sccp_src_ref_to_int(&con_found->real_ref));
		abort();
	}
	talloc_free(parsed);


	bsc_config_free(con->cfg);
	bsc_nat_free(nat);
	msgb_free(msg);
}

static void test_paging(void)
{
	struct bsc_nat *nat;
	struct bsc_connection *con;
	struct bsc_config *cfg;

	printf("Testing paging by lac.\n");

	nat = bsc_nat_alloc();
	con = bsc_connection_alloc(nat);
	cfg = bsc_config_alloc(nat, "unknown");
	con->cfg = cfg;
	bsc_config_add_lac(cfg, 23);
	con->authenticated = 1;
	llist_add(&con->list_entry, &nat->bsc_connections);

	/* Test it by not finding it */
	if (bsc_config_handles_lac(cfg, 8213) != 0) {
		printf("Should not be handled.\n");
		abort();
	}

	/* Test by finding it */
	bsc_config_del_lac(cfg, 23);
	bsc_config_add_lac(cfg, 8213);
	if (bsc_config_handles_lac(cfg, 8213) == 0) {
		printf("Should have found it.\n");
		abort();
	}

	bsc_nat_free(nat);
}

static void test_mgcp_allocations(void)
{
#if 0
	struct bsc_connection *bsc;
	struct bsc_nat *nat;
	struct nat_sccp_connection con;
	int i, j, multiplex;

	printf("Testing MGCP.\n");
	memset(&con, 0, sizeof(con));

	nat = bsc_nat_alloc();
	nat->bsc_endpoints = talloc_zero_array(nat,
					       struct bsc_endpoint,
					       65);
	nat->mgcp_cfg = mgcp_config_alloc();
	nat->mgcp_cfg->trunk.number_endpoints = 64;

	bsc = bsc_connection_alloc(nat);
	bsc->cfg = bsc_config_alloc(nat, "foo");
	bsc->cfg->max_endpoints = 60;
	bsc_config_add_lac(bsc->cfg, 2323);
	bsc->last_endpoint = 0x22;
	con.bsc = bsc;

	bsc_init_endps_if_needed(bsc);

	i  = 1;
	do {
		if (bsc_assign_endpoint(bsc, &con) != 0) {
			printf("failed to allocate... on iteration %d\n", i);
			break;
		}
		++i;
	} while(1);

	multiplex = bsc_mgcp_nr_multiplexes(bsc->cfg->max_endpoints);
	for (i = 0; i < multiplex; ++i) {
		for (j = 0; j < 32; ++j)
			printf("%d", bsc->_endpoint_status[i*32 + j]);
		printf(": %d of %d\n", i*32 + 32, 32 * 8);
	}
#endif
}

static void test_mgcp_ass_tracking(void)
{
	struct bsc_connection *bsc;
	struct bsc_nat *nat;
	struct nat_sccp_connection con;
	struct bsc_nat_parsed *parsed;
	struct msgb *msg;

	printf("Testing MGCP.\n");
	memset(&con, 0, sizeof(con));

	nat = bsc_nat_alloc();
	nat->bsc_endpoints = talloc_zero_array(nat,
					       struct bsc_endpoint,
					       33);
	nat->mgcp_cfg = mgcp_config_alloc();
	nat->mgcp_cfg->trunk.number_endpoints = 64;
	mgcp_endpoints_allocate(&nat->mgcp_cfg->trunk);

	bsc = bsc_connection_alloc(nat);
	bsc->cfg = bsc_config_alloc(nat, "foo");
	bsc_config_add_lac(bsc->cfg, 2323);
	bsc->last_endpoint = 0x1e;
	con.bsc = bsc;

	msg = msgb_alloc(4096, "foo");
	copy_to_msg(msg, ass_cmd, sizeof(ass_cmd));
	parsed = bsc_nat_parse(msg);

	if (msg->l2h[16] != 0 ||
	    msg->l2h[17] != 0x1) {
		printf("Input is not as expected.. %s 0x%x\n",
			osmo_hexdump(msg->l2h, msgb_l2len(msg)),
			msg->l2h[17]);
		abort();
	}

	if (bsc_mgcp_assign_patch(&con, msg) != 0) {
		printf("Failed to handle assignment.\n");
		abort();
	}

	if (con.msc_endp != 1) {
		printf("Timeslot should be 1.\n");
		abort();
	}

	if (con.bsc_endp != 0x1) {
		printf("Assigned timeslot should have been 1.\n");
		abort();
	}
	if (con.bsc->_endpoint_status[0x1] != 1) {
		printf("The status on the BSC is wrong.\n");
		abort();
	}

	int multiplex, timeslot;
	mgcp_endpoint_to_timeslot(0x1, &multiplex, &timeslot);

	uint16_t cic = htons(timeslot & 0x1f);
	if (memcmp(&cic, &msg->l2h[16], sizeof(cic)) != 0) {
		printf("Message was not patched properly\n");
		printf("data cic: 0x%x %s\n", cic, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
		abort();
	}

	talloc_free(parsed);

	bsc_mgcp_dlcx(&con);
	if (con.bsc_endp != -1 || con.msc_endp != -1 ||
	    con.bsc->_endpoint_status[1] != 0 || con.bsc->last_endpoint != 0x1) {
		printf("Clearing should remove the mapping.\n");
		abort();
	}

	bsc_config_free(bsc->cfg);
	bsc_nat_free(nat);
}

/* test the code to find a given connection */
static void test_mgcp_find(void)
{
	struct bsc_nat *nat;
	struct bsc_connection *con;
	struct nat_sccp_connection *sccp_con;

	printf("Testing finding of a BSC Connection\n");

	nat = bsc_nat_alloc();
	con = bsc_connection_alloc(nat);
	llist_add(&con->list_entry, &nat->bsc_connections);

	sccp_con = talloc_zero(con, struct nat_sccp_connection);
	sccp_con->msc_endp = 12;
	sccp_con->bsc_endp = 12;
	sccp_con->bsc = con;
	llist_add(&sccp_con->list_entry, &nat->sccp_connections);

	if (bsc_mgcp_find_con(nat, 11) != NULL) {
		printf("Found the wrong connection.\n");
		abort();
	}

	if (bsc_mgcp_find_con(nat, 12) != sccp_con) {
		printf("Didn't find the connection\n");
		abort();
	}

	/* free everything */
	bsc_nat_free(nat);
}

static void test_mgcp_rewrite(void)
{
	int i;
	struct msgb *output;
	printf("Testing rewriting MGCP messages.\n");

	for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) {
		const char *orig = mgcp_messages[i].orig;
		const char *patc = mgcp_messages[i].patch;
		const char *ip = mgcp_messages[i].ip;
		const int port = mgcp_messages[i].port;
		const int expected_payload_type = mgcp_messages[i].payload_type;
		const int ensure_mode_set = mgcp_messages[i].ensure_mode_set;
		int payload_type = -1;

		char *input = strdup(orig);

		output = bsc_mgcp_rewrite(input, strlen(input), 0x1e,
					  ip, port, -1, &payload_type, ensure_mode_set);

		if (payload_type != -1) {
			fprintf(stderr, "Found media payload type %d in SDP data\n",
				payload_type);
			if (payload_type != expected_payload_type) {
				printf("Wrong payload type %d (expected %d)\n",
				       payload_type, expected_payload_type);
				abort();
			}
		}

		if (msgb_l2len(output) != strlen(patc)) {
			printf("Wrong sizes for test: %d  %u != %zu != %zu\n", i, msgb_l2len(output), strlen(patc), strlen(orig));
			printf("String '%s' vs '%s'\n", (const char *) output->l2h, patc);
			abort();
		}

		if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) {
			printf("Broken on %d msg: '%s'\n", i, (const char *) output->l2h);
			abort();
		}

		msgb_free(output);
		free(input);
	}
}

static void test_mgcp_parse(void)
{
	int code, ci;
	char transaction[60];

	printf("Testing MGCP response parsing.\n");

	if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) {
		printf("Failed to parse CRCX resp.\n");
		abort();
	}

	if (code != 200) {
		printf("Failed to parse the CODE properly. Got: %d\n", code);
		abort();
	}

	if (strcmp(transaction, "23265295") != 0) {
		printf("Failed to parse transaction id: '%s'\n", transaction);
		abort();
	}

	ci = bsc_mgcp_extract_ci(crcx_resp);
	if (ci != 1) {
		printf("Failed to parse the CI. Got: %d\n", ci);
		abort();
	}
}

struct cr_filter {
	const uint8_t *data;
	int length;
	int result;
	int contype;

	const char *bsc_imsi_allow;
	const char *bsc_imsi_deny;
	const char *nat_imsi_deny;
	int nat_cm_reject_cause;
	int nat_lu_reject_cause;
	int bsc_cm_reject_cause;
	int bsc_lu_reject_cause;
	int want_cm_reject_cause;
	int want_lu_reject_cause;
};

static struct cr_filter cr_filter[] = {
	{
		.data = bssmap_cr,
		.length = sizeof(bssmap_cr),
		.result = 1,
		.contype = FLT_CON_TYPE_CM_SERV_REQ,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = 1,
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		.data = pag_resp,
		.length = sizeof(pag_resp),
		.result = 1,
		.contype = FLT_CON_TYPE_PAG_RESP,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		/* nat deny is before blank/null BSC */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = -3,
		.nat_imsi_deny = "[0-9]*",
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		/* BSC allow is before NAT deny */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = 1,
		.nat_imsi_deny = "[0-9]*",
		.bsc_imsi_allow = "2440[0-9]*",
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		/* BSC allow is before NAT deny */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = 1,
		.bsc_imsi_allow = "[0-9]*",
		.nat_imsi_deny = "[0-9]*",
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		/* filter as deny is first */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = 1,
		.bsc_imsi_deny = "[0-9]*",
		.bsc_imsi_allow = "[0-9]*",
		.nat_imsi_deny = "[0-9]*",
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		/* deny by nat rule */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = -3,
		.bsc_imsi_deny = "000[0-9]*",
		.nat_imsi_deny = "[0-9]*",
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		/* deny by nat rule */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = -3,
		.bsc_imsi_deny = "000[0-9]*",
		.nat_imsi_deny = "[0-9]*",
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = 0x23,
		.nat_lu_reject_cause = 0x42,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = 0x42,
		.want_cm_reject_cause = 0x23,
	},
	{
		/* deny by bsc rule */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = -2,
		.bsc_imsi_deny = "[0-9]*",
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
	},
	{
		/* deny by bsc rule */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = -2,
		.bsc_imsi_deny = "[0-9]*",
		.contype = FLT_CON_TYPE_LU,
		.nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
		.bsc_cm_reject_cause = 0x42,
		.bsc_lu_reject_cause = 0x23,
		.want_lu_reject_cause = 0x23,
		.want_cm_reject_cause = 0x42,
	},
};

static void test_cr_filter()
{
	int i, res, contype;
	struct msgb *msg = msgb_alloc(4096, "test_cr_filter");
	struct bsc_nat_parsed *parsed;
	struct bsc_msg_acc_lst *nat_lst, *bsc_lst;
	struct bsc_msg_acc_lst_entry *nat_entry, *bsc_entry;
	struct bsc_filter_reject_cause cause;

	struct bsc_nat *nat = bsc_nat_alloc();
	struct bsc_connection *bsc = bsc_connection_alloc(nat);
	bsc->cfg = bsc_config_alloc(nat, "foo");
	bsc_config_add_lac(bsc->cfg, 1234);
	bsc->cfg->acc_lst_name = "bsc";
	nat->acc_lst_name = "nat";

	nat_lst = bsc_msg_acc_lst_get(nat, &nat->access_lists, "nat");
	bsc_lst = bsc_msg_acc_lst_get(nat, &nat->access_lists, "bsc");

	bsc_entry = bsc_msg_acc_lst_entry_create(bsc_lst);
	nat_entry = bsc_msg_acc_lst_entry_create(nat_lst);

	/* test the default value as we are going to overwrite it */
	OSMO_ASSERT(bsc_entry->cm_reject_cause == GSM48_REJECT_PLMN_NOT_ALLOWED);
	OSMO_ASSERT(bsc_entry->lu_reject_cause == GSM48_REJECT_PLMN_NOT_ALLOWED);

	for (i = 0; i < ARRAY_SIZE(cr_filter); ++i) {
		char *imsi;
		msgb_reset(msg);
		copy_to_msg(msg, cr_filter[i].data, cr_filter[i].length);

		bsc_entry->cm_reject_cause = cr_filter[i].bsc_cm_reject_cause;
		bsc_entry->lu_reject_cause = cr_filter[i].bsc_lu_reject_cause;
		nat_entry->cm_reject_cause = cr_filter[i].nat_cm_reject_cause;
		nat_entry->lu_reject_cause = cr_filter[i].nat_lu_reject_cause;

		if (gsm_parse_reg(nat_entry, &nat_entry->imsi_deny_re, &nat_entry->imsi_deny,
			      cr_filter[i].nat_imsi_deny ? 1 : 0,
			      &cr_filter[i].nat_imsi_deny) != 0)
			abort();
		if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_allow_re, &bsc_entry->imsi_allow,
			      cr_filter[i].bsc_imsi_allow ? 1 : 0,
			      &cr_filter[i].bsc_imsi_allow) != 0)
			abort();
		if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_deny_re, &bsc_entry->imsi_deny,
			      cr_filter[i].bsc_imsi_deny ? 1 : 0,
			      &cr_filter[i].bsc_imsi_deny) != 0)
			abort();

		parsed = bsc_nat_parse(msg);
		if (!parsed) {
			printf("FAIL: Failed to parse the message\n");
			abort();
		}

		memset(&cause, 0, sizeof(cause));
		res = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &contype, &imsi, &cause);
		if (res != cr_filter[i].result) {
			printf("FAIL: Wrong result %d for test %d.\n", res, i);
			abort();
		}


		OSMO_ASSERT(cause.cm_reject_cause == cr_filter[i].want_cm_reject_cause);
		OSMO_ASSERT(cause.lu_reject_cause == cr_filter[i].want_lu_reject_cause);

		if (contype != cr_filter[i].contype) {
			printf("FAIL: Wrong contype %d for test %d.\n", res, contype);
			abort();
		}

		talloc_steal(parsed, imsi);
		talloc_free(parsed);
	}

	msgb_free(msg);
	bsc_nat_free(nat);
}

static void test_dt_filter()
{
	int i;
	struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
	struct bsc_nat_parsed *parsed;
	struct bsc_filter_reject_cause cause;

	struct bsc_nat *nat = bsc_nat_alloc();
	struct bsc_connection *bsc = bsc_connection_alloc(nat);
	struct nat_sccp_connection *con = talloc_zero(0, struct nat_sccp_connection);

	bsc->cfg = bsc_config_alloc(nat, "foo");
	bsc_config_add_lac(bsc->cfg, 23);
	con->bsc = bsc;

	msgb_reset(msg);
	copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp));

	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp\n");
		abort();
	}

	if (parsed->bssap != BSSAP_MSG_DTAP) {
		printf("FAIL: It should be dtap\n");
		abort();
	}

	/* gsm_type is actually the size of the dtap */
	if (parsed->gsm_type < msgb_l3len(msg) - 3) {
		printf("FAIL: Not enough space for the content\n");
		abort();
	}

	memset(&cause, 0, sizeof(cause));
	if (bsc_nat_filter_dt(bsc, msg, con, parsed, &cause) != 1) {
		printf("FAIL: Should have passed..\n");
		abort();
	}

	/* just some basic length checking... */
	for (i = ARRAY_SIZE(id_resp); i >= 0; --i) {
		msgb_reset(msg);
		copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp));

		parsed = bsc_nat_parse(msg);
		if (!parsed)
			continue;

		con->filter_state.imsi_checked = 0;
		memset(&cause, 0, sizeof(cause));
		bsc_nat_filter_dt(bsc, msg, con, parsed, &cause);
	}

	msgb_free(msg);
	bsc_nat_free(nat);
}

static void test_setup_rewrite()
{
	struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
	struct msgb *out;
	struct bsc_nat_parsed *parsed;
	const char *imsi = "27408000001234";

	struct bsc_nat *nat = bsc_nat_alloc();

	/* a fake list */
	struct osmo_config_list entries;
	struct osmo_config_entry entry;

	INIT_LLIST_HEAD(&entries.entry);
	entry.mcc = "274";
	entry.mnc = "08";
	entry.option = "^0([1-9])";
	entry.text = "0049";
	llist_add_tail(&entry.list, &entries.entry);
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);

	/* verify that nothing changed */
	msgb_reset(msg);
	copy_to_msg(msg, cc_setup_international, ARRAY_SIZE(cc_setup_international));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (msg != out) {
		printf("FAIL: The message should not have been changed\n");
		abort();
	}

	verify_msg(out, cc_setup_international, ARRAY_SIZE(cc_setup_international));
	talloc_free(parsed);

	/* verify that something in the message changes */
	msgb_reset(msg);
	copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (!out) {
		printf("FAIL: A new message should be created.\n");
		abort();
	}

	if (msg == out) {
		printf("FAIL: The message should have changed\n");
		abort();
	}

	verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
	msgb_free(out);

	/* Make sure that a wildcard is matching */
	entry.mnc = "*";
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
	msg = msgb_alloc(4096, "test_dt_filter");
	copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (!out) {
		printf("FAIL: A new message should be created.\n");
		abort();
	}

	if (msg == out) {
		printf("FAIL: The message should have changed\n");
		abort();
	}

	verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
	msgb_free(out);

	/* Make sure that a wildcard is matching */
	entry.mnc = "09";
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
	msg = msgb_alloc(4096, "test_dt_filter");
	copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (out != msg) {
		printf("FAIL: The message should be unchanged.\n");
		abort();
	}

	verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national));
	msgb_free(out);

	/* Now see what happens to an international number */
	entry.mnc = "*";
	entry.option = "^\\+[0-9][0-9]([1-9])";
	entry.text = "0036";
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
	msg = msgb_alloc(4096, "test_dt_filter");
	copy_to_msg(msg, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp %d\n", __LINE__);
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (!out) {
		printf("FAIL: A new message should be created %d.\n", __LINE__);
		abort();
	}

	if (msg == out) {
		printf("FAIL: The message should have changed %d\n", __LINE__);
		abort();
	}

	verify_msg(out, cc_setup_national_patched_patched,
			ARRAY_SIZE(cc_setup_national_patched_patched));
	msgb_free(out);

	/* go from international back to national */
	entry.mnc = "*";
	entry.option = "^\\+([0-9])";
	entry.text = "36";
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
	msg = msgb_alloc(4096, "test_dt_filter");
	copy_to_msg(msg, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp %d\n", __LINE__);
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (!out) {
		printf("FAIL: A new message should be created %d.\n", __LINE__);
		abort();
	}

	if (msg == out) {
		printf("FAIL: The message should have changed %d\n", __LINE__);
		abort();
	}

	verify_msg(out, cc_setup_national_again,
			ARRAY_SIZE(cc_setup_national_again));
	msgb_free(out);
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL);
	bsc_nat_free(nat);
}

static void test_setup_rewrite_prefix(void)
{
	struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
	struct msgb *out;
	struct bsc_nat_parsed *parsed;
	const char *imsi = "27408000001234";

	struct bsc_nat *nat = bsc_nat_alloc();

	/* a fake list */
	struct osmo_config_list entries;
	struct osmo_config_entry entry;

	INIT_LLIST_HEAD(&entries.entry);
	entry.mcc = "274";
	entry.mnc = "08";
	entry.option = "^0([1-9])";
	entry.text = "prefix_lookup";
	llist_add_tail(&entry.list, &entries.entry);
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);

        nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv");

	msgb_reset(msg);
	copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (!out) {
		printf("FAIL: A new message should be created.\n");
		abort();
	}

	if (msg == out) {
		printf("FAIL: The message should have changed\n");
		abort();
	}

	verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
	msgb_free(out);

	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL);
	bsc_nat_free(nat);
}

static void test_setup_rewrite_post(void)
{
	struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
	struct msgb *out;
	struct bsc_nat_parsed *parsed;
	const char *imsi = "27408000001234";

	struct bsc_nat *nat = bsc_nat_alloc();

	/* a fake list */
	struct osmo_config_list entries;
	struct osmo_config_entry entry;
	struct osmo_config_list entries_post;
	struct osmo_config_entry entry_post;

	INIT_LLIST_HEAD(&entries.entry);
	entry.mcc = "274";
	entry.mnc = "08";
	entry.option = "^0([1-9])";
	entry.text = "0049";
	llist_add_tail(&entry.list, &entries.entry);
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);

	/* attempt to undo the previous one */
	INIT_LLIST_HEAD(&entries_post.entry);
	entry_post.mcc = "274";
	entry_post.mnc = "08";
	entry_post.option = "^\\+49([1-9])";
	entry_post.text = "prefix_lookup";
	llist_add_tail(&entry_post.list, &entries_post.entry);
	bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr_post, &entries_post);

        nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv");

	msgb_reset(msg);
	copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse ID resp\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (!out) {
		printf("FAIL: A new message should be created.\n");
		abort();
	}

	if (msg == out) {
		printf("FAIL: The message should have changed\n");
		abort();
	}

	verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national));
	msgb_free(out);

	bsc_nat_free(nat);
}

static void test_sms_smsc_rewrite()
{
	struct msgb *msg = msgb_alloc(4096, "SMSC rewrite"), *out;
	struct bsc_nat_parsed *parsed;
	const char *imsi = "515039900406700";

	struct bsc_nat *nat = bsc_nat_alloc();

	/* a fake list */
	struct osmo_config_list smsc_entries, dest_entries, clear_entries;
	struct osmo_config_entry smsc_entry, dest_entry, clear_entry;

	INIT_LLIST_HEAD(&smsc_entries.entry);
	INIT_LLIST_HEAD(&dest_entries.entry);
	INIT_LLIST_HEAD(&clear_entries.entry);
	smsc_entry.mcc = "^515039";
	smsc_entry.option = "639180000105()";
	smsc_entry.text   = "6666666666667";
	llist_add_tail(&smsc_entry.list, &smsc_entries.entry);
	dest_entry.mcc = "515";
	dest_entry.mnc = "03";
	dest_entry.option = "^0049";
	dest_entry.text   = "";
	llist_add_tail(&dest_entry.list, &dest_entries.entry);
	clear_entry.mcc = "^515039";
	clear_entry.option = "^0049";
	clear_entry.text   = "";
	llist_add_tail(&clear_entry.list, &clear_entries.entry);

	bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, &smsc_entries);
	bsc_nat_num_rewr_entry_adapt(nat, &nat->tpdest_match, &dest_entries);
	bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries);

	printf("Testing SMSC rewriting.\n");

	/*
	 * Check if the SMSC address is changed
	 */
	copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse SMS\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (out == msg) {
		printf("FAIL: This should have changed.\n");
		abort();
	}

	verify_msg(out, smsc_rewrite_patched, ARRAY_SIZE(smsc_rewrite_patched));
	msgb_free(out);

	/* clear out the filter for SMSC */
	printf("Attempting to only rewrite the HDR\n");
	bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, NULL);
	msg = msgb_alloc(4096, "SMSC rewrite");
	copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse SMS\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (out == msg) {
		printf("FAIL: This should have changed.\n");
		abort();
	}

	verify_msg(out, smsc_rewrite_patched_hdr, ARRAY_SIZE(smsc_rewrite_patched_hdr));
	msgb_free(out);

	/* clear out the next filter */
	printf("Attempting to change nothing.\n");
	bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, NULL);
	msg = msgb_alloc(4096, "SMSC rewrite");
	copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse SMS\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (out != msg) {
		printf("FAIL: This should not have changed.\n");
		abort();
	}

	verify_msg(out, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
	msgb_free(out);
	bsc_nat_free(nat);
}

static void test_sms_number_rewrite(void)
{
	struct msgb *msg, *out;
	struct bsc_nat_parsed *parsed;
	const char *imsi = "515039900406700";

	struct bsc_nat *nat = bsc_nat_alloc();

	/* a fake list */
	struct osmo_config_list num_entries, clear_entries;
	struct osmo_config_entry num_entry, clear_entry;

	INIT_LLIST_HEAD(&num_entries.entry);
	num_entry.mcc = "^515039";
	num_entry.option = "^0049()";
	num_entry.text   = "0032";
	llist_add_tail(&num_entry.list, &num_entries.entry);

	bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_num_rewr, &num_entries);

	printf("Testing SMS TP-DA rewriting.\n");

	/*
	 * Check if the SMSC address is changed
	 */
 	msg = msgb_alloc(4096, "SMSC rewrite");
	copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse SMS\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (out == msg) {
		printf("FAIL: This should have changed.\n");
		abort();
	}

	verify_msg(out, smsc_rewrite_num_patched,
		   ARRAY_SIZE(smsc_rewrite_num_patched));
	msgb_free(out);

	/*
	 * Now with TP-SRR rewriting enabled
	 */
	INIT_LLIST_HEAD(&clear_entries.entry);
	clear_entry.mcc = "^515039";
	clear_entry.option = "";
	clear_entry.text   = "";
	llist_add_tail(&clear_entry.list, &clear_entries.entry);
	bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries);

 	msg = msgb_alloc(4096, "SMSC rewrite");
	copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
	parsed = bsc_nat_parse(msg);
	if (!parsed) {
		printf("FAIL: Could not parse SMS\n");
		abort();
	}

	out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
	if (out == msg) {
		printf("FAIL: This should have changed.\n");
		abort();
	}

	verify_msg(out, smsc_rewrite_num_patched_tp_srr,
		   ARRAY_SIZE(smsc_rewrite_num_patched_tp_srr));
	msgb_free(out);
	bsc_nat_free(nat);
}

static void test_barr_list_parsing(void)
{
	int rc;
	int cm, lu;
	struct rb_node *node;
	struct rb_root root = RB_ROOT;
	struct osmo_config_list *lst = osmo_config_list_parse(NULL, "barr.cfg");
	if (lst == NULL)
		abort();

	rc = bsc_filter_barr_adapt(NULL, &root, lst);
	if (rc != 0)
		abort();
	talloc_free(lst);


	for (node = rb_first(&root); node; node = rb_next(node)) {
		struct bsc_filter_barr_entry *entry;
		entry = rb_entry(node, struct bsc_filter_barr_entry, node);
		printf("IMSI: %s CM: %d LU: %d\n", entry->imsi,
			entry->cm_reject_cause, entry->lu_reject_cause);
	}

	/* do the look up now.. */
	rc = bsc_filter_barr_find(&root, "12123119", &cm, &lu);
	if (!rc) {
		printf("Failed to find the IMSI.\n");
		abort();
	}

	if (cm != 3 || lu != 4) {
		printf("Found CM(%d) and LU(%d)\n", cm, lu);
		abort();
	}

	/* empty and check that it is empty */
	bsc_filter_barr_adapt(NULL, &root, NULL);
	if (!RB_EMPTY_ROOT(&root)) {
		printf("Failed to empty the list.\n");
		abort();
	}

	/* check that dup results in an error */
	lst = osmo_config_list_parse(NULL, "barr_dup.cfg");
	if (lst == NULL) {
		printf("Failed to parse list with dups\n");
		abort();
	}

	rc = bsc_filter_barr_adapt(NULL, &root, lst);
	if (rc != -1) {
		printf("It should have failed due dup\n");
		abort();
	}
	talloc_free(lst);

	/* dump for reference */
	for (node = rb_first(&root); node; node = rb_next(node)) {
		struct bsc_filter_barr_entry *entry;
		entry = rb_entry(node, struct bsc_filter_barr_entry, node);
		printf("IMSI: %s CM: %d LU: %d\n", entry->imsi,
			entry->cm_reject_cause, entry->lu_reject_cause);

	}
	rc = bsc_filter_barr_adapt(NULL, &root, NULL);
}

static void test_nat_extract_lac()
{
	int res;
	struct bsc_connection *bsc;
	struct bsc_nat *nat;
	struct nat_sccp_connection con;
	struct bsc_nat_parsed *parsed;
	struct msgb *msg = msgb_alloc(4096, "test-message");

	printf("Testing LAC extraction from SCCP CR\n");

	/* initialize the testcase */
	nat = bsc_nat_alloc();
	bsc = bsc_connection_alloc(nat);
	bsc->cfg = bsc_config_alloc(nat, "foo");

	memset(&con, 0, sizeof(con));
	con.bsc = bsc;

	/* create the SCCP CR */
	msg->l2h = msgb_put(msg, ARRAY_SIZE(bssmap_cr));
	memcpy(msg->l2h, bssmap_cr, ARRAY_SIZE(bssmap_cr));

	/* parse it and pass it on */
	parsed = bsc_nat_parse(msg);
	res = bsc_nat_extract_lac(bsc, &con, parsed, msg);
	OSMO_ASSERT(res == 0);

	/* verify the LAC */
	OSMO_ASSERT(con.lac == 8210);
	OSMO_ASSERT(con.ci == 50000);

	bsc_nat_free(nat);
}

int main(int argc, char **argv)
{
	sccp_set_log_area(DSCCP);
	osmo_init_logging(&log_info);

	test_filter();
	test_contrack();
	test_paging();
	test_mgcp_ass_tracking();
	test_mgcp_find();
	test_mgcp_rewrite();
	test_mgcp_parse();
	test_cr_filter();
	test_dt_filter();
	test_setup_rewrite();
	test_setup_rewrite_prefix();
	test_setup_rewrite_post();
	test_sms_smsc_rewrite();
	test_sms_number_rewrite();
	test_mgcp_allocations();
	test_barr_list_parsing();
	test_nat_extract_lac();

	printf("Testing execution completed.\n");
	return 0;
}

/* stub */
void bsc_nat_send_mgcp_to_msc(struct bsc_nat *nat, struct msgb *msg)
{
	abort();
}