summaryrefslogtreecommitdiff
path: root/tests/test_sdp.c
blob: 0ab8ec8aee0b4b9712365435c2d3f7c14aa276ec (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
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2017, Digium Inc.
 *
 * Mark Michelson <mmmichelson@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/sdp.h"
#include "asterisk/stream.h"
#include "asterisk/format.h"
#include "asterisk/format_cache.h"
#include "asterisk/format_cap.h"
#include "asterisk/rtp_engine.h"

static int validate_o_line(struct ast_test *test, const struct ast_sdp_o_line *o_line,
	const char *sdpowner, const char *address_type, const char *address)
{
	if (!o_line) {
		return -1;
	}

	if (strcmp(o_line->username, sdpowner)) {
		ast_test_status_update(test, "Expected o-line SDP owner %s but got %s\n",
			sdpowner, o_line->username);
		return -1;
	}

	if (strcmp(o_line->address_type, address_type)) {
		ast_test_status_update(test, "Expected o-line SDP address type %s but got %s\n",
			address_type, o_line->address_type);
		return -1;
	}

	if (strcmp(o_line->address, address)) {
		ast_test_status_update(test, "Expected o-line SDP address %s but got %s\n",
			address, o_line->address);
		return -1;
	}

	ast_test_status_update(test, "SDP o-line is as expected!\n");
	return 0;
}

static int validate_c_line(struct ast_test *test, const struct ast_sdp_c_line *c_line,
	const char *address_type, const char *address)
{
	if (strcmp(c_line->address_type, address_type)) {
		ast_test_status_update(test, "Expected c-line SDP address type %s but got %s\n",
			address_type, c_line->address_type);
		return -1;
	}

	if (strcmp(c_line->address, address)) {
		ast_test_status_update(test, "Expected c-line SDP address %s but got %s\n",
			address, c_line->address);
		return -1;
	}

	ast_test_status_update(test, "SDP c-line is as expected!\n");
	return 0;
}

static int validate_m_line(struct ast_test *test, const struct ast_sdp_m_line *m_line,
	const char *media_type, int num_payloads)
{
	if (strcmp(m_line->type, media_type)) {
		ast_test_status_update(test, "Expected m-line media type %s but got %s\n",
			media_type, m_line->type);
		return -1;
	}

	if (m_line->port == 0) {
		ast_test_status_update(test, "Expected %s m-line to not be declined\n",
			media_type);
		return -1;
	}

	if (ast_sdp_m_get_payload_count(m_line) != num_payloads) {
		ast_test_status_update(test, "Expected %s m-line payload count %d but got %d\n",
			media_type, num_payloads, ast_sdp_m_get_payload_count(m_line));
		return -1;
	}

	ast_test_status_update(test, "SDP %s m-line is as expected\n", media_type);
	return 0;
}

static int validate_m_line_declined(struct ast_test *test,
	const struct ast_sdp_m_line *m_line, const char *media_type)
{
	if (strcmp(m_line->type, media_type)) {
		ast_test_status_update(test, "Expected m-line media type %s but got %s\n",
			media_type, m_line->type);
		return -1;
	}

	if (m_line->port != 0) {
		ast_test_status_update(test, "Expected %s m-line to be declined but got port %u\n",
			media_type, m_line->port);
		return -1;
	}

	ast_test_status_update(test, "SDP %s m-line is as expected\n", media_type);
	return 0;
}

static int validate_rtpmap(struct ast_test *test, const struct ast_sdp_m_line *m_line,
	const char *media_name)
{
	struct ast_sdp_a_line *a_line;
	int i;

	for (i = 0; i < ast_sdp_m_get_a_count(m_line); ++i) {
		struct ast_sdp_rtpmap *rtpmap;
		int match;

		a_line = ast_sdp_m_get_a(m_line, i);
		if (strcmp(a_line->name, "rtpmap")) {
			continue;
		}

		rtpmap = ast_sdp_a_get_rtpmap(a_line);
		if (!rtpmap) {
			return -1;
		}

		match = !strcmp(rtpmap->encoding_name, media_name);

		ast_sdp_rtpmap_free(rtpmap);
		if (match) {
			return 0;
		}
	}

	ast_test_status_update(test, "Could not find rtpmap with encoding name %s\n", media_name);

	return -1;
}

static enum ast_test_result_state validate_t38(struct ast_test *test, const struct ast_sdp_m_line *m_line)
{
	struct ast_sdp_a_line *a_line;

	a_line = ast_sdp_m_find_attribute(m_line, "T38FaxVersion", -1);
	ast_test_validate(test, a_line && !strcmp(a_line->value, "0"));

	a_line = ast_sdp_m_find_attribute(m_line, "T38FaxMaxBitRate", -1);
	ast_test_validate(test, a_line && !strcmp(a_line->value, "14400"));

	a_line = ast_sdp_m_find_attribute(m_line, "T38FaxRateManagement", -1);
	ast_test_validate(test, a_line && !strcmp(a_line->value, "transferredTCF"));

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(invalid_rtpmap)
{
	/* a=rtpmap: is already assumed. This is the part after that */
	static const char *invalids[] = {
		"J PCMU/8000",
		"0 PCMU:8000",
		"0 PCMU/EIGHT-THOUSAND",
		"0 PCMU/8000million/2",
		"0 PCMU//2",
		"0 /8000/2",
		"0 PCMU/8000/",
		"0 PCMU/8000million",
	};
	int i;
	enum ast_test_result_state res = AST_TEST_PASS;

	switch(cmd) {
	case TEST_INIT:
		info->name = "invalid_rtpmap";
		info->category = "/main/sdp/";
		info->summary = "Ensure invalid rtpmaps are rejected";
		info->description =
			"Try to convert several invalid rtpmap attributes. If\n"
			"any succeeds, the test fails.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	for (i = 0; i < ARRAY_LEN(invalids); ++i) {
		struct ast_sdp_a_line *a_line;
		struct ast_sdp_rtpmap *rtpmap;

		a_line = ast_sdp_a_alloc("rtpmap", invalids[i]);
		rtpmap = ast_sdp_a_get_rtpmap(a_line);
		if (rtpmap) {
			ast_test_status_update(test, "Invalid rtpmap '%s' was accepted as valid\n",
				invalids[i]);
			res = AST_TEST_FAIL;
		}
		ast_sdp_a_free(a_line);
		ast_sdp_rtpmap_free(rtpmap);
	}

	return res;
}

AST_TEST_DEFINE(rtpmap)
{
	static const char *valids[] = {
		"0 PCMU/8000",
		"107 opus/48000/2",
	};
	static int payloads[] = {
		0,
		107,
	};
	static const char *encoding_names[] = {
		"PCMU",
		"opus",
	};
	static int clock_rates[] = {
		8000,
		48000,
	};
	static const char *encoding_parameters[] = {
		"",
		"2",
	};
	int i;
	enum ast_test_result_state res = AST_TEST_PASS;

	switch(cmd) {
	case TEST_INIT:
		info->name = "rtpmap";
		info->category = "/main/sdp/";
		info->summary = "Ensure rtpmap attribute values are parsed correctly";
		info->description =
			"Parse several valid rtpmap attributes. Ensure that the parsed values\n"
			"are what we expect";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	for (i = 0; i < ARRAY_LEN(valids); ++i) {
		struct ast_sdp_a_line *a_line;
		struct ast_sdp_rtpmap *rtpmap;

		a_line = ast_sdp_a_alloc("rtpmap", valids[i]);
		rtpmap = ast_sdp_a_get_rtpmap(a_line);
		if (!rtpmap) {
			ast_test_status_update(test, "Valid rtpmap '%s' was rejected as invalid\n",
				valids[i]);
			res = AST_TEST_FAIL;
			continue;
		}
		if (rtpmap->payload != payloads[i]) {
			ast_test_status_update(test, "RTPmap payload '%d' does not match expected '%d'\n",
				rtpmap->payload, payloads[i]);
			res = AST_TEST_FAIL;
		}
		if (strcmp(rtpmap->encoding_name, encoding_names[i])) {
			ast_test_status_update(test, "RTPmap encoding_name '%s' does not match expected '%s'\n",
				rtpmap->encoding_name, encoding_names[i]);
			res = AST_TEST_FAIL;
		}
		if (rtpmap->clock_rate != clock_rates[i]) {
			ast_test_status_update(test, "RTPmap clock rate '%d' does not match expected '%d'\n",
				rtpmap->clock_rate, clock_rates[i]);
			res = AST_TEST_FAIL;
		}
		if (strcmp(rtpmap->encoding_parameters, encoding_parameters[i])) {
			ast_test_status_update(test, "RTPmap encoding_parameter '%s' does not match expected '%s'\n",
				rtpmap->encoding_parameters, encoding_parameters[i]);
			res = AST_TEST_FAIL;
		}
		ast_sdp_a_free(a_line);
		ast_sdp_rtpmap_free(rtpmap);
	}

	return res;
}

AST_TEST_DEFINE(find_attr)
{
	enum ast_test_result_state res = AST_TEST_PASS;
	struct ast_sdp_m_line *m_line;
	struct ast_sdp_a_line *a_line;
	int idx;

	switch(cmd) {
	case TEST_INIT:
		info->name = "find_attr";
		info->category = "/main/sdp/";
		info->summary = "Ensure that finding attributes works as expected";
		info->description =
			"A SDP m-line is created, and attributes are added.\n"
			"We then attempt a series of attribute-finding calls that are expected to work\n"
			"followed by a series of attribute-finding calls that are expected fo fail.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	m_line = ast_sdp_m_alloc("audio", 666, 1, "RTP/AVP", NULL);
	if (!m_line) {
		res = AST_TEST_FAIL;
		goto end;
	}
	a_line = ast_sdp_a_alloc("foo", "0 bar");
	if (!a_line) {
		res = AST_TEST_FAIL;
		goto end;
	}
	ast_sdp_m_add_a(m_line, a_line);
	a_line = ast_sdp_a_alloc("foo", "0 bee");
	if (!a_line) {
		res = AST_TEST_FAIL;
		goto end;
	}
	ast_sdp_m_add_a(m_line, a_line);

	a_line = ast_sdp_a_alloc("baz", "howdy");
	if (!a_line) {
		res = AST_TEST_FAIL;
		goto end;
	}
	ast_sdp_m_add_a(m_line, a_line);

	/* These should work */
	a_line = ast_sdp_m_find_attribute(m_line, "foo", 0);
	if (!a_line || strcmp(a_line->value, "0 bar")) {
		ast_test_status_update(test, "Failed to find attribute 'foo' with payload '0'\n");
		res = AST_TEST_FAIL;
	}
	a_line = ast_sdp_m_find_attribute(m_line, "foo", -1);
	if (!a_line || strcmp(a_line->value, "0 bar")) {
		ast_test_status_update(test, "Failed to find attribute 'foo' with unspecified payload\n");
		res = AST_TEST_FAIL;
	}
	a_line = ast_sdp_m_find_attribute(m_line, "baz", -1);
	if (!a_line || strcmp(a_line->value, "howdy")) {
		ast_test_status_update(test, "Failed to find attribute 'baz' with unspecified payload\n");
		res = AST_TEST_FAIL;
	}

	idx = ast_sdp_m_find_a_first(m_line, "foo", 0);
	if (idx < 0) {
		ast_test_status_update(test, "Failed to find first attribute 'foo' with payload '0'\n");
		res = AST_TEST_FAIL;
		goto end;
	}
	a_line = ast_sdp_m_get_a(m_line, idx);
	if (!a_line || strcmp(a_line->value, "0 bar")) {
		ast_test_status_update(test, "Find first attribute 'foo' with payload '0' didn't match\n");
		res = AST_TEST_FAIL;
	}
	idx = ast_sdp_m_find_a_next(m_line, idx, "foo", 0);
	if (idx < 0) {
		ast_test_status_update(test, "Failed to find next attribute 'foo' with payload '0'\n");
		res = AST_TEST_FAIL;
		goto end;
	}
	a_line = ast_sdp_m_get_a(m_line, idx);
	if (!a_line || strcmp(a_line->value, "0 bee")) {
		ast_test_status_update(test, "Find next attribute 'foo' with payload '0' didn't match\n");
		res = AST_TEST_FAIL;
	}
	idx = ast_sdp_m_find_a_next(m_line, idx, "foo", 0);
	if (0 <= idx) {
		ast_test_status_update(test, "Find next attribute 'foo' with payload '0' found too many\n");
		res = AST_TEST_FAIL;
	}

	idx = ast_sdp_m_find_a_first(m_line, "foo", -1);
	if (idx < 0) {
		ast_test_status_update(test, "Failed to find first attribute 'foo' with unspecified payload\n");
		res = AST_TEST_FAIL;
		goto end;
	}
	a_line = ast_sdp_m_get_a(m_line, idx);
	if (!a_line || strcmp(a_line->value, "0 bar")) {
		ast_test_status_update(test, "Find first attribute 'foo' with unspecified payload didn't match\n");
		res = AST_TEST_FAIL;
	}
	idx = ast_sdp_m_find_a_next(m_line, idx, "foo", -1);
	if (idx < 0) {
		ast_test_status_update(test, "Failed to find next attribute 'foo' with unspecified payload\n");
		res = AST_TEST_FAIL;
		goto end;
	}
	a_line = ast_sdp_m_get_a(m_line, idx);
	if (!a_line || strcmp(a_line->value, "0 bee")) {
		ast_test_status_update(test, "Find next attribute 'foo' with unspecified payload didn't match\n");
		res = AST_TEST_FAIL;
	}
	idx = ast_sdp_m_find_a_next(m_line, idx, "foo", -1);
	if (0 <= idx) {
		ast_test_status_update(test, "Find next attribute 'foo' with unspecified payload found too many\n");
		res = AST_TEST_FAIL;
	}

	/* These should fail */
	a_line = ast_sdp_m_find_attribute(m_line, "foo", 1);
	if (a_line) {
		ast_test_status_update(test, "Found non-existent attribute 'foo' with payload '1'\n");
		res = AST_TEST_FAIL;
	}
	a_line = ast_sdp_m_find_attribute(m_line, "baz", 0);
	if (a_line) {
		ast_test_status_update(test, "Found non-existent attribute 'baz' with payload '0'\n");
		res = AST_TEST_FAIL;
	}
	a_line = ast_sdp_m_find_attribute(m_line, "wibble", 0);
	if (a_line) {
		ast_test_status_update(test, "Found non-existent attribute 'wibble' with payload '0'\n");
		res = AST_TEST_FAIL;
	}
	a_line = ast_sdp_m_find_attribute(m_line, "wibble", -1);
	if (a_line) {
		ast_test_status_update(test, "Found non-existent attribute 'wibble' with unspecified payload\n");
		res = AST_TEST_FAIL;
	}

end:
	ast_sdp_m_free(m_line);
	return res;
}

static struct ast_sdp_options *sdp_options_common(void)
{
	struct ast_sdp_options *options;

	options = ast_sdp_options_alloc();
	if (!options) {
		return NULL;
	}
	ast_sdp_options_set_media_address(options, "127.0.0.1");
	ast_sdp_options_set_sdpowner(options, "me");
	ast_sdp_options_set_rtp_engine(options, "asterisk");
	ast_sdp_options_set_impl(options, AST_SDP_IMPL_PJMEDIA);

	return options;
}

struct sdp_format {
	enum ast_media_type type;
	const char *formats;
};

static int build_sdp_option_formats(struct ast_sdp_options *options, int num_streams, const struct sdp_format *formats)
{
	int idx;

	for (idx = 0; idx < num_streams; ++idx) {
		struct ast_format_cap *caps;

		if (ast_strlen_zero(formats[idx].formats)) {
			continue;
		}

		caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
		if (!caps
			|| ast_format_cap_update_by_allow_disallow(caps, formats[idx].formats, 1) < 0) {
			ao2_cleanup(caps);
			return -1;
		}
		ast_sdp_options_set_format_cap_type(options, formats[idx].type, caps);
		ao2_cleanup(caps);
	}
	return 0;
}

/*!
 * \brief Common method to build an SDP state for a test.
 *
 * This uses the passed-in formats to create a stream topology, which is then used to create the SDP
 * state.
 *
 * There is an optional test_options field you can use if your test has specific options you need to
 * set. If your test does not require anything special, it can just pass NULL for this parameter. If
 * you do pass in test_options, this function steals ownership of those options.
 *
 * \param num_streams The number of elements in the formats array.
 * \param formats Array of media types and formats that will be in the state.
 * \param opt_num_streams The number of new stream types allowed to create.
 *           Not used if test_options provided.
 * \param opt_formats Array of new stream media types and formats allowed to create.
 *           NULL if use a default stream creation.
 *           Not used if test_options provided.
 * \param max_streams 0 if set max to max(3, num_streams) else max(max_streams, num_streams)
 *           Not used if test_options provided.
 * \param test_options Optional SDP options.
 */
static struct ast_sdp_state *build_sdp_state(int num_streams, const struct sdp_format *formats,
	int opt_num_streams, const struct sdp_format *opt_formats, unsigned int max_streams,
	struct ast_sdp_options *test_options)
{
	struct ast_stream_topology *topology = NULL;
	struct ast_sdp_state *state = NULL;
	struct ast_sdp_options *options;
	int i;

	if (!test_options) {
		static const struct sdp_format sdp_formats[] = {
			{ AST_MEDIA_TYPE_AUDIO, "ulaw" },
			{ AST_MEDIA_TYPE_VIDEO, "vp8" },
			{ AST_MEDIA_TYPE_IMAGE, "t38" },
		};

		options = sdp_options_common();
		if (!options) {
			goto end;
		}

		/* Determine max_streams to allow */
		if (!max_streams) {
			max_streams = ARRAY_LEN(sdp_formats);
		}
		if (max_streams < num_streams) {
			max_streams = num_streams;
		}
		ast_sdp_options_set_max_streams(options, max_streams);

		/* Determine new stream formats and types allowed */
		if (!opt_formats) {
			opt_num_streams = ARRAY_LEN(sdp_formats);
			opt_formats = sdp_formats;
		}
		if (build_sdp_option_formats(options, opt_num_streams, opt_formats)) {
			goto end;
		}
	} else {
		options = test_options;
	}

	topology = ast_stream_topology_alloc();
	if (!topology) {
		goto end;
	}

	for (i = 0; i < num_streams; ++i) {
		struct ast_stream *stream;

		stream = ast_stream_alloc("sure_thing", formats[i].type);
		if (!stream) {
			goto end;
		}
		if (!ast_strlen_zero(formats[i].formats)) {
			struct ast_format_cap *caps;

			caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
			if (!caps
				|| ast_format_cap_update_by_allow_disallow(caps, formats[i].formats, 1) < 0) {
				ao2_cleanup(caps);
				ast_stream_free(stream);
				goto end;
			}
			ast_stream_set_formats(stream, caps);
			ao2_cleanup(caps);
		} else {
			ast_stream_set_state(stream, AST_STREAM_STATE_REMOVED);
		}
		if (ast_stream_topology_append_stream(topology, stream) < 0) {
			ast_stream_free(stream);
			goto end;
		}
	}

	state = ast_sdp_state_alloc(topology, options);
	if (!state) {
		goto end;
	}

end:
	ast_stream_topology_free(topology);
	if (!state) {
		ast_sdp_options_free(options);
	}

	return state;
}

AST_TEST_DEFINE(topology_to_sdp)
{
	enum ast_test_result_state res = AST_TEST_FAIL;
	struct ast_sdp_state *sdp_state = NULL;
	const struct ast_sdp *sdp = NULL;
	struct ast_sdp_m_line *m_line = NULL;
	struct sdp_format formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "ulaw,alaw,g722,opus" },
		{ AST_MEDIA_TYPE_VIDEO, "h264,vp8" },
		{ AST_MEDIA_TYPE_IMAGE, "t38" },
	};

	switch(cmd) {
	case TEST_INIT:
		info->name = "topology_to_sdp";
		info->category = "/main/sdp/";
		info->summary = "Convert a topology into an SDP";
		info->description =
			"Ensure SDPs get converted to expected stream topology";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	sdp_state = build_sdp_state(ARRAY_LEN(formats), formats,
		ARRAY_LEN(formats), formats, 0, NULL);
	if (!sdp_state) {
		goto end;
	}

	sdp = ast_sdp_state_get_local_sdp(sdp_state);
	if (!sdp) {
		goto end;
	}

	if (validate_o_line(test, sdp->o_line, "me", "IP4", "127.0.0.1")) {
		goto end;
	}

	if (validate_c_line(test, sdp->c_line, "IP4", "127.0.0.1")) {
		goto end;
	}

	if (ast_sdp_get_m_count(sdp) != 3) {
		ast_test_status_update(test, "Unexpected number of streams in generated SDP: %d\n",
			ast_sdp_get_m_count(sdp));
		goto end;
	}

	m_line = ast_sdp_get_m(sdp, 0);

	if (validate_m_line(test, m_line, "audio", 4)) {
		goto end;
	}

	if (validate_rtpmap(test, m_line, "PCMU")) {
		goto end;
	}

	if (validate_rtpmap(test, m_line, "PCMA")) {
		goto end;
	}

	if (validate_rtpmap(test, m_line, "G722")) {
		goto end;
	}

	if (validate_rtpmap(test, m_line, "opus")) {
		goto end;
	}

	m_line = ast_sdp_get_m(sdp, 1);
	if (validate_m_line(test, m_line, "video", 2)) {
		goto end;
	}

	if (validate_rtpmap(test, m_line, "VP8")) {
		goto end;
	}

	if (validate_rtpmap(test, m_line, "H264")) {
		goto end;
	}

	m_line = ast_sdp_get_m(sdp, 2);
	if (validate_m_line(test, m_line, "image", 1)) {
		goto end;
	}
	if (validate_t38(test, m_line) != AST_TEST_PASS) {
		goto end;
	}

	res = AST_TEST_PASS;

end:
	ast_sdp_state_free(sdp_state);
	return res;
}

static int validate_formats(struct ast_test *test, struct ast_stream_topology *topology, int index,
	enum ast_media_type type, int format_count, const char **expected_formats)
{
	struct ast_stream *stream;
	struct ast_format_cap *caps;
	struct ast_format *format;
	int i;

	stream = ast_stream_topology_get_stream(topology, index);
	if (ast_stream_get_type(stream) != type) {
		ast_test_status_update(test, "Unexpected stream type encountered\n");
		return -1;
	}
	caps = ast_stream_get_formats(stream);

	if (ast_format_cap_count(caps) != format_count) {
		ast_test_status_update(test, "Unexpected format count '%d'. Expecting '%d'\n",
			(int) ast_format_cap_count(caps), format_count);
		return -1;
	}

	for (i = 0; i < ast_format_cap_count(caps); ++i) {
		format = ast_format_cap_get_format(caps, i);
		if (strcmp(ast_format_get_name(format), expected_formats[i])) {
			ast_test_status_update(test, "Unexpected format '%s'at index %d. Expected '%s'\n",
				ast_format_get_name(format), i, expected_formats[i]);
			return -1;
		}
	}

	return 0;
}

AST_TEST_DEFINE(sdp_to_topology)
{
	enum ast_test_result_state res = AST_TEST_PASS;
	struct ast_sdp_state *sdp_state;
	const struct ast_sdp *sdp;
	struct ast_stream_topology *topology = NULL;
	struct sdp_format sdp_formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "ulaw,alaw,g722,opus" },
		{ AST_MEDIA_TYPE_VIDEO, "h264,vp8" },
		{ AST_MEDIA_TYPE_IMAGE, "t38" },
	};
	static const char *expected_audio_formats[] = {
		"ulaw",
		"alaw",
		"g722",
		"opus",
	};
	static const char *expected_video_formats[] = {
		"h264",
		"vp8",
	};
	static const char *expected_image_formats[] = {
		"t38",
	};

	switch(cmd) {
	case TEST_INIT:
		info->name = "sdp_to_topology";
		info->category = "/main/sdp/";
		info->summary = "Convert an SDP into a topology";
		info->description =
			"Ensure SDPs get converted to expected stream topology";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	sdp_state = build_sdp_state(ARRAY_LEN(sdp_formats), sdp_formats,
		ARRAY_LEN(sdp_formats), sdp_formats, 0, NULL);
	if (!sdp_state) {
		res = AST_TEST_FAIL;
		goto end;
	}

	sdp = ast_sdp_state_get_local_sdp(sdp_state);
	if (!sdp) {
		res = AST_TEST_FAIL;
		goto end;
	}

	topology = ast_get_topology_from_sdp(sdp, 0);
	if (!topology) {
		res = AST_TEST_FAIL;
		goto end;
	}

	if (ast_stream_topology_get_count(topology) != 3) {
		ast_test_status_update(test, "Unexpected topology count '%d'. Expecting 3\n",
			ast_stream_topology_get_count(topology));
		res = AST_TEST_FAIL;
		goto end;
	}

	if (validate_formats(test, topology, 0, AST_MEDIA_TYPE_AUDIO,
			ARRAY_LEN(expected_audio_formats), expected_audio_formats)) {
		res = AST_TEST_FAIL;
		goto end;
	}

	if (validate_formats(test, topology, 1, AST_MEDIA_TYPE_VIDEO,
			ARRAY_LEN(expected_video_formats), expected_video_formats)) {
		res = AST_TEST_FAIL;
		goto end;
	}

	if (validate_formats(test, topology, 2, AST_MEDIA_TYPE_IMAGE,
			ARRAY_LEN(expected_image_formats), expected_image_formats)) {
		res = AST_TEST_FAIL;
		goto end;
	}

end:
	ast_sdp_state_free(sdp_state);
	ast_stream_topology_free(topology);
	return res;
}

static int validate_avi_sdp_streams(struct ast_test *test, const struct ast_sdp *sdp)
{
	struct ast_sdp_m_line *m_line;

	if (!sdp) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 0);
	if (validate_m_line(test, m_line, "audio", 1)) {
		return -1;
	}
	if (validate_rtpmap(test, m_line, "PCMU")) {
		return -1;
	}

	/* The other audio formats should *NOT* be present */
	if (!validate_rtpmap(test, m_line, "PCMA")) {
		return -1;
	}
	if (!validate_rtpmap(test, m_line, "G722")) {
		return -1;
	}
	if (!validate_rtpmap(test, m_line, "opus")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 1);
	if (validate_m_line(test, m_line, "video", 1)) {
		return -1;
	}
	if (validate_rtpmap(test, m_line, "VP8")) {
		return -1;
	}
	if (!validate_rtpmap(test, m_line, "H264")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 2);
	if (validate_m_line(test, m_line, "image", 1)) {
		return -1;
	}

	return 0;
}

static enum ast_test_result_state sdp_negotiation_completed_tests(struct ast_test *test,
	int offer_num_streams, const struct sdp_format *offer_formats,
	int answer_num_streams, const struct sdp_format *answer_formats,
	int allowed_ans_num_streams, const struct sdp_format *allowed_ans_formats,
	unsigned int max_streams,
	int (*validate_sdp)(struct ast_test *test, const struct ast_sdp *sdp))
{
	enum ast_test_result_state res = AST_TEST_PASS;
	struct ast_sdp_state *sdp_state_offerer = NULL;
	struct ast_sdp_state *sdp_state_answerer = NULL;
	const struct ast_sdp *offerer_sdp;
	const struct ast_sdp *answerer_sdp;

	sdp_state_offerer = build_sdp_state(offer_num_streams, offer_formats,
		offer_num_streams, offer_formats, max_streams, NULL);
	if (!sdp_state_offerer) {
		ast_test_status_update(test, "Building offerer SDP state failed\n");
		res = AST_TEST_FAIL;
		goto end;
	}

	sdp_state_answerer = build_sdp_state(answer_num_streams, answer_formats,
		allowed_ans_num_streams, allowed_ans_formats, max_streams, NULL);
	if (!sdp_state_answerer) {
		ast_test_status_update(test, "Building answerer SDP state failed\n");
		res = AST_TEST_FAIL;
		goto end;
	}

	offerer_sdp = ast_sdp_state_get_local_sdp(sdp_state_offerer);
	if (!offerer_sdp) {
		ast_test_status_update(test, "Building offerer offer failed\n");
		res = AST_TEST_FAIL;
		goto end;
	}

	if (ast_sdp_state_set_remote_sdp(sdp_state_answerer, offerer_sdp)) {
		ast_test_status_update(test, "Setting answerer offer failed\n");
		res = AST_TEST_FAIL;
		goto end;
	}
	answerer_sdp = ast_sdp_state_get_local_sdp(sdp_state_answerer);
	if (!answerer_sdp) {
		ast_test_status_update(test, "Building answerer answer failed\n");
		res = AST_TEST_FAIL;
		goto end;
	}

	if (ast_sdp_state_set_remote_sdp(sdp_state_offerer, answerer_sdp)) {
		ast_test_status_update(test, "Setting offerer answer failed\n");
		res = AST_TEST_FAIL;
		goto end;
	}

	/*
	 * Restart SDP negotiations to build the joint SDP on the offerer
	 * side.  Otherwise we will get the original offer for use in
	 * case of retransmissions.
	 */
	if (ast_sdp_state_restart_negotiations(sdp_state_offerer)) {
		ast_test_status_update(test, "Restarting negotiations failed\n");
		res = AST_TEST_FAIL;
		goto end;
	}
	offerer_sdp = ast_sdp_state_get_local_sdp(sdp_state_offerer);
	if (!offerer_sdp) {
		ast_test_status_update(test, "Building offerer current sdp failed\n");
		res = AST_TEST_FAIL;
		goto end;
	}
	if (validate_sdp(test, offerer_sdp)) {
		res = AST_TEST_FAIL;
		goto end;
	}
	if (validate_sdp(test, answerer_sdp)) {
		res = AST_TEST_FAIL;
		goto end;
	}

end:
	ast_sdp_state_free(sdp_state_offerer);
	ast_sdp_state_free(sdp_state_answerer);

	return res;
}

AST_TEST_DEFINE(sdp_negotiation_initial)
{
	static const struct sdp_format offerer_formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "ulaw,alaw,g722,opus" },
		{ AST_MEDIA_TYPE_VIDEO, "h264,vp8" },
		{ AST_MEDIA_TYPE_IMAGE, "t38" },
	};

	switch(cmd) {
	case TEST_INIT:
		info->name = "sdp_negotiation_initial";
		info->category = "/main/sdp/";
		info->summary = "Simulate an initial negotiation";
		info->description =
			"Initial negotiation tests creating new streams on the answering side.\n"
			"After negotiation both offerer and answerer sides should have the same\n"
			"expected stream types and formats.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	return sdp_negotiation_completed_tests(test,
		ARRAY_LEN(offerer_formats), offerer_formats,
		0, NULL,
		0, NULL,
		0,
		validate_avi_sdp_streams);
}

AST_TEST_DEFINE(sdp_negotiation_type_change)
{
	static const struct sdp_format offerer_formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "ulaw,alaw,g722,opus" },
		{ AST_MEDIA_TYPE_VIDEO, "h264,vp8" },
		{ AST_MEDIA_TYPE_IMAGE, "t38" },
	};
	static const struct sdp_format answerer_formats[] = {
		{ AST_MEDIA_TYPE_IMAGE, "t38" },
		{ AST_MEDIA_TYPE_VIDEO, "vp8" },
		{ AST_MEDIA_TYPE_AUDIO, "ulaw" },
	};

	switch(cmd) {
	case TEST_INIT:
		info->name = "sdp_negotiation_type_change";
		info->category = "/main/sdp/";
		info->summary = "Simulate a re-negotiation changing stream types";
		info->description =
			"Reinvite negotiation tests changing stream types on the answering side.\n"
			"After negotiation both offerer and answerer sides should have the same\n"
			"expected stream types and formats.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	return sdp_negotiation_completed_tests(test,
		ARRAY_LEN(offerer_formats), offerer_formats,
		ARRAY_LEN(answerer_formats), answerer_formats,
		0, NULL,
		0,
		validate_avi_sdp_streams);
}

static int validate_aviavia_declined_sdp_streams(struct ast_test *test, const struct ast_sdp *sdp)
{
	struct ast_sdp_m_line *m_line;

	if (!sdp) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 0);
	if (validate_m_line_declined(test, m_line, "audio")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 1);
	if (validate_m_line_declined(test, m_line, "video")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 2);
	if (validate_m_line_declined(test, m_line, "image")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 3);
	if (validate_m_line_declined(test, m_line, "audio")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 4);
	if (validate_m_line_declined(test, m_line, "video")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 5);
	if (validate_m_line_declined(test, m_line, "image")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 6);
	if (validate_m_line(test, m_line, "audio", 1)) {
		return -1;
	}
	if (validate_rtpmap(test, m_line, "PCMU")) {
		return -1;
	}

	/* The other audio formats should *NOT* be present */
	if (!validate_rtpmap(test, m_line, "PCMA")) {
		return -1;
	}

	return 0;
}

AST_TEST_DEFINE(sdp_negotiation_decline_incompatible)
{
	static const struct sdp_format offerer_formats[] = {
		/* Incompatible declined streams */
		{ AST_MEDIA_TYPE_AUDIO, "alaw" },
		{ AST_MEDIA_TYPE_VIDEO, "vp8" },
		{ AST_MEDIA_TYPE_IMAGE, "t38" },
		/* Initially declined streams */
		{ AST_MEDIA_TYPE_AUDIO, "" },
		{ AST_MEDIA_TYPE_VIDEO, "" },
		{ AST_MEDIA_TYPE_IMAGE, "" },
		/* Compatible stream so not all are declined */
		{ AST_MEDIA_TYPE_AUDIO, "ulaw,alaw" },
	};
	static const struct sdp_format allowed_formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "ulaw" },
	};

	switch(cmd) {
	case TEST_INIT:
		info->name = "sdp_negotiation_decline_incompatible";
		info->category = "/main/sdp/";
		info->summary = "Simulate an initial negotiation declining streams";
		info->description =
			"Initial negotiation tests declining incompatible streams.\n"
			"After negotiation both offerer and answerer sides should have\n"
			"the same expected stream types and formats.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	return sdp_negotiation_completed_tests(test,
		ARRAY_LEN(offerer_formats), offerer_formats,
		0, NULL,
		ARRAY_LEN(allowed_formats), allowed_formats,
		ARRAY_LEN(offerer_formats),
		validate_aviavia_declined_sdp_streams);
}

static int validate_aaaa_declined_sdp_streams(struct ast_test *test, const struct ast_sdp *sdp)
{
	struct ast_sdp_m_line *m_line;

	if (!sdp) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 0);
	if (validate_m_line(test, m_line, "audio", 1)) {
		return -1;
	}
	if (validate_rtpmap(test, m_line, "PCMU")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 1);
	if (validate_m_line(test, m_line, "audio", 1)) {
		return -1;
	}
	if (validate_rtpmap(test, m_line, "PCMU")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 2);
	if (validate_m_line(test, m_line, "audio", 1)) {
		return -1;
	}
	if (validate_rtpmap(test, m_line, "PCMU")) {
		return -1;
	}

	m_line = ast_sdp_get_m(sdp, 3);
	if (validate_m_line_declined(test, m_line, "audio")) {
		return -1;
	}

	return 0;
}

AST_TEST_DEFINE(sdp_negotiation_decline_max_streams)
{
	static const struct sdp_format offerer_formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "ulaw" },
		{ AST_MEDIA_TYPE_AUDIO, "ulaw" },
		{ AST_MEDIA_TYPE_AUDIO, "ulaw" },
		{ AST_MEDIA_TYPE_AUDIO, "ulaw" },
	};

	switch(cmd) {
	case TEST_INIT:
		info->name = "sdp_negotiation_decline_max_streams";
		info->category = "/main/sdp/";
		info->summary = "Simulate an initial negotiation declining excessive streams";
		info->description =
			"Initial negotiation tests declining too many streams on the answering side.\n"
			"After negotiation both offerer and answerer sides should have the same\n"
			"expected stream types and formats.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	return sdp_negotiation_completed_tests(test,
		ARRAY_LEN(offerer_formats), offerer_formats,
		0, NULL,
		0, NULL,
		0,
		validate_aaaa_declined_sdp_streams);
}

AST_TEST_DEFINE(sdp_negotiation_not_acceptable)
{
	enum ast_test_result_state res = AST_TEST_PASS;
	struct ast_sdp_state *sdp_state_offerer = NULL;
	struct ast_sdp_state *sdp_state_answerer = NULL;
	const struct ast_sdp *offerer_sdp;

	static const struct sdp_format offerer_formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "alaw" },
		{ AST_MEDIA_TYPE_AUDIO, "alaw" },
	};

	switch(cmd) {
	case TEST_INIT:
		info->name = "sdp_negotiation_not_acceptable";
		info->category = "/main/sdp/";
		info->summary = "Simulate an initial negotiation declining all streams";
		info->description =
			"Initial negotiation tests declining all streams for a 488 on the answering side.\n"
			"Negotiations should fail because there are no acceptable streams.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	sdp_state_offerer = build_sdp_state(ARRAY_LEN(offerer_formats), offerer_formats,
		ARRAY_LEN(offerer_formats), offerer_formats, 0, NULL);
	if (!sdp_state_offerer) {
		res = AST_TEST_FAIL;
		goto end;
	}

	sdp_state_answerer = build_sdp_state(0, NULL, 0, NULL, 0, NULL);
	if (!sdp_state_answerer) {
		res = AST_TEST_FAIL;
		goto end;
	}

	offerer_sdp = ast_sdp_state_get_local_sdp(sdp_state_offerer);
	if (!offerer_sdp) {
		res = AST_TEST_FAIL;
		goto end;
	}

	if (!ast_sdp_state_set_remote_sdp(sdp_state_answerer, offerer_sdp)) {
		ast_test_status_update(test, "Bad.  Setting remote SDP was successful.\n");
		res = AST_TEST_FAIL;
		goto end;
	}
	if (!ast_sdp_state_is_offer_rejected(sdp_state_answerer)) {
		ast_test_status_update(test, "Bad.  Negotiation failed for some other reason.\n");
		res = AST_TEST_FAIL;
		goto end;
	}

end:
	ast_sdp_state_free(sdp_state_offerer);
	ast_sdp_state_free(sdp_state_answerer);

	return res;
}

static int validate_ssrc(struct ast_test *test, struct ast_sdp_m_line *m_line,
	struct ast_rtp_instance *rtp)
{
	unsigned int ssrc;
	const char *cname;
	struct ast_sdp_a_line *a_line;
	char attr_value[128];

	ssrc = ast_rtp_instance_get_ssrc(rtp);
	cname = ast_rtp_instance_get_cname(rtp);

	snprintf(attr_value, sizeof(attr_value), "%u cname:%s", ssrc, cname);

	a_line = ast_sdp_m_find_attribute(m_line, "ssrc", -1);
	if (!a_line) {
		ast_test_status_update(test, "Could not find 'ssrc' attribute\n");
		return -1;
	}

	if (strcmp(a_line->value, attr_value)) {
		ast_test_status_update(test, "SDP attribute '%s' did not match expected attribute '%s'\n",
			a_line->value, attr_value);
		return -1;
	}

	return 0;
}

AST_TEST_DEFINE(sdp_ssrc_attributes)
{
	enum ast_test_result_state res;
	struct ast_sdp_state *test_state = NULL;
	struct ast_sdp_options *options;
	struct sdp_format formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "ulaw,alaw,g722,opus" },
	};
	const struct ast_sdp *sdp;
	struct ast_sdp_m_line *m_line;
	struct ast_rtp_instance *rtp;

	switch(cmd) {
	case TEST_INIT:
		info->name = "sdp_ssrc_attributes";
		info->category = "/main/sdp/";
		info->summary = "Ensure SSRC-level attributes are added to local SDPs";
		info->description =
			"An SDP is created and is instructed to include SSRC-level attributes.\n"
			"This test ensures that the CNAME SSRC-level attribute is present and\n"
			"that the values match what the RTP instance reports";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	res = AST_TEST_FAIL;

	options = sdp_options_common();
	if (!options) {
		ast_test_status_update(test, "Failed to allocate SDP options\n");
		goto end;
	}
	if (build_sdp_option_formats(options, ARRAY_LEN(formats), formats)) {
		goto end;
	}
	ast_sdp_options_set_ssrc(options, 1);

	test_state = build_sdp_state(ARRAY_LEN(formats), formats, 0, NULL, 0, options);
	if (!test_state) {
		ast_test_status_update(test, "Failed to create SDP state\n");
		goto end;
	}

	sdp = ast_sdp_state_get_local_sdp(test_state);
	if (!sdp) {
		ast_test_status_update(test, "Failed to get local SDP\n");
		goto end;
	}

	/* Need a couple of sanity checks */
	if (ast_sdp_get_m_count(sdp) != ARRAY_LEN(formats)) {
		ast_test_status_update(test, "SDP m count is %d instead of %zu\n",
			ast_sdp_get_m_count(sdp), ARRAY_LEN(formats));
		goto end;
	}

	m_line = ast_sdp_get_m(sdp, 0);
	if (!m_line) {
		ast_test_status_update(test, "Failed to get SDP m-line\n");
		goto end;
	}

	rtp = ast_sdp_state_get_rtp_instance(test_state, 0);
	if (!rtp) {
		ast_test_status_update(test, "Failed to get the RTP instance\n");
		goto end;
	}

	if (validate_ssrc(test, m_line, rtp)) {
		goto end;
	}

	res = AST_TEST_PASS;

end:
	ast_sdp_state_free(test_state);
	return res;
}

struct sdp_topology_stream {
	/*! Media stream type: audio, video, image */
	enum ast_media_type type;
	/*! Media stream state: removed/declined, sendrecv */
	enum ast_stream_state state;
	/*! Comma separated list of formats allowed on the stream.  Can be NULL if stream is removed/declined. */
	const char *formats;
	/*! Optional name of stream.  NULL for default name. */
	const char *name;
};

struct sdp_update_test {
	/*! Maximum number of streams.  (0 if default) */
	int max_streams;
	/*! Optional initial SDP state topology (NULL if not present) */
	const struct sdp_topology_stream * const *initial;
	/*! Required first topology update */
	const struct sdp_topology_stream * const *update_1;
	/*! Optional second topology update (NULL if not present) */
	const struct sdp_topology_stream * const *update_2;
	/*! Expected topology to be offered */
	const struct sdp_topology_stream * const *expected;
};

static struct ast_stream_topology *build_update_topology(const struct sdp_topology_stream * const *spec)
{
	struct ast_stream_topology *topology;
	const struct sdp_topology_stream *desc;

	topology = ast_stream_topology_alloc();
	if (!topology) {
		return NULL;
	}

	for (desc = *spec; desc; ++spec, desc = *spec) {
		struct ast_stream *stream;
		const char *name;

		name = desc->name ?: ast_codec_media_type2str(desc->type);
		stream = ast_stream_alloc(name, desc->type);
		if (!stream) {
			goto fail;
		}
		ast_stream_set_state(stream, desc->state);
		if (desc->formats) {
			struct ast_format_cap *caps;

			caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
			if (!caps
				|| ast_format_cap_update_by_allow_disallow(caps, desc->formats, 1) < 0) {
				ao2_cleanup(caps);
				ast_stream_free(stream);
				goto fail;
			}
			ast_stream_set_formats(stream, caps);
			ao2_ref(caps, -1);
		}
		if (ast_stream_topology_append_stream(topology, stream) < 0) {
			ast_stream_free(stream);
			goto fail;
		}
	}
	return topology;

fail:
	ast_stream_topology_free(topology);
	return NULL;
}

static int cmp_update_topology(struct ast_test *test,
	const struct ast_stream_topology *expected, const struct ast_stream_topology *merged)
{
	int status = 0;
	int idx;
	int max_streams;
	struct ast_stream *exp_stream;
	struct ast_stream *mrg_stream;

	idx = ast_stream_topology_get_count(expected);
	max_streams = ast_stream_topology_get_count(merged);
	if (idx != max_streams) {
		ast_test_status_update(test, "Expected %d streams got %d streams\n",
			idx, max_streams);
		status = -1;
	}
	if (idx < max_streams) {
		max_streams = idx;
	}

	/* Compare common streams by position */
	for (idx = 0; idx < max_streams; ++idx) {
		exp_stream = ast_stream_topology_get_stream(expected, idx);
		mrg_stream = ast_stream_topology_get_stream(merged, idx);

		if (strcmp(ast_stream_get_name(exp_stream), ast_stream_get_name(mrg_stream))) {
			ast_test_status_update(test,
				"Stream %d: Expected stream name '%s' got stream name '%s'\n",
				idx,
				ast_stream_get_name(exp_stream),
				ast_stream_get_name(mrg_stream));
			status = -1;
		}

		if (ast_stream_get_state(exp_stream) != ast_stream_get_state(mrg_stream)) {
			ast_test_status_update(test,
				"Stream %d: Expected stream state '%s' got stream state '%s'\n",
				idx,
				ast_stream_state2str(ast_stream_get_state(exp_stream)),
				ast_stream_state2str(ast_stream_get_state(mrg_stream)));
			status = -1;
		}

		if (ast_stream_get_type(exp_stream) != ast_stream_get_type(mrg_stream)) {
			ast_test_status_update(test,
				"Stream %d: Expected stream type '%s' got stream type '%s'\n",
				idx,
				ast_codec_media_type2str(ast_stream_get_type(exp_stream)),
				ast_codec_media_type2str(ast_stream_get_type(mrg_stream)));
			status = -1;
			continue;
		}

		if (ast_stream_get_state(exp_stream) == AST_STREAM_STATE_REMOVED
			|| ast_stream_get_state(mrg_stream) == AST_STREAM_STATE_REMOVED) {
			/*
			 * Cannot compare formats if one of the streams is
			 * declined because there may not be any on the declined
			 * stream.
			 */
			continue;
		}
		if (!ast_format_cap_identical(ast_stream_get_formats(exp_stream),
			ast_stream_get_formats(mrg_stream))) {
			ast_test_status_update(test,
				"Stream %d: Expected formats do not match merged formats\n",
				idx);
			status = -1;
		}
	}

	return status;
}


static const struct sdp_topology_stream audio_declined_no_name = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_REMOVED, NULL, NULL
};

static const struct sdp_topology_stream audio_ulaw_no_name = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "ulaw", NULL
};

static const struct sdp_topology_stream audio_alaw_no_name = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "alaw", NULL
};

static const struct sdp_topology_stream audio_g722_no_name = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "g722", NULL
};

static const struct sdp_topology_stream audio_g723_no_name = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "g723", NULL
};

static const struct sdp_topology_stream video_declined_no_name = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_REMOVED, NULL, NULL
};

static const struct sdp_topology_stream video_h261_no_name = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h261", NULL
};

static const struct sdp_topology_stream video_h263_no_name = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h263", NULL
};

static const struct sdp_topology_stream video_h264_no_name = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h264", NULL
};

static const struct sdp_topology_stream video_vp8_no_name = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "vp8", NULL
};

static const struct sdp_topology_stream image_declined_no_name = {
	AST_MEDIA_TYPE_IMAGE, AST_STREAM_STATE_REMOVED, NULL, NULL
};

static const struct sdp_topology_stream image_t38_no_name = {
	AST_MEDIA_TYPE_IMAGE, AST_STREAM_STATE_SENDRECV, "t38", NULL
};


static const struct sdp_topology_stream *top_ulaw_alaw_h264__vp8[] = {
	&audio_ulaw_no_name,
	&audio_alaw_no_name,
	&video_h264_no_name,
	&video_vp8_no_name,
	NULL
};

static const struct sdp_topology_stream *top__vp8_alaw_h264_ulaw[] = {
	&video_vp8_no_name,
	&audio_alaw_no_name,
	&video_h264_no_name,
	&audio_ulaw_no_name,
	NULL
};

static const struct sdp_topology_stream *top_alaw_ulaw__vp8_h264[] = {
	&audio_alaw_no_name,
	&audio_ulaw_no_name,
	&video_vp8_no_name,
	&video_h264_no_name,
	NULL
};

/* Sorting by type with no new or deleted streams */
static const struct sdp_update_test mrg_by_type_00 = {
	.initial  = top_ulaw_alaw_h264__vp8,
	.update_1 = top__vp8_alaw_h264_ulaw,
	.expected = top_alaw_ulaw__vp8_h264,
};


static const struct sdp_topology_stream *top_alaw__vp8[] = {
	&audio_alaw_no_name,
	&video_vp8_no_name,
	NULL
};

static const struct sdp_topology_stream *top_h264__vp8_ulaw[] = {
	&video_h264_no_name,
	&video_vp8_no_name,
	&audio_ulaw_no_name,
	NULL
};

static const struct sdp_topology_stream *top_ulaw_h264__vp8[] = {
	&audio_ulaw_no_name,
	&video_h264_no_name,
	&video_vp8_no_name,
	NULL
};

/* Sorting by type and adding a stream */
static const struct sdp_update_test mrg_by_type_01 = {
	.initial  = top_alaw__vp8,
	.update_1 = top_h264__vp8_ulaw,
	.expected = top_ulaw_h264__vp8,
};


static const struct sdp_topology_stream *top_alaw__vp8_vdec[] = {
	&audio_alaw_no_name,
	&video_vp8_no_name,
	&video_declined_no_name,
	NULL
};

/* Sorting by type and deleting a stream */
static const struct sdp_update_test mrg_by_type_02 = {
	.initial  = top_ulaw_h264__vp8,
	.update_1 = top_alaw__vp8,
	.expected = top_alaw__vp8_vdec,
};


static const struct sdp_topology_stream *top_h264_alaw_ulaw[] = {
	&video_h264_no_name,
	&audio_alaw_no_name,
	&audio_ulaw_no_name,
	NULL
};

static const struct sdp_topology_stream *top__t38[] = {
	&image_t38_no_name,
	NULL
};

static const struct sdp_topology_stream *top_vdec__t38_adec[] = {
	&video_declined_no_name,
	&image_t38_no_name,
	&audio_declined_no_name,
	NULL
};

/* Sorting by type changing stream types for T.38 */
static const struct sdp_update_test mrg_by_type_03 = {
	.initial  = top_h264_alaw_ulaw,
	.update_1 = top__t38,
	.expected = top_vdec__t38_adec,
};


/* Sorting by type changing stream types back from T.38 */
static const struct sdp_update_test mrg_by_type_04 = {
	.initial  = top_vdec__t38_adec,
	.update_1 = top_h264_alaw_ulaw,
	.expected = top_h264_alaw_ulaw,
};


static const struct sdp_topology_stream *top_h264[] = {
	&video_h264_no_name,
	NULL
};

static const struct sdp_topology_stream *top_vdec__t38[] = {
	&video_declined_no_name,
	&image_t38_no_name,
	NULL
};

/* Sorting by type changing stream types for T.38 */
static const struct sdp_update_test mrg_by_type_05 = {
	.initial  = top_h264,
	.update_1 = top__t38,
	.expected = top_vdec__t38,
};


static const struct sdp_topology_stream *top_h264_idec[] = {
	&video_h264_no_name,
	&image_declined_no_name,
	NULL
};

/* Sorting by type changing stream types back from T.38 */
static const struct sdp_update_test mrg_by_type_06 = {
	.initial  = top_vdec__t38,
	.update_1 = top_h264,
	.expected = top_h264_idec,
};


static const struct sdp_topology_stream *top_ulaw_adec_h264__vp8[] = {
	&audio_ulaw_no_name,
	&audio_declined_no_name,
	&video_h264_no_name,
	&video_vp8_no_name,
	NULL
};

static const struct sdp_topology_stream *top_h263_alaw_h261_h264_vp8[] = {
	&video_h263_no_name,
	&audio_alaw_no_name,
	&video_h261_no_name,
	&video_h264_no_name,
	&video_vp8_no_name,
	NULL
};

static const struct sdp_topology_stream *top_alaw_h264_h263_h261_vp8[] = {
	&audio_alaw_no_name,
	&video_h264_no_name,
	&video_h263_no_name,
	&video_h261_no_name,
	&video_vp8_no_name,
	NULL
};

/* Sorting by type with backfill and adding streams */
static const struct sdp_update_test mrg_by_type_07 = {
	.initial  = top_ulaw_adec_h264__vp8,
	.update_1 = top_h263_alaw_h261_h264_vp8,
	.expected = top_alaw_h264_h263_h261_vp8,
};


static const struct sdp_topology_stream *top_ulaw_alaw_h264__vp8_h261[] = {
	&audio_ulaw_no_name,
	&audio_alaw_no_name,
	&video_h264_no_name,
	&video_vp8_no_name,
	&video_h261_no_name,
	NULL
};

/* Sorting by type overlimit of 4 and drop */
static const struct sdp_update_test mrg_by_type_08 = {
	.max_streams = 4,
	.initial  = top_ulaw_alaw_h264__vp8,
	.update_1 = top_ulaw_alaw_h264__vp8_h261,
	.expected = top_ulaw_alaw_h264__vp8,
};


static const struct sdp_topology_stream *top_ulaw_alaw_h264[] = {
	&audio_ulaw_no_name,
	&audio_alaw_no_name,
	&video_h264_no_name,
	NULL
};

static const struct sdp_topology_stream *top_alaw_h261__vp8[] = {
	&audio_alaw_no_name,
	&video_h261_no_name,
	&video_vp8_no_name,
	NULL
};

static const struct sdp_topology_stream *top_alaw_adec_h261__vp8[] = {
	&audio_alaw_no_name,
	&audio_declined_no_name,
	&video_h261_no_name,
	&video_vp8_no_name,
	NULL
};

/* Sorting by type with delete and add of streams */
static const struct sdp_update_test mrg_by_type_09 = {
	.initial  = top_ulaw_alaw_h264,
	.update_1 = top_alaw_h261__vp8,
	.expected = top_alaw_adec_h261__vp8,
};


static const struct sdp_topology_stream *top_ulaw_adec_h264[] = {
	&audio_ulaw_no_name,
	&audio_declined_no_name,
	&video_h264_no_name,
	NULL
};

/* Sorting by type and adding streams */
static const struct sdp_update_test mrg_by_type_10 = {
	.initial  = top_ulaw_adec_h264,
	.update_1 = top_alaw_ulaw__vp8_h264,
	.expected = top_alaw_ulaw__vp8_h264,
};


static const struct sdp_topology_stream *top_adec_g722_h261[] = {
	&audio_declined_no_name,
	&audio_g722_no_name,
	&video_h261_no_name,
	NULL
};

/* Sorting by type and deleting old streams */
static const struct sdp_update_test mrg_by_type_11 = {
	.initial  = top_ulaw_alaw_h264,
	.update_1 = top_adec_g722_h261,
	.expected = top_adec_g722_h261,
};


static const struct sdp_topology_stream audio_alaw4dave = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "alaw", "dave"
};

static const struct sdp_topology_stream audio_g7224dave = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "g722", "dave"
};

static const struct sdp_topology_stream audio_ulaw4fred = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "ulaw", "fred"
};

static const struct sdp_topology_stream audio_alaw4fred = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "alaw", "fred"
};

static const struct sdp_topology_stream audio_ulaw4rose = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "ulaw", "rose"
};

static const struct sdp_topology_stream audio_g7224rose = {
	AST_MEDIA_TYPE_AUDIO, AST_STREAM_STATE_SENDRECV, "g722", "rose"
};


static const struct sdp_topology_stream video_h2614dave = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h261", "dave"
};

static const struct sdp_topology_stream video_h2634dave = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h263", "dave"
};

static const struct sdp_topology_stream video_h2634fred = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h263", "fred"
};

static const struct sdp_topology_stream video_h2644fred = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h264", "fred"
};

static const struct sdp_topology_stream video_h2644rose = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h264", "rose"
};

static const struct sdp_topology_stream video_h2614rose = {
	AST_MEDIA_TYPE_VIDEO, AST_STREAM_STATE_SENDRECV, "h261", "rose"
};


static const struct sdp_topology_stream *top_adave_alaw_afred_ulaw_arose_g722_vdave_h261_vfred_h263_vrose_h264[] = {
	&audio_alaw4dave,
	&audio_alaw_no_name,
	&audio_ulaw4fred,
	&audio_ulaw_no_name,
	&audio_g7224rose,
	&audio_g722_no_name,
	&video_h2614dave,
	&video_h261_no_name,
	&video_h2634fred,
	&video_h263_no_name,
	&video_h2644rose,
	&video_h264_no_name,
	NULL
};

static const struct sdp_topology_stream *top_vfred_vrose_vdave_h263_h264_h261_afred_ulaw_arose_g722_adave_alaw[] = {
	&video_h2644fred,
	&video_h2614rose,
	&video_h2634dave,
	&video_h263_no_name,
	&video_h264_no_name,
	&video_h261_no_name,
	&audio_alaw4fred,
	&audio_ulaw_no_name,
	&audio_ulaw4rose,
	&audio_g722_no_name,
	&audio_g7224dave,
	&audio_alaw_no_name,
	NULL
};

static const struct sdp_topology_stream *top_adave_ulaw_afred_g722_arose_alaw_vdave_h263_vfred_h264_vrose_h261[] = {
	&audio_g7224dave,
	&audio_ulaw_no_name,
	&audio_alaw4fred,
	&audio_g722_no_name,
	&audio_ulaw4rose,
	&audio_alaw_no_name,
	&video_h2634dave,
	&video_h263_no_name,
	&video_h2644fred,
	&video_h264_no_name,
	&video_h2614rose,
	&video_h261_no_name,
	NULL
};

/* Sorting by name and type with no new or deleted streams */
static const struct sdp_update_test mrg_by_name_00 = {
	.initial  = top_adave_alaw_afred_ulaw_arose_g722_vdave_h261_vfred_h263_vrose_h264,
	.update_1 = top_vfred_vrose_vdave_h263_h264_h261_afred_ulaw_arose_g722_adave_alaw,
	.expected = top_adave_ulaw_afred_g722_arose_alaw_vdave_h263_vfred_h264_vrose_h261,
};


static const struct sdp_topology_stream *top_adave_g723_h261[] = {
	&audio_g7224dave,
	&audio_g723_no_name,
	&video_h261_no_name,
	NULL
};

/* Sorting by name and type adding names to streams */
static const struct sdp_update_test mrg_by_name_01 = {
	.initial  = top_ulaw_alaw_h264,
	.update_1 = top_adave_g723_h261,
	.expected = top_adave_g723_h261,
};


/* Sorting by name and type removing names from streams */
static const struct sdp_update_test mrg_by_name_02 = {
	.initial  = top_adave_g723_h261,
	.update_1 = top_ulaw_alaw_h264,
	.expected = top_ulaw_alaw_h264,
};


static const struct sdp_update_test *sdp_update_cases[] = {
	/* Merging by type */
	/* 00 */ &mrg_by_type_00,
	/* 01 */ &mrg_by_type_01,
	/* 02 */ &mrg_by_type_02,
	/* 03 */ &mrg_by_type_03,
	/* 04 */ &mrg_by_type_04,
	/* 05 */ &mrg_by_type_05,
	/* 06 */ &mrg_by_type_06,
	/* 07 */ &mrg_by_type_07,
	/* 08 */ &mrg_by_type_08,
	/* 09 */ &mrg_by_type_09,
	/* 10 */ &mrg_by_type_10,
	/* 11 */ &mrg_by_type_11,

	/* Merging by name and type */
	/* 12 */ &mrg_by_name_00,
	/* 13 */ &mrg_by_name_01,
	/* 14 */ &mrg_by_name_02,
};

AST_TEST_DEFINE(sdp_update_topology)
{
	enum ast_test_result_state res;
	unsigned int idx;
	int status;
	struct ast_sdp_options *options;
	struct ast_stream_topology *topology;
	struct ast_sdp_state *test_state = NULL;

	static const struct sdp_format sdp_formats[] = {
		{ AST_MEDIA_TYPE_AUDIO, "ulaw,alaw,g722,g723" },
		{ AST_MEDIA_TYPE_VIDEO, "h261,h263,h264,vp8" },
		{ AST_MEDIA_TYPE_IMAGE, "t38" },
	};

	switch(cmd) {
	case TEST_INIT:
		info->name = "sdp_update_topology";
		info->category = "/main/sdp/";
		info->summary = "Merge topology updates from the system";
		info->description =
			"1) Create a SDP state with an optional initial topology.\n"
			"2) Update the initial topology with one or two new topologies.\n"
			"3) Get the SDP offer to merge the updates into the initial topology.\n"
			"4) Check that the offered topology matches the expected topology.\n"
			"5) Repeat these steps for each test case defined.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	res = AST_TEST_FAIL;
	for (idx = 0; idx < ARRAY_LEN(sdp_update_cases); ++idx) {
		ast_test_status_update(test, "Starting update case %d\n", idx);

		/* Create a SDP state with an optional initial topology. */
		options = sdp_options_common();
		if (!options) {
			ast_test_status_update(test, "Failed to allocate SDP options\n");
			goto end;
		}
		if (sdp_update_cases[idx]->max_streams) {
			ast_sdp_options_set_max_streams(options, sdp_update_cases[idx]->max_streams);
		}
		if (build_sdp_option_formats(options, ARRAY_LEN(sdp_formats), sdp_formats)) {
			ast_test_status_update(test, "Failed to setup SDP options new stream formats\n");
			goto end;
		}
		if (sdp_update_cases[idx]->initial) {
			topology = build_update_topology(sdp_update_cases[idx]->initial);
			if (!topology) {
				ast_test_status_update(test, "Failed to build initial SDP state topology\n");
				goto end;
			}
		} else {
			topology = NULL;
		}
		test_state = ast_sdp_state_alloc(topology, options);
		ast_stream_topology_free(topology);
		if (!test_state) {
			ast_test_status_update(test, "Failed to build SDP state\n");
			goto end;
		}

		/* Update the initial topology with one or two new topologies. */
		topology = build_update_topology(sdp_update_cases[idx]->update_1);
		if (!topology) {
			ast_test_status_update(test, "Failed to build first update SDP state topology\n");
			goto end;
		}
		status = ast_sdp_state_update_local_topology(test_state, topology);
		ast_stream_topology_free(topology);
		if (status) {
			ast_test_status_update(test, "Failed to update first update SDP state topology\n");
			goto end;
		}
		if (sdp_update_cases[idx]->update_2) {
			topology = build_update_topology(sdp_update_cases[idx]->update_2);
			if (!topology) {
				ast_test_status_update(test, "Failed to build second update SDP state topology\n");
				goto end;
			}
			status = ast_sdp_state_update_local_topology(test_state, topology);
			ast_stream_topology_free(topology);
			if (status) {
				ast_test_status_update(test, "Failed to update second update SDP state topology\n");
				goto end;
			}
		}

		/* Get the SDP offer to merge the updates into the initial topology. */
		if (!ast_sdp_state_get_local_sdp(test_state)) {
			ast_test_status_update(test, "Failed to create offer SDP\n");
			goto end;
		}

		/* Check that the offered topology matches the expected topology. */
		topology = build_update_topology(sdp_update_cases[idx]->expected);
		if (!topology) {
			ast_test_status_update(test, "Failed to build expected topology\n");
			goto end;
		}
		status = cmp_update_topology(test, topology,
			ast_sdp_state_get_local_topology(test_state));
		ast_stream_topology_free(topology);
		if (status) {
			ast_test_status_update(test, "Failed to match expected topology\n");
			goto end;
		}

		/* Repeat for each test case defined. */
		ast_sdp_state_free(test_state);
		test_state = NULL;
	}
	res = AST_TEST_PASS;

end:
	ast_sdp_state_free(test_state);
	return res;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(invalid_rtpmap);
	AST_TEST_UNREGISTER(rtpmap);
	AST_TEST_UNREGISTER(find_attr);
	AST_TEST_UNREGISTER(topology_to_sdp);
	AST_TEST_UNREGISTER(sdp_to_topology);
	AST_TEST_UNREGISTER(sdp_negotiation_initial);
	AST_TEST_UNREGISTER(sdp_negotiation_type_change);
	AST_TEST_UNREGISTER(sdp_negotiation_decline_incompatible);
	AST_TEST_UNREGISTER(sdp_negotiation_decline_max_streams);
	AST_TEST_UNREGISTER(sdp_negotiation_not_acceptable);
	AST_TEST_UNREGISTER(sdp_ssrc_attributes);
	AST_TEST_UNREGISTER(sdp_update_topology);

	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(invalid_rtpmap);
	AST_TEST_REGISTER(rtpmap);
	AST_TEST_REGISTER(find_attr);
	AST_TEST_REGISTER(topology_to_sdp);
	AST_TEST_REGISTER(sdp_to_topology);
	AST_TEST_REGISTER(sdp_negotiation_initial);
	AST_TEST_REGISTER(sdp_negotiation_type_change);
	AST_TEST_REGISTER(sdp_negotiation_decline_incompatible);
	AST_TEST_REGISTER(sdp_negotiation_decline_max_streams);
	AST_TEST_REGISTER(sdp_negotiation_not_acceptable);
	AST_TEST_REGISTER(sdp_ssrc_attributes);
	AST_TEST_REGISTER(sdp_update_topology);

	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SDP tests");