Index: tests/corelib/uri_test.dart |
diff --git a/tests/corelib/uri_test.dart b/tests/corelib/uri_test.dart |
index 19d4019a875629fdd1feaadf7364630b37806561..6ae45ea4ffa6712723a2596a391c9d238d72b997 100644 |
--- a/tests/corelib/uri_test.dart |
+++ b/tests/corelib/uri_test.dart |
@@ -124,6 +124,199 @@ void testResolvePath(String expected, String path) { |
Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); |
} |
+const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ"; |
+const DIGIT = r"0123456789"; |
+const PERCENT_ENCODED = "%00%ff"; |
+const SUBDELIM = r"!$&'()*+,;="; |
+ |
+const SCHEMECHAR = "$ALPHA$DIGIT+-."; |
+const UNRESERVED = "$ALPHA$DIGIT-._~"; |
+const REGNAMECHAR = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED"; |
+const USERINFOCHAR = "$REGNAMECHAR:"; |
+ |
+const PCHAR_NC = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED@"; |
+const PCHAR = "$PCHAR_NC:"; |
+const QUERYCHAR = "$PCHAR/?"; |
+ |
+void testValidCharacters() { |
+ // test that all valid characters are accepted. |
+ |
+ for (var scheme in ["", "$SCHEMECHAR$SCHEMECHAR:"]) { |
+ for (var userinfo in ["", "@", "$USERINFOCHAR$USERINFOCHAR@", |
+ "$USERINFOCHAR:$DIGIT@"]) { |
+ for (var host in ["", "$REGNAMECHAR$REGNAMECHAR", |
+ "255.255.255.256", // valid reg-name. |
+ "[ffff::ffff:ffff]", "[ffff::255.255.255.255]"]) { |
+ for (var port in ["", ":$DIGIT$DIGIT"]) { |
+ var auth = "$userinfo$host$port"; |
+ if (auth.isNotEmpty) auth = "//$auth"; |
+ var paths = ["", "/", "/$PCHAR", "/$PCHAR/"]; // Absolute or empty. |
+ if (auth.isNotEmpty) { |
+ // Initial segment may be empty. |
+ paths..add("//$PCHAR"); |
+ } else { |
+ // Path may begin with non-slash. |
+ if (scheme.isEmpty) { |
+ // Initial segment must not contain colon. |
+ paths..add(PCHAR_NC) |
+ ..add("$PCHAR_NC/$PCHAR") |
+ ..add("$PCHAR_NC/$PCHAR/"); |
+ } else { |
+ paths..add(PCHAR) |
+ ..add("$PCHAR/$PCHAR") |
+ ..add("$PCHAR/$PCHAR/"); |
+ } |
+ } |
+ for (var path in paths) { |
+ for (var query in ["", "?", "?$QUERYCHAR"]) { |
+ for (var fragment in ["", "#", "#$QUERYCHAR"]) { |
+ var uri = "$scheme$auth$path$query$fragment"; |
+ // Should not throw. |
+ var result = Uri.parse(uri); |
+ } |
+ } |
+ } |
+ } |
+ } |
+ } |
+ } |
+} |
+ |
+void testInvalidUrls() { |
+ void checkInvalid(uri) { |
+ try { |
+ var result = Uri.parse(uri); |
+ Expect.fail("Invalid URI `$uri` parsed to $result\n" |
+ " Scheme: ${result.scheme}\n" |
+ " User-info: ${result.userInfo}\n" |
+ " Host: ${result.host}\n" |
+ " Port: ${result.port}\n" |
+ " Path: ${result.path}\n" |
+ " Query: ${result.query}\n" |
+ " Fragment: ${result.fragment}\n"); |
+ } on FormatException { |
+ // Success. |
+ } |
+ } |
+ |
+ // Regression test for http://dartbug.com/16081 |
+ checkInvalid("http://www.example.org/red%09ros\u00E9#red"); |
+ checkInvalid("http://r\u00E9sum\u00E9.example.org"); |
+ |
+ // Invalid characters. The characters must be rejected, even if normalizing |
+ // the input would cause them to be valid (normalization happens after |
+ // validation). |
+ var invalidChars = [ |
+ "\xe7", // Arbitrary non-ASCII letter |
+ " ", // Space, not allowed anywhere. |
+ '"', // Quote, not allowed anywhere |
+ "\x7f", // DEL, not allowed anywhere |
+ "\xdf", // German lower-case scharf-S. Becomes ASCII when upper-cased. |
+ "\u0130" // Latin capital dotted I, becomes ASCII lower-case in Turkish. |
+ "%\uFB03", // % + Ligature ffi, becomes ASCII when upper-cased, |
+ // should not be read as "%FFI". |
+ "\u212a", // Kelvin sign. Becomes ASCII when lower-cased. |
+ "%1g", // Invalid escape. |
+ ]; |
+ for (var invalid in invalidChars) { |
+ checkInvalid("A${invalid}b:///"); |
+ checkInvalid("${invalid}b:///"); |
+ checkInvalid("s://user${invalid}info@x.x/"); |
+ checkInvalid("s://reg${invalid}name/"); |
+ checkInvalid("s://regname:12${invalid}45/"); |
+ checkInvalid("s://regname/p${invalid}ath/"); |
+ checkInvalid("/p${invalid}ath/"); |
+ checkInvalid("p${invalid}ath/"); |
+ checkInvalid("s://regname/path/?x${invalid}x"); |
+ checkInvalid("s://regname/path/#x${invalid}x"); |
+ checkInvalid("s://regname/path/?#x${invalid}x"); |
+ } |
+ checkInvalid("s%41://x.x/"); // No escapes in scheme, |
+ // and no colon before slash in path. |
+ checkInvalid("1a://x.x/"); // Scheme must start with letter, |
+ // and no colon before slash in path. |
+ checkInvalid(".a://x.x/"); // Scheme must start with letter, |
+ // and no colon before slash in path. |
+ checkInvalid("_:"); // Character not valid in scheme, |
+ // and no colon before slash in path. |
+ checkInvalid(":"); // Scheme must start with letter, |
+ // and no colon before slash in path. |
+ checkInvalid("s://x@x@x.x/"); // At most one @ in userinfo. |
+ checkInvalid("s://x@x:x/"); // No colon in host except before a port. |
+ checkInvalid("s://x@x:9:9/"); // At most one port. |
+ checkInvalid("s://x/x#foo#bar"); // At most one #. |
+ checkInvalid("s://:/"); // Colon in host requires port, |
+ // and path may not start with //. |
+ checkInvalid("s@://x:9/x?x#x"); // @ not allowed in scheme. |
+} |
+ |
+void testNormalization() { |
+ // The Uri constructor and the Uri.parse function performs RFC-3986 |
+ // syntax based normalization. |
+ |
+ var uri; |
+ |
+ // Scheme: Only case normalization. Schemes cannot contain escapes. |
+ uri = Uri.parse("A:"); |
+ Expect.equals("a", uri.scheme); |
+ uri = Uri.parse("Z:"); |
+ Expect.equals("z", uri.scheme); |
+ uri = Uri.parse("$SCHEMECHAR:"); |
+ Expect.equals(SCHEMECHAR.toLowerCase(), uri.scheme); |
+ |
+ // Percent escape normalization. |
+ // Escapes of unreserved characters are converted to the character, |
+ // subject to case normalization in reg-name. |
+ for (var i = 0; i < UNRESERVED.length; i++) { |
+ var char = UNRESERVED[i]; |
+ var escape = "%" + char.codeUnitAt(0).toRadixString(16); // all > 0xf. |
+ |
+ uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" |
+ "?vV${escape}vV#wW${escape}wW"); |
+ Expect.equals("xX${char}xX", uri.userInfo); |
+ Expect.equals("yY${char}yY".toLowerCase(), uri.host); |
+ Expect.equals("/zZ${char}zZ", uri.path); |
+ Expect.equals("vV${char}vV", uri.query); |
+ Expect.equals("wW${char}wW", uri.fragment); |
+ } |
+ |
+ // Escapes of reserved characters are kept, but upper-cased. |
+ for (var escape in ["%00", "%1f", "%7F", "%fF"]) { |
+ uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" |
+ "?vV${escape}vV#wW${escape}wW"); |
+ var normalizedEscape = escape.toUpperCase(); |
+ Expect.equals("xX${normalizedEscape}xX", uri.userInfo); |
+ Expect.equals("yy${normalizedEscape}yy", uri.host); |
+ Expect.equals("/zZ${normalizedEscape}zZ", uri.path); |
+ Expect.equals("vV${normalizedEscape}vV", uri.query); |
+ Expect.equals("wW${normalizedEscape}wW", uri.fragment); |
+ } |
+ |
+ // Some host normalization edge cases. |
+ uri = Uri.parse("x://x%61X%41x%41X%61x/"); |
+ Expect.equals("xaxaxaxax", uri.host); |
+ |
+ uri = Uri.parse("x://Xxxxxxxx/"); |
+ Expect.equals("xxxxxxxx", uri.host); |
+ |
+ uri = Uri.parse("x://xxxxxxxX/"); |
+ Expect.equals("xxxxxxxx", uri.host); |
+ |
+ uri = Uri.parse("x://xxxxxxxx%61/"); |
+ Expect.equals("xxxxxxxxa", uri.host); |
+ |
+ uri = Uri.parse("x://%61xxxxxxxx/"); |
+ Expect.equals("axxxxxxxx", uri.host); |
+ |
+ uri = Uri.parse("x://X/"); |
+ Expect.equals("x", uri.host); |
+ |
+ uri = Uri.parse("x://%61/"); |
+ Expect.equals("a", uri.host); |
+ |
+ // TODO(lrn): Also do path normalization: /./ -> / and /x/../ -> / |
+} |
+ |
main() { |
testUri("http:", true); |
testUri("file://", true); |
@@ -150,7 +343,6 @@ main() { |
query: null, |
fragment: null).toString()); |
Expect.stringEquals("file://", Uri.parse("file:").toString()); |
- |
testResolvePath("/a/g", "/a/b/c/./../../g"); |
testResolvePath("/a/g", "/a/b/c/./../../g"); |
testResolvePath("/mid/6", "mid/content=5/../6"); |
@@ -261,4 +453,8 @@ main() { |
Expect.throws( |
() => Uri.parse("file://user@password:host/path"), |
(e) => e is FormatException); |
+ |
+ testValidCharacters(); |
+ testInvalidUrls(); |
+ testNormalization(); |
} |