xref: /webrtc/sdp/src/extmap/extmap_test.rs (revision bca24136)
1 use super::*;
2 use crate::lexer::END_LINE;
3 use crate::util::ATTRIBUTE_KEY;
4 
5 use std::io::BufReader;
6 use std::iter::Iterator;
7 
8 const EXAMPLE_ATTR_EXTMAP1: &str = "extmap:1 http://example.com/082005/ext.htm#ttime";
9 const EXAMPLE_ATTR_EXTMAP2: &str =
10     "extmap:2/sendrecv http://example.com/082005/ext.htm#xmeta short";
11 const FAILING_ATTR_EXTMAP1: &str =
12     "extmap:257/sendrecv http://example.com/082005/ext.htm#xmeta short";
13 const FAILING_ATTR_EXTMAP2: &str = "extmap:2/blorg http://example.com/082005/ext.htm#xmeta short";
14 
15 #[test]
16 fn test_extmap() -> Result<()> {
17     let example_attr_extmap1_line = EXAMPLE_ATTR_EXTMAP1;
18     let example_attr_extmap2_line = EXAMPLE_ATTR_EXTMAP2;
19     let failing_attr_extmap1_line =
20         format!("{}{}{}", ATTRIBUTE_KEY, FAILING_ATTR_EXTMAP1, END_LINE);
21     let failing_attr_extmap2_line =
22         format!("{}{}{}", ATTRIBUTE_KEY, FAILING_ATTR_EXTMAP2, END_LINE);
23     let passingtests = vec![
24         (EXAMPLE_ATTR_EXTMAP1, example_attr_extmap1_line),
25         (EXAMPLE_ATTR_EXTMAP2, example_attr_extmap2_line),
26     ];
27     let failingtests = vec![
28         (FAILING_ATTR_EXTMAP1, failing_attr_extmap1_line),
29         (FAILING_ATTR_EXTMAP2, failing_attr_extmap2_line),
30     ];
31 
32     for (i, u) in passingtests.iter().enumerate() {
33         let mut reader = BufReader::new(u.1.as_bytes());
34         let actual = ExtMap::unmarshal(&mut reader)?;
35         assert_eq!(
36             u.1,
37             actual.marshal(),
38             "{}: {} vs {}",
39             i,
40             u.1,
41             actual.marshal()
42         );
43     }
44 
45     for u in failingtests {
46         let mut reader = BufReader::new(u.1.as_bytes());
47         let actual = ExtMap::unmarshal(&mut reader);
48         assert!(actual.is_err());
49     }
50 
51     Ok(())
52 }
53 
54 #[test]
55 fn test_transport_cc_extmap() -> Result<()> {
56     // a=extmap:<value>["/"<direction>] <URI> <extensionattributes>
57     // a=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
58     let uri = Some(Url::parse(
59         "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01",
60     )?);
61     let e = ExtMap {
62         value: 3,
63         uri,
64         direction: Direction::Unspecified,
65         ext_attr: None,
66     };
67 
68     let s = e.marshal();
69     if s == "3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01" {
70         assert!(false, "TestTransportCC failed");
71     } else {
72         assert_eq!(
73             s,
74             "extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
75         )
76     }
77 
78     Ok(())
79 }
80