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

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

Issue 321543003: New, more validating, parser for URI. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add validation and normalization to URI components. Created 6 years, 6 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 | Annotate | Revision Log
« sdk/lib/core/uri.dart ('K') | « sdk/lib/core/uri.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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 library uriTest; 5 library uriTest;
6 6
7 import "package:expect/expect.dart"; 7 import "package:expect/expect.dart";
8 import 'dart:convert'; 8 import 'dart:convert';
9 9
10 testUri(String uri, bool isAbsolute) { 10 testUri(String uri, bool isAbsolute) {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 base.resolve("../g;p/h;s").toString()); 117 base.resolve("../g;p/h;s").toString());
118 } 118 }
119 119
120 void testResolvePath(String expected, String path) { 120 void testResolvePath(String expected, String path) {
121 Expect.equals(expected, new Uri().resolveUri(new Uri(path: path)).path); 121 Expect.equals(expected, new Uri().resolveUri(new Uri(path: path)).path);
122 Expect.equals( 122 Expect.equals(
123 "http://localhost$expected", 123 "http://localhost$expected",
124 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); 124 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString());
125 } 125 }
126 126
127 const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ";
128 const DIGIT = r"0123456789";
129 const PERCENT_ENCODED = "%00%ff";
130 const SUBDELIM = r"!$&'()*+,;=";
131
132 const SCHEMECHAR = "$ALPHA$DIGIT+-.";
133 const UNRESERVED = "$ALPHA$DIGIT-._~";
134 const REGNAMECHAR = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED";
135 const USERINFOCHAR = "$REGNAMECHAR:";
136
137 const PCHAR_NC = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED@";
138 const PCHAR = "$PCHAR_NC:";
139 const QUERYCHAR = "$PCHAR/?";
140
141 void testValidCharacters() {
142 // test that all valid characters are accepted.
143
144 for (var scheme in ["", "$SCHEMECHAR$SCHEMECHAR:"]) {
145 for (var userinfo in ["", "@", "$USERINFOCHAR$USERINFOCHAR@",
146 "$USERINFOCHAR:$DIGIT@"]) {
147 for (var host in ["", "$REGNAMECHAR$REGNAMECHAR",
148 "255.255.255.256", // valid reg-name.
149 "[ffff::ffff:ffff]", "[ffff::255.255.255.255]"]) {
150 for (var port in ["", ":$DIGIT$DIGIT"]) {
151 var auth = "$userinfo$host$port";
152 if (auth.isNotEmpty) auth = "//$auth";
153 var paths = ["", "/", "/$PCHAR", "/$PCHAR/"]; // Absolute or empty.
154 if (auth.isNotEmpty) {
155 // Initial segment may be empty.
156 paths..add("//$PCHAR");
157 } else {
158 // Path may begin with non-slash.
159 if (scheme.isEmpty) {
160 // Initial segment must not contain colon.
161 paths..add(PCHAR_NC)
162 ..add("$PCHAR_NC/$PCHAR")
163 ..add("$PCHAR_NC/$PCHAR/");
164 } else {
165 paths..add(PCHAR)
166 ..add("$PCHAR/$PCHAR")
167 ..add("$PCHAR/$PCHAR/");
168 }
169 }
170 for (var path in paths) {
171 for (var query in ["", "?", "?$QUERYCHAR"]) {
172 for (var fragment in ["", "#", "#$QUERYCHAR"]) {
173 var uri = "$scheme$auth$path$query$fragment";
174 // Should not throw.
175 var result = Uri.parse(uri);
176 }
177 }
178 }
179 }
180 }
181 }
182 }
183 }
184
185 void testInvalidUrls() {
186 void checkInvalid(uri) {
187 try {
188 var result = Uri.parse(uri);
189 Expect.fail("Invalid URI `$uri` parsed to $result\n"
190 " Scheme: ${result.scheme}\n"
191 " User-info: ${result.userInfo}\n"
192 " Host: ${result.host}\n"
193 " Port: ${result.port}\n"
194 " Path: ${result.path}\n"
195 " Query: ${result.query}\n"
196 " Fragment: ${result.fragment}\n");
197 } on FormatException {
198 // Success.
199 }
200 }
201
202 // Regression test for http://dartbug.com/16081
203 checkInvalid("http://www.example.org/red%09ros\u00E9#red");
204 checkInvalid("http://r\u00E9sum\u00E9.example.org");
205
206 // Invalid characters. The characters must be rejected, even if normalizing
207 // the input would cause them to be valid (normalization happens after
208 // validation).
209 var invalidChars = [
210 "\xe7", // Arbitrary non-ASCII letter
211 " ", // Space, not allowed anywhere.
212 '"', // Quote, not allowed anywhere
213 "\x7f", // DEL, not allowed anywhere
214 "\xdf", // German lower-case scharf-S. Becomes ASCII when upper-cased.
215 "\u0130" // Latin capital dotted I, becomes ASCII lower-case in Turkish.
216 "%\uFB03", // % + Ligature ffi, becomes ASCII when upper-cased,
217 // should not be read as "%FFI".
218 "\u212a", // Kelvin sign. Becomes ASCII when lower-cased.
219 "%1g", // Invalid escape.
220 ];
221 for (var invalid in invalidChars) {
222 checkInvalid("A${invalid}b:///");
223 checkInvalid("${invalid}b:///");
224 checkInvalid("s://user${invalid}info@x.x/");
225 checkInvalid("s://reg${invalid}name/");
226 checkInvalid("s://regname:12${invalid}45/");
227 checkInvalid("s://regname/p${invalid}ath/");
228 checkInvalid("/p${invalid}ath/");
229 checkInvalid("p${invalid}ath/");
230 checkInvalid("s://regname/path/?x${invalid}x");
231 checkInvalid("s://regname/path/#x${invalid}x");
232 checkInvalid("s://regname/path/?#x${invalid}x");
233 }
234 checkInvalid("s%41://x.x/"); // No escapes in scheme,
235 // and no colon before slash in path.
236 checkInvalid("1a://x.x/"); // Scheme must start with letter,
237 // and no colon before slash in path.
238 checkInvalid(".a://x.x/"); // Scheme must start with letter,
239 // and no colon before slash in path.
240 checkInvalid(":"); // Scheme must start with letter,
241 // and no colon before slash in path.
242 checkInvalid("s://x@x@x.x/"); // At most one @ in userinfo.
243 checkInvalid("s://x@x:x/"); // No colon in host except before a port.
244 checkInvalid("s://x@x:9:9/"); // At most one port.
245 checkInvalid("s://x/x#foo#bar"); // At most one #.
246 checkInvalid("s://:/"); // Colon in host requires port,
247 // and path may not start with //.
248 checkInvalid("s@://x:9/x?x#x"); // @ not allowed in scheme.
249 }
250
251 void testNormalization() {
252 // The Uri constructor and the Uri.parse function performs RFC-3986
253 // syntax based normalization.
254
255 var uri;
256
257 // Scheme: Only case normalization. Schemes cannot contain escapes.
258 uri = Uri.parse("A:");
259 Expect.equals("a", uri.scheme);
260 uri = Uri.parse("Z:");
261 Expect.equals("z", uri.scheme);
262 uri = Uri.parse("$SCHEMECHAR:");
263 Expect.equals(SCHEMECHAR.toLowerCase(), uri.scheme);
264
265 // Percent escape normalization.
266 // Escapes of unreserved characters are converted to the character,
267 // subject to case normalization in reg-name.
268 for (var i = 0; i < UNRESERVED.length; i++) {
269 var char = UNRESERVED[i];
270 var escape = "%" + char.codeUnitAt(0).toRadixString(16); // all > 0xf.
271
272 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ"
273 "?vV${escape}vV#wW${escape}wW");
274 Expect.equals("xX${char}xX", uri.userInfo);
275 Expect.equals("yY${char}yY".toLowerCase(), uri.host);
276 Expect.equals("/zZ${char}zZ", uri.path);
277 Expect.equals("vV${char}vV", uri.query);
278 Expect.equals("wW${char}wW", uri.fragment);
279 }
280
281 // Escapes of reserved characters are kept, but upper-cased.
282 for (var escape in ["%00", "%1f", "%7F", "%fF"]) {
283 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ"
284 "?vV${escape}vV#wW${escape}wW");
285 var normalizedEscape = escape.toUpperCase();
286 Expect.equals("xX${normalizedEscape}xX", uri.userInfo);
287 Expect.equals("yy${normalizedEscape}yy", uri.host);
288 Expect.equals("/zZ${normalizedEscape}zZ", uri.path);
289 Expect.equals("vV${normalizedEscape}vV", uri.query);
290 Expect.equals("wW${normalizedEscape}wW", uri.fragment);
291 }
292
293 // Some host normalization edge cases.
294 uri = Uri.parse("x://x%61X%41x%41X%61x/");
295 Expect.equals("xaxaxaxax", uri.host);
296
297 uri = Uri.parse("x://Xxxxxxxx/");
298 Expect.equals("xxxxxxxx", uri.host);
299
300 uri = Uri.parse("x://xxxxxxxX/");
301 Expect.equals("xxxxxxxx", uri.host);
302
303 uri = Uri.parse("x://xxxxxxxx%61/");
304 Expect.equals("xxxxxxxxa", uri.host);
305
306 uri = Uri.parse("x://%61xxxxxxxx/");
307 Expect.equals("axxxxxxxx", uri.host);
308
309 uri = Uri.parse("x://X/");
310 Expect.equals("x", uri.host);
311
312 uri = Uri.parse("x://%61/");
313 Expect.equals("a", uri.host);
314
315 // TODO(lrn): Also do path normalization: /./ -> / and /x/../ -> /
316 }
317
127 main() { 318 main() {
128 testUri("http:", true); 319 testUri("http:", true);
129 testUri("file://", true); 320 testUri("file://", true);
130 testUri("file", false); 321 testUri("file", false);
131 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); 322 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true);
132 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment", 323 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment",
133 false); 324 false);
134 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment", 325 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment",
135 new Uri( 326 new Uri(
136 scheme: "http", 327 scheme: "http",
137 userInfo: "user", 328 userInfo: "user",
138 host: "example.com", 329 host: "example.com",
139 port: 80, 330 port: 80,
140 path: "/a/b/c", 331 path: "/a/b/c",
141 query: "query", 332 query: "query",
142 fragment: "fragment").toString()); 333 fragment: "fragment").toString());
143 Expect.stringEquals("//null@null/a/b/c/", 334 Expect.stringEquals("//null@null/a/b/c/",
144 new Uri( 335 new Uri(
145 scheme: null, 336 scheme: null,
146 userInfo: null, 337 userInfo: null,
147 host: null, 338 host: null,
148 port: 0, 339 port: 0,
149 path: "/a/b/c/", 340 path: "/a/b/c/",
150 query: null, 341 query: null,
151 fragment: null).toString()); 342 fragment: null).toString());
152 Expect.stringEquals("file://", Uri.parse("file:").toString()); 343 Expect.stringEquals("file://", Uri.parse("file:").toString());
153
154 testResolvePath("/a/g", "/a/b/c/./../../g"); 344 testResolvePath("/a/g", "/a/b/c/./../../g");
155 testResolvePath("/a/g", "/a/b/c/./../../g"); 345 testResolvePath("/a/g", "/a/b/c/./../../g");
156 testResolvePath("/mid/6", "mid/content=5/../6"); 346 testResolvePath("/mid/6", "mid/content=5/../6");
157 testResolvePath("/a/b/e", "a/b/c/d/../../e"); 347 testResolvePath("/a/b/e", "a/b/c/d/../../e");
158 testResolvePath("/a/b/e", "../a/b/c/d/../../e"); 348 testResolvePath("/a/b/e", "../a/b/c/d/../../e");
159 testResolvePath("/a/b/e", "./a/b/c/d/../../e"); 349 testResolvePath("/a/b/e", "./a/b/c/d/../../e");
160 testResolvePath("/a/b/e", "../a/b/./c/d/../../e"); 350 testResolvePath("/a/b/e", "../a/b/./c/d/../../e");
161 testResolvePath("/a/b/e", "./a/b/./c/d/../../e"); 351 testResolvePath("/a/b/e", "./a/b/./c/d/../../e");
162 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/."); 352 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/.");
163 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./."); 353 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./.");
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); 444 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24");
255 testEncodeDecodeComponent(s, "%F0%90%80%80"); 445 testEncodeDecodeComponent(s, "%F0%90%80%80");
256 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); 446 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B");
257 testEncodeDecodeQueryComponent( 447 testEncodeDecodeQueryComponent(
258 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); 448 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null);
259 449
260 // Invalid URI - : and @ is swapped, port ("host") should be numeric. 450 // Invalid URI - : and @ is swapped, port ("host") should be numeric.
261 Expect.throws( 451 Expect.throws(
262 () => Uri.parse("file://user@password:host/path"), 452 () => Uri.parse("file://user@password:host/path"),
263 (e) => e is FormatException); 453 (e) => e is FormatException);
454
455 testValidCharacters();
456 testInvalidUrls();
457 testNormalization();
264 } 458 }
OLDNEW
« sdk/lib/core/uri.dart ('K') | « sdk/lib/core/uri.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698