Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: tests/corelib/data_uri_test.dart

Issue 2694373003: Normalize UriData.parse result. (Closed)
Patch Set: Merge to head. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« sdk/lib/convert/base64.dart ('K') | « sdk/lib/internal/internal.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.
floitsch 2017/02/15 17:03:09 -is-
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
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 // "URI normalization" of non-base64 content.
165 var uri = UriData.parse("data:,\x20\xa0");
166 Expect.equals("data:,%20%C2%A0", uri.toString());
167 uri = UriData.parse("data:,x://x@y:[z]:42/p/./?q=x&y=z#?#\u1234\u{12345}");
168 Expect.equals(
169 "data:,x://x@y:%5Bz%5D:42/p/./?q=x&y=z%23?%23%E1%88%B4%F0%92%8D%85",
170 uri.toString());
171 }
172
161 bool badArgument(e) => e is ArgumentError; 173 bool badArgument(e) => e is ArgumentError;
162 bool badFormat(e) => e is FormatException; 174 bool badFormat(e) => e is FormatException;
163 175
164 void testErrors() { 176 void testErrors() {
165 // Invalid constructor parameters. 177 // Invalid constructor parameters.
166 Expect.throws(() { new UriData.fromBytes([], mimeType: "noslash"); }, 178 Expect.throws(() { new UriData.fromBytes([], mimeType: "noslash"); },
167 badArgument); 179 badArgument);
168 Expect.throws(() { new UriData.fromBytes([257]); }, 180 Expect.throws(() { new UriData.fromBytes([257]); },
169 badArgument); 181 badArgument);
170 Expect.throws(() { new UriData.fromBytes([-1]); }, 182 Expect.throws(() { new UriData.fromBytes([-1]); },
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 badFormat); 223 badFormat);
212 Expect.throws(() { UriData.parse("data:type/sub;knocomma=");}, 224 Expect.throws(() { UriData.parse("data:type/sub;knocomma=");},
213 badFormat); 225 badFormat);
214 Expect.throws(() { UriData.parse("data:type/sub;k=v;nocomma");}, 226 Expect.throws(() { UriData.parse("data:type/sub;k=v;nocomma");},
215 badFormat); 227 badFormat);
216 Expect.throws(() { UriData.parse("data:type/sub;k=nocomma");}, 228 Expect.throws(() { UriData.parse("data:type/sub;k=nocomma");},
217 badFormat); 229 badFormat);
218 Expect.throws(() { UriData.parse("data:type/sub;k=v;base64");}, 230 Expect.throws(() { UriData.parse("data:type/sub;k=v;base64");},
219 badFormat); 231 badFormat);
220 232
221 // Invalid base64 format (only detected when decodeing). 233 void formatError(String input) {
234 Expect.throws(() => UriData.parse("data:;base64,$input"), badFormat, input);
235 }
236
237 // Invalid base64 format (detected when parsed).
222 for (var a = 0; a <= 4; a++) { 238 for (var a = 0; a <= 4; a++) {
223 for (var p = 0; p <= 4; p++) { 239 for (var p = 0; p <= 4; p++) {
224 // Base-64 encoding must have length divisible by four and no more 240 // Base-64 encoding must have length divisible by four and no more
225 // than two padding characters at the end. 241 // than two padding characters at the end.
226 if (p < 3 && (a + p) % 4 == 0) continue; 242 if (p < 3 && (a + p) % 4 == 0) continue;
227 uri = UriData.parse("data:;base64," + "A" * a + "=" * p); 243 if (p == 0 && a > 1) continue;
228 Expect.throws(uri.contentAsBytes, badFormat); 244 formatError("A" * a + "=" * p);
245 formatError("A" * a + "%3D" * p);
229 } 246 }
230 } 247 }
231 // Invalid base64 encoding: padding not at end. 248 // Invalid base64 encoding: padding not at end.
232 uri = UriData.parse("data:;base64,AA=A"); 249 formatError("AA=A");
233 Expect.throws(uri.contentAsBytes, badFormat); 250 formatError("A=AA");
234 uri = UriData.parse("data:;base64,A=AA"); 251 formatError("=AAA");
235 Expect.throws(uri.contentAsBytes, badFormat); 252 formatError("A==A");
236 uri = UriData.parse("data:;base64,=AAA"); 253 formatError("==AA");
237 Expect.throws(uri.contentAsBytes, badFormat); 254 formatError("===A");
238 uri = UriData.parse("data:;base64,A==A"); 255 formatError("AAA%3D=");
239 Expect.throws(uri.contentAsBytes, badFormat); 256 formatError("A%3D==");
240 uri = UriData.parse("data:;base64,==AA"); 257
241 Expect.throws(uri.contentAsBytes, badFormat); 258 // Normalized padded data.
242 uri = UriData.parse("data:;base64,===A"); 259 Expect.equals("data:;base64,AA==",
243 Expect.throws(uri.contentAsBytes, badFormat); 260 UriData.parse("data:;base64,AA%3D%3D").toString());
261 Expect.equals("data:;base64,AAA=",
262 UriData.parse("data:;base64,AAA%3D").toString());
263
264 // Invalid unpadded data.
265 formatError("A");
266 formatError("AAAAA");
267 // Normalized unpadded data.
268 Expect.equals("data:;base64,AA==",
269 UriData.parse("data:;base64,AA").toString());
270 Expect.equals("data:;base64,AAA=",
271 UriData.parse("data:;base64,AAA").toString());
272
273 // Invalid characters.
274 formatError("AAA*");
275 formatError("AAA\x00");
276 formatError("AAA\\");
277 formatError("AAA,");
278
279 // Normalized URI-alphabet characters.
280 Expect.equals("data:;base64,AA/+",
281 UriData.parse("data:;base64,AA_-").toString());
244 } 282 }
245 283
246 /// Checks that two [Uri]s are exactly the same. 284 /// Checks that two [Uri]s are exactly the same.
247 expectUriEquals(Uri expect, Uri actual) { 285 expectUriEquals(Uri expect, Uri actual) {
248 Expect.equals(expect.scheme, actual.scheme, "scheme"); 286 Expect.equals(expect.scheme, actual.scheme, "scheme");
249 Expect.equals(expect.hasAuthority, actual.hasAuthority, "hasAuthority"); 287 Expect.equals(expect.hasAuthority, actual.hasAuthority, "hasAuthority");
250 Expect.equals(expect.userInfo, actual.userInfo, "userInfo"); 288 Expect.equals(expect.userInfo, actual.userInfo, "userInfo");
251 Expect.equals(expect.host, actual.host, "host"); 289 Expect.equals(expect.host, actual.host, "host");
252 Expect.equals(expect.hasPort, actual.hasPort, "hasPort"); 290 Expect.equals(expect.hasPort, actual.hasPort, "hasPort");
253 Expect.equals(expect.port, actual.port, "port"); 291 Expect.equals(expect.port, actual.port, "port");
254 Expect.equals(expect.port, actual.port, "port"); 292 Expect.equals(expect.port, actual.port, "port");
255 Expect.equals(expect.hasQuery, actual.hasQuery, "hasQuery"); 293 Expect.equals(expect.hasQuery, actual.hasQuery, "hasQuery");
256 Expect.equals(expect.query, actual.query, "query"); 294 Expect.equals(expect.query, actual.query, "query");
257 Expect.equals(expect.hasFragment, actual.hasFragment, "hasFragment"); 295 Expect.equals(expect.hasFragment, actual.hasFragment, "hasFragment");
258 Expect.equals(expect.fragment, actual.fragment, "fragment"); 296 Expect.equals(expect.fragment, actual.fragment, "fragment");
259 } 297 }
260 298
261 void testUriEquals(String uriText) { 299 void testUriEquals(String uriText) {
262 var data = UriData.parse(uriText); 300 var data = UriData.parse(uriText);
263 var uri = Uri.parse(uriText); 301 var uri = Uri.parse(uriText);
264 Expect.equals(data.uri, uri); 302 Expect.equals(data.uri, uri);
265 Expect.equals(data.toString(), uri.data.toString()); 303 Expect.equals(data.toString(), uri.data.toString());
266 Expect.equals(data.toString(), uri.toString()); 304 Expect.equals(data.toString(), uri.toString());
267 } 305 }
OLDNEW
« sdk/lib/convert/base64.dart ('K') | « sdk/lib/internal/internal.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698