xref: /webrtc/ice/src/url/url_test.rs (revision 5d8fe953)
1 use super::*;
2 
3 #[test]
test_parse_url_success() -> Result<()>4 fn test_parse_url_success() -> Result<()> {
5     let tests = vec![
6         (
7             "stun:google.de",
8             "stun:google.de:3478",
9             SchemeType::Stun,
10             false,
11             "google.de",
12             3478,
13             ProtoType::Udp,
14         ),
15         (
16             "stun:google.de:1234",
17             "stun:google.de:1234",
18             SchemeType::Stun,
19             false,
20             "google.de",
21             1234,
22             ProtoType::Udp,
23         ),
24         (
25             "stuns:google.de",
26             "stuns:google.de:5349",
27             SchemeType::Stuns,
28             true,
29             "google.de",
30             5349,
31             ProtoType::Tcp,
32         ),
33         (
34             "stun:[::1]:123",
35             "stun:[::1]:123",
36             SchemeType::Stun,
37             false,
38             "::1",
39             123,
40             ProtoType::Udp,
41         ),
42         (
43             "turn:google.de",
44             "turn:google.de:3478?transport=udp",
45             SchemeType::Turn,
46             false,
47             "google.de",
48             3478,
49             ProtoType::Udp,
50         ),
51         (
52             "turns:google.de",
53             "turns:google.de:5349?transport=tcp",
54             SchemeType::Turns,
55             true,
56             "google.de",
57             5349,
58             ProtoType::Tcp,
59         ),
60         (
61             "turn:google.de?transport=udp",
62             "turn:google.de:3478?transport=udp",
63             SchemeType::Turn,
64             false,
65             "google.de",
66             3478,
67             ProtoType::Udp,
68         ),
69         (
70             "turns:google.de?transport=tcp",
71             "turns:google.de:5349?transport=tcp",
72             SchemeType::Turns,
73             true,
74             "google.de",
75             5349,
76             ProtoType::Tcp,
77         ),
78     ];
79 
80     for (
81         raw_url,
82         expected_url_string,
83         expected_scheme,
84         expected_secure,
85         expected_host,
86         expected_port,
87         expected_proto,
88     ) in tests
89     {
90         let url = Url::parse_url(raw_url)?;
91 
92         assert_eq!(url.scheme, expected_scheme, "testCase: {raw_url:?}");
93         assert_eq!(
94             expected_url_string,
95             url.to_string(),
96             "testCase: {raw_url:?}"
97         );
98         assert_eq!(url.is_secure(), expected_secure, "testCase: {raw_url:?}");
99         assert_eq!(url.host, expected_host, "testCase: {raw_url:?}");
100         assert_eq!(url.port, expected_port, "testCase: {raw_url:?}");
101         assert_eq!(url.proto, expected_proto, "testCase: {raw_url:?}");
102     }
103 
104     Ok(())
105 }
106 
107 #[test]
test_parse_url_failure() -> Result<()>108 fn test_parse_url_failure() -> Result<()> {
109     let tests = vec![
110         ("", Error::ErrSchemeType),
111         (":::", Error::ErrUrlParse),
112         ("stun:[::1]:123:", Error::ErrPort),
113         ("stun:[::1]:123a", Error::ErrPort),
114         ("google.de", Error::ErrSchemeType),
115         ("stun:", Error::ErrHost),
116         ("stun:google.de:abc", Error::ErrPort),
117         ("stun:google.de?transport=udp", Error::ErrStunQuery),
118         ("stuns:google.de?transport=udp", Error::ErrStunQuery),
119         ("turn:google.de?trans=udp", Error::ErrInvalidQuery),
120         ("turns:google.de?trans=udp", Error::ErrInvalidQuery),
121         (
122             "turns:google.de?transport=udp&another=1",
123             Error::ErrInvalidQuery,
124         ),
125         ("turn:google.de?transport=ip", Error::ErrProtoType),
126     ];
127 
128     for (raw_url, expected_err) in tests {
129         let result = Url::parse_url(raw_url);
130         if let Err(err) = result {
131             assert_eq!(
132                 err.to_string(),
133                 expected_err.to_string(),
134                 "testCase: '{raw_url}', expected err '{expected_err}', but got err '{err}'"
135             );
136         } else {
137             panic!("expected error, but got ok");
138         }
139     }
140 
141     Ok(())
142 }
143