OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import "dart:convert"; | 6 import "dart:convert"; |
7 import "dart:typed_data"; | 7 import "dart:typed_data"; |
8 | 8 |
9 main() { | 9 main() { |
10 testMediaType(); | 10 testMediaType(); |
11 | 11 |
12 testRoundTrip(""); | 12 testRoundTrip(""); |
13 testRoundTrip("a"); | 13 testRoundTrip("a"); |
14 testRoundTrip("ab"); | 14 testRoundTrip("ab"); |
15 testRoundTrip("abc"); | 15 testRoundTrip("abc"); |
16 testRoundTrip("abcd"); | 16 testRoundTrip("abcd"); |
17 testRoundTrip("Content with special%25 characters: # ? = % # ? = %"); | 17 testRoundTrip("Content with special%25 characters: # ? = % # ? = %"); |
18 testRoundTrip("blåbærgrød", UTF8); | 18 testRoundTrip("blåbærgrød", UTF8); |
19 testRoundTrip("blåbærgrød", LATIN1); | 19 testRoundTrip("blåbærgrød", LATIN1); |
20 | 20 |
21 testUriEquals("data:,abc?d#e"); | 21 testUriEquals("data:,abc?d"); |
22 testUriEquals("DATA:,ABC?D#E"); | 22 testUriEquals("DATA:,ABC?D"); |
23 testUriEquals("data:,a%20bc?d#e"); | 23 testUriEquals("data:,a%20bc?d"); |
24 testUriEquals("DATA:,A%20BC?D#E"); | 24 testUriEquals("DATA:,A%20BC?D"); |
25 testUriEquals("data:,a%62c?d#e"); | 25 testUriEquals("data:,abc?d%23e"); // # must and will be is escaped. |
26 testUriEquals("DATA:,A%42C?D#E"); | 26 |
| 27 // Test that UriData.uri normalizes path and query. |
27 | 28 |
28 testUtf8Encoding("\u1000\uffff"); | 29 testUtf8Encoding("\u1000\uffff"); |
29 testBytes(); | 30 testBytes(); |
30 testInvalidCharacters(); | 31 testInvalidCharacters(); |
| 32 testNormalization(); |
31 testErrors(); | 33 testErrors(); |
32 } | 34 } |
33 | 35 |
34 void testMediaType() { | 36 void testMediaType() { |
35 for (var mimeType in ["", "text/plain", "text/javascript"]) { | 37 for (var mimeType in ["", "text/plain", "text/javascript"]) { |
36 for (var charset in ["", ";charset=US-ASCII", ";charset=UTF-8"]) { | 38 for (var charset in ["", ";charset=US-ASCII", ";charset=UTF-8"]) { |
37 for (var base64 in ["", ";base64"]) { | 39 for (var base64 in ["", ";base64"]) { |
38 bool isBase64 = base64.isNotEmpty; | 40 bool isBase64 = base64.isNotEmpty; |
39 var text = "data:$mimeType$charset$base64,"; | 41 var text = "data:$mimeType$charset$base64,"; |
40 var uri = UriData.parse(text); | 42 var uri = UriData.parse(text); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 153 |
152 var bytes = new Uint8List(512); | 154 var bytes = new Uint8List(512); |
153 for (int i = 0; i < bytes.length; i++) { | 155 for (int i = 0; i < bytes.length; i++) { |
154 bytes[i] = i; | 156 bytes[i] = i; |
155 } | 157 } |
156 testLists(bytes); | 158 testLists(bytes); |
157 testLists(new List.from(bytes)); | 159 testLists(new List.from(bytes)); |
158 testLists(new List.unmodifiable(bytes)); | 160 testLists(new List.unmodifiable(bytes)); |
159 } | 161 } |
160 | 162 |
| 163 void testNormalization() { |
| 164 // Base-64 normalization. |
| 165 |
| 166 // Normalized URI-alphabet characters. |
| 167 Expect.equals("data:;base64,AA/+", |
| 168 UriData.parse("data:;base64,AA_-").toString()); |
| 169 // Normalized escapes. |
| 170 Expect.equals("data:;base64,AB==", |
| 171 UriData.parse("data:;base64,A%42=%3D").toString()); |
| 172 Expect.equals("data:;base64,/+/+", |
| 173 UriData.parse("data:;base64,%5F%2D%2F%2B").toString()); |
| 174 // Normalized padded data. |
| 175 Expect.equals("data:;base64,AA==", |
| 176 UriData.parse("data:;base64,AA%3D%3D").toString()); |
| 177 Expect.equals("data:;base64,AAA=", |
| 178 UriData.parse("data:;base64,AAA%3D").toString()); |
| 179 // Normalized unpadded data. |
| 180 Expect.equals("data:;base64,AA==", |
| 181 UriData.parse("data:;base64,AA").toString()); |
| 182 Expect.equals("data:;base64,AAA=", |
| 183 UriData.parse("data:;base64,AAA").toString()); |
| 184 |
| 185 // "URI normalization" of non-base64 content. |
| 186 var uri = UriData.parse("data:,\x20\xa0"); |
| 187 Expect.equals("data:,%20%C2%A0", uri.toString()); |
| 188 uri = UriData.parse("data:,x://x@y:[z]:42/p/./?q=x&y=z#?#\u1234\u{12345}"); |
| 189 Expect.equals( |
| 190 "data:,x://x@y:%5Bz%5D:42/p/./?q=x&y=z%23?%23%E1%88%B4%F0%92%8D%85", |
| 191 uri.toString()); |
| 192 } |
| 193 |
161 bool badArgument(e) => e is ArgumentError; | 194 bool badArgument(e) => e is ArgumentError; |
162 bool badFormat(e) => e is FormatException; | 195 bool badFormat(e) => e is FormatException; |
163 | 196 |
164 void testErrors() { | 197 void testErrors() { |
165 // Invalid constructor parameters. | 198 // Invalid constructor parameters. |
166 Expect.throws(() { new UriData.fromBytes([], mimeType: "noslash"); }, | 199 Expect.throws(() { new UriData.fromBytes([], mimeType: "noslash"); }, |
167 badArgument); | 200 badArgument); |
168 Expect.throws(() { new UriData.fromBytes([257]); }, | 201 Expect.throws(() { new UriData.fromBytes([257]); }, |
169 badArgument); | 202 badArgument); |
170 Expect.throws(() { new UriData.fromBytes([-1]); }, | 203 Expect.throws(() { new UriData.fromBytes([-1]); }, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 badFormat); | 244 badFormat); |
212 Expect.throws(() { UriData.parse("data:type/sub;knocomma=");}, | 245 Expect.throws(() { UriData.parse("data:type/sub;knocomma=");}, |
213 badFormat); | 246 badFormat); |
214 Expect.throws(() { UriData.parse("data:type/sub;k=v;nocomma");}, | 247 Expect.throws(() { UriData.parse("data:type/sub;k=v;nocomma");}, |
215 badFormat); | 248 badFormat); |
216 Expect.throws(() { UriData.parse("data:type/sub;k=nocomma");}, | 249 Expect.throws(() { UriData.parse("data:type/sub;k=nocomma");}, |
217 badFormat); | 250 badFormat); |
218 Expect.throws(() { UriData.parse("data:type/sub;k=v;base64");}, | 251 Expect.throws(() { UriData.parse("data:type/sub;k=v;base64");}, |
219 badFormat); | 252 badFormat); |
220 | 253 |
221 // Invalid base64 format (only detected when decodeing). | 254 void formatError(String input) { |
| 255 Expect.throws(() => UriData.parse("data:;base64,$input"), badFormat, input); |
| 256 } |
| 257 |
| 258 // Invalid base64 format (detected when parsed). |
222 for (var a = 0; a <= 4; a++) { | 259 for (var a = 0; a <= 4; a++) { |
223 for (var p = 0; p <= 4; p++) { | 260 for (var p = 0; p <= 4; p++) { |
224 // Base-64 encoding must have length divisible by four and no more | 261 // Base-64 encoding must have length divisible by four and no more |
225 // than two padding characters at the end. | 262 // than two padding characters at the end. |
226 if (p < 3 && (a + p) % 4 == 0) continue; | 263 if (p < 3 && (a + p) % 4 == 0) continue; |
227 uri = UriData.parse("data:;base64," + "A" * a + "=" * p); | 264 if (p == 0 && a > 1) continue; |
228 Expect.throws(uri.contentAsBytes, badFormat); | 265 formatError("A" * a + "=" * p); |
| 266 formatError("A" * a + "%3D" * p); |
229 } | 267 } |
230 } | 268 } |
231 // Invalid base64 encoding: padding not at end. | 269 // Invalid base64 encoding: padding not at end. |
232 uri = UriData.parse("data:;base64,AA=A"); | 270 formatError("AA=A"); |
233 Expect.throws(uri.contentAsBytes, badFormat); | 271 formatError("A=AA"); |
234 uri = UriData.parse("data:;base64,A=AA"); | 272 formatError("=AAA"); |
235 Expect.throws(uri.contentAsBytes, badFormat); | 273 formatError("A==A"); |
236 uri = UriData.parse("data:;base64,=AAA"); | 274 formatError("==AA"); |
237 Expect.throws(uri.contentAsBytes, badFormat); | 275 formatError("===A"); |
238 uri = UriData.parse("data:;base64,A==A"); | 276 formatError("AAA%3D="); |
239 Expect.throws(uri.contentAsBytes, badFormat); | 277 formatError("A%3D=="); |
240 uri = UriData.parse("data:;base64,==AA"); | 278 |
241 Expect.throws(uri.contentAsBytes, badFormat); | 279 // Invalid unpadded data. |
242 uri = UriData.parse("data:;base64,===A"); | 280 formatError("A"); |
243 Expect.throws(uri.contentAsBytes, badFormat); | 281 formatError("AAAAA"); |
| 282 |
| 283 // Invalid characters. |
| 284 formatError("AAA*"); |
| 285 formatError("AAA\x00"); |
| 286 formatError("AAA\\"); |
| 287 formatError("AAA,"); |
| 288 |
| 289 // Invalid escapes. |
| 290 formatError("AAA%25"); |
| 291 formatError("AAA%7F"); |
| 292 formatError("AAA%7F"); |
244 } | 293 } |
245 | 294 |
246 /// Checks that two [Uri]s are exactly the same. | 295 /// Checks that two [Uri]s are exactly the same. |
247 expectUriEquals(Uri expect, Uri actual) { | 296 expectUriEquals(Uri expect, Uri actual) { |
248 Expect.equals(expect.scheme, actual.scheme, "scheme"); | 297 Expect.equals(expect.scheme, actual.scheme, "scheme"); |
249 Expect.equals(expect.hasAuthority, actual.hasAuthority, "hasAuthority"); | 298 Expect.equals(expect.hasAuthority, actual.hasAuthority, "hasAuthority"); |
250 Expect.equals(expect.userInfo, actual.userInfo, "userInfo"); | 299 Expect.equals(expect.userInfo, actual.userInfo, "userInfo"); |
251 Expect.equals(expect.host, actual.host, "host"); | 300 Expect.equals(expect.host, actual.host, "host"); |
252 Expect.equals(expect.hasPort, actual.hasPort, "hasPort"); | 301 Expect.equals(expect.hasPort, actual.hasPort, "hasPort"); |
253 Expect.equals(expect.port, actual.port, "port"); | 302 Expect.equals(expect.port, actual.port, "port"); |
254 Expect.equals(expect.port, actual.port, "port"); | 303 Expect.equals(expect.port, actual.port, "port"); |
255 Expect.equals(expect.hasQuery, actual.hasQuery, "hasQuery"); | 304 Expect.equals(expect.hasQuery, actual.hasQuery, "hasQuery"); |
256 Expect.equals(expect.query, actual.query, "query"); | 305 Expect.equals(expect.query, actual.query, "query"); |
257 Expect.equals(expect.hasFragment, actual.hasFragment, "hasFragment"); | 306 Expect.equals(expect.hasFragment, actual.hasFragment, "hasFragment"); |
258 Expect.equals(expect.fragment, actual.fragment, "fragment"); | 307 Expect.equals(expect.fragment, actual.fragment, "fragment"); |
259 } | 308 } |
260 | 309 |
261 void testUriEquals(String uriText) { | 310 void testUriEquals(String uriText) { |
262 var data = UriData.parse(uriText); | 311 var data = UriData.parse(uriText); |
263 var uri = Uri.parse(uriText); | 312 var uri = Uri.parse(uriText); |
264 Expect.equals(data.uri, uri); | 313 Expect.equals(data.uri, uri); |
265 Expect.equals(data.toString(), uri.data.toString()); | 314 Expect.equals(data.toString(), uri.data.toString()); |
266 Expect.equals(data.toString(), uri.toString()); | 315 Expect.equals(data.toString(), uri.toString()); |
267 } | 316 } |
OLD | NEW |