1 // Copyright 2013-2014 The rust-url developers.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 //! Data-driven tests
10 
11 use std::str::FromStr;
12 
13 use serde_json::Value;
14 use url::{quirks, Url};
15 
16 #[test]
urltestdata()17 fn urltestdata() {
18     let idna_skip_inputs = [
19         "http://www.foo。bar.com",
20         "http://Go.com",
21         "http://你好你好",
22         "https://faß.ExAmPlE/",
23         "http://0Xc0.0250.01",
24         "ftp://%e2%98%83",
25         "https://%e2%98%83",
26         "file://a\u{ad}b/p",
27         "file://a%C2%ADb/p",
28         "http://GOO\u{200b}\u{2060}\u{feff}goo.com",
29     ];
30 
31     // Copied from https://github.com/web-platform-tests/wpt/blob/master/url/
32     let mut json = Value::from_str(include_str!("urltestdata.json"))
33         .expect("JSON parse error in urltestdata.json");
34 
35     let mut passed = true;
36     for entry in json.as_array_mut().unwrap() {
37         if entry.is_string() {
38             continue; // ignore comments
39         }
40 
41         let maybe_base = entry
42             .take_key("base")
43             .expect("missing base key")
44             .maybe_string();
45         let input = entry.take_string("input");
46         let failure = entry.take_key("failure").is_some();
47 
48         {
49             if idna_skip_inputs.contains(&input.as_str()) {
50                 continue;
51             }
52         }
53 
54         let res = if let Some(base) = maybe_base {
55             let base = match Url::parse(&base) {
56                 Ok(base) => base,
57                 Err(_) if failure => continue,
58                 Err(message) => {
59                     eprint_failure(
60                         format!("  failed: error parsing base {:?}: {}", base, message),
61                         &format!("parse base for {:?}", input),
62                         None,
63                     );
64                     passed = false;
65                     continue;
66                 }
67             };
68             base.join(&input)
69         } else {
70             Url::parse(&input)
71         };
72 
73         let url = match (res, failure) {
74             (Ok(url), false) => url,
75             (Err(_), true) => continue,
76             (Err(message), false) => {
77                 eprint_failure(
78                     format!("  failed: {}", message),
79                     &format!("parse URL for {:?}", input),
80                     None,
81                 );
82                 passed = false;
83                 continue;
84             }
85             (Ok(_), true) => {
86                 eprint_failure(
87                     format!("  failed: expected parse error for URL {:?}", input),
88                     &format!("parse URL for {:?}", input),
89                     None,
90                 );
91                 passed = false;
92                 continue;
93             }
94         };
95 
96         passed &= check_invariants(&url, &format!("invariants for {:?}", input), None);
97 
98         for &attr in ATTRIBS {
99             passed &= test_eq_eprint(
100                 entry.take_string(attr),
101                 get(&url, attr),
102                 &format!("{:?} - {}", input, attr),
103                 None,
104             );
105         }
106 
107         if let Some(expected_origin) = entry.take_key("origin").map(|s| s.string()) {
108             passed &= test_eq_eprint(
109                 expected_origin,
110                 &quirks::origin(&url),
111                 &format!("origin for {:?}", input),
112                 None,
113             );
114         }
115     }
116 
117     assert!(passed)
118 }
119 
120 #[test]
setters_tests()121 fn setters_tests() {
122     let mut json = Value::from_str(include_str!("setters_tests.json"))
123         .expect("JSON parse error in setters_tests.json");
124 
125     let mut passed = true;
126     for &attr in ATTRIBS {
127         if attr == "href" {
128             continue;
129         }
130 
131         let mut tests = json.take_key(attr).unwrap();
132         for mut test in tests.as_array_mut().unwrap().drain(..) {
133             let comment = test.take_key("comment").map(|s| s.string());
134             {
135                 if let Some(comment) = comment.as_ref() {
136                     if comment.starts_with("IDNA Nontransitional_Processing") {
137                         continue;
138                     }
139                 }
140             }
141             let href = test.take_string("href");
142             let new_value = test.take_string("new_value");
143             let name = format!("{:?}.{} = {:?}", href, attr, new_value);
144             let mut expected = test.take_key("expected").unwrap();
145 
146             let mut url = Url::parse(&href).unwrap();
147             let comment_ref = comment.as_deref();
148             passed &= check_invariants(&url, &name, comment_ref);
149             set(&mut url, attr, &new_value);
150 
151             for attr in ATTRIBS {
152                 if let Some(value) = expected.take_key(attr) {
153                     passed &= test_eq_eprint(value.string(), get(&url, attr), &name, comment_ref);
154                 };
155             }
156 
157             passed &= check_invariants(&url, &name, comment_ref);
158         }
159     }
160 
161     assert!(passed);
162 }
163 
check_invariants(url: &Url, name: &str, comment: Option<&str>) -> bool164 fn check_invariants(url: &Url, name: &str, comment: Option<&str>) -> bool {
165     let mut passed = true;
166     if let Err(e) = url.check_invariants() {
167         passed = false;
168         eprint_failure(
169             format!("  failed: invariants checked -> {:?}", e),
170             name,
171             comment,
172         );
173     }
174 
175     #[cfg(feature = "serde")]
176     {
177         let bytes = serde_json::to_vec(url).unwrap();
178         let new_url: Url = serde_json::from_slice(&bytes).unwrap();
179         passed &= test_eq_eprint(url.to_string(), &new_url.to_string(), name, comment);
180     }
181 
182     passed
183 }
184 
185 trait JsonExt {
take_key(&mut self, key: &str) -> Option<Value>186     fn take_key(&mut self, key: &str) -> Option<Value>;
string(self) -> String187     fn string(self) -> String;
maybe_string(self) -> Option<String>188     fn maybe_string(self) -> Option<String>;
take_string(&mut self, key: &str) -> String189     fn take_string(&mut self, key: &str) -> String;
190 }
191 
192 impl JsonExt for Value {
take_key(&mut self, key: &str) -> Option<Value>193     fn take_key(&mut self, key: &str) -> Option<Value> {
194         self.as_object_mut().unwrap().remove(key)
195     }
196 
string(self) -> String197     fn string(self) -> String {
198         self.maybe_string().expect("")
199     }
200 
maybe_string(self) -> Option<String>201     fn maybe_string(self) -> Option<String> {
202         match self {
203             Value::String(s) => Some(s),
204             Value::Null => None,
205             _ => panic!("Not a Value::String or Value::Null"),
206         }
207     }
208 
take_string(&mut self, key: &str) -> String209     fn take_string(&mut self, key: &str) -> String {
210         self.take_key(key).unwrap().string()
211     }
212 }
213 
get<'a>(url: &'a Url, attr: &str) -> &'a str214 fn get<'a>(url: &'a Url, attr: &str) -> &'a str {
215     match attr {
216         "href" => quirks::href(url),
217         "protocol" => quirks::protocol(url),
218         "username" => quirks::username(url),
219         "password" => quirks::password(url),
220         "hostname" => quirks::hostname(url),
221         "host" => quirks::host(url),
222         "port" => quirks::port(url),
223         "pathname" => quirks::pathname(url),
224         "search" => quirks::search(url),
225         "hash" => quirks::hash(url),
226         _ => unreachable!(),
227     }
228 }
229 
230 #[allow(clippy::unit_arg)]
set<'a>(url: &'a mut Url, attr: &str, new: &str)231 fn set<'a>(url: &'a mut Url, attr: &str, new: &str) {
232     let _ = match attr {
233         "protocol" => quirks::set_protocol(url, new),
234         "username" => quirks::set_username(url, new),
235         "password" => quirks::set_password(url, new),
236         "hostname" => quirks::set_hostname(url, new),
237         "host" => quirks::set_host(url, new),
238         "port" => quirks::set_port(url, new),
239         "pathname" => Ok(quirks::set_pathname(url, new)),
240         "search" => Ok(quirks::set_search(url, new)),
241         "hash" => Ok(quirks::set_hash(url, new)),
242         _ => unreachable!(),
243     };
244 }
245 
test_eq_eprint(expected: String, actual: &str, name: &str, comment: Option<&str>) -> bool246 fn test_eq_eprint(expected: String, actual: &str, name: &str, comment: Option<&str>) -> bool {
247     if expected == actual {
248         return true;
249     }
250     eprint_failure(
251         format!("expected: {}\n  actual: {}", expected, actual),
252         name,
253         comment,
254     );
255     false
256 }
257 
eprint_failure(err: String, name: &str, comment: Option<&str>)258 fn eprint_failure(err: String, name: &str, comment: Option<&str>) {
259     eprintln!("    test: {}\n{}", name, err);
260     if let Some(comment) = comment {
261         eprintln!("{}\n", comment);
262     } else {
263         eprintln!();
264     }
265 }
266 
267 const ATTRIBS: &[&str] = &[
268     "href", "protocol", "username", "password", "host", "hostname", "port", "pathname", "search",
269     "hash",
270 ];
271