aboutsummaryrefslogtreecommitdiffstats
path: root/tests/msc_vlr/msc_vlr_test_call.c
blob: 6df5d9c6bd5096e8acf61a1fb3dc5d7f614a2bae (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
/* Osmocom MSC+VLR end-to-end tests */

/* (C) 2017 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 *
 * All Rights Reserved
 *
 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "msc_vlr_tests.h"

#include <osmocom/msc/gsm_04_08.h>
#include <osmocom/msc/codec_sdp_cc_t9n.h>

#define mncc_sends_to_cc(MSG_TYPE, MNCC) do { \
		(MNCC)->msg_type = MSG_TYPE; \
		log("MSC <-- MNCC: callref 0x%x: %s\n%s", (MNCC)->callref, \
		    get_mncc_name((MNCC)->msg_type), \
		    (MNCC)->sdp); \
		mncc_tx_to_cc(net, MNCC); \
	} while(0)

/*
static void on_call_release_mncc_sends_to_cc(uint32_t msg_type, struct gsm_mncc *mncc)
{
	mncc->msg_type = msg_type;
	on_call_release_mncc_sends_to_cc_data = mncc;
}
*/

#define IMSI "901700000010650"

static void lu_utran_tmsi()
{
	struct vlr_subscr *vsub;

	net->authentication_required = true;
	net->vlr->cfg.assign_tmsi = true;
	rx_from_ran = OSMO_RAT_UTRAN_IU;

	btw("Location Update request causes a GSUP Send Auth Info request to HLR");
	lu_result_sent = RES_NONE;
	gsup_expect_tx("080108" "09710000000156f0" CN_DOMAIN VLR_TO_HLR);
	ms_sends_msg("0508" /* MM LU */
		     "7" /* ciph key seq: no key available */
		     "0" /* LU type: normal */
		     "09f107" "0017" /* LAI, LAC */
		     "57" /* classmark 1: R99, early classmark, no power lvl */
		     "089910070000106005" /* IMSI */
		     "3303575886" /* classmark 2 */
		     );
	OSMO_ASSERT(gsup_tx_confirmed);
	VERBOSE_ASSERT(lu_result_sent, == RES_NONE, "%d");

	btw("from HLR, rx _SEND_AUTH_INFO_RESULT; VLR sends Auth Req to MS");
	/* based on auc_3g:
	 * K = 'EB215756028D60E3275E613320AEC880',
	 * OPC = 'FB2A3D1B360F599ABAB99DB8669F8308'
	 * SQN = 0
	 */
	auth_request_sent = false;
	auth_request_expect_rand = "39fa2f4e3d523d8619a73b4f65c3e14d";
	auth_request_expect_autn = "8704f5ba55f30000d2ee44b22c8ea919";
	gsup_rx("0a"
		/* imsi */
		"0108" "09710000000156f0"
		/* 5 auth vectors... */
		/* TL    TL     rand */
		"0362"  "2010" "39fa2f4e3d523d8619a73b4f65c3e14d"
		/*       TL     sres       TL     kc */
			"2104" "9b36efdf" "2208" "059a4f668f6fbe39"
		/*       TL     3G IK */
			"2310" "27497388b6cb044648f396aa155b95ef"
		/*       TL     3G CK */
			"2410" "f64735036e5871319c679f4742a75ea1"
		/*       TL     AUTN */
			"2510" "8704f5ba55f30000d2ee44b22c8ea919"
		/*       TL     RES */
			"2708" "e229c19e791f2e41"
		/* TL    TL     rand */
		"0362"  "2010" "c187a53a5e6b9d573cac7c74451fd46d"
			"2104" "85aa3130" "2208" "d3d50a000bf04f6e"
			"2310" "1159ec926a50e98c034a6b7d7c9f418d"
			"2410" "df3a03d9ca5335641efc8e36d76cd20b"
			"2510" "1843a645b98d00005b2d666af46c45d9"
			"2708" "7db47cf7f81e4dc7"
		"0362"  "2010" "efa9c29a9742148d5c9070348716e1bb"
			"2104" "69d5f9fb" "2208" "3df176f0c29f1a3d"
			"2310" "eb50e770ddcc3060101d2f43b6c2b884"
			"2410" "76542abce5ff9345b0e8947f4c6e019c"
			"2510" "f9375e6d41e1000096e7fe4ff1c27e39"
			"2708" "706f996719ba609c"
		"0362"  "2010" "f023d5a3b24726e0631b64b3840f8253"
			"2104" "d570c03f" "2208" "ec011be8919883d6"
			"2310" "c4e58af4ba43f3bcd904e16984f086d7"
			"2410" "0593f65e752e5cb7f473862bda05aa0a"
			"2510" "541ff1f077270000c5ea00d658bc7e9a"
			"2708" "3fd26072eaa2a04d"
		"0362"  "2010" "2f8f90c780d6a9c0c53da7ac57b6707e"
			"2104" "b072446f220823f39f9f425ad6e6"
			"2310" "65af0527fda95b0dc5ae4aa515cdf32f"
			"2410" "537c3b35a3b13b08d08eeb28098f45cc"
			"2510" "4bf4e564f75300009bc796706bc65744"
			"2708" "0edb0eadbea94ac2"
		HLR_TO_VLR,
		NULL);
	VERBOSE_ASSERT(auth_request_sent, == true, "%d");
	VERBOSE_ASSERT(lu_result_sent, == RES_NONE, "%d");

	btw("MS sends Authen Response, VLR accepts and sends SecurityModeControl");
	expect_security_mode_ctrl(NULL, "27497388b6cb044648f396aa155b95ef");
	ms_sends_msg("0554" "e229c19e" "2104" "791f2e41");
	VERBOSE_ASSERT(security_mode_ctrl_sent, == true, "%d");
	VERBOSE_ASSERT(lu_result_sent, == RES_NONE, "%d");

	btw("MS sends SecurityModeControl acceptance, VLR accepts and sends GSUP LU Req to HLR");
	gsup_expect_tx("04010809710000000156f0" CN_DOMAIN VLR_TO_HLR);
	ms_sends_security_mode_complete();
	VERBOSE_ASSERT(gsup_tx_confirmed, == true, "%d");
	VERBOSE_ASSERT(lu_result_sent, == RES_NONE, "%d");

	btw("HLR sends _INSERT_DATA_REQUEST, VLR responds with _INSERT_DATA_RESULT");
	gsup_rx("10010809710000000156f00804032443f2" HLR_TO_VLR,
		"12010809710000000156f0" VLR_TO_HLR);
	VERBOSE_ASSERT(lu_result_sent, == RES_NONE, "%d");

	btw("HLR also sends GSUP _UPDATE_LOCATION_RESULT");
	gsup_rx("06010809710000000156f0" HLR_TO_VLR, NULL);

	VERBOSE_ASSERT(lu_result_sent, == RES_ACCEPT, "%d");

	btw("a LU Accept with a new TMSI was sent, waiting for TMSI Realloc Compl");
	EXPECT_CONN_COUNT(1);
	EXPECT_ACCEPTED(false);

	btw("MS sends TMSI Realloc Complete");
	iu_release_expected = true;
	iu_release_sent = false;
	ms_sends_msg("055b");
	VERBOSE_ASSERT(iu_release_sent, == true, "%d"); \

	btw("LU was successful, and the conn has already been closed");
	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);

	vsub = vlr_subscr_find_by_imsi(net->vlr, IMSI, __func__);
	VERBOSE_ASSERT(vsub != NULL, == true, "%d");
	VERBOSE_ASSERT(strcmp(vsub->imsi, IMSI), == 0, "%d");
	VAL_ASSERT("LAC", vsub->cgi.lai.lac, == 23, "%u");
	vlr_subscr_put(vsub, __func__);
}

static void lu_geran_noauth()
{
	rx_from_ran = OSMO_RAT_GERAN_A;
	net->authentication_required = false;
	net->vlr->cfg.assign_tmsi = false;

	btw("Location Update request causes a GSUP LU request to HLR");
	lu_result_sent = RES_NONE;
	gsup_expect_tx("04010809710000000156f0" CN_DOMAIN VLR_TO_HLR);
	ms_sends_msg("0508" /* MM LU */
		     "7" /* ciph key seq: no key available */
		     "0" /* LU type: normal */
		     "09f107" "0017" /* LAI, LAC */
		     "57" /* classmark 1: R99, early classmark, no power lvl */
		     "089910070000106005" /* IMSI */
		     "3303575886" /* classmark 2 */
		     );
	OSMO_ASSERT(gsup_tx_confirmed);
	VERBOSE_ASSERT(lu_result_sent, == RES_NONE, "%d");

	btw("HLR sends _INSERT_DATA_REQUEST, VLR responds with _INSERT_DATA_RESULT");
	gsup_rx("10010809710000000156f00804036470f1" HLR_TO_VLR,
		"12010809710000000156f0" VLR_TO_HLR);
	VERBOSE_ASSERT(lu_result_sent, == RES_NONE, "%d");

	btw("HLR also sends GSUP _UPDATE_LOCATION_RESULT");
	expect_bssap_clear();
	gsup_rx("06010809710000000156f0" HLR_TO_VLR, NULL);

	btw("LU was successful, and the conn has already been closed");
	VERBOSE_ASSERT(lu_result_sent, == RES_ACCEPT, "%d");
	VERBOSE_ASSERT(bssap_clear_sent, == true, "%d");

	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);
}


static void test_call_mo()
{
	struct gsm_mncc mncc = {
		.imsi = IMSI,
	};

	comment_start();

	fake_time_start();

	lu_utran_tmsi();

	BTW("after a while, a new conn sends a CM Service Request. VLR responds with Auth Req, 2nd auth vector");
	auth_request_sent = false;
	auth_request_expect_rand = "c187a53a5e6b9d573cac7c74451fd46d";
	auth_request_expect_autn = "1843a645b98d00005b2d666af46c45d9";
	cm_service_result_sent = RES_NONE;
	ms_sends_msg("052471"
		     "03575886" /* classmark 2 */
		     "089910070000106005" /* IMSI */);
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");
	VERBOSE_ASSERT(auth_request_sent, == true, "%d");

	btw("needs auth, not yet accepted");
	EXPECT_ACCEPTED(false);

	/* On UTRAN */
	btw("MS sends Authen Response, VLR accepts and sends SecurityModeControl");
	expect_security_mode_ctrl(NULL, "1159ec926a50e98c034a6b7d7c9f418d");
	ms_sends_msg("0554" "7db47cf7" "2104" "f81e4dc7"); /* 2nd vector's res, s.a. */
	VERBOSE_ASSERT(security_mode_ctrl_sent, == true, "%d");
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");

	btw("MS sends SecurityModeControl acceptance, VLR accepts; above Ciphering is an implicit CM Service Accept");
	ms_sends_security_mode_complete();
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");

	BTW("a call is initiated");

	btw("CC SETUP causes CRCX towards CN");
	expect_crcx(RTP_TO_CN);
	expect_crcx(RTP_TO_RAN);
	ms_sends_msg("0385" /* CC, seq = 2 -> 0x80 | CC Setup = 0x5 */
		     "0406600402000581" /* Bearer Capability */
		     "5e038121f3" /* Called Number BCD */
		     "15020100" /* CC Capabilities */
		     "4008" /* Supported Codec List */
		       "04026000" /* UMTS: AMR 2 | AMR */
		       "00021f00" /* GSM: HR AMR | FR AMR | GSM EFR | GSM HR | GSM FR */
		    );
	OSMO_ASSERT(crcx_scheduled(RTP_TO_CN));
	OSMO_ASSERT(crcx_scheduled(RTP_TO_RAN));

	btw("As soon as the MGW port towards CN is created, MNCC_SETUP_IND is triggered");
	cc_to_mncc_expect_tx(IMSI, MNCC_SETUP_IND);
	crcx_ok(RTP_TO_CN);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	mncc.callref = cc_to_mncc_tx_got_callref;

	btw("MNCC replies with MNCC_RTP_CREATE, causing MGW endpoint CRCX to RAN");
	mncc_sends_to_cc(MNCC_RTP_CREATE, &mncc);

	btw("MGW acknowledges the CRCX, triggering Assignment");
	expect_iu_rab_assignment();
	crcx_ok(RTP_TO_RAN);
	OSMO_ASSERT(iu_rab_assignment_sent);

	btw("Assignment succeeds, triggering MNCC_RTP_CREATE ack to MNCC");
	cc_to_mncc_expect_tx("", MNCC_RTP_CREATE);
	ms_sends_assignment_complete("AMR");
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	btw("MNCC says that's fine");
	dtap_expect_tx("8302" /* CC: Call Proceeding */);
	mncc_sends_to_cc(MNCC_CALL_PROC_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	fake_time_passes(1, 23);

	btw("The other call leg got established (not shown here), MNCC tells us so");
	dtap_expect_tx("8301" /* CC: Call Alerting */);
	mncc_sends_to_cc(MNCC_ALERT_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	dtap_expect_tx("8307" /* CC: Connect */);
	mncc_sends_to_cc(MNCC_SETUP_RSP, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	fake_time_passes(1, 23);

	cc_to_mncc_expect_tx("", MNCC_SETUP_COMPL_IND);
	ms_sends_msg("03cf" /* CC: Connect Acknowledge */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	BTW("RTP stream goes ahead, not shown here.");
	fake_time_passes(123, 45);

	BTW("Call ends");
	cc_to_mncc_expect_tx("", MNCC_DISC_IND);
	ms_sends_msg("032502e090" /* CC: Disconnect, cause: Normal Call Clearing */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	dtap_expect_tx("832d" /* CC: Release */);
	mncc_sends_to_cc(MNCC_REL_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	cc_to_mncc_expect_tx("", MNCC_REL_CNF);
	expect_iu_release();
	ms_sends_msg("036a" /* CC: Release Complete */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	OSMO_ASSERT(iu_release_sent);

	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);
	clear_vlr();
	comment_end();
}

static void test_call_mt()
{
	struct gsm_mncc mncc = {
		.imsi = IMSI,
		.callref = 0x423,
		.fields = MNCC_F_BEARER_CAP,
		.bearer_cap = {
			.speech_ver = {
				GSM48_BCAP_SV_AMR_F,
				GSM48_BCAP_SV_EFR,
				GSM48_BCAP_SV_FR,
				GSM48_BCAP_SV_AMR_H,
				GSM48_BCAP_SV_HR,
				-1 },
		},
		.sdp =  "v=0\r\n"
			"o=OsmoMSC 0 0 IN IP4 10.23.23.1\r\n"
			"s=GSM Call\r\n"
			"c=IN IP4 10.23.23.1\r\n"
			"t=0 0\r\n"
			"m=audio 23 RTP/AVP 112\r\n"
			"a=rtpmap:112 AMR/8000\r\n"
			"a=fmtp:112 octet-align=1\r\n"
			"a=ptime:20\r\n",
	};

	comment_start();

	fake_time_start();

	lu_utran_tmsi();

	BTW("after a while, MNCC asks us to setup a call, causing Paging");

	paging_expect_imsi(IMSI);
	paging_sent = false;
	mncc_sends_to_cc(MNCC_SETUP_REQ, &mncc);
	mncc.sdp[0] = '\0';

	VERBOSE_ASSERT(paging_sent, == true, "%d");

	btw("MS replies with Paging Response, and VLR sends Auth Request");
	auth_request_sent = false;
	auth_request_expect_rand = "c187a53a5e6b9d573cac7c74451fd46d";
	auth_request_expect_autn = "1843a645b98d00005b2d666af46c45d9";
	ms_sends_msg("062707"
		     "03575886" /* classmark 2 */
		     "089910070000106005" /* IMSI */);
	VERBOSE_ASSERT(auth_request_sent, == true, "%d");

	btw("MS sends Authen Response, VLR accepts and sends SecurityModeControl");
	expect_security_mode_ctrl(NULL, "1159ec926a50e98c034a6b7d7c9f418d");
	ms_sends_msg("0554" "7db47cf7" "2104" "f81e4dc7"); /* 2nd vector's res, s.a. */
	VERBOSE_ASSERT(security_mode_ctrl_sent, == true, "%d");

	btw("MS sends SecurityModeControl acceptance, VLR accepts, sends CC Setup");
	dtap_expect_tx("0305" /* CC: Setup */ "04 04 60 04 05 8b" /* Bearer Cap */);
	ms_sends_security_mode_complete();

	btw("MS confirms call, we create a RAN-side RTP and forward MNCC_CALL_CONF_IND");
	expect_crcx(RTP_TO_CN);
	expect_crcx(RTP_TO_RAN);
	cc_to_mncc_expect_tx(IMSI, MNCC_CALL_CONF_IND);
	ms_sends_msg("8348" /* CC: Call Confirmed */
		     "0406600402000581" /* Bearer Capability */
		     "15020100" /* Call Control Capabilities */
		     "40080402600400021f00" /* Supported Codec List */);
	OSMO_ASSERT(crcx_scheduled(RTP_TO_CN));
	OSMO_ASSERT(crcx_scheduled(RTP_TO_RAN));
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	btw("MGW acknowledges the CRCX to RAN, triggering Assignment");
	expect_iu_rab_assignment();
	crcx_ok(RTP_TO_RAN);
	OSMO_ASSERT(iu_rab_assignment_sent);

	btw("Assignment completes, triggering CRCX to CN");
	expect_crcx(RTP_TO_CN);
	ms_sends_assignment_complete("AMR");

	btw("MNCC sends MNCC_RTP_CREATE, which first waits for the CN side RTP");
	mncc_sends_to_cc(MNCC_RTP_CREATE, &mncc);

	btw("When the CN side RTP address is known, ack MNCC_RTP_CREATE with full SDP");
	cc_to_mncc_expect_tx("", MNCC_RTP_CREATE);
	crcx_ok(RTP_TO_CN);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	fake_time_passes(1, 23);

	cc_to_mncc_expect_tx("", MNCC_ALERT_IND);
	ms_sends_msg("8381" /* CC: Alerting */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	fake_time_passes(1, 23);

	cc_to_mncc_expect_tx(IMSI, MNCC_SETUP_CNF);
	ms_sends_msg("83c7" /* CC: Connect */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	dtap_expect_tx("030f" /* CC: Connect Acknowledge */);
	mncc_sends_to_cc(MNCC_SETUP_COMPL_REQ, &mncc);

	BTW("RTP stream goes ahead, not shown here.");
	fake_time_passes(123, 45);

	BTW("Call ends");
	cc_to_mncc_expect_tx("", MNCC_DISC_IND);
	ms_sends_msg("832502e090" /* CC: Disconnect, cause: Normal Call Clearing */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	dtap_expect_tx("032d" /* CC: Release */);
	mncc_sends_to_cc(MNCC_REL_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	cc_to_mncc_expect_tx("", MNCC_REL_CNF);
	expect_iu_release();
	ms_sends_msg("836a" /* CC: Release Complete */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	OSMO_ASSERT(iu_release_sent);

	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);
	clear_vlr();
	comment_end();
}

static void test_call_mt2()
{
	struct gsm_mncc mncc = {
		.imsi = IMSI,
		.callref = 0x423,
		.fields = MNCC_F_BEARER_CAP,
		.bearer_cap = {
			.speech_ver = { GSM48_BCAP_SV_FR, -1, },
		},
		.sdp =  "v=0\r\n"
			"o=OsmoMSC 0 0 IN IP4 10.23.23.1\r\n"
			"s=GSM Call\r\n"
			"c=IN IP4 10.23.23.1\r\n"
			"t=0 0\r\n"
			"m=audio 23 RTP/AVP 112\r\n"
			"a=rtpmap:112 AMR/8000\r\n"
			"a=fmtp:112 octet-align=1\r\n"
			"a=ptime:20\r\n",
	};

	comment_start();

	fake_time_start();

	lu_utran_tmsi();

	BTW("after a while, MNCC asks us to setup a call, causing Paging");

	paging_expect_imsi(IMSI);
	paging_sent = false;
	mncc_sends_to_cc(MNCC_SETUP_REQ, &mncc);

	VERBOSE_ASSERT(paging_sent, == true, "%d");

	btw("MS replies with Paging Response, and VLR sends Auth Request");
	auth_request_sent = false;
	auth_request_expect_rand = "c187a53a5e6b9d573cac7c74451fd46d";
	auth_request_expect_autn = "1843a645b98d00005b2d666af46c45d9";
	ms_sends_msg("062707"
		     "03575886" /* classmark 2 */
		     "089910070000106005" /* IMSI */);
	VERBOSE_ASSERT(auth_request_sent, == true, "%d");

	btw("MS sends Authen Response, VLR accepts and sends SecurityModeControl");
	expect_security_mode_ctrl(NULL, "1159ec926a50e98c034a6b7d7c9f418d");
	ms_sends_msg("0554" "7db47cf7" "2104" "f81e4dc7"); /* 2nd vector's res, s.a. */
	VERBOSE_ASSERT(security_mode_ctrl_sent, == true, "%d");

	btw("MS sends SecurityModeControl acceptance, VLR accepts, sends CC Setup");
	dtap_expect_tx("0305" /* CC: Setup */ "04 04 60 04 05 8b" /* Bearer Cap */);
	ms_sends_security_mode_complete();

	btw("MS confirms call, we create a RAN-side RTP and forward MNCC_CALL_CONF_IND");
	expect_crcx(RTP_TO_CN);
	expect_crcx(RTP_TO_RAN);
	cc_to_mncc_expect_tx(IMSI, MNCC_CALL_CONF_IND);
	ms_sends_msg("8348" /* CC: Call Confirmed */
		     "0406600402000581" /* Bearer Capability */
		     "15020100" /* Call Control Capabilities */
		     "40080402600400021f00" /* Supported Codec List */);
	OSMO_ASSERT(crcx_scheduled(RTP_TO_CN));
	OSMO_ASSERT(crcx_scheduled(RTP_TO_RAN));
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	btw("MNCC sends MNCC_RTP_CREATE, which first waits for the CN side RTP");
	mncc_sends_to_cc(MNCC_RTP_CREATE, &mncc);

	btw("MGW acknowledges the CRCX to RAN, triggering Assignment");
	expect_iu_rab_assignment();
	crcx_ok(RTP_TO_RAN);
	OSMO_ASSERT(iu_rab_assignment_sent);

	btw("Assignment completes, triggering CRCX to CN");
	ms_sends_assignment_complete("AMR");

	btw("When the CN side RTP address is known, ack MNCC_RTP_CREATE with full SDP");
	cc_to_mncc_expect_tx("", MNCC_RTP_CREATE);
	crcx_ok(RTP_TO_CN);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	fake_time_passes(1, 23);

	cc_to_mncc_expect_tx("", MNCC_ALERT_IND);
	ms_sends_msg("8381" /* CC: Alerting */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	fake_time_passes(15, 23);

	btw("The call failed, the BSC sends a BSSMAP Clear Request");
	/* FIXME: in this scenario, we send an MNCC_REL_CNF even though MNCC never asked us to MNCC_REL_REQ.  Legacy
	 * behavior did get to both MNCC_REL_IND, then an MNCC_REL_REQ from MNCC as well as a final MNCC_REL_CNF, but
	 * this only worked synchronously, i.e. only with internal MNCC. Instead of mimicking that, we need a proper
	 * async solution that also works with a PBX. */
	cc_to_mncc_expect_tx("", MNCC_REL_CNF);
	dtap_expect_tx("032d0802e1af"); /* CC: Release */
	expect_iu_release();
	msc_a_release_cn(msub_msc_a(g_msub));
	OSMO_ASSERT(dtap_tx_confirmed);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	OSMO_ASSERT(iu_release_sent);

	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);

	/* Make sure a pending release timer doesn't fire later to access freed data */
	fake_time_passes(15, 23);

	clear_vlr();
	comment_end();
}

static void test_call_mo_to_unknown()
{
	struct gsm_mncc mncc = {
		.imsi = IMSI,
	};

	comment_start();

	fake_time_start();

	lu_utran_tmsi();

	BTW("after a while, a new conn sends a CM Service Request. VLR responds with Auth Req, 2nd auth vector");
	auth_request_sent = false;
	auth_request_expect_rand = "c187a53a5e6b9d573cac7c74451fd46d";
	auth_request_expect_autn = "1843a645b98d00005b2d666af46c45d9";
	cm_service_result_sent = RES_NONE;
	ms_sends_msg("052471"
		     "03575886" /* classmark 2 */
		     "089910070000106005" /* IMSI */);
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");
	VERBOSE_ASSERT(auth_request_sent, == true, "%d");

	btw("needs auth, not yet accepted");
	EXPECT_ACCEPTED(false);

	/* On UTRAN */
	btw("MS sends Authen Response, VLR accepts and sends SecurityModeControl");
	expect_security_mode_ctrl(NULL, "1159ec926a50e98c034a6b7d7c9f418d");
	ms_sends_msg("0554" "7db47cf7" "2104" "f81e4dc7"); /* 2nd vector's res, s.a. */
	VERBOSE_ASSERT(security_mode_ctrl_sent, == true, "%d");
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");

	btw("MS sends SecurityModeControl acceptance, VLR accepts; above Ciphering is an implicit CM Service Accept");
	ms_sends_security_mode_complete();
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");

	BTW("a call is initiated");

	btw("CC SETUP causes CRCX towards CN");
	expect_crcx(RTP_TO_CN);
	expect_crcx(RTP_TO_RAN);
	ms_sends_msg("0385" /* CC, seq = 2 -> 0x80 | CC Setup = 0x5 */
		     "0406600402000581" /* Bearer Capability */
		     "5e038121f3" /* Called Number BCD */
		     "15020100" /* CC Capabilities */
		     "4008" /* Supported Codec List */
		       "04026000" /* UMTS: AMR 2 | AMR */
		       "00021f00" /* GSM: HR AMR | FR AMR | GSM EFR | GSM HR | GSM FR */
		    );
	OSMO_ASSERT(crcx_scheduled(RTP_TO_CN));
	OSMO_ASSERT(crcx_scheduled(RTP_TO_RAN));

	btw("As soon as the MGW port towards CN is created, MNCC_SETUP_IND is triggered");
	cc_to_mncc_expect_tx(IMSI, MNCC_SETUP_IND);
	crcx_ok(RTP_TO_CN);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	mncc.callref = cc_to_mncc_tx_got_callref;

	btw("MNCC replies with MNCC_RTP_CREATE, causing MGW endpoint CRCX to RAN");
	mncc_sends_to_cc(MNCC_RTP_CREATE, &mncc);

	btw("MGW acknowledges the CRCX, triggering Assignment");
	expect_iu_rab_assignment();
	crcx_ok(RTP_TO_RAN);
	OSMO_ASSERT(iu_rab_assignment_sent);

	btw("Assignment succeeds, triggering MNCC_RTP_CREATE ack to MNCC");
	cc_to_mncc_expect_tx("", MNCC_RTP_CREATE);
	ms_sends_assignment_complete("AMR");
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	btw("MNCC says that's fine");
	dtap_expect_tx("8302" /* CC: Call Proceeding */);
	mncc_sends_to_cc(MNCC_CALL_PROC_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	btw("But the other side's MSISDN could not be resolved, MNCC tells us to cancel");
	dtap_expect_tx("832d" /* CC: Release Request */);
	mncc_sends_to_cc(MNCC_REL_REQ, &mncc);

	dtap_expect_tx("832d" /* CC: Release Request */);
	fake_time_passes(10, 23);

	expect_iu_release();
	cc_to_mncc_expect_tx("", MNCC_REL_CNF);
	ms_sends_msg("036a" /* CC: Release Complete */);
	OSMO_ASSERT(iu_release_sent);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);
	clear_vlr();
	comment_end();
}

static void test_call_mo_to_unknown_timeout()
{
	struct gsm_mncc mncc = {
		.imsi = IMSI,
	};

	comment_start();

	fake_time_start();

	lu_utran_tmsi();

	BTW("after a while, a new conn sends a CM Service Request. VLR responds with Auth Req, 2nd auth vector");
	auth_request_sent = false;
	auth_request_expect_rand = "c187a53a5e6b9d573cac7c74451fd46d";
	auth_request_expect_autn = "1843a645b98d00005b2d666af46c45d9";
	cm_service_result_sent = RES_NONE;
	ms_sends_msg("052471"
		     "03575886" /* classmark 2 */
		     "089910070000106005" /* IMSI */);
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");
	VERBOSE_ASSERT(auth_request_sent, == true, "%d");

	btw("needs auth, not yet accepted");
	EXPECT_ACCEPTED(false);

	/* On UTRAN */
	btw("MS sends Authen Response, VLR accepts and sends SecurityModeControl");
	expect_security_mode_ctrl(NULL, "1159ec926a50e98c034a6b7d7c9f418d");
	ms_sends_msg("0554" "7db47cf7" "2104" "f81e4dc7"); /* 2nd vector's res, s.a. */
	VERBOSE_ASSERT(security_mode_ctrl_sent, == true, "%d");
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");

	btw("MS sends SecurityModeControl acceptance, VLR accepts; above Ciphering is an implicit CM Service Accept");
	ms_sends_security_mode_complete();
	VERBOSE_ASSERT(cm_service_result_sent, == RES_NONE, "%d");

	BTW("a call is initiated");

	btw("CC SETUP causes CRCX towards CN");
	expect_crcx(RTP_TO_CN);
	expect_crcx(RTP_TO_RAN);
	ms_sends_msg("0385" /* CC, seq = 2 -> 0x80 | CC Setup = 0x5 */
		     "0406600402000581" /* Bearer Capability */
		     "5e038121f3" /* Called Number BCD */
		     "15020100" /* CC Capabilities */
		     "4008" /* Supported Codec List */
		       "04026000" /* UMTS: AMR 2 | AMR */
		       "00021f00" /* GSM: HR AMR | FR AMR | GSM EFR | GSM HR | GSM FR */
		    );
	OSMO_ASSERT(crcx_scheduled(RTP_TO_CN));
	OSMO_ASSERT(crcx_scheduled(RTP_TO_RAN));

	btw("As soon as the MGW port towards CN is created, MNCC_SETUP_IND is triggered");
	cc_to_mncc_expect_tx(IMSI, MNCC_SETUP_IND);
	crcx_ok(RTP_TO_CN);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	mncc.callref = cc_to_mncc_tx_got_callref;

	btw("MNCC replies with MNCC_RTP_CREATE, causing MGW endpoint CRCX to RAN");
	mncc_sends_to_cc(MNCC_RTP_CREATE, &mncc);

	btw("MGW acknowledges the CRCX, triggering Assignment");
	expect_iu_rab_assignment();
	crcx_ok(RTP_TO_RAN);
	OSMO_ASSERT(iu_rab_assignment_sent);

	btw("Assignment succeeds, triggering MNCC_RTP_CREATE ack to MNCC");
	cc_to_mncc_expect_tx("", MNCC_RTP_CREATE);
	ms_sends_assignment_complete("AMR");
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	btw("MNCC says that's fine");
	dtap_expect_tx("8302" /* CC: Call Proceeding */);
	mncc_sends_to_cc(MNCC_CALL_PROC_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	btw("But the other side's MSISDN could not be resolved, MNCC tells us to cancel");
	dtap_expect_tx("832d" /* CC: Release Request */);
	mncc_sends_to_cc(MNCC_REL_REQ, &mncc);

	btw("Despite our repeated CC Release Requests, the MS does not respond anymore");
	dtap_expect_tx("832d" /* CC: Release Request */);
	fake_time_passes(10, 23);

	btw("The CC Release times out and we still properly clear the conn");
	cc_to_mncc_expect_tx("", MNCC_REL_CNF);
	expect_iu_release();
	fake_time_passes(10, 23);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	OSMO_ASSERT(iu_release_sent);

	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);
	clear_vlr();
	comment_end();
}

#define LIST_END 0xffff

struct codec_test {
	const char *desc;

	/* What to send during Complete Layer 3 as Codec List (BSS Supported). List ends with a LIST_END entry */
	enum gsm0808_speech_codec_type mo_rx_compl_l3_codec_list_bss_supported[8];

	/* What to send during CC Setup as MS Bearer Capability. List ends with a LIST_END entry */
	enum gsm48_bcap_speech_ver mo_rx_ms_bcap[8];

	/* What codecs should osmo-msc send in the MNCC_SETUP_IND message.
	 * Just the SDP subtype names like "GSM", "GSM-EFR", "AMR", ..., list ends with NULL entry */
	const char *mo_tx_sdp_mncc_setup_ind[8];

	/* What codecs the remote call leg should send as SDP via MNCC during MNCC_RTP_CREATE (if any). */
	const char *mo_rx_sdp_mncc_rtp_create[8];

	/* What the MSC should send as Channel Type IE in the Assignment Command to the BSS. List ends with a
	 * LIST_END entry */
	enum gsm0808_permitted_speech mo_tx_assignment_perm_speech[8];

	/* What codec to assign in the Assignment Complete's Codec (Chosen) IE. Just a subtype name. */
	const char *mo_rx_assigned_codec;

	/* MO acks the MNCC_RTP_CREATE with these codecs (if any). */
	const char *mo_tx_sdp_mncc_rtp_create[8];

	/* mt_rx_sdp_mncc_setup_req == mo_tx_sdp_mncc_rtp_create */
#define mt_rx_sdp_mncc_setup_req  mo_tx_sdp_mncc_rtp_create

	enum gsm0808_speech_codec_type mt_rx_compl_l3_codec_list_bss_supported[8];
	bool expect_codec_mismatch_on_paging_response;
	enum gsm48_bcap_speech_ver mt_tx_cc_setup_bcap[8];
	enum gsm48_bcap_speech_ver mt_rx_ms_bcap[8];
	bool expect_codec_mismatch_on_cc_call_conf;
	const char *mt_tx_sdp_mncc_call_conf_ind[8];

	enum gsm0808_permitted_speech mt_tx_assignment_perm_speech[8];
	const char *mt_rx_assigned_codec;

	const char *mt_rx_sdp_mncc_rtp_create[8];
	const char *mt_tx_sdp_mncc_rtp_create[8];

	const char *mt_tx_sdp_mncc_alert_ind[8];
	const char *mt_tx_sdp_mncc_setup_cnf[8];
	const char *mt_rx_sdp_mncc_setup_compl_req[8];

	/* mo_rx_sdp_mncc_alert_req == mt_tx_sdp_mncc_alert_ind */
#define mo_rx_sdp_mncc_alert_req  mt_tx_sdp_mncc_alert_ind
#define mo_rx_sdp_mncc_setup_rsp  mt_tx_sdp_mncc_alert_ind

	const char *mo_tx_sdp_mncc_setup_compl_ind[8];
};

#define CODEC_LIST_ALL_GSM { \
			GSM0808_SCT_FR1, \
			GSM0808_SCT_FR2, \
			GSM0808_SCT_FR3, \
			GSM0808_SCT_HR1, \
			GSM0808_SCT_HR3, \
			LIST_END \
		}

#define BCAP_ALL_GSM { \
			GSM48_BCAP_SV_AMR_F, \
			GSM48_BCAP_SV_AMR_H, \
			GSM48_BCAP_SV_AMR_OH, \
			GSM48_BCAP_SV_EFR, \
			GSM48_BCAP_SV_FR, \
			GSM48_BCAP_SV_HR, \
			LIST_END \
		}

#define PERM_SPEECH_ALL_GSM { \
			GSM0808_PERM_FR3, \
			GSM0808_PERM_HR3, \
			GSM0808_PERM_FR2, \
			GSM0808_PERM_FR1, \
			GSM0808_PERM_HR1, \
			LIST_END \
		}

#define SDP_CODECS_ALL_GSM { \
			"AMR", \
			"GSM-EFR", \
			"GSM", \
			"GSM-HR-08", \
		}

static const struct codec_test codec_tests[] = {
	{
		.desc = "AMR picked by both MO and MT",
		.mo_rx_compl_l3_codec_list_bss_supported = CODEC_LIST_ALL_GSM,
		.mo_rx_ms_bcap = BCAP_ALL_GSM,
		.mo_tx_sdp_mncc_setup_ind = SDP_CODECS_ALL_GSM,
		.mo_rx_sdp_mncc_rtp_create = {},
		.mo_tx_assignment_perm_speech = PERM_SPEECH_ALL_GSM,
		.mo_rx_assigned_codec = "AMR",
		.mo_tx_sdp_mncc_rtp_create = { "AMR" },
		/* mt_rx_sdp_mncc_setup_req == mo_tx_sdp_mncc_rtp_create */
		.mt_rx_compl_l3_codec_list_bss_supported = CODEC_LIST_ALL_GSM,
		.mt_tx_cc_setup_bcap = {
			GSM48_BCAP_SV_AMR_F,
			GSM48_BCAP_SV_AMR_H,
			GSM48_BCAP_SV_AMR_OH,
			LIST_END
		},
		.mt_rx_ms_bcap = BCAP_ALL_GSM,
		.mt_tx_sdp_mncc_call_conf_ind = {},
		.mt_rx_sdp_mncc_rtp_create = {},
		.mt_tx_assignment_perm_speech = { GSM0808_PERM_FR3, GSM0808_PERM_HR3, LIST_END },
		.mt_rx_assigned_codec = "AMR",
		.mt_tx_sdp_mncc_rtp_create = { "AMR" },
		.mt_tx_sdp_mncc_alert_ind = { "AMR" },
		.mt_tx_sdp_mncc_setup_cnf = { "AMR" },
		.mo_tx_sdp_mncc_setup_compl_ind = {},
	},

	{
		.desc = "FR1 picked by MO from Codec List (BSS Supported), MT hence also picks FR1",
		.mo_rx_compl_l3_codec_list_bss_supported = { GSM0808_SCT_FR1, LIST_END },
		.mo_rx_ms_bcap = BCAP_ALL_GSM,
		.mo_tx_sdp_mncc_setup_ind = { "GSM" },
		.mo_rx_sdp_mncc_rtp_create = {},
		.mo_tx_assignment_perm_speech = { GSM0808_PERM_FR1, LIST_END },
		.mo_rx_assigned_codec = "GSM",
		.mo_tx_sdp_mncc_rtp_create = { "GSM" },
		/* .mt_rx_sdp_mncc_setup_req == .mo_tx_sdp_mncc_setup_ind */
		.mt_rx_compl_l3_codec_list_bss_supported = CODEC_LIST_ALL_GSM,
		.mt_tx_cc_setup_bcap = { GSM48_BCAP_SV_FR, LIST_END },
		.mt_rx_ms_bcap = BCAP_ALL_GSM,
		.mt_tx_sdp_mncc_call_conf_ind = {},
		.mt_rx_sdp_mncc_rtp_create = {},
		.mt_tx_assignment_perm_speech = { GSM0808_PERM_FR1, LIST_END },
		.mt_rx_assigned_codec = "GSM",
		.mt_tx_sdp_mncc_rtp_create = { "GSM" },
		.mt_tx_sdp_mncc_alert_ind = { "GSM" },
		.mt_tx_sdp_mncc_setup_cnf = { "GSM" },
		.mo_tx_sdp_mncc_setup_compl_ind = {},
	},

	{
		.desc = "FR1 picked by MO from Bearer Cap, MT hence also picks FR1",
		.mo_rx_compl_l3_codec_list_bss_supported = CODEC_LIST_ALL_GSM,
		.mo_rx_ms_bcap = { GSM48_BCAP_SV_FR, LIST_END },
		.mo_tx_sdp_mncc_setup_ind = { "GSM" },
		.mo_rx_sdp_mncc_rtp_create = {},
		.mo_tx_assignment_perm_speech = { GSM0808_PERM_FR1, LIST_END },
		.mo_rx_assigned_codec = "GSM",
		.mo_tx_sdp_mncc_rtp_create = { "GSM" },
		/* .mt_rx_sdp_mncc_setup_req == .mo_tx_sdp_mncc_setup_ind */
		.mt_rx_compl_l3_codec_list_bss_supported = CODEC_LIST_ALL_GSM,
		.mt_tx_cc_setup_bcap = { GSM48_BCAP_SV_FR, LIST_END },
		.mt_rx_ms_bcap = BCAP_ALL_GSM,
		.mt_tx_sdp_mncc_call_conf_ind = {},
		.mt_rx_sdp_mncc_rtp_create = {},
		.mt_tx_assignment_perm_speech = { GSM0808_PERM_FR1, LIST_END },
		.mt_rx_assigned_codec = "GSM",
		.mt_tx_sdp_mncc_rtp_create = { "GSM" },
		.mt_tx_sdp_mncc_alert_ind = { "GSM" },
		.mt_tx_sdp_mncc_setup_cnf = { "GSM" },
		.mo_tx_sdp_mncc_setup_compl_ind = {},
	},

	{
		.desc = "FR1 picked by MT's Codec List (BSS Supported), hence MO also picks FR1 (EXPECTED FAILURE)",
		/* Currently the MO Assignment happens before MT gets a chance to send its available codecs.
		 * So even though the MO side would be able to assign FR1 and match MT, this is established too late
		 * and MO mismatches MT. This can only be fixed by a) moving MO Assignment to after MT Assignment
		 * or b) doing a Channel Mode Change or re-assignment after MT Assignment -- since re-assigning might
		 * need an lchan type change and means more overhead, a) would be the best option. */
		.mo_rx_compl_l3_codec_list_bss_supported = CODEC_LIST_ALL_GSM,
		.mo_rx_ms_bcap = BCAP_ALL_GSM,
		.mo_tx_sdp_mncc_setup_ind = SDP_CODECS_ALL_GSM,
		.mo_rx_sdp_mncc_rtp_create = {},
		.mo_tx_assignment_perm_speech = PERM_SPEECH_ALL_GSM,
		.mo_rx_assigned_codec = "AMR", /* <- Early Assignment means codec mismatch */
		.mo_tx_sdp_mncc_rtp_create = { "AMR" },

		.mt_rx_compl_l3_codec_list_bss_supported = { GSM0808_SCT_FR1, LIST_END },
		.expect_codec_mismatch_on_paging_response = true,
		/* The mismatching codec AMR vs. GSM means the call fails (in the lack of transcoding) */
	},

	{
		.desc = "FR1 picked by MT's MS Bearer Capability, hence MO also picks FR1 (EXPECTED FAILURE)",
		/* Like above, MO Assignment happens too early to be able to match MT's codec availability. */
		.mo_rx_compl_l3_codec_list_bss_supported = CODEC_LIST_ALL_GSM,
		.mo_rx_ms_bcap = BCAP_ALL_GSM,
		.mo_tx_sdp_mncc_setup_ind = SDP_CODECS_ALL_GSM,
		.mo_rx_sdp_mncc_rtp_create = {},
		.mo_tx_assignment_perm_speech = PERM_SPEECH_ALL_GSM,
		.mo_rx_assigned_codec = "AMR", /* <- Early Assignment means codec mismatch */
		.mo_tx_sdp_mncc_rtp_create = { "AMR" },

		.mt_rx_compl_l3_codec_list_bss_supported = CODEC_LIST_ALL_GSM,
		.mt_tx_cc_setup_bcap = {
			GSM48_BCAP_SV_AMR_F,
			GSM48_BCAP_SV_AMR_H,
			GSM48_BCAP_SV_AMR_OH,
			LIST_END
		},
		.mt_rx_ms_bcap = { GSM48_BCAP_SV_FR, LIST_END },
		.mt_tx_sdp_mncc_call_conf_ind = {},
		.mt_rx_sdp_mncc_rtp_create = {},
		.mt_tx_assignment_perm_speech = { GSM0808_PERM_FR3, GSM0808_PERM_HR3, LIST_END },
		.expect_codec_mismatch_on_cc_call_conf = true,
		/* The mismatching codec AMR vs. GSM means the call fails (in the lack of transcoding) */
	},

};

static char namebuf[4][1024];
static int use_namebuf = 0;

static const char *codec_list_name(const enum gsm0808_speech_codec_type compl_l3_codec_list_bss_supported[])
{
	struct osmo_strbuf sb = { .buf = namebuf[use_namebuf], .len = sizeof(namebuf[0]) };
	use_namebuf = (use_namebuf + 1) % ARRAY_SIZE(namebuf);

	const enum gsm0808_speech_codec_type *pos;
	sb.buf[0] = '\0';
	for (pos = compl_l3_codec_list_bss_supported; *pos != LIST_END; pos++)
		OSMO_STRBUF_PRINTF(sb, " %s", gsm0808_speech_codec_type_name(*pos));
	return sb.buf;
}

static const struct gsm0808_speech_codec_list *codec_list(const enum gsm0808_speech_codec_type compl_l3_codec_list_bss_supported[])
{
	static struct gsm0808_speech_codec_list scl;
	scl = (struct gsm0808_speech_codec_list){};
	const enum gsm0808_speech_codec_type *pos;
	for (pos = compl_l3_codec_list_bss_supported; *pos != LIST_END; pos++) {
		scl.codec[scl.len] = (struct gsm0808_speech_codec){
			.fi = true,
			.type = *pos,
		};
		scl.len++;
	}
	return &scl;
}

static const char *bcap_name(const enum gsm48_bcap_speech_ver ms_bcap[])
{
	struct osmo_strbuf sb = { .buf = namebuf[use_namebuf], .len = sizeof(namebuf[0]) };
	use_namebuf = (use_namebuf + 1) % ARRAY_SIZE(namebuf);

	const enum gsm48_bcap_speech_ver *pos;
	sb.buf[0] = '\0';
	for (pos = ms_bcap; *pos != LIST_END; pos++) {
		const struct codec_mapping *m = codec_mapping_by_speech_ver(*pos);
		OSMO_STRBUF_PRINTF(sb, " %s", m? m->sdp.subtype_name : "NULL");
	}
	return sb.buf;
}

static const char *perm_speech_name(const enum gsm0808_permitted_speech perm_speech[])
{
	struct osmo_strbuf sb = { .buf = namebuf[use_namebuf], .len = sizeof(namebuf[0]) };
	use_namebuf = (use_namebuf + 1) % ARRAY_SIZE(namebuf);

	const enum gsm0808_permitted_speech *pos;
	sb.buf[0] = '\0';
	for (pos = perm_speech; *pos != LIST_END; pos++) {
		OSMO_STRBUF_PRINTF(sb, " %s", gsm0808_permitted_speech_name(*pos));
	}
	return sb.buf;
}

static const char *strlist_name(const char *const* strs)
{
	struct osmo_strbuf sb = { .buf = namebuf[use_namebuf], .len = sizeof(namebuf[0]) };
	use_namebuf = (use_namebuf + 1) % ARRAY_SIZE(namebuf);

	const char * const *pos;
	sb.buf[0] = '\0';
	for (pos = strs; *pos != NULL; pos++)
		OSMO_STRBUF_PRINTF(sb, " %s", *pos);
	return sb.buf;
}

static bool validate_sdp(const char *func, const char *desc,
			 const char *sdp_str, const char * const expected_codecs[])
{
	const char * const *expect_pos;
	struct sdp_audio_codec *codec;
	struct sdp_msg sdp;
	if (sdp_msg_from_str(&sdp, sdp_str)) {
		BTW("%s: %s: ERROR: failed to parse SDP\n%s", func, desc, sdp_str);
		return false;
	}

	expect_pos = expected_codecs;
	foreach_sdp_audio_codec(codec, &sdp.audio_codecs) {
		if (!*expect_pos) {
			BTW("%s: %s: ERROR: did not expect %s", func, desc, codec->subtype_name);
			return false;
		}
		if (strcmp(*expect_pos, codec->subtype_name)) {
			BTW("%s: %s: ERROR: mismatch: in idx %d, expect %s, got %s", func, desc,
			    (int)(expect_pos - expected_codecs), *expect_pos, codec->subtype_name);
			return false;
		}
		expect_pos++;
	}
	if (*expect_pos) {
		BTW("%s: %s: ERROR: mismatch: expected %s to be listed, but not found", func, desc, *expect_pos);
		return false;
	}
	return true;
}

#define VALIDATE_SDP(GOT_SDP_STR, EXPECT_SDP_STR) do { \
		if (validate_sdp(__func__, t->desc, GOT_SDP_STR, EXPECT_SDP_STR)) { \
			btw("VALIDATE_SDP OK: " #GOT_SDP_STR " == " #EXPECT_SDP_STR " ==%s", strlist_name(EXPECT_SDP_STR)); \
		} else { \
			btw("Failed to validate SDP:\nexpected%s\ngot\n%s", \
			    strlist_name(EXPECT_SDP_STR), GOT_SDP_STR); \
			OSMO_ASSERT(false); \
		} \
	} while (0)

static bool validate_perm_speech(const char *func, const char *desc,
				 const struct gsm0808_channel_type *ct,
				 const enum gsm0808_permitted_speech perm_speech[])
{
	const enum gsm0808_permitted_speech *pos;
	const uint8_t *pos2 = ct->perm_spch;
	for (pos = perm_speech; *pos != LIST_END; pos++, pos2++) {
		if (pos2 - ct->perm_spch >= ct->perm_spch_len) {
			BTW("%s: %s: ERROR: mismatch: expected %s to be listed, but not found", func, desc,
			    gsm0808_permitted_speech_name(*pos));
			return false;
		}
		if (*pos2 != *pos) {
			BTW("%s: %s: ERROR: mismatch: in idx %d, expect %s", func, desc,
			    (int)(pos - perm_speech), gsm0808_permitted_speech_name(*pos));
			btw("in idx %d, got %s", (int)(pos - perm_speech), gsm0808_permitted_speech_name(*pos2));
			return false;
		}
	}
	if (pos2 - ct->perm_spch < ct->perm_spch_len) {
		BTW("%s: %s: ERROR: did not expect %s", func, desc, gsm0808_permitted_speech_name(*pos2));
		return false;
	}
	return true;
}

#define VALIDATE_PERM_SPEECH(GOT_PERM_SPEECH, EXPECT_PERM_SPEECH) do { \
		if (validate_perm_speech(__func__, t->desc, GOT_PERM_SPEECH, EXPECT_PERM_SPEECH)) { \
			btw("VALIDATE_PERM_SPEECH OK: " #GOT_PERM_SPEECH " == " #EXPECT_PERM_SPEECH " ==%s", \
			    perm_speech_name(EXPECT_PERM_SPEECH)); \
		} else { \
			btw("Failed to validate Permitted Speech:\nexpected%s", \
			    perm_speech_name(EXPECT_PERM_SPEECH)); \
			btw("got:"); \
			int i; \
			for (i = 0; i < (GOT_PERM_SPEECH)->perm_spch_len; i++) { \
				btw("%s", gsm0808_permitted_speech_name((GOT_PERM_SPEECH)->perm_spch[i])); \
			}\
			OSMO_ASSERT(false); \
		} \
	} while (0)

static struct sdp_msg *sdp_from_subtype_names(const char *const *subtype_names)
{
	static struct sdp_msg sdp;
	sdp = (struct sdp_msg){};
	const char *const *subtype_name;
	osmo_sockaddr_str_from_str(&sdp.rtp, "1.2.3.4", 56);
	for (subtype_name = subtype_names; *subtype_name; subtype_name++) {
		const struct codec_mapping *m = codec_mapping_by_subtype_name(*subtype_name);
		if (!m) {
			BTW("ERROR: unknown subtype_name: %s", *subtype_name);
			abort();
		}
		sdp_audio_codec_add_copy(&sdp.audio_codecs, &m->sdp);
	}
	return &sdp;
}

static int sdp_str_from_subtype_names(char *buf, size_t buflen, const char *const *subtype_names)
{
	if (!subtype_names[0]) {
		buf[0] = '\0';
		return 0;
	}
	return sdp_msg_to_str(buf, buflen, sdp_from_subtype_names(subtype_names));
}

static const char *bcap_hexstr(const enum gsm48_bcap_speech_ver ms_bcap[])
{
	struct gsm_mncc_bearer_cap bcap = {
		.transfer = GSM_MNCC_BCAP_SPEECH,
		.speech_ver = { -1 },
		.radio = GSM48_BCAP_RRQ_DUAL_FR,
	};
	const enum gsm48_bcap_speech_ver *pos;
	for (pos = ms_bcap; *pos != LIST_END; pos++) {
		bearer_cap_add_speech_ver(&bcap, *pos);
	}
	struct msgb *msg = msgb_alloc(128, "bcap");
	gsm48_encode_bearer_cap(msg, 0, &bcap);
	char *ret = osmo_hexdump_nospc(msg->data, msg->len);
	msgb_free(msg);
	return ret;
}

static void test_codecs_mo(const struct codec_test *t)
{
	struct gsm_mncc mncc = {
		.imsi = IMSI,
	};

	struct gsm_mncc_rtp *mncc_rtp = (void*)&mncc;

	BTW("\n\n======================== MO call: %s", t->desc);
	btw("CM Service Request with Codec List (BSS Supported) =%s",
	    codec_list_name(t->mo_rx_compl_l3_codec_list_bss_supported));

	cm_service_result_sent = RES_NONE;
	ms_sends_compl_l3("052471"
			  "03575886" /* classmark 2 */
			  "089910070000106005" /* IMSI */,
			  codec_list(t->mo_rx_compl_l3_codec_list_bss_supported));
	VERBOSE_ASSERT(cm_service_result_sent, == RES_ACCEPT, "%d");
	EXPECT_ACCEPTED(true);

	btw("MS sends CC SETUP with Bearer Capability = %s",
	    bcap_name(t->mo_rx_ms_bcap));
	expect_crcx(RTP_TO_CN);
	expect_crcx(RTP_TO_RAN);
	ms_sends_msgf("0385" /* CC, seq = 2 -> 0x80 | CC Setup = 0x5 */
		      "%s" /* Bearer Capability */
		      "5e038121f3" /* Called Number BCD */
		      "15020100" /* CC Capabilities */
		      "4008" /* Supported Codec List */
		      "04026000" /* UMTS: AMR 2 | AMR */
		      "00021f00" /* GSM: HR AMR | FR AMR | GSM EFR | GSM HR | GSM FR */,
		      bcap_hexstr(t->mo_rx_ms_bcap)
		     );
	OSMO_ASSERT(crcx_scheduled(RTP_TO_CN));
	OSMO_ASSERT(crcx_scheduled(RTP_TO_RAN));

	btw("As soon as the MGW port towards CN is created, MNCC_SETUP_IND is triggered");
	cc_to_mncc_expect_tx(IMSI, MNCC_SETUP_IND);
	crcx_ok(RTP_TO_CN);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	mncc.callref = cc_to_mncc_tx_got_callref;
	VALIDATE_SDP(cc_to_mncc_tx_last_sdp, t->mo_tx_sdp_mncc_setup_ind);

	btw("MNCC replies with MNCC_RTP_CREATE, causing MGW endpoint CRCX to RAN");
	sdp_str_from_subtype_names(mncc_rtp->sdp, sizeof(mncc_rtp->sdp), t->mo_rx_sdp_mncc_rtp_create);
	mncc_sends_to_cc(MNCC_RTP_CREATE, &mncc);

	btw("MGW acknowledges the CRCX, triggering Assignment with%s", perm_speech_name(t->mo_tx_assignment_perm_speech));
	expect_bssap_assignment();
	crcx_ok(RTP_TO_RAN);
	OSMO_ASSERT(bssap_assignment_sent);
	VALIDATE_PERM_SPEECH(&bssap_assignment_command_last_channel_type, t->mo_tx_assignment_perm_speech);

	btw("Assignment succeeds, triggering MNCC_RTP_CREATE ack to MNCC");
	cc_to_mncc_expect_tx("", MNCC_RTP_CREATE);
	ms_sends_assignment_complete(t->mo_rx_assigned_codec);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	VALIDATE_SDP(cc_to_mncc_tx_last_sdp, t->mo_tx_sdp_mncc_rtp_create);

	btw("MNCC says that's fine");
	dtap_expect_tx("8302" /* CC: Call Proceeding */);
	mncc_sends_to_cc(MNCC_CALL_PROC_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	fake_time_passes(1, 23);

	btw("The other call leg got established (not shown here), MNCC tells us so, with codecs %s",
	    strlist_name(t->mo_rx_sdp_mncc_alert_req));
	dtap_expect_tx("8301" /* CC: Call Alerting */);
	sdp_str_from_subtype_names(mncc.sdp, sizeof(mncc.sdp), t->mo_rx_sdp_mncc_alert_req);
	mncc_sends_to_cc(MNCC_ALERT_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	dtap_expect_tx("8307" /* CC: Connect */);
	sdp_str_from_subtype_names(mncc.sdp, sizeof(mncc.sdp), t->mo_rx_sdp_mncc_setup_rsp);
	mncc_sends_to_cc(MNCC_SETUP_RSP, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	fake_time_passes(1, 23);

	cc_to_mncc_expect_tx("", MNCC_SETUP_COMPL_IND);
	ms_sends_msg("03cf" /* CC: Connect Acknowledge */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	VALIDATE_SDP(cc_to_mncc_tx_last_sdp, t->mo_tx_sdp_mncc_setup_compl_ind);

	BTW("RTP stream goes ahead, not shown here.");
	fake_time_passes(123, 45);

	BTW("Call ends");
	cc_to_mncc_expect_tx("", MNCC_DISC_IND);
	ms_sends_msg("032502e090" /* CC: Disconnect, cause: Normal Call Clearing */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	dtap_expect_tx("832d" /* CC: Release */);
	mncc_sends_to_cc(MNCC_REL_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	cc_to_mncc_expect_tx("", MNCC_REL_CNF);
	expect_bssap_clear();
	ms_sends_msg("036a" /* CC: Release Complete */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	OSMO_ASSERT(bssap_clear_sent);

	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);
	BTW("======================== SUCCESS: MO call: %s", t->desc);
}

static void test_codecs_mt(const struct codec_test *t)
{
	struct gsm_mncc mncc = {
		.imsi = IMSI,
		.callref = 0x423,
		.fields = MNCC_F_BEARER_CAP,
		.bearer_cap = {
			.speech_ver = { GSM48_BCAP_SV_FR, -1, },
		},
	};
	struct gsm_mncc_rtp *mncc_rtp = (void*)&mncc;

	BTW("\n\n======================== MT call: %s", t->desc);

	BTW("MNCC asks us to setup a call, causing Paging");

	paging_expect_imsi(IMSI);
	paging_sent = false;
	sdp_str_from_subtype_names(mncc.sdp, sizeof(mncc.sdp), t->mt_rx_sdp_mncc_setup_req);
	mncc_sends_to_cc(MNCC_SETUP_REQ, &mncc);
	mncc.sdp[0] = '\0';

	VERBOSE_ASSERT(paging_sent, == true, "%d");

	btw("MS replies with Paging Response, with Codec List (BSS Supported) =%s",
	    codec_list_name(t->mt_rx_compl_l3_codec_list_bss_supported));

	if (t->expect_codec_mismatch_on_paging_response) {
		btw("VLR accepts, but MSC notices a codec mismatch and aborts");
		cc_to_mncc_expect_tx("", MNCC_REL_IND);
		dtap_expect_tx("032d0802e1af" /* CC Release */);
		expect_bssap_clear();
		ms_sends_compl_l3("062707"
				  "03575886" /* classmark 2 */
				  "089910070000106005" /* IMSI */,
				  codec_list(t->mt_rx_compl_l3_codec_list_bss_supported));
		OSMO_ASSERT(cc_to_mncc_tx_confirmed);
		OSMO_ASSERT(bssap_clear_sent);

		ran_sends_clear_complete();
		EXPECT_CONN_COUNT(0);

		BTW("======================== SUCCESS: MT call: %s", t->desc);
		return;
	}

	btw("VLR accepts, MSC sends CC Setup with Bearer Capability = %s",
	    bcap_name(t->mt_tx_cc_setup_bcap));
	char *cc_setup_bcap = talloc_asprintf(msc_vlr_tests_ctx, "0305%s",
					      bcap_hexstr(t->mt_tx_cc_setup_bcap));
	dtap_expect_tx(cc_setup_bcap);
	ms_sends_compl_l3("062707"
			  "03575886" /* classmark 2 */
			  "089910070000106005" /* IMSI */,
			  codec_list(t->mt_rx_compl_l3_codec_list_bss_supported));
	OSMO_ASSERT(dtap_tx_confirmed);
	talloc_free(cc_setup_bcap);

	btw("MS confirms call, we create a RAN-side RTP and forward MNCC_CALL_CONF_IND");
	expect_crcx(RTP_TO_CN);
	expect_crcx(RTP_TO_RAN);
	cc_to_mncc_expect_tx(IMSI, MNCC_CALL_CONF_IND);
	ms_sends_msgf("8348" /* CC: Call Confirmed */
		      "%s" /* Bearer Capability */
		      "15020100" /* Call Control Capabilities */
		      "40080402600400021f00" /* Supported Codec List */,
		      bcap_hexstr(t->mt_rx_ms_bcap)
		     );
	OSMO_ASSERT(crcx_scheduled(RTP_TO_CN));
	OSMO_ASSERT(crcx_scheduled(RTP_TO_RAN));
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	VALIDATE_SDP(cc_to_mncc_tx_last_sdp, t->mt_tx_sdp_mncc_call_conf_ind);

	btw("MGW acknowledges the CRCX to RAN, triggering Assignment with%s", perm_speech_name(t->mt_tx_assignment_perm_speech));

	if (t->expect_codec_mismatch_on_cc_call_conf) {
		btw("MS Bearer Capability leads to a codec mismatch, Assignment aborts");

		dtap_expect_tx("032d0802e1af" /* CC Release */);
		cc_to_mncc_expect_tx("", MNCC_REL_CNF);
		expect_bssap_clear();
		crcx_ok(RTP_TO_RAN);

		OSMO_ASSERT(cc_to_mncc_tx_confirmed);
		OSMO_ASSERT(bssap_clear_sent);

		ran_sends_clear_complete();
		EXPECT_CONN_COUNT(0);
		BTW("======================== SUCCESS: MT call: %s", t->desc);
		return;
	}

	expect_bssap_assignment();
	crcx_ok(RTP_TO_RAN);
	OSMO_ASSERT(bssap_assignment_sent);
	VALIDATE_PERM_SPEECH(&bssap_assignment_command_last_channel_type, t->mt_tx_assignment_perm_speech);

	btw("Assignment completes, triggering CRCX to CN");
	ms_sends_assignment_complete(t->mt_rx_assigned_codec);

	btw("MNCC sends MNCC_RTP_CREATE, which first waits for the CN side RTP");
	sdp_str_from_subtype_names(mncc_rtp->sdp, sizeof(mncc_rtp->sdp), t->mt_rx_sdp_mncc_rtp_create);
	mncc_sends_to_cc(MNCC_RTP_CREATE, &mncc);

	btw("When the CN side RTP address is known, ack MNCC_RTP_CREATE");
	cc_to_mncc_expect_tx("", MNCC_RTP_CREATE);
	crcx_ok(RTP_TO_CN);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	VALIDATE_SDP(cc_to_mncc_tx_last_sdp, t->mt_tx_sdp_mncc_rtp_create);

	fake_time_passes(1, 23);

	cc_to_mncc_expect_tx("", MNCC_ALERT_IND);
	ms_sends_msg("8381" /* CC: Alerting */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	VALIDATE_SDP(cc_to_mncc_tx_last_sdp, t->mt_tx_sdp_mncc_alert_ind);

	fake_time_passes(1, 23);

	cc_to_mncc_expect_tx(IMSI, MNCC_SETUP_CNF);
	ms_sends_msg("83c7" /* CC: Connect */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	VALIDATE_SDP(cc_to_mncc_tx_last_sdp, t->mt_tx_sdp_mncc_setup_cnf);

	dtap_expect_tx("030f" /* CC: Connect Acknowledge */);
	sdp_str_from_subtype_names(mncc.sdp, sizeof(mncc.sdp), t->mt_rx_sdp_mncc_setup_compl_req);
	mncc_sends_to_cc(MNCC_SETUP_COMPL_REQ, &mncc);

	BTW("RTP stream goes ahead, not shown here.");
	fake_time_passes(123, 45);

	BTW("Call ends");
	cc_to_mncc_expect_tx("", MNCC_DISC_IND);
	ms_sends_msg("832502e090" /* CC: Disconnect, cause: Normal Call Clearing */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);

	dtap_expect_tx("032d" /* CC: Release */);
	mncc_sends_to_cc(MNCC_REL_REQ, &mncc);
	OSMO_ASSERT(dtap_tx_confirmed);

	cc_to_mncc_expect_tx("", MNCC_REL_CNF);
	expect_bssap_clear();
	ms_sends_msg("836a" /* CC: Release Complete */);
	OSMO_ASSERT(cc_to_mncc_tx_confirmed);
	OSMO_ASSERT(bssap_clear_sent);

	ran_sends_clear_complete();
	EXPECT_CONN_COUNT(0);
	BTW("======================== SUCCESS: MT call: %s", t->desc);
}

static void test_codecs()
{
	const struct codec_test *t;
	clear_vlr();

	comment_start();

	fake_time_start();

	lu_geran_noauth();

	for (t = codec_tests; t - codec_tests < ARRAY_SIZE(codec_tests); t++) {
		test_codecs_mo(t);
		test_codecs_mt(t);
	}

	EXPECT_CONN_COUNT(0);
	clear_vlr();
	comment_end();
}

msc_vlr_test_func_t msc_vlr_tests[] = {
	test_call_mo,
	test_call_mt,
	test_call_mt2,
	test_call_mo_to_unknown,
	test_call_mo_to_unknown_timeout,
	test_codecs,
	NULL
};