summaryrefslogtreecommitdiffstats
path: root/hlr/HLR_Tests.ttcn
blob: 27a44de630e3f35b079deecc871abe85ac657173 (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
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
module HLR_Tests {

/* HLR test suite in TTCN-3
 * (C) 2017-2018 Harald Welte <laforge@gnumonks.org>
 * (C) 2018 sysmocom - s.f.m.c. GmbH
 * (C) 2018 Vadim Yanitskiy <axilirator@gmail.com>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */



import from GSUP_Types all;
import from GSUP_Emulation all;
import from IPA_Emulation all;

import from General_Types all;
import from Osmocom_Types all;
import from Osmocom_CTRL_Adapter all;

import from TCCEncoding_Functions all;
import from SS_Types all;
import from SS_Templates all;
import from MAP_Errors all;
import from USSD_Helpers all;

import from Osmocom_VTY_Functions all;
import from TELNETasp_PortType all;

import from MSLookup_mDNS_Types all;
import from MSLookup_mDNS_Emulation all;
import from MSLookup_mDNS_Templates all;
import from DNS_Helpers all;

type component test_CT extends CTRL_Adapter_CT {
	/* emulated GSUP client (MSC/SGSN) */
	var IPA_Emulation_CT vc_IPA;
	var IPA_CCM_Parameters ccm_pars;
	/* legacy tests without ConnHdlr */
	port IPA_GSUP_PT GSUP;
	/* new tests using ConnHdlr + GSUP_Emulation */
	var GSUP_Emulation_CT vc_GSUP;
	/* only to get events from IPA underneath GSUP */
	port IPA_CTRL_PT GSUP_IPA_EVENT;

	port TELNETasp_PT VTY;

	var MSLookup_mDNS_Emulation_CT vc_MSLookup_mDNS;

	/* emulated GSUP server (second HLR) */
	var IPA_Emulation_CT vc_IPA_server;
	var GSUP_Emulation_CT vc_GSUP_server;

	timer g_Tguard := 10.0;
};

modulepar {
	/* OsmoHLR */
	charstring mp_hlr_ip := "127.0.0.1";
	integer mp_hlr_gsup_port := 4222;
	integer mp_hlr_ctrl_port := 4259;
	/* how many auth tuples are expected
	   when IE ts_GSUP_IE_NUM_VECTORS_REQ is absent */
	integer mp_default_num_auth_tuples := 5;

	/* emulated GSUP server (second HLR) */
	charstring mp_hlr_ts_ip := "127.0.0.99";
	integer mp_hlr_ts_port := 4222;

	/* drop after osmo-hlr release > 1.2.0 */
	boolean mp_hlr_supports_dgsm := true;
};

type record HlrSubscrAud2G {
	charstring algo,
	OCT16 ki
}

type record HlrSubscrAud3G {
	charstring algo,
	OCT16 k,
	OCT16 op,
	boolean op_is_opc
}

type record HlrSubscriber {
	hexstring imsi,
	hexstring msisdn,
	HlrSubscrAud2G aud2g optional,
	HlrSubscrAud3G aud3g optional
}

type record of HlrSubscriber HlrSubscriberList;

type component HLR_ConnHdlr extends GSUP_ConnHdlr {
	timer g_Tguard := 10.0;
	var HLR_ConnHdlrPars g_pars;
	port TELNETasp_PT VTY;
	port MSLookup_mDNS_PT mDNS;

	/* Pass from test_CT for explicit addressing */
	var GSUP_Emulation_CT vc_GSUP_server;
	var GSUP_Emulation_CT vc_GSUP_client;
	var IPA_CCM_Parameters ccm_pars_client;
}

type record HLR_ConnHdlrPars {
	HlrSubscriber sub,
	HLR_ConnHdlrParsUssd ussd optional,
	octetstring source_name optional
}

type record HLR_ConnHdlrParsUssd {
	OCT4 sid
}

template (value) HLR_ConnHdlrPars t_Pars(hexstring imsi, hexstring msisdn := ''H,
					 template (omit) octetstring source_name := omit) := {
	sub := {
		imsi := imsi,
		msisdn := msisdn,
		aud2g := omit,
		aud3g := omit
	},
	ussd := omit,
	source_name := source_name
}
template (value) HLR_ConnHdlrPars t_Pars_via_proxy(hexstring imsi, hexstring msisdn := ''H) :=
	t_Pars(imsi, msisdn, source_name := char2oct("the-source\n"));

template (value) HLR_ConnHdlrPars t_Pars_sub(HlrSubscriber sub) := {
	sub := sub,
	ussd := omit,
	source_name := omit
}

template (value) HLR_ConnHdlrPars t_Pars_sub_via_proxy(HlrSubscriber sub) := {
	sub := sub,
	ussd := omit,
	source_name := char2oct("the-source\n")
}

type function void_fn() runs on HLR_ConnHdlr;

/***********************************************************************
 * Main Component
 ***********************************************************************/

function f_init_vty() runs on test_CT {
	map(self:VTY, system:VTY);
	f_vty_set_prompts(VTY);
	f_vty_transceive(VTY, "enable");
	if (mp_hlr_supports_dgsm) {
		f_vty_config(VTY, "mslookup", "no mdns bind");
	}
}

private altstep as_Tguard() runs on test_CT {
	[] g_Tguard.timeout {
		setverdict(fail, "g_Tguard timeout");
		mtc.stop;
	}
}

/* emulated GSUP client (MSC/SGSN) */
function f_init_gsup(charstring id, boolean legacy) runs on test_CT {
	id := id & "-GSUP";
	var GsupOps ops := {
		create_cb := refers(GSUP_Emulation.ExpectedCreateCallback)
	}

	ccm_pars := c_IPA_default_ccm_pars;
	ccm_pars.name := "Osmocom TTCN-3 GSUP Simulator";
	ccm_pars.ser_nr := "MSC-00-00-00-00-00-00";

	vc_IPA := IPA_Emulation_CT.create(id & "-IPA");
	log("legacy= ", legacy);
	if (not legacy) {
		log("in not legacy case 1");
		vc_GSUP := GSUP_Emulation_CT.create(id);
	}

	map(vc_IPA:IPA_PORT, system:IPA_CODEC_PT);
	if (not legacy) {
		log("in not legacy case 2");
		connect(vc_GSUP:GSUP, vc_IPA:IPA_GSUP_PORT);
		connect(vc_IPA:IPA_CTRL_PORT, self:GSUP_IPA_EVENT);
		vc_GSUP.start(GSUP_Emulation.main(ops, id));
	} else {
		connect(vc_IPA:IPA_GSUP_PORT, self:GSUP);
	}

	vc_IPA.start(IPA_Emulation.main_client(mp_hlr_ip, mp_hlr_gsup_port, "", -1, ccm_pars));

	/* wait for incoming connection to GSUP port before proceeding */
	timer T := 10.0;
	T.start;
	alt {
		[not legacy] GSUP_IPA_EVENT.receive(tr_ASP_IPA_EV(ASP_IPA_EVENT_UP)) { }
		[legacy] GSUP.receive(ASP_IPA_Event:{up_down := ASP_IPA_EVENT_UP}) { }
		[] T.timeout {
			setverdict(fail, "No connection to GSUP Port");
			mtc.stop;
		}
	}
}

/* emulated GSUP server (second HLR) */
function f_init_gsup_server(charstring id) runs on test_CT {
	id := id & "-GSUP-server";
	var GsupOps ops := {
		create_cb := refers(GSUP_Emulation.ExpectedCreateCallback)
	}

	vc_GSUP_server := GSUP_Emulation_CT.create(id);
	vc_IPA_server := IPA_Emulation_CT.create(id & "-IPA");

	connect(vc_GSUP_server:GSUP, vc_IPA_server:IPA_GSUP_PORT);
	map(vc_IPA_server:IPA_PORT, system:IPA_CODEC_PT);

	vc_GSUP_server.start(GSUP_Emulation.main(ops, id));
	vc_IPA_server.start(IPA_Emulation.main_server(mp_hlr_ts_ip, mp_hlr_ts_port));
}

function f_init_mslookup() runs on test_CT {
	vc_MSLookup_mDNS := MSLookup_mDNS_Emulation_CT.create;
	vc_MSLookup_mDNS.start(MSLookup_mDNS_Emulation.f_main());
}

function f_init(boolean legacy := true, boolean gsup_server := false) runs on test_CT {

	/* activate default guard timer to ensure all tests eventually terminate */
	g_Tguard.start;
	activate(as_Tguard());

	f_init_gsup("HLR_Test", legacy);
	f_init_vty();
	f_init_mslookup();

	if (gsup_server) {
		f_init_gsup_server("HLR_Test");
	}

	f_ipa_ctrl_start(mp_hlr_ip, mp_hlr_ctrl_port);
}

/*! Start HLR_ConnHdlr from testCT in a separate thread.
 * \param void_fn  function to run inside the thread.
 * \param gsup_server  if true, connect the emulated GSUP server ports to the new HLR_ConnHdlr. Then both the emulated
 *                     GSUP client (MSC/SGSN) and server (second HLR) are connected. Explicit addressing with "to" and
 *                     "from" must be used, i.e. 'GSUP.receive(tr_GSUP_UL_REQ(...)) from vc_GSUP_server'. */
function f_start_handler(void_fn fn, HLR_ConnHdlrPars pars, boolean gsup_server := false) runs on test_CT
return HLR_ConnHdlr {
	var HLR_ConnHdlr vc_conn;
	var charstring id := testcasename();

	vc_conn := HLR_ConnHdlr.create(id);
	connect(vc_conn:GSUP, vc_GSUP:GSUP_CLIENT);
	connect(vc_conn:GSUP_PROC, vc_GSUP:GSUP_PROC);
	connect(vc_conn:mDNS, vc_MSLookup_mDNS:mDNS);

	if (gsup_server) {
		connect(vc_conn:GSUP, vc_GSUP_server:GSUP_CLIENT);
		connect(vc_conn:GSUP_PROC, vc_GSUP_server:GSUP_PROC);
		vc_conn.start(f_handler_init(fn, id, vc_GSUP_server, vc_GSUP, ccm_pars, pars));
	} else {
		vc_conn.start(f_handler_init(fn, id, omit, vc_GSUP, ccm_pars, pars));
	}

	return vc_conn;
}

private function f_handler_init_vty() runs on HLR_ConnHdlr {
	map(self:VTY, system:VTY);
	f_vty_set_prompts(VTY);
	f_vty_transceive(VTY, "enable");
}

/* first function inside ConnHdlr component; sets g_pars + starts function */
function f_handler_init(void_fn fn, charstring id,
			template (omit) GSUP_Emulation_CT vc_GSUP_server_test_ct,
			GSUP_Emulation_CT vc_GSUP_client_test_ct,
			IPA_CCM_Parameters ccm_pars_client_test_ct,
			template (omit) HLR_ConnHdlrPars pars := omit)
runs on HLR_ConnHdlr
{
	/* Pass from test_CT for explicit addressing */
	if (isvalue(vc_GSUP_server_test_ct)) {
		vc_GSUP_server := valueof(vc_GSUP_server_test_ct);
	}
	vc_GSUP_client := vc_GSUP_client_test_ct;
	ccm_pars_client := ccm_pars_client_test_ct;

	if (isvalue(pars)) {
		g_pars := valueof(pars);
		f_create_gsup_expect_explicit(hex2str(g_pars.sub.imsi), vc_GSUP_client);
	}
	f_handler_init_vty();
	fn.apply();
}

/***********************************************************************
 * Subscriber creation via VTY
 ***********************************************************************/

template (value) HlrSubscriber t_SubNoAuth(hexstring imsi, hexstring msisdn) := {
	imsi := imsi,
	msisdn := msisdn,
	aud2g := omit,
	aud3g := omit
}

const OCT16 c_KI_DEFAULT := '000102030405060708090a0b0c0d0e0f'O;
const OCT16 c_K_DEFAULT := '101112131415161718191a1b1c1d1e1f'O;
const OCT16 c_OP_DEFAULT := '202122232425262728292a2b2c2d2e2f'O;
//const OCT16 c_OPC_DEFAULT := '303132333435363738393a3b3c3d3f'O;

template (value) HlrSubscriber t_Sub2G(hexstring imsi, hexstring msisdn, charstring algo) := {
	imsi := imsi,
	msisdn := msisdn,
	aud2g := {
		algo := algo,
		ki := c_KI_DEFAULT
	},
	aud3g := omit
}

template (value) HlrSubscriber t_Sub3G(hexstring imsi, hexstring msisdn, charstring algo, boolean is_opc) := {
	imsi := imsi,
	msisdn := msisdn,
	aud2g := omit,
	aud3g := {
		algo := algo,
		k := c_K_DEFAULT,
		op := c_OP_DEFAULT,
		op_is_opc := is_opc
	}
}

template (value) HlrSubscriber t_Sub2G3G(hexstring imsi, hexstring msisdn, charstring algo2g, charstring algo3g, boolean is_opc) := {
	imsi := imsi,
	msisdn := msisdn,
	aud2g := {
		algo := algo2g,
		ki := c_KI_DEFAULT
	},
	aud3g := {
		algo := algo3g,
		k := c_K_DEFAULT,
		op := c_OP_DEFAULT,
		op_is_opc := is_opc
	}
}

/* generate a variety of subscribers with different parameters */
function f_gen_subs() runs on test_CT return HlrSubscriberList {
	var HlrSubscriber sub;
	var HlrSubscriberList sl := {};

	sub := valueof(t_Sub2G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "comp128v1"));
	sl := sl & { sub };

	sub := valueof(t_Sub2G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "comp128v2"));
	sl := sl & { sub };

	sub := valueof(t_Sub2G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "comp128v3"));
	sl := sl & { sub };

	sub := valueof(t_Sub3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "milenage", false));
	sl := sl & { sub };

	sub := valueof(t_Sub3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "milenage", true));
	sl := sl & { sub };

	sub := valueof(t_Sub2G3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9),
				 "comp128v1", "milenage", false));
	sl := sl & { sub };

	sub := valueof(t_Sub2G3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9),
				 "comp128v2", "milenage", false));
	sl := sl & { sub };

	sub := valueof(t_Sub2G3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9),
				 "comp128v3", "milenage", false));
	sl := sl & { sub };

	return sl;
}
function f_gen_3G_subs() runs on test_CT return HlrSubscriberList {
	var HlrSubscriber sub;
	var HlrSubscriberList sl := {};

	sub := valueof(t_Sub3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "milenage", false));
	sl := sl & { sub };

	sub := valueof(t_Sub3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "milenage", true));
	sl := sl & { sub };

	sub := valueof(t_Sub2G3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9),
				 "comp128v1", "milenage", false));
	sl := sl & { sub };

	sub := valueof(t_Sub2G3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9),
				 "comp128v2", "milenage", false));
	sl := sl & { sub };

	sub := valueof(t_Sub2G3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9),
				 "comp128v3", "milenage", false));
	sl := sl & { sub };

	return sl;
}

function f_vty_transceive_match(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
	var charstring ret := f_vty_transceive_ret(pt, cmd);
	if (not match(ret, exp_ret)) {
		setverdict(fail, "Non-matching VTY response: ", ret);
		mtc.stop;
	}
}

function f_vty_transceive_nomatch(TELNETasp_PT pt, charstring cmd, template charstring exp_ret) {
	var charstring ret := f_vty_transceive_ret(pt, cmd);
	if (match(ret, exp_ret)) {
		setverdict(fail, "Matching VTY response: ", ret, ", should *not* have matched: ", exp_ret);
		mtc.stop;
	}
}

private template (value) charstring t_subscr_prefix(hexstring imsi) :=
	"subscriber imsi " & hex2str(imsi) & " ";

/* create a given subscriber using the VTY */
function f_vty_subscr_create(TELNETasp_PT VTY, HlrSubscriber sub) {
	var charstring prefix := valueof(t_subscr_prefix(sub.imsi));
	f_vty_transceive_match(VTY, prefix & "create", pattern "% Created subscriber *");
	f_vty_transceive_match(VTY, prefix & "update msisdn " & hex2str(sub.msisdn),
				pattern "% Updated subscriber *");
	if (ispresent(sub.aud2g)) {
		f_vty_transceive_match(VTY, prefix & "update aud2g " & sub.aud2g.algo &
					" ki " & oct2str(sub.aud2g.ki),
					pattern "");
	} else {
		f_vty_transceive_match(VTY, prefix & "update aud2g none", pattern "");
	}

	if (ispresent(sub.aud3g)) {
		var charstring op_mode := "op";
		if (sub.aud3g.op_is_opc) {
			op_mode := "opc";
		}
		f_vty_transceive_match(VTY, prefix & "update aud3g " & sub.aud3g.algo &
					" k " & oct2str(sub.aud3g.k) & " " & op_mode & " " &
					oct2str(sub.aud3g.op), pattern "");
	} else {
		f_vty_transceive_match(VTY, prefix & "update aud3g none", pattern "");
	}
}

function f_vty_subscr_update_msisdn(TELNETasp_PT VTY, HlrSubscriber sub, hexstring new_msisdn) {
	var charstring prefix := valueof(t_subscr_prefix(sub.imsi));
	f_vty_transceive_match(VTY, prefix & "update msisdn " & hex2str(new_msisdn),
				pattern "% Updated subscriber *");
}

/* perform 'delete' on subscriber */
function f_vty_subscr_delete(TELNETasp_PT VTY, HlrSubscriber sub) {
	var charstring prefix := valueof(t_subscr_prefix(sub.imsi));
	f_vty_transceive_match(VTY, prefix & "delete",
				pattern "% Deleted subscriber for IMSI *");
}

/* perform 'show' on subscriber; match result with pattern 'exp' */
function f_vty_subscr_show(TELNETasp_PT VTY, HlrSubscriber sub, template charstring exp) {
	var charstring prefix := valueof(t_subscr_prefix(sub.imsi));
	f_vty_transceive_match(VTY, prefix & "show", exp);
}

/* perform 'show' on subscriber; result must not match with pattern 'exp' */
function f_vty_subscr_show_nomatch(TELNETasp_PT VTY, HlrSubscriber sub, template charstring exp) {
	var charstring prefix := valueof(t_subscr_prefix(sub.imsi));
	f_vty_transceive_nomatch(VTY, prefix & "show", exp);
}

/***********************************************************************
 * Helper functions for ConnHdlr
 ***********************************************************************/

/* perform SendAuthInfo for given imsi, return the GSUP response/error */
function f_perform_SAI(hexstring imsi, template (omit) integer exp_err_cause := omit,
		       boolean is_eps := false, template (omit) integer num_auth_tuple := omit)
runs on HLR_ConnHdlr return GSUP_PDU {
	var template GSUP_PDU sai_msg;
	var GSUP_PDU ret;
	timer T := 3.0;
	var boolean exp_fail := false;
	if (not istemplatekind(exp_err_cause, "omit")) {
		exp_fail := true;
	}

	if (is_eps) {
		sai_msg := ts_GSUP_SAI_REQ_EPS(imsi);
	} else {
		sai_msg := ts_GSUP_SAI_REQ(imsi);
	}
	if (not istemplatekind(num_auth_tuple, "omit")) {
		sai_msg.ies := valueof(sai_msg.ies) & {
			valueof(ts_GSUP_IE_NUM_VECTORS_REQ(int2oct(valueof(num_auth_tuple), 1)))
		};
	}
	GSUP.send(sai_msg);

	T.start;
	alt {
	[exp_fail] GSUP.receive(tr_GSUP_SAI_ERR(imsi, exp_err_cause)) -> value ret {
		setverdict(pass);
		}
	[exp_fail] GSUP.receive(tr_GSUP_SAI_ERR(imsi, ?)) -> value ret {
		setverdict(fail, "Unexpected SAI ERROR Cause");
		}
	[exp_fail] GSUP.receive(tr_GSUP_SAI_RES(imsi)) -> value ret {
		setverdict(fail, "Unexpected SAI.res for unknown IMSI");
		}
	[not exp_fail] GSUP.receive(tr_GSUP_SAI_ERR(imsi, ?)) -> value ret {
		setverdict(fail, "Unexpected SAI ERROR");
		}
	[not exp_fail] GSUP.receive(tr_GSUP_SAI_RES(imsi)) -> value ret {
		setverdict(pass);
		}
	[] GSUP.receive { repeat; }
	[] T.timeout {
		setverdict(fail, "Timeout waiting for SAI response");
		mtc.stop;
		}
	}
	return ret;
}

function f_perform_UL(hexstring imsi, template hexstring msisdn,
			template (omit) integer exp_err_cause := omit,
			GSUP_CnDomain dom := OSMO_GSUP_CN_DOMAIN_PS,
			template (omit) octetstring source_name := omit)
runs on HLR_ConnHdlr return GSUP_PDU {
	var GSUP_PDU ret;
	timer T := 3.0;
	var boolean exp_fail := false;
	var boolean isd_done := false;
	if (not istemplatekind(exp_err_cause, "omit")) {
		exp_fail := true;
	}

	GSUP.send(valueof(ts_GSUP_UL_REQ(imsi, dom, source_name := source_name)));
	T.start;
	alt {
	[exp_fail] GSUP.receive(tr_GSUP_UL_ERR(imsi, exp_err_cause, destination_name := source_name)) -> value ret {
		setverdict(pass);
		}
	[exp_fail] GSUP.receive(tr_GSUP_UL_ERR(imsi, ?, destination_name := source_name)) -> value ret {
		setverdict(fail, "Unexpected UL ERROR Cause");
		mtc.stop;
		}
	[exp_fail] GSUP.receive(tr_GSUP_UL_RES(imsi, destination_name := source_name)) -> value ret {
		setverdict(fail, "Unexpected UL.res for unknown IMSI");
		mtc.stop;
		}
	[exp_fail] GSUP.receive(tr_GSUP_ISD_REQ(imsi, destination_name := source_name)) -> value ret {
		setverdict(fail, "Unexpected ISD.req in error case");
		mtc.stop;
		}
	[not exp_fail] GSUP.receive(tr_GSUP_UL_ERR(imsi, ?, destination_name := source_name)) -> value ret {
		setverdict(fail, "Unexpected UL ERROR");
		mtc.stop;
		}
	[not exp_fail and not isd_done] GSUP.receive(tr_GSUP_ISD_REQ(imsi, msisdn, destination_name := source_name)) -> value ret {
		GSUP.send(ts_GSUP_ISD_RES(imsi, source_name := source_name));
		isd_done := true;
		repeat;
		}
	[not exp_fail and isd_done] GSUP.receive(tr_GSUP_UL_RES(imsi, destination_name := source_name)) -> value ret {
		setverdict(pass);
		}
	[] GSUP.receive { repeat; }
	[] T.timeout {
		setverdict(fail, "Timeout waiting for UL response");
		mtc.stop;
		}
	}
	return ret;
}

/* Perform Location Update with IMSI that is unknown to OsmoHLR. Let it ask for the HLR that knows the IMSI via
 * mslookup. The TTCN-3 testsuite pretends to be the home HLR, that knows the IMSI. The proxy HLR connects to the
 * home HLR and forwards GSUP messages, until LU is complete.
 * Make sure to let a timer run while calling this function (see TC_MSLookup_GSUP_proxy). */
function f_perform_UL_proxy(hexstring imsi, hexstring msisdn, GSUP_CnDomain dom := OSMO_GSUP_CN_DOMAIN_PS)
runs on HLR_ConnHdlr {
	var MSLookup_mDNS mdns_msg;
	var integer id;
	var charstring domain := "gsup.hlr." & hex2str(imsi) & ".imsi.mdns.osmocom.org";
	var octetstring destination_name := char2oct(ccm_pars_client.ser_nr) & '00'O; /* TS-MSC IPA name */

	/* Testing the HLR as proxy. The MSC asking the proxy and the home HLR are both enacted by TTCN3 (TS).
	 * MSC=vc_GSUP_client <---> osmo-hlr as proxy <---> vc_GSUP_server=home HLR
	 * GSUP.send(..) to vc_GSUP_client ---> osmo-hlr ---> GSUP.receive(..) from vc_GSUP_server
	 * GSUP.receive(..) from vc_GSUP_client <--- osmo-hlr <--- GSUP.send(..) to vc_GSUP_server */

	/* [GSUP] TS-MSC  => HLR proxy: Update Location Request with unknown IMSI */
	GSUP.send(ts_GSUP_UL_REQ(imsi, dom)) to vc_GSUP_client;

	/* [GSUP] TS-HLR: expect GSUP messages with that IMSI */
	f_create_gsup_expect_explicit(hex2str(imsi), vc_GSUP_server);

	/* [mDNS] TS-HLR <=  HLR proxy: query for GSUP server who knows the IMSI */
	mDNS.receive(tr_MSLookup_mDNS_query(domain)) -> value mdns_msg;

	/* [mDNS] TS-HLR  => HLR proxy: result with IP/port of TS GSUP server */
	id := mdns_msg.dnsMessage.header.id;
	mDNS.send(ts_MSLookup_mDNS_result_IPv4(id, domain, f_enc_IPv4(mp_hlr_ts_ip), mp_hlr_ts_port));

	/* [GSUP] TS-HLR <=> HLR proxy <=> TS-MSC: finish up UL */
	GSUP.receive(tr_GSUP_UL_REQ(imsi)) from vc_GSUP_server;
	GSUP.send(ts_GSUP_ISD_REQ(imsi, msisdn, destination_name)) to vc_GSUP_server;
	GSUP.receive(tr_GSUP_ISD_REQ(imsi, g_pars.sub.msisdn)) from vc_GSUP_client;
	GSUP.send(ts_GSUP_ISD_RES(imsi, destination_name := destination_name)) to vc_GSUP_server;
	GSUP.receive(tr_GSUP_ISD_RES(imsi)) from vc_GSUP_client;
	GSUP.send(ts_GSUP_UL_RES(imsi, destination_name)) to vc_GSUP_server;
	GSUP.receive(tr_GSUP_UL_RES(imsi)) from vc_GSUP_client;
}

/* perform PurgeMS for given imsi, return the GSUP response/error */
function f_perform_PURGE(hexstring imsi, GSUP_CnDomain cn_dom,
			 template (omit) integer exp_err_cause := omit)
runs on HLR_ConnHdlr return GSUP_PDU {
	var GSUP_PDU ret;
	timer T := 3.0;
	var boolean exp_fail := false;
	if (not istemplatekind(exp_err_cause, "omit")) {
		exp_fail := true;
	}

	GSUP.send(valueof(ts_GSUP_PURGE_MS_REQ(imsi, cn_dom)));
	T.start;
	alt {
	[exp_fail] GSUP.receive(tr_GSUP_PURGE_MS_ERR(imsi, exp_err_cause)) -> value ret {
		setverdict(pass);
		}
	[exp_fail] GSUP.receive(tr_GSUP_PURGE_MS_ERR(imsi, ?)) -> value ret {
		setverdict(fail, "Unexpected PURGE ERROR Cause");
		}
	[exp_fail] GSUP.receive(tr_GSUP_PURGE_MS_RES(imsi)) -> value ret {
		setverdict(fail, "Unexpected PURGE.res for unknown IMSI");
		}
	[not exp_fail] GSUP.receive(tr_GSUP_PURGE_MS_ERR(imsi, ?)) -> value ret {
		setverdict(fail, "Unexpected PURGE ERROR");
		}
	[not exp_fail] GSUP.receive(tr_GSUP_PURGE_MS_RES(imsi)) -> value ret {
		setverdict(pass);
		}
	[] GSUP.receive { repeat; }
	[] T.timeout {
		setverdict(fail, "Timeout waiting for PURGE response");
		mtc.stop;
		}
	}
	return ret;
}

function f_SS_xceive(hexstring imsi, OCT4 sid, GSUP_SessionState state, octetstring ss,
			 template (omit) integer exp_err_cause := omit)
runs on HLR_ConnHdlr return GSUP_PDU {
	var GSUP_PDU ret;
	timer T := 3.0;
	var boolean exp_fail := false;
	if (not istemplatekind(exp_err_cause, "omit")) {
		exp_fail := true;
	}

	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(imsi, sid, state, ss)));
	T.start;
	alt {
	[exp_fail] GSUP.receive(tr_GSUP_PROC_SS_ERR(imsi, sid, ?, exp_err_cause)) -> value ret {
		setverdict(pass);
		}
	[exp_fail] GSUP.receive(tr_GSUP_PROC_SS_ERR(imsi, sid, ?, ?)) -> value ret {
		setverdict(fail, "Unexpected PROC_SS ERROR Cause");
		}
	[exp_fail] GSUP.receive(tr_GSUP_PROC_SS_RES(imsi, sid, ?, ?)) -> value ret {
		setverdict(fail, "Unexpected PROC_SS.res for unknown IMSI");
		}
	[not exp_fail] GSUP.receive(tr_GSUP_PROC_SS_ERR(imsi, sid, ?, ?)) -> value ret {
		setverdict(fail, "Unexpected PROC_SS ERROR");
		}
	[not exp_fail] GSUP.receive(tr_GSUP_PROC_SS_RES(imsi, sid, ?, ?)) -> value ret {
		setverdict(pass);
		}
	[] GSUP.receive { repeat; }
	[] T.timeout {
		setverdict(fail, "Timeout waiting for PROC_SS response");
		self.stop;
		}
	}
	return ret;
}

private function f_SS_expect(hexstring imsi, OCT4 sid, GSUP_SessionState state,
			     template SS_FacilityInformation facility := *,
			     template octetstring destination_name := omit)
runs on HLR_ConnHdlr return GSUP_PDU {
	var GSUP_PDU ret;
	timer T := 3.0;
	var boolean exp_ss := true;
	if (istemplatekind(facility, "omit")) {
		exp_ss := false;
	}
	T.start;
	alt {
	[] GSUP.receive(tr_GSUP_PROC_SS_ERR(imsi, sid, ?, ?)) -> value ret {
		setverdict(fail, "Unexpected PROC_SS ERROR Cause");
		mtc.stop;
		}
	[not exp_ss] GSUP.receive(tr_GSUP_PROC_SS_RES(imsi, sid, state, omit, destination_name := destination_name)) -> value ret {
		setverdict(pass);
		}
	[exp_ss] GSUP.receive(tr_GSUP_PROC_SS_RES(imsi, sid, state, omit)) -> value ret {
		setverdict(fail, "Unexpected PROC_SS.res without SS IE");
		mtc.stop;
		}
/*
	[exp_ss] GSUP.receive(tr_GSUP_PROC_SS_RES(imsi, sid, state, decmatch facility, destination_name := destination_name)) -> value ret {
		setverdict(pass);
		}
*/

	[exp_ss] GSUP.receive(tr_GSUP_PROC_SS_RES(imsi, sid, state, ?, destination_name := destination_name)) -> value ret {
		var GSUP_IeValue ss_ie;
		f_gsup_find_ie(ret, OSMO_GSUP_SS_INFO_IE, ss_ie);
		var SS_FacilityInformation dec_fac := dec_SS_FacilityInformation(ss_ie.ss_info);
		log("pattern: ", facility);
		if (match(dec_fac, facility)) {
			setverdict(pass);
		} else {
			setverdict(fail, "Unexpected PROC_SS.res with non-matching facility IE");
			mtc.stop;
		}
		}
	[] GSUP.receive {
		setverdict(fail, "Unexpected GSUP");
		mtc.stop;
		}
	[] T.timeout {
		setverdict(fail, "Timeout waiting for PROC_SS response");
		self.stop;
		}
	}

	return ret;
}

function f_perform_CHECK_IMEI(hexstring imsi, hexstring imei,
			      template (omit) integer exp_err_cause := omit,
			      template (omit) GSUP_IMEIResult result := omit,
			      template (omit) octetstring source_name := omit)
runs on HLR_ConnHdlr {
	var GSUP_PDU pdu;
	timer T := 3.0;
	var boolean exp_fail := false;
	if (not istemplatekind(exp_err_cause, "omit")) {
		exp_fail := true;
	}

	GSUP.send(valueof(ts_GSUP_CHECK_IMEI_REQ(imsi, imei, source_name := source_name)));
	T.start;
	alt {
	[exp_fail] GSUP.receive(tr_GSUP_CHECK_IMEI_ERR(imsi, exp_err_cause, destination_name := source_name)) -> value pdu {
		setverdict(pass);
		}
	[exp_fail] GSUP.receive(tr_GSUP_CHECK_IMEI_ERR(imsi, ?, destination_name := source_name)) -> value pdu {
		setverdict(fail, "Unexpected CHECK IMEI ERROR Cause: ", pdu);
		mtc.stop;
		}
	[exp_fail] GSUP.receive(tr_GSUP_CHECK_IMEI_RES(imsi, ?, destination_name := source_name)) -> value pdu {
		setverdict(fail, "Unexpected CHECK IMEI RES instead of ERR");
		mtc.stop;
		}
	[not exp_fail] GSUP.receive(tr_GSUP_CHECK_IMEI_ERR(imsi, ?, destination_name := source_name)) -> value pdu {
		setverdict(fail, "Unexpected CHECK IMEI ERROR");
		mtc.stop;
		}
	[not exp_fail] GSUP.receive(tr_GSUP_CHECK_IMEI_RES(imsi, result, destination_name := source_name)) -> value pdu {
		setverdict(pass);
		}
	[not exp_fail] GSUP.receive(tr_GSUP_CHECK_IMEI_RES(imsi, ?, destination_name := source_name)) -> value pdu {
		setverdict(fail, "Unexpected CHECK IMEI RES");
		mtc.stop;
		}
	[] GSUP.receive { repeat; }
	[] T.timeout {
		setverdict(fail, "Timeout waiting for CHECK IMEI response");
		mtc.stop;
		}
	}
}




/***********************************************************************
 * Testcases
 ***********************************************************************/

/* 23.003 Section 2.2 clearly states that an IMSI with less
 * than 5 digits is impossible. Even 5 digits is still questionable */
private function f_TC_gsup_sai_err_invalid_imsi() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_SAI(g_pars.sub.imsi, 96); /* Invalid Mandatory information */
	setverdict(pass);
}
testcase TC_gsup_sai_err_invalid_imsi() runs on test_CT {
	var HLR_ConnHdlr vc_conn;
	var HLR_ConnHdlrPars pars := valueof(t_Pars('0123'H));
	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_gsup_sai_err_invalid_imsi), pars);
	vc_conn.done;
}


private function f_TC_gsup_sai_err_unknown_imsi() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_SAI(g_pars.sub.imsi, 2);
	setverdict(pass);
}

testcase TC_gsup_sai_err_unknown_imsi() runs on test_CT {
	var HLR_ConnHdlr vc_conn;
	var HLR_ConnHdlrPars pars := valueof(t_Pars(f_rnd_imsi('26242'H)));
	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_gsup_sai_err_unknown_imsi), pars);
	vc_conn.done;
}

function f_start_handler_per_sub(void_fn fn, HlrSubscriberList sl, boolean via_proxy := false) runs on test_CT {
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HlrSubscriber sub := sl[i];
		var HLR_ConnHdlrPars pars;
		if (via_proxy) {
			pars := valueof(t_Pars_sub_via_proxy(sub));
		} else {
			pars := valueof(t_Pars_sub(sub));
		}
		var HLR_ConnHdlr vc_conn;

		f_vty_subscr_create(VTY, sub);
		vc_conn := f_start_handler(fn, pars);
		vc_conn.done;
		f_vty_subscr_delete(VTY, sub);
	}
}

/* test SAI for a number of different subscriber cases (algo, 2g/3g, ...) */
private function f_TC_gsup_sai() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_SAI(g_pars.sub.imsi);
	if (ispresent(g_pars.sub.aud3g)) {
		f_ensure_amf_separation_bit(res, '0'B);
	}

	f_count_auth_tuples(res, mp_default_num_auth_tuples);
	setverdict(pass);
}
testcase TC_gsup_sai() runs on test_CT {
	var HlrSubscriberList sl;
	var GSUP_PDU res;

	f_init(false);

	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_sai), sl);

	setverdict(pass);
}

/* test SAI for a number of different subscriber cases (algo, 2g/3g, ...) */
private function f_TC_gsup_sai_num_auth_vectors() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_SAI(g_pars.sub.imsi, num_auth_tuple := 1);
	f_count_auth_tuples(res, 1);
	res := f_perform_SAI(g_pars.sub.imsi, num_auth_tuple := 4);
	f_count_auth_tuples(res, 4);
	res := f_perform_SAI(g_pars.sub.imsi, num_auth_tuple := 5);
	f_count_auth_tuples(res, 5);
	res := f_perform_SAI(g_pars.sub.imsi, num_auth_tuple := 254);
	f_count_auth_tuples(res, 5);
	setverdict(pass);
}
testcase TC_gsup_sai_num_auth_vectors() runs on test_CT {
	var HlrSubscriberList sl;
	var GSUP_PDU res;

	f_init(false);

	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_sai_num_auth_vectors), sl);

	setverdict(pass);
}

private function f_ensure_amf_separation_bit(GSUP_PDU res, BIT1 sep_bit)
{
	for (var integer i := 0; i < lengthof(res.ies); i := i+1) {
		var GSUP_IE tuple := res.ies[i];
		if (tuple.tag != OSMO_GSUP_AUTH_TUPLE_IE) {
			continue;
		}
		var GSUP_IeValue autn;
		if (f_gsup_find_nested_ie(tuple.val.auth_tuple, OSMO_GSUP_AUTN_IE, autn) == false) {
			setverdict(fail, "Couldn't find AUTN IE in tuple ", i);
			mtc.stop;
		}
		var bitstring amf := oct2bit(substr(autn.autn, 6, 2));
		if (amf[0] != sep_bit) {
			setverdict(fail, "AMF bit 0 (separation bit) must be ", sep_bit,"  but was not");
			mtc.stop;
		}
	}
}

private function f_count_auth_tuples(GSUP_PDU res, template (omit) integer expected_auth_tuples := omit)
{
	var integer auth_tuples := 0;
	for (var integer i := 0; i < lengthof(res.ies); i := i+1) {
		var GSUP_IE tuple := res.ies[i];
		if (tuple.tag == OSMO_GSUP_AUTH_TUPLE_IE) {
			auth_tuples := auth_tuples + 1;
		}
	}

	if ((not istemplatekind(expected_auth_tuples, "omit")) and
			not match(auth_tuples, valueof(expected_auth_tuples))) {
		setverdict(fail,
			"Did not received expected number of auth tuples. Expected ",
			mp_default_num_auth_tuples,
			" but received ", auth_tuples);
	}
}

/* test SAI for a number of different subscriber cases (algo, 2g/3g, ...) */
private function f_TC_gsup_sai_eps() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_SAI(g_pars.sub.imsi, is_eps := true);
	f_ensure_amf_separation_bit(res, '1'B);

	/* TODO: match if tuple[s] matches expectation */
	setverdict(pass);
}
testcase TC_gsup_sai_eps() runs on test_CT {
	var HlrSubscriberList sl;
	var GSUP_PDU res;

	f_init(false);

	sl := f_gen_3G_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_sai_eps), sl);

	setverdict(pass);
}


/* test UL for unknown IMSI */
private function f_TC_ul_unknown_imsi() runs on HLR_ConnHdlr {
	f_perform_UL(g_pars.sub.imsi, ?, 2, source_name := g_pars.source_name);
	setverdict(pass);
}
testcase TC_gsup_ul_unknown_imsi() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi));
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_ul_unknown_imsi), pars);
	vc_conn.done;
}
testcase TC_gsup_ul_unknown_imsi_via_proxy() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var HLR_ConnHdlrPars pars := valueof(t_Pars_via_proxy(imsi));
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_ul_unknown_imsi), pars);
	vc_conn.done;
}

/* test UL for a number of different subscriber cases (algo, 2g/3g, ...) */
private function f_TC_gsup_ul() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_UL(g_pars.sub.imsi, g_pars.sub.msisdn, source_name := g_pars.source_name);
	setverdict(pass);
}
testcase TC_gsup_ul() runs on test_CT {
	var HlrSubscriberList sl;
	var GSUP_PDU res;

	f_init(false);
	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_ul), sl);

	setverdict(pass);
}
testcase TC_gsup_ul_via_proxy() runs on test_CT {
	var HlrSubscriberList sl;
	var GSUP_PDU res;

	f_init(false);
	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_ul), sl, via_proxy := true);

	setverdict(pass);
}

/* Test only the VTY commands */
testcase TC_vty() runs on test_CT {
	var HlrSubscriber sub;

	f_init();

	/* we're not using f_gen_subs() here as the expect pattern for the 'show' are different
	 * from case to case */
	sub := valueof(t_Sub2G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "comp128v1"));
	f_vty_subscr_create(VTY, sub);
	f_vty_subscr_show(VTY, sub, pattern "*IMSI: *2G auth: COMP128v1*");
	f_vty_subscr_delete(VTY, sub);

	sub := valueof(t_Sub3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9),
				"milenage", false));
	f_vty_subscr_create(VTY, sub);
	f_vty_subscr_show(VTY, sub, pattern "*IMSI: *3G auth: MILENAGE*");
	f_vty_subscr_delete(VTY, sub);

	sub := valueof(t_Sub2G3G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9),
				 "comp128v1", "milenage", false));
	f_vty_subscr_create(VTY, sub);
	f_vty_subscr_show(VTY, sub, pattern "*IMSI: *2G auth: COMP128v1*3G auth: MILENAGE*");
	f_vty_subscr_delete(VTY, sub);

	setverdict(pass);
}

/* VTY changes to MSISDN should result in ISD to current VLR */
private function f_TC_vty_msisdn_isd() runs on HLR_ConnHdlr {
	var hexstring new_msisdn;
	var GSUP_PDU res;
	timer T := 5.0;

	/* Create Subscriber */
	f_vty_subscr_create(VTY, g_pars.sub);

	/* Perform UpdateLocation (VLR now known to HLR) */
	res := f_perform_UL(g_pars.sub.imsi, g_pars.sub.msisdn);

	/* Then change IMSI via VTY */
	new_msisdn := '49161'H & f_rnd_hexstring(7, 9);
	f_vty_subscr_update_msisdn(VTY, g_pars.sub, new_msisdn);
	/* And expect InsertSubscriberData as result */
	T.start;
	alt {
	[] GSUP.receive(tr_GSUP_ISD_REQ(g_pars.sub.imsi, new_msisdn)) {
		GSUP.send(ts_GSUP_ISD_RES(g_pars.sub.imsi));
		g_pars.sub.msisdn := new_msisdn;
		setverdict(pass);
		}
	[] GSUP.receive(tr_GSUP_ISD_REQ(g_pars.sub.imsi, g_pars.sub.msisdn)) {
		log("received ISD req with old MSISDN");
		setverdict(fail);
		}
	[] GSUP.receive { repeat; }
	[] T.timeout {
		setverdict(fail, "Timeout waiting for ISD.req");
		}
	}
}
testcase TC_vty_msisdn_isd() runs on test_CT {
	var HlrSubscriber sub;
	var HLR_ConnHdlr vc_conn;

	f_init(false);

	/* Create Subscriber */
	sub := valueof(t_Sub2G(f_rnd_imsi('26242'H), '49161'H & f_rnd_hexstring(7, 9), "comp128v1"));

	vc_conn := f_start_handler(refers(f_TC_vty_msisdn_isd), valueof(t_Pars_sub(sub)));
	vc_conn.done;
}

/* Test PURGE MS for CS services */
private function f_TC_gsup_purge_cs() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_UL(g_pars.sub.imsi, g_pars.sub.msisdn);
	res := f_perform_PURGE(g_pars.sub.imsi, OSMO_GSUP_CN_DOMAIN_CS);
}
testcase TC_gsup_purge_cs() runs on test_CT {
	var HlrSubscriberList sl;
	var GSUP_PDU res;

	f_init(false);
	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_purge_cs), sl);

	setverdict(pass);
}

/* Test PURGE MS for PS services */
private function f_TC_gsup_purge_ps() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_UL(g_pars.sub.imsi, g_pars.sub.msisdn);
	res := f_perform_PURGE(g_pars.sub.imsi, OSMO_GSUP_CN_DOMAIN_PS);
}
testcase TC_gsup_purge_ps() runs on test_CT {
	var HlrSubscriberList sl;

	f_init(false);
	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_purge_ps), sl);

	setverdict(pass);
}

/* Test PURGEG MS procedure for unknown IMSI */

private function f_TC_gsup_purge_unknown() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	res := f_perform_PURGE(g_pars.sub.imsi, OSMO_GSUP_CN_DOMAIN_CS, 2);
}
testcase TC_gsup_purge_unknown() runs on test_CT {
	var hexstring imsi := '2345743413463'H;
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi));
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_ul_unknown_imsi), pars);
	vc_conn.done;

	setverdict(pass);
}

import from HLR_EUSE all;

/* Test for USSD request to undefined/unrouted short-code. Expect ss-NotAvailable(18) */
private function f_TC_mo_ussd_unknown() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	var octetstring ss := f_USSD_FACILITY_IE_INVOKE(
					op_code := SS_OP_CODE_PROCESS_USS_REQ,
					ussd_string := "*#200#");
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(g_pars.sub.imsi, g_pars.ussd.sid,
						OSMO_GSUP_SESSION_STATE_BEGIN, ss)));
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_END,
			   tr_SS_FACILITY_RETURN_ERROR(1, 18));
}
testcase TC_mo_ussd_unknown() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_unknown), pars);
		vc_conn.done;
	}
}

/* Test for USSD request to currently disconnected EUSE. Expect ss-SystemFailure(34) */
private function f_TC_mo_ussd_euse_disc() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	var octetstring ss := f_USSD_FACILITY_IE_INVOKE(
					op_code := SS_OP_CODE_PROCESS_USS_REQ,
					ussd_string := "*100#");
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(g_pars.sub.imsi, g_pars.ussd.sid,
						OSMO_GSUP_SESSION_STATE_BEGIN, ss)));
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_END,
			   tr_SS_FACILITY_RETURN_ERROR(1, 34));
}
testcase TC_mo_ussd_euse_disc() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_euse_disc), pars);
		vc_conn.done;
	}
}

/* Test for USSD request to internal own-imsi IUSE. */
private function f_TC_mo_ussd_iuse_imsi() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	var charstring resp_str;
	var octetstring ss := f_USSD_FACILITY_IE_INVOKE(
					op_code := SS_OP_CODE_PROCESS_USS_REQ,
					ussd_string := "*#101#");
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(g_pars.sub.imsi, g_pars.ussd.sid,
						OSMO_GSUP_SESSION_STATE_BEGIN, ss,
						source_name := g_pars.source_name)));
	resp_str := "Your IMSI is " & hex2str(g_pars.sub.imsi);
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_END,
			   tr_SS_USSD_FACILITY_RETURN_RESULT(1, 59, SS_USSD_DEFAULT_DCS,
							     f_encGSM7bit(resp_str)),
			   destination_name := g_pars.source_name);
}
testcase TC_mo_ussd_iuse_imsi() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_iuse_imsi), pars);
		vc_conn.done;
	}
}
testcase TC_mo_ussd_iuse_imsi_via_proxy() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub_via_proxy(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_iuse_imsi), pars);
		vc_conn.done;
	}
}

/* Test for USSD request to internal own-msisdn IUSE. */
private function f_TC_mo_ussd_iuse_msisdn() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	var charstring resp_str;
	var octetstring ss := f_USSD_FACILITY_IE_INVOKE(
					op_code := SS_OP_CODE_PROCESS_USS_REQ,
					ussd_string := "*#100#");
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(g_pars.sub.imsi, g_pars.ussd.sid,
						OSMO_GSUP_SESSION_STATE_BEGIN, ss,
						source_name := g_pars.source_name)));
	resp_str := "Your extension is " & hex2str(g_pars.sub.msisdn);
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_END,
			   tr_SS_USSD_FACILITY_RETURN_RESULT(1, 59, SS_USSD_DEFAULT_DCS,
							     f_encGSM7bit(resp_str)),
			   destination_name := g_pars.source_name);
}
testcase TC_mo_ussd_iuse_msisdn() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_iuse_msisdn), pars);
		vc_conn.done;
	}
}
testcase TC_mo_ussd_iuse_msisdn_via_proxy() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub_via_proxy(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_iuse_msisdn), pars);
		vc_conn.done;
	}
}

/* Test routing of USSD to EUSE by a specific route */
private function f_TC_mo_ussd_100() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	/* invoke / invoke id 1 / processUSS-req */
	//var octetstring ss := 'a11202010102013b300a04010f0405aa180c3602'O;
	var octetstring ss := f_USSD_FACILITY_IE_INVOKE(
					op_code := SS_OP_CODE_PROCESS_USS_REQ,
					ussd_string := "*100#");
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(g_pars.sub.imsi, g_pars.ussd.sid,
						OSMO_GSUP_SESSION_STATE_BEGIN, ss)));
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_END,
			   tr_SS_USSD_FACILITY_RETURN_RESULT(1, 59, SS_USSD_DEFAULT_DCS,
							     f_encGSM7bit("*100#")));
}
testcase TC_mo_ussd_euse() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	var HLR_EUSE_CT vc_EUSE := HLR_EUSE_CT.create("EUSE-" & testcasename());
	vc_EUSE.start(HLR_EUSE.f_main_mo(mp_hlr_ip, mp_hlr_gsup_port, "foobar", refers(f_ss_echo)));

	f_init(false);
	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_100), pars);
		vc_conn.done;
	}

	vc_EUSE.stop;
}

/* Test routing of USSD to EUSE by a specific route, with CONTINUE */
private function f_TC_mo_ussd_100_continue() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	/* Simulate BEGIN from MS/MSC */
	var octetstring ss := f_USSD_FACILITY_IE_INVOKE(op_code := SS_OP_CODE_PROCESS_USS_REQ,
							ussd_string := "*100#");
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(g_pars.sub.imsi, g_pars.ussd.sid,
						OSMO_GSUP_SESSION_STATE_BEGIN, ss)));
	/* expect echo response from EUSE */
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_CONTINUE,
			   tr_SS_USSD_FACILITY_RETURN_RESULT(1, 59, SS_USSD_DEFAULT_DCS,
							     f_encGSM7bit("*100#")));

	/* Simulate CONTINUE from MS/MSC */
	ss := f_USSD_FACILITY_IE_INVOKE(op_code := SS_OP_CODE_PROCESS_USS_REQ,
					ussd_string := "mahlzeit");
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(g_pars.sub.imsi, g_pars.ussd.sid,
						OSMO_GSUP_SESSION_STATE_CONTINUE, ss)));

	/* expect echo response from EUSE */
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_END,
			   tr_SS_USSD_FACILITY_RETURN_RESULT(1, 59, SS_USSD_DEFAULT_DCS,
							     f_encGSM7bit("mahlzeit")));
}
testcase TC_mo_ussd_euse_continue() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	var HLR_EUSE_CT vc_EUSE := HLR_EUSE_CT.create("EUSE-" & testcasename());
	vc_EUSE.start(HLR_EUSE.f_main_mo(mp_hlr_ip, mp_hlr_gsup_port, "foobar",
					 refers(f_ss_echo_continue)));

	f_init(false);
	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_100_continue), pars);
		vc_conn.done;
	}

	vc_EUSE.stop;
}


/* Test routing of USSD to EUSE by default-route */
private function f_TC_mo_ussd_999() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	var octetstring ss := f_USSD_FACILITY_IE_INVOKE(
					op_code := SS_OP_CODE_PROCESS_USS_REQ,
					ussd_string := "*999#");
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(g_pars.sub.imsi, g_pars.ussd.sid,
						OSMO_GSUP_SESSION_STATE_BEGIN, ss)));
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_END,
			   tr_SS_USSD_FACILITY_RETURN_RESULT(1, 59, SS_USSD_DEFAULT_DCS,
							     f_encGSM7bit("*999#")));
}
testcase TC_mo_ussd_euse_defaultroute() runs on test_CT {
	var HlrSubscriberList sl;
	var HLR_ConnHdlr vc_conn;

	var HLR_EUSE_CT vc_EUSE := HLR_EUSE_CT.create("EUSE-" & testcasename());
	vc_EUSE.start(HLR_EUSE.f_main_mo(mp_hlr_ip, mp_hlr_gsup_port, "foobar", refers(f_ss_echo)));

	f_init(false);
	f_vty_config(VTY, "hlr", "ussd default-route external foobar");

	sl := f_gen_subs();
	for (var integer i := 0; i < sizeof(sl); i := i+1) {
		var HLR_ConnHdlrPars pars := valueof(t_Pars_sub(sl[i]));
		pars.ussd.sid := f_rnd_octstring(4);
		f_vty_subscr_create(VTY, pars.sub);
		vc_conn := f_start_handler(refers(f_TC_mo_ussd_999), pars);
		vc_conn.done;
	}

	f_vty_config(VTY, "hlr", "no ussd default-route");
	vc_EUSE.stop;
}

/**
 * Test "Structured" Supplementary Services
 *
 * NOTE: at the moment, OsmoHLR doesn't support "structured" SS,
 * so such requests are being rejected (actually ReturnError
 * with GSM0480_ERR_CODE_FACILITY_NOT_SUPPORTED).
 */
private function f_TC_mo_sss_reject() runs on HLR_ConnHdlr {
	var SS_FacilityInformation ss_req;
	var GSUP_PDU res;

	/* Prepare '*#21#' SS request */
	ss_req := valueof(ts_SS_FACILITY_INVOKE(
		invoke_id := 0,
		op_code := SS_OP_CODE_INTERROGATE_SS,
		ss_code := '21'O));

	/* Send it towards HLR */
	GSUP.send(valueof(ts_GSUP_PROC_SS_REQ(
		g_pars.sub.imsi,
		g_pars.ussd.sid,
		OSMO_GSUP_SESSION_STATE_BEGIN,
		enc_SS_FacilityInformation(ss_req))));

	/* Expect ReturnError with FACILITY_NOT_SUPPORTED */
	res := f_SS_expect(g_pars.sub.imsi, g_pars.ussd.sid,
			   OSMO_GSUP_SESSION_STATE_END,
			   tr_SS_FACILITY_RETURN_ERROR(0, 21));
}
testcase TC_mo_sss_reject() runs on test_CT {
	var HLR_ConnHdlrPars pars;
	var HLR_ConnHdlr vc_conn;
	var HlrSubscriber sub;

	f_init(false);

	/* Create a random subscriber */
	sub := valueof(t_Sub2G(f_rnd_imsi('26242'H),
		'49161'H & f_rnd_hexstring(7, 9), "comp128v1"));
	pars := valueof(t_Pars_sub(sub));
	pars.ussd.sid := f_rnd_octstring(4);

	f_vty_subscr_create(VTY, sub);
	vc_conn := f_start_handler(refers(f_TC_mo_sss_reject), pars);
	vc_conn.done;
	f_vty_subscr_delete(VTY, sub);
}


/* TODO USSD:
   * MO USSD for IMSI of non-existant subscriber
   * MT USSD from EUSE
   * timeout cases
 */


/* Test Check IMEI */
private function f_TC_gsup_check_imei() runs on HLR_ConnHdlr {
	/* store-imei disabled */
	f_vty_config(VTY, "hlr", "no store-imei");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '12345678901234'H, result := OSMO_GSUP_IMEI_RESULT_ACK,
		source_name := g_pars.source_name);
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*IMEI: *12345678901234*");

	/* store-imei enabled */
	f_vty_config(VTY, "hlr", "store-imei");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '12345678901234'H, result := OSMO_GSUP_IMEI_RESULT_ACK,
		source_name := g_pars.source_name);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*IMEI: *12345678901234*");
}
testcase TC_gsup_check_imei() runs on test_CT {
	var HlrSubscriberList sl;

	f_init(false);
	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_check_imei), sl);

	setverdict(pass);
}
testcase TC_gsup_check_imei_via_proxy() runs on test_CT {
	var HlrSubscriberList sl;

	f_init(false);
	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_check_imei), sl, via_proxy := true);

	setverdict(pass);
}

/* Test Check IMEI with invalid IMEI length */
private function f_TC_gsup_check_imei_invalid_len() runs on HLR_ConnHdlr {
	/* IMEI too long */
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '111456789012345F'H, 96 /* Invalid Mandatory Information */);
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '2224567890123456'H, 96 /* Invalid Mandatory Information */);

	/* IMEI too short */
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '3334567890123F'H, 96 /* Invalid Mandatory Information */);
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '444456789012'H, 96 /* Invalid Mandatory Information */);
	f_perform_CHECK_IMEI(g_pars.sub.imsi, ''H, 96 /* Invalid Mandatory Information */);
}
testcase TC_gsup_check_imei_invalid_len() runs on test_CT {
	var HlrSubscriberList sl;

	f_init(false);
	sl := f_gen_subs();
	f_start_handler_per_sub(refers(f_TC_gsup_check_imei_invalid_len), sl);

	setverdict(pass);
}

/* Test Check IMEI with unknown IMSI */
private function f_TC_gsup_check_imei_unknown_imsi() runs on HLR_ConnHdlr {
	f_vty_config(VTY, "hlr", "no store-imei");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '22245678901234'H, 96 /* Invalid Mandatory Information */);

	/* Check again with store-imei set (different code path) */
	f_vty_config(VTY, "hlr", "store-imei");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '22245678901234'H, 96 /* Invalid Mandatory Information */);

	setverdict(pass);
}
testcase TC_gsup_check_imei_unknown_imsi() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi));
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_gsup_check_imei_unknown_imsi), pars);
	vc_conn.done;
}

/* Test create-subscriber-on-demand during Check IMEI (OsmoMSC would be set to "check-imei-rqd early") */
private function f_TC_subscr_create_on_demand_check_imei_early() runs on HLR_ConnHdlr {
	var GSUP_PDU res; /* save various return values to prevent ttcn3 compiler warnings */
	var charstring imsi_pattern := "*IMSI: " & hex2str(g_pars.sub.imsi) & "*";

	/* Random MSISDN and CS+PS NAM (LU must pass) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 cs+ps");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '12345678901234'H, result := OSMO_GSUP_IMEI_RESULT_ACK);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d*"); /* last line, hence not matching "\d\d\d\n" */
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*[CP]S disabled*");
	res := f_perform_UL(g_pars.sub.imsi, ?);
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* Random MSISDN and PS NAM (LU must pass) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 ps");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '12345678901234'H, result := OSMO_GSUP_IMEI_RESULT_ACK);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d*");
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*CS disabled*");
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*PS disabled*");
	res := f_perform_UL(g_pars.sub.imsi, ?);
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* Random MSISDN and CS NAM (LU must fail) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 cs");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '12345678901234'H, result := OSMO_GSUP_IMEI_RESULT_ACK);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d*");
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*PS disabled*");
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*CS disabled*");
	res := f_perform_UL(g_pars.sub.imsi, ?, 7 /* GPRS services not allowed */);
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* Random MSISDN and no NAM (LU must fail) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 none");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '12345678901234'H, result := OSMO_GSUP_IMEI_RESULT_ACK);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d*");
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*CS disabled*PS disabled*");
	res := f_perform_UL(g_pars.sub.imsi, ?, 7 /* GPRS services not allowed */);
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* No MSISDN and no NAM (LU must fail) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand no-msisdn none");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '12345678901234'H, result := OSMO_GSUP_IMEI_RESULT_ACK);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: none*");
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*CS disabled*PS disabled*");
	res := f_perform_UL(g_pars.sub.imsi, ?, 7 /* GPRS services not allowed */);
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* No MSISDN and CS+PS NAM (LU must pass) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand no-msisdn cs+ps");
	f_perform_CHECK_IMEI(g_pars.sub.imsi, '12345678901234'H, result := OSMO_GSUP_IMEI_RESULT_ACK);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: none*");
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*[CP]S disabled*");
	res := f_perform_UL(g_pars.sub.imsi, omit);
	f_vty_subscr_delete(VTY, g_pars.sub);

	setverdict(pass);
}
testcase TC_subscr_create_on_demand_check_imei_early() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi));
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_subscr_create_on_demand_check_imei_early), pars);
	vc_conn.done;
}

/* Test create-subscriber-on-demand during LU (Location Update) */
private function f_TC_subscr_create_on_demand_ul() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	var charstring imsi_pattern := "*IMSI: " & hex2str(g_pars.sub.imsi) & "*";

	/* Random MSISDN and CS+PS NAM (LU must pass) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 cs+ps");
	res := f_perform_UL(g_pars.sub.imsi, ?);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d\n*");
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*[CP]S disabled*");
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* Random MSISDN and PS NAM (LU must pass) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 ps");
	res := f_perform_UL(g_pars.sub.imsi, ?);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d\n*");
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*CS disabled*");
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*PS disabled*");
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* Random MSISDN and CS NAM (LU must fail) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 cs");
	res := f_perform_UL(g_pars.sub.imsi, ?, 7 /* GPRS services not allowed */);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d\n*");
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*PS disabled*");
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*CS disabled*");
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* Random MSISDN and no NAM (LU must fail) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 none");
	res := f_perform_UL(g_pars.sub.imsi, ?, 7 /* GPRS services not allowed */);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d\n*");
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*CS disabled*PS disabled*");
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* No MSISDN and no NAM (LU must fail) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand no-msisdn none");
	res := f_perform_UL(g_pars.sub.imsi, ?, 7 /* GPRS services not allowed */);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: none*");
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*CS disabled*PS disabled*");
	f_vty_subscr_delete(VTY, g_pars.sub);

	/* No MSISDN and CS+PS NAM (LU must pass) */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand no-msisdn cs+ps");
	res := f_perform_UL(g_pars.sub.imsi, omit);
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: none*");
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*[CP]S disabled*");
	f_vty_subscr_delete(VTY, g_pars.sub);

	setverdict(pass);
}
testcase TC_subscr_create_on_demand_ul() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi));
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_subscr_create_on_demand_ul), pars);
	vc_conn.done;
}

/* Test create-subscriber-on-demand during SAI (SendAuthInfo) */
private function f_TC_subscr_create_on_demand_sai() runs on HLR_ConnHdlr {
	var GSUP_PDU res;
	var charstring imsi_pattern := "*IMSI: " & hex2str(g_pars.sub.imsi) & "*";

	/* HLR creates the subscriber on demand. Then the IMSI is known, but there is no auth data, so the HLR returns
         * the "slightly inaccurate cause 'IMSI Unknown' via GSUP". The MS is able to do a LU afterwards. */
	f_vty_config(VTY, "hlr", "subscriber-create-on-demand 3 cs+ps");
	res := f_perform_SAI(g_pars.sub.imsi, 2 /* IMSI Unknown */ );

	/* Verify that it was created before the LU */
	f_vty_subscr_show(VTY, g_pars.sub, pattern imsi_pattern);
	f_vty_subscr_show(VTY, g_pars.sub, pattern "*MSISDN: \d\d\d*"); /* last line, hence not matching "\d\d\d\n" */
	f_vty_subscr_show_nomatch(VTY, g_pars.sub, pattern "*[CP]S disabled*");

	/* Location update */
	res := f_perform_UL(g_pars.sub.imsi, ?);
	f_vty_subscr_delete(VTY, g_pars.sub);
	setverdict(pass);
}
testcase TC_subscr_create_on_demand_sai() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi));
	var HLR_ConnHdlr vc_conn;

	f_init(false);
	vc_conn := f_start_handler(refers(f_TC_subscr_create_on_demand_sai), pars);
	vc_conn.done;
}

/* Send an mslookup mDNS request to the home HLR, asking about a service that is not "gsup.hlr". (Hence the "_other" in
 * the test name, service "gsup.hlr" has different code paths, see related tests). */
private function f_TC_MSLookup_mDNS_service_other_home() runs on HLR_ConnHdlr {
	timer T;
	var MSLookup_mDNS vl_rmsg;
	var integer id := f_rnd_int(65535); /* mDNS packet ID */
	var hexstring msisdn := '49161'H & f_rnd_hexstring(7, 9);
	var charstring domain := "sip.voice." & hex2str(msisdn) & ".msisdn.mdns.osmocom.org";

	/* Create subscriber */
	g_pars.sub.msisdn := msisdn;
	f_vty_subscr_create(VTY, g_pars.sub);

	/* Send mDNS query, expect timeout */
	mDNS.send(ts_MSLookup_mDNS_query(id, domain));
	T.start(1.0);
	alt {
	[] mDNS.receive(tr_MSLookup_mDNS_result_IPv4(id, domain, '42424242'O, 5060)) {
		setverdict(fail, "OsmoHLR answered to mDNS query before subscriber did LU");
		}
	[] mDNS.receive(MSLookup_mDNS:?) -> value vl_rmsg {
		repeat;
		}
	[] T.timeout {
		}
	}

	/* Location update */
	f_perform_UL(g_pars.sub.imsi, msisdn, dom := OSMO_GSUP_CN_DOMAIN_CS);

	/* Send mDNS query again, expect result */
	mDNS.send(ts_MSLookup_mDNS_query(id, domain));
	T.start(1.0);
	alt {
	[] mDNS.receive(tr_MSLookup_mDNS_result_IPv4(id, domain, '42424242'O, 5060)) {
		setverdict(pass);
		}
	[] mDNS.receive(MSLookup_mDNS:?) -> value vl_rmsg {
		repeat;
		}
	[] T.timeout {
		setverdict(fail, "OsmoHLR did not answer to mDNS query");
		}
	}

	f_vty_subscr_delete(VTY, g_pars.sub);
}
testcase TC_MSLookup_mDNS_service_other_home() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var HLR_ConnHdlr vc_conn;
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi));

	f_init(false);
	f_vty_config(VTY, "mslookup", "mdns bind");
	f_vty_config2(VTY, {"mslookup", "server"}, "service sip.voice at 66.66.66.66 5060");

	vc_conn := f_start_handler(refers(f_TC_MSLookup_mDNS_service_other_home), pars);
	vc_conn.done;
}

/* Let OsmoHLR act as proxy between MSC and another HLR during UL */
private function f_TC_MSLookup_GSUP_proxy() runs on HLR_ConnHdlr {
	f_perform_UL_proxy(g_pars.sub.imsi, g_pars.sub.msisdn, OSMO_GSUP_CN_DOMAIN_CS);
	setverdict(pass);
}
testcase TC_MSLookup_GSUP_proxy() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var hexstring msisdn := '49161'H & f_rnd_hexstring(7, 9);
	var HLR_ConnHdlr vc_conn;
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi, msisdn));

	f_init(false, true);
	f_vty_config(VTY, "mslookup", "mdns bind");
	vc_conn := f_start_handler(refers(f_TC_MSLookup_GSUP_proxy), pars, true);

	timer T := 3.0;
	T.start;
	alt {
	[] vc_conn.done {}
	[] T.timeout { setverdict(fail, "Timeout"); mtc.stop; };
	}
}

/* Request "gsup.hlr" service right after creating subscriber from the home HLR. "TC_MSLookup_mDNS_service_other_home"
 * is similar, but does not query the "gsup.hlr" service. The "gsup.hlr" service has a different code path in OsmoHLR:
 * - it exists without being explicitly configured and returns the IP and port of the HLR's own GSUP server
 * - the request is answered, even if the subscriber is not attached to the HLR (for Location Update via proxy) */
private function f_TC_MSLookup_mDNS_service_GSUP_HLR_home() runs on HLR_ConnHdlr {
	timer T;
	var MSLookup_mDNS vl_rmsg;
	var integer id := f_rnd_int(65535); /* mDNS packet ID */
	var charstring domain := "gsup.hlr." & hex2str(g_pars.sub.imsi) & ".imsi.mdns.osmocom.org";

	/* Create subscriber */
	g_pars.sub.msisdn := '133713381339'H;
	f_vty_subscr_create(VTY, g_pars.sub);

	/* Send mDNS query, expect result */
	mDNS.send(ts_MSLookup_mDNS_query(id, domain));
	T.start(1.0);
	alt {
	[] mDNS.receive(tr_MSLookup_mDNS_result_IPv4(id, domain, f_enc_IPv4(mp_hlr_ip), mp_hlr_gsup_port)) {
		setverdict(pass);
		}
	[] mDNS.receive(MSLookup_mDNS:?) -> value vl_rmsg {
		repeat;
		}
	[] T.timeout {
		setverdict(fail, "OsmoHLR did not answer to mDNS query");
		}
	}

	f_vty_subscr_delete(VTY, g_pars.sub);
}
testcase TC_MSLookup_mDNS_service_GSUP_HLR_home() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var HLR_ConnHdlr vc_conn;
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi));

	f_init(false);
	f_vty_config(VTY, "mslookup", "mdns bind");

	vc_conn := f_start_handler(refers(f_TC_MSLookup_mDNS_service_GSUP_HLR_home), pars);
	vc_conn.done;
}

/* Ask proxy HLR about gsup.hlr service, and expect no answer. */
private function f_TC_MSLookup_mDNS_service_GSUP_HLR_proxy() runs on HLR_ConnHdlr {
	timer T;
	var integer id := f_rnd_int(65535); /* mDNS packet ID */
	var charstring domain := "gsup.hlr." & hex2str(g_pars.sub.imsi) & ".imsi.mdns.osmocom.org";

	f_perform_UL_proxy(g_pars.sub.imsi, g_pars.sub.msisdn, OSMO_GSUP_CN_DOMAIN_CS);

	/* Send mDNS query, expect timeout */
	mDNS.send(ts_MSLookup_mDNS_query(id, domain));
	T.start(1.0);
	alt {
	[] mDNS.receive(tr_MSLookup_mDNS_result_IPv4(id, domain, f_enc_IPv4(mp_hlr_ip), mp_hlr_gsup_port)) {
		setverdict(fail, "mslookup result received from proxy HLR for gsup.hlr service");
		}
	[] mDNS.receive(MSLookup_mDNS:?) {
		repeat;
		}
	[] T.timeout {
		setverdict(pass);
		}
	}
}
testcase TC_MSLookup_mDNS_service_GSUP_HLR_proxy() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var hexstring msisdn := '49161'H & f_rnd_hexstring(7, 9);
	var HLR_ConnHdlr vc_conn;
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi, msisdn));

	f_init(false, true);
	f_vty_config(VTY, "mslookup", "mdns bind");
	vc_conn := f_start_handler(refers(f_TC_MSLookup_mDNS_service_GSUP_HLR_proxy), pars, true);

	timer T := 3.0;
	T.start;
	alt {
	[] vc_conn.done {}
	[] T.timeout { setverdict(fail, "Timeout"); mtc.stop; };
	}
}

/* Ask proxy HLR about configured sip.voice service, and expect result. */
private function f_TC_MSLookup_mDNS_service_other_proxy() runs on HLR_ConnHdlr {
	timer T;
	var integer id := f_rnd_int(65535); /* mDNS packet ID */
	var charstring domain := "sip.voice." & hex2str(g_pars.sub.msisdn) & ".msisdn.mdns.osmocom.org";

	f_perform_UL_proxy(g_pars.sub.imsi, g_pars.sub.msisdn, OSMO_GSUP_CN_DOMAIN_CS);

	/* Send mDNS query, expect result */
	mDNS.send(ts_MSLookup_mDNS_query(id, domain));
	T.start(1.0);
	alt {
	[] mDNS.receive(tr_MSLookup_mDNS_result_IPv4(id, domain, f_enc_IPv4("66.66.66.66"), 5060)) {
		setverdict(pass);
		}
	[] mDNS.receive(MSLookup_mDNS:?) {
		repeat;
		}
	[] T.timeout {
		setverdict(fail, "Timeout");
		}
	}
}
testcase TC_MSLookup_mDNS_service_other_proxy() runs on test_CT {
	var hexstring imsi := f_rnd_imsi('26242'H);
	var hexstring msisdn := '49161'H & f_rnd_hexstring(7, 9);
	var HLR_ConnHdlr vc_conn;
	var HLR_ConnHdlrPars pars := valueof(t_Pars(imsi, msisdn));

	f_init(false, true);
	f_vty_config(VTY, "mslookup", "mdns bind");
	f_vty_config2(VTY, {"mslookup", "server"}, "service sip.voice at 66.66.66.66 5060");
	vc_conn := f_start_handler(refers(f_TC_MSLookup_mDNS_service_other_proxy), pars, true);

	timer T := 3.0;
	T.start;
	alt {
	[] vc_conn.done {}
	[] T.timeout { setverdict(fail, "Timeout"); mtc.stop; };
	}
}

/* TODO:
  * UL with ISD error
  * UL with ISD timeout
  * LOCATION CANCEL
  * AUTH FAIL REP
  * DELETE DATA after hlr_subscr_nam() change
  * correctness
  ** wrong message type
  ** wrong length of PDU
  ** too short message
  ** missing IMSI IE

 */

control {
	execute( TC_gsup_sai_err_invalid_imsi() );
	execute( TC_gsup_sai() );
	execute( TC_gsup_sai_num_auth_vectors() );
	execute( TC_gsup_sai_eps() );
	execute( TC_gsup_ul_unknown_imsi() );
	execute( TC_gsup_ul_unknown_imsi_via_proxy() );
	execute( TC_gsup_sai_err_unknown_imsi() );
	execute( TC_gsup_ul() );
	execute( TC_gsup_ul_via_proxy() );
	execute( TC_vty() );
	execute( TC_vty_msisdn_isd() );
	execute( TC_gsup_purge_cs() );
	execute( TC_gsup_purge_ps() );
	execute( TC_gsup_purge_unknown() );

	execute( TC_mo_ussd_unknown() );
	execute( TC_mo_ussd_euse_disc() );
	execute( TC_mo_ussd_iuse_imsi() );
	execute( TC_mo_ussd_iuse_imsi_via_proxy() );
	execute( TC_mo_ussd_iuse_msisdn() );
	execute( TC_mo_ussd_iuse_msisdn_via_proxy() );
	execute( TC_mo_ussd_euse() );
	execute( TC_mo_ussd_euse_continue() );
	execute( TC_mo_ussd_euse_defaultroute() );

	/* "Structured" Supplementary Services */
	execute( TC_mo_sss_reject() );

	execute( TC_gsup_check_imei() );
	execute( TC_gsup_check_imei_via_proxy() );
	execute( TC_gsup_check_imei_invalid_len() );
	execute( TC_gsup_check_imei_unknown_imsi() );
	execute( TC_subscr_create_on_demand_check_imei_early() );
	execute( TC_subscr_create_on_demand_ul() );
	execute( TC_subscr_create_on_demand_sai() );

	if (mp_hlr_supports_dgsm) {
		execute( TC_MSLookup_mDNS_service_other_home() );
		execute( TC_MSLookup_GSUP_proxy() );
		execute( TC_MSLookup_mDNS_service_GSUP_HLR_home() );
		execute( TC_MSLookup_mDNS_service_GSUP_HLR_proxy() );
		execute( TC_MSLookup_mDNS_service_other_proxy() );
	}
};

};