aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/tests/bsc-nat/bsc_nat_test.c
blob: aac28704e867342b48cf53e8d32491536c8d4338 (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
/*
 * BSC NAT Message filtering
 *
 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
 * (C) 2010 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 General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */


#include <openbsc/debug.h>
#include <openbsc/gsm_data.h>
#include <openbsc/bsc_nat.h>
#include <openbsc/bsc_nat_sccp.h>

#include <osmocore/talloc.h>

#include <osmocom/sccp/sccp.h>
#include <osmocore/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 */
	fprintf(stderr, "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");

		fprintf(stderr, "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) {
			fprintf(stderr, "FAIL: Failed to parse the message\n");
			continue;
		}

		result = bsc_nat_filter_ipa(results[i].dir, msg, parsed);
		if (result != results[i].result) {
			fprintf(stderr, "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));
}

#define VERIFY(con_found, con, msg, ver, str) \
	if (!con_found || con_found->bsc != con) { \
		fprintf(stderr, "Failed to find the con: %p\n", con_found); \
		abort(); \
	} \
	if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \
		fprintf(stderr, "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 sccp_connections *con_found;
	struct sccp_connections *rc_con;
	struct bsc_nat_parsed *parsed;
	struct msgb *msg;

	fprintf(stderr, "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) {
		fprintf(stderr, "Con should not exist %p\n", con_found);
		abort();
	}
	rc_con = create_sccp_src_ref(con, parsed);
	if (!rc_con) {
		fprintf(stderr, "Failed to create a ref\n");
		abort();
	}
	con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
	if (!con_found || con_found->bsc != con) {
		fprintf(stderr, "Failed to find the con: %p\n", con_found);
		abort();
	}
	if (con_found != rc_con) {
		fprintf(stderr, "Failed to find the right connection.\n");
		abort();
	}
	if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) {
		fprintf(stderr, "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) {
		fprintf(stderr, "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 || con_found->bsc != con) {
		fprintf(stderr, "Failed to find the con: %p\n", con_found);
		abort();
	}
	if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) {
		fprintf(stderr, "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) {
		fprintf(stderr, "Con should be gone. %p\n", con_found);
		abort();
	}
	talloc_free(parsed);


	talloc_free(nat);
	msgb_free(msg);
}

static void test_paging(void)
{
	int lac;
	struct bsc_nat *nat;
	struct bsc_connection *con;
	struct bsc_nat_parsed *parsed;
	struct bsc_config *cfg;
	struct msgb *msg;

	fprintf(stderr, "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);
	msg = msgb_alloc(4096, "test");

	/* Test completely bad input */
	copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd));
	if (bsc_nat_find_bsc(nat, msg, &lac) != 0) {
		fprintf(stderr, "Should have not found anything.\n");
		abort();
	}

	/* Test it by not finding it */
	copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd));
	parsed = bsc_nat_parse(msg);
	if (bsc_nat_find_bsc(nat, msg, &lac) != 0) {
		fprintf(stderr, "Should have not found aynthing.\n");
		abort();
	}
	talloc_free(parsed);

	/* Test by finding it */
	bsc_config_del_lac(cfg, 23);
	bsc_config_add_lac(cfg, 8213);
	copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd));
	parsed = bsc_nat_parse(msg);
	if (bsc_nat_find_bsc(nat, msg, &lac) != con) {
		fprintf(stderr, "Should have found it.\n");
		abort();
	}
	talloc_free(parsed);
}

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

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

	nat = bsc_nat_alloc();
	nat->bsc_endpoints = talloc_zero_array(nat,
					       struct bsc_endpoint,
					       33);
	bsc = bsc_connection_alloc(nat);
	bsc->cfg = bsc_config_alloc(nat, "foo");
	bsc_config_add_lac(bsc->cfg, 2323);
	bsc->last_endpoint = 0x1a;
	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) {
		fprintf(stderr, "Input is not as expected.. %s 0x%x\n",
			hexdump(msg->l2h, msgb_l2len(msg)),
			msg->l2h[17]);
		abort();
	}

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

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

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

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

	uint16_t cic = htons(timeslot & 0x1f);
	if (memcmp(&cic, &msg->l2h[16], sizeof(cic)) != 0) {
		fprintf(stderr, "Message was not patched properly\n");
		fprintf(stderr, "data cic: 0x%x %s\n", cic, 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 != 0x1b) {
		fprintf(stderr, "Clearing should remove the mapping.\n");
		abort();
	}

	talloc_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 sccp_connections *sccp_con;

	fprintf(stderr, "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 sccp_connections);
	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) {
		fprintf(stderr, "Found the wrong connection.\n");
		abort();
	}

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

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

static void test_mgcp_rewrite(void)
{
	int i;
	struct msgb *output;
	fprintf(stderr, "Test 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;

		char *input = strdup(orig);

		output = bsc_mgcp_rewrite(input, strlen(input), 0x1e, ip, port);
		if (msgb_l2len(output) != strlen(patc)) {
			fprintf(stderr, "Wrong sizes for test: %d  %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig));
			fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc);
			abort();
		}

		if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) {
			fprintf(stderr, "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];

	fprintf(stderr, "Test MGCP response parsing.\n");

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

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

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

	ci = bsc_mgcp_extract_ci(crcx_resp);
	if (ci != 1) {
		fprintf(stderr, "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;
};

static struct cr_filter cr_filter[] = {
	{
		.data = bssmap_cr,
		.length = sizeof(bssmap_cr),
		.result = 1,
		.contype = NAT_CON_TYPE_CM_SERV_REQ,
	},
	{
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = 1,
		.contype = NAT_CON_TYPE_LU,
	},
	{
		.data = pag_resp,
		.length = sizeof(pag_resp),
		.result = 1,
		.contype = NAT_CON_TYPE_PAG_RESP,
	},
	{
		/* nat deny is before blank/null BSC */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = -3,
		.nat_imsi_deny = "[0-9]*",
		.contype = NAT_CON_TYPE_LU,
	},
	{
		/* 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 = NAT_CON_TYPE_LU,
	},
	{
		/* 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 = NAT_CON_TYPE_LU,
	},
	{
		/* 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 = NAT_CON_TYPE_LU,
	},
	{
		/* 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 = NAT_CON_TYPE_LU,
	},
	{
		/* deny by bsc rule */
		.data = bss_lu,
		.length = sizeof(bss_lu),
		.result = -2,
		.bsc_imsi_deny = "[0-9]*",
		.contype = NAT_CON_TYPE_LU,
	},

};

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_nat_acc_lst *nat_lst, *bsc_lst;
	struct bsc_nat_acc_lst_entry *nat_entry, *bsc_entry;

	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_nat_acc_lst_get(nat, "nat");
	bsc_lst = bsc_nat_acc_lst_get(nat, "bsc");

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

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

		nat_lst = bsc_nat_acc_lst_get(nat, "nat");
		bsc_lst = bsc_nat_acc_lst_get(nat, "bsc");

		bsc_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);
		bsc_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);
		bsc_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);

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

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

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

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

	msgb_free(msg);
}

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

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

	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) {
		fprintf(stderr, "FAIL: Could not parse ID resp\n");
		abort();
	}

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

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

	if (bsc_nat_filter_dt(bsc, msg, con, parsed) != 1) {
		fprintf(stderr, "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->imsi_checked = 0;
		bsc_nat_filter_dt(bsc, msg, con, parsed);
	}
}

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 msg_entries entries;
	struct msg_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);
	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) {
		fprintf(stderr, "FAIL: Could not parse ID resp\n");
		abort();
	}

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

	if (out->len != ARRAY_SIZE(cc_setup_international)) {
		fprintf(stderr, "FAIL: Length of message changed\n");
		abort();
	}

	if (memcmp(out->data, cc_setup_international, out->len) != 0) {
		fprintf(stderr, "FAIL: Content modified..\n");
		abort();
	}
	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) {
		fprintf(stderr, "FAIL: Could not parse ID resp\n");
		abort();
	}

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

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

	if (out->len != ARRAY_SIZE(cc_setup_national_patched)) {
		fprintf(stderr, "FAIL: Length is wrong.\n");
		abort();
	}

	if (memcmp(cc_setup_national_patched, out->data, out->len) != 0) {
		fprintf(stderr, "FAIL: Data is wrong.\n");
		fprintf(stderr, "Data was: %s\n", hexdump(out->data, out->len));
		abort();
	}

	msgb_free(out);

	/* Make sure that a wildcard is matching */
	entry.mnc = "*";
	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) {
		fprintf(stderr, "FAIL: Could not parse ID resp\n");
		abort();
	}

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

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

	if (out->len != ARRAY_SIZE(cc_setup_national_patched)) {
		fprintf(stderr, "FAIL: Length is wrong.\n");
		abort();
	}

	if (memcmp(cc_setup_national_patched, out->data, out->len) != 0) {
		fprintf(stderr, "FAIL: Data is wrong.\n");
		fprintf(stderr, "Data was: %s\n", hexdump(out->data, out->len));
		abort();
	}

	msgb_free(out);

	/* Make sure that a wildcard is matching */
	entry.mnc = "09";
	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) {
		fprintf(stderr, "FAIL: Could not parse ID resp\n");
		abort();
	}

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

	if (out->len != ARRAY_SIZE(cc_setup_national)) {
		fprintf(stderr, "FAIL: Foo\n");
		abort();
	}

	if (memcmp(out->data, cc_setup_national, ARRAY_SIZE(cc_setup_national)) != 0) {
		fprintf(stderr, "FAIL: The message should really be unchanged.\n");
		abort();
	}

	msgb_free(out);
}

int main(int argc, char **argv)
{
	struct log_target *stderr_target;

	sccp_set_log_area(DSCCP);
	log_init(&log_info);
	stderr_target = log_target_create_stderr();
	log_add_target(stderr_target);
	log_set_all_filter(stderr_target, 1);

	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();
	return 0;
}

void input_event()
{}
int nm_state_event()
{
	return -1;
}