| 1 | use tester as test; |
| 2 | |
| 3 | #[macro_use] |
| 4 | extern crate serde; |
| 5 | |
| 6 | fn run_data_url( |
| 7 | input: String, |
| 8 | expected_mime: Option<String>, |
| 9 | expected_body: Option<Vec<u8>>, |
| 10 | expected_panic: bool, |
| 11 | ) { |
| 12 | let priorhook = std::panic::take_hook(); |
| 13 | std::panic::set_hook(Box::new(move |p| { |
| 14 | if !expected_panic { |
| 15 | priorhook(p); |
| 16 | } |
| 17 | })); |
| 18 | let url = data_url::DataUrl::process(&input); |
| 19 | if let Some(expected_mime) = expected_mime { |
| 20 | let url = url.unwrap(); |
| 21 | let (body, _) = url.decode_to_vec().unwrap(); |
| 22 | if expected_mime.is_empty() { |
| 23 | assert_eq!(url.mime_type().to_string(), "text/plain;charset=US-ASCII") |
| 24 | } else { |
| 25 | assert_eq!(url.mime_type().to_string(), expected_mime) |
| 26 | } |
| 27 | if let Some(expected_body) = expected_body { |
| 28 | assert_eq!(body, expected_body) |
| 29 | } |
| 30 | } else if let Ok(url) = url { |
| 31 | assert!(url.decode_to_vec().is_err(), "{:?}", url.mime_type()) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn collect_data_url<F>(add_test: &mut F) |
| 36 | where |
| 37 | F: FnMut(String, bool, test::TestFn), |
| 38 | { |
| 39 | let known_failures = ["data://test:test/,X"]; |
| 40 | |
| 41 | #[derive(Deserialize)] |
| 42 | #[serde(untagged)] |
| 43 | enum TestCase { |
| 44 | Two(String, Option<String>), |
| 45 | Three(String, Option<String>, Vec<u8>), |
| 46 | } |
| 47 | |
| 48 | let v: Vec<TestCase> = serde_json::from_str(include_str!("data-urls.json")).unwrap(); |
| 49 | for test in v { |
| 50 | let (input, expected_mime, expected_body) = match test { |
| 51 | TestCase::Two(i, m) => (i, m, None), |
| 52 | TestCase::Three(i, m, b) => (i, m, Some(b)), |
| 53 | }; |
| 54 | let should_panic = known_failures.contains(&&*input); |
| 55 | add_test( |
| 56 | format!("data: URL {:?}", input), |
| 57 | should_panic, |
| 58 | test::TestFn::DynTestFn(Box::new(move || { |
| 59 | run_data_url(input, expected_mime, expected_body, should_panic) |
| 60 | })), |
| 61 | ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn run_base64(input: String, expected: Option<Vec<u8>>) { |
| 66 | let result = data_url::forgiving_base64::decode_to_vec(input.as_bytes()); |
| 67 | match (result, expected) { |
| 68 | (Ok(bytes), Some(expected)) => assert_eq!(bytes, expected), |
| 69 | (Ok(bytes), None) => panic!("Expected error, got {:?}", bytes), |
| 70 | (Err(e), Some(expected)) => panic!("Expected {:?}, got error {:?}", expected, e), |
| 71 | (Err(_), None) => {} |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn collect_base64<F>(add_test: &mut F) |
| 76 | where |
| 77 | F: FnMut(String, bool, test::TestFn), |
| 78 | { |
| 79 | let known_failures = []; |
| 80 | |
| 81 | let v: Vec<(String, Option<Vec<u8>>)> = |
| 82 | serde_json::from_str(include_str!("base64.json")).unwrap(); |
| 83 | for (input, expected) in v { |
| 84 | let should_panic = known_failures.contains(&&*input); |
| 85 | add_test( |
| 86 | format!("base64 {:?}", input), |
| 87 | should_panic, |
| 88 | test::TestFn::DynTestFn(Box::new(move || run_base64(input, expected))), |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | fn run_mime(input: String, expected: Option<String>) { |
| 94 | let result = input.parse::<data_url::mime::Mime>(); |
| 95 | match (result, expected) { |
| 96 | (Ok(mime), Some(expected)) => assert_eq!(mime.to_string(), expected), |
| 97 | (Ok(mime), None) => panic!("Expected error, got {:?}", mime), |
| 98 | (Err(e), Some(expected)) => panic!("Expected {:?}, got error {:?}", expected, e), |
| 99 | (Err(_), None) => {} |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn collect_mime<F>(add_test: &mut F) |
| 104 | where |
| 105 | F: FnMut(String, bool, test::TestFn), |
| 106 | { |
| 107 | let known_failures = []; |
| 108 | |
| 109 | #[derive(Deserialize)] |
| 110 | #[serde(untagged)] |
| 111 | enum Entry { |
| 112 | Comment(String), |
| 113 | TestCase { |
| 114 | input: String, |
| 115 | output: Option<String>, |
| 116 | }, |
| 117 | } |
| 118 | |
| 119 | let v: Vec<Entry> = serde_json::from_str(include_str!("mime-types.json")).unwrap(); |
| 120 | let v2: Vec<Entry> = serde_json::from_str(include_str!("generated-mime-types.json")).unwrap(); |
| 121 | let entries = v.into_iter().chain(v2); |
| 122 | |
| 123 | let mut last_comment = None; |
| 124 | for entry in entries { |
| 125 | let (input, expected) = match entry { |
| 126 | Entry::TestCase { input, output } => (input, output), |
| 127 | Entry::Comment(s) => { |
| 128 | last_comment = Some(s); |
| 129 | continue; |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | let should_panic = known_failures.contains(&&*input); |
| 134 | add_test( |
| 135 | if let Some(ref s) = last_comment { |
| 136 | format!("MIME type {:?} {:?}", s, input) |
| 137 | } else { |
| 138 | format!("MIME type {:?}", input) |
| 139 | }, |
| 140 | should_panic, |
| 141 | test::TestFn::DynTestFn(Box::new(move || run_mime(input, expected))), |
| 142 | ); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fn main() { |
| 147 | let mut tests = Vec::new(); |
| 148 | { |
| 149 | let mut add_one = |name: String, should_panic: bool, run: test::TestFn| { |
| 150 | let desc = test::TestDesc { |
| 151 | name: test::DynTestName(name), |
| 152 | ignore: false, |
| 153 | should_panic: match should_panic { |
| 154 | true => test::ShouldPanic::Yes, |
| 155 | false => test::ShouldPanic::No, |
| 156 | }, |
| 157 | allow_fail: false, |
| 158 | test_type: test::TestType::Unknown, |
| 159 | }; |
| 160 | tests.push(test::TestDescAndFn { desc, testfn: run }) |
| 161 | }; |
| 162 | collect_data_url(&mut add_one); |
| 163 | collect_base64(&mut add_one); |
| 164 | collect_mime(&mut add_one); |
| 165 | } |
| 166 | test::test_main(&std::env::args().collect::<Vec<_>>(), tests, None) |
| 167 | } |