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

Unified 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: Update and add tests. 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 side-by-side diff with in-line comments
Download patch
« sdk/lib/core/uri.dart ('K') | « sdk/lib/core/uri.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/corelib/uri_test.dart
diff --git a/tests/corelib/uri_test.dart b/tests/corelib/uri_test.dart
index 19d4019a875629fdd1feaadf7364630b37806561..053aed6bafd5bff43411e8db230fbb36170b539e 100644
--- a/tests/corelib/uri_test.dart
+++ b/tests/corelib/uri_test.dart
@@ -124,6 +124,121 @@ void testResolvePath(String expected, String path) {
Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString());
}
+void testValidCharacters() {
+ 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 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.
+ }
+ }
+
+ // test that all valid characters are accepted.
kevmoo 2014/06/10 21:29:08 split valid test into its own test method?
+
+ 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/"];
+ if (auth.isNotEmpty) {
+ // Initial segemnt may be empty.
+ paths.add("//$PCHAR");
+ } else {
+ // Path may begin with non-slash.
+ paths.add("");
+ if (scheme.isEmpty) {
+ // Initial segment must not contain colon.
+ paths..add("$PCHAR_NC/$PCHAR")..add("$PCHAR_NC/$PCHAR/");
+ } else {
+ paths..add("$PCHAR")..add("$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);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // 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
+ "%1g", // Invalid escape.
+ "\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.
+ ];
+ for (var invalid in invalidChars) {
+ checkInvalid("$SCHEMECHAR${invalid}$SCHEMECHAR:///");
+ checkInvalid("${invalid}$SCHEMECHAR:///");
+ 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(":"); // 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.
+}
kevmoo 2014/06/10 21:32:04 Include examples provided in the bug - https://cod
+
main() {
testUri("http:", true);
testUri("file://", true);
@@ -150,7 +265,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 +375,6 @@ main() {
Expect.throws(
() => Uri.parse("file://user@password:host/path"),
(e) => e is FormatException);
+
+ testValidCharacters();
}
« 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