Index: runtime/vm/uri_test.cc |
diff --git a/runtime/vm/uri_test.cc b/runtime/vm/uri_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aa8a4aa27b0df3476fff6524c4eee55b2c8b5bb9 |
--- /dev/null |
+++ b/runtime/vm/uri_test.cc |
@@ -0,0 +1,545 @@ |
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+#include "vm/uri.h" |
+#include "vm/unit_test.h" |
+ |
+namespace dart { |
+ |
+TEST_CASE(ParseUri_WithScheme_NoQueryNoUser) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo://example.com:8042/over/there", &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT_STREQ("example.com", uri.host); |
+ EXPECT_STREQ("8042", uri.port); |
+ EXPECT_STREQ("/over/there", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_WithScheme_WithQuery) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo://example.com:8042/over/there?name=ferret", &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT_STREQ("example.com", uri.host); |
+ EXPECT_STREQ("8042", uri.port); |
+ EXPECT_STREQ("/over/there", uri.path); |
+ EXPECT_STREQ("name=ferret", uri.query); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_WithScheme_WithFragment) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo://example.com:8042/over/there#fragment", &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT_STREQ("example.com", uri.host); |
+ EXPECT_STREQ("8042", uri.port); |
+ EXPECT_STREQ("/over/there", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT_STREQ("fragment", uri.fragment); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_WithScheme_WithQueryWithFragment) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo://example.com:8042/over/there?name=ferret#fragment", |
+ &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT_STREQ("example.com", uri.host); |
+ EXPECT_STREQ("8042", uri.port); |
+ EXPECT_STREQ("/over/there", uri.path); |
+ EXPECT_STREQ("name=ferret", uri.query); |
+ EXPECT_STREQ("fragment", uri.fragment); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_WithScheme_WithUser) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo://user@example.com:8042/over/there", &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT_STREQ("user", uri.userinfo); |
+ EXPECT_STREQ("example.com", uri.host); |
+ EXPECT_STREQ("8042", uri.port); |
+ EXPECT_STREQ("/over/there", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_WithScheme_ShortPath) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo://example.com:8042/", &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT_STREQ("example.com", uri.host); |
+ EXPECT_STREQ("8042", uri.port); |
+ EXPECT_STREQ("/", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_WithScheme_EmptyPath) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo://example.com:8042", &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT_STREQ("example.com", uri.host); |
+ EXPECT_STREQ("8042", uri.port); |
+ EXPECT_STREQ("", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_WithScheme_Rootless1) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo:here", &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("here", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_WithScheme_Rootless2) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("foo:or/here", &uri)); |
+ EXPECT_STREQ("foo", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("or/here", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NoScheme_AbsPath_WithAuthority) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("//example.com:8042/over/there", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT_STREQ("example.com", uri.host); |
+ EXPECT_STREQ("8042", uri.port); |
+ EXPECT_STREQ("/over/there", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NoScheme_AbsPath_NoAuthority) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("/over/there", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("/over/there", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+// Colons are permitted in path segments, in many cases. |
+TEST_CASE(ParseUri_NoScheme_AbsPath_StrayColon) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("/ov:er/there", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("/ov:er/there", uri.path); |
+ EXPECT(uri.query == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NoScheme_Rootless1) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("here", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("here", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NoScheme_Rootless2) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("or/here", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("or/here", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NoScheme_Empty) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NoScheme_QueryOnly) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("?name=ferret", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("", uri.path); |
+ EXPECT_STREQ("name=ferret", uri.query); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NoScheme_FragmentOnly) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("#fragment", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT_STREQ("fragment", uri.fragment); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_DoubleSlash1) { |
+ ParsedUri uri; |
+ EXPECT(!ParseUri("foo://example.com:8042/over//there?name=ferret", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT(uri.path == NULL); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_DoubleSlash2) { |
+ ParsedUri uri; |
+ EXPECT(!ParseUri("or//here", &uri)); |
+ EXPECT(uri.scheme == NULL); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT(uri.path == NULL); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_LowerCaseScheme) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri("ScHeMe:path", &uri)); |
+ EXPECT_STREQ("scheme", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("path", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NormalizeEscapes_PathQueryFragment) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri( |
+ "scheme:/This%09Is A P%61th?This%09Is A Qu%65ry#A Fr%61gment", &uri)); |
+ EXPECT_STREQ("scheme", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("/This%09Is%20A%20Path", uri.path); |
+ EXPECT_STREQ("This%09Is%20A%20Query", uri.query); |
+ EXPECT_STREQ("A%20Fragment", uri.fragment); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NormalizeEscapes_UppercaseEscapesPreferred) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri( |
+ "scheme:/%1b%1B", &uri)); |
+ EXPECT_STREQ("scheme", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("/%1B%1B", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_NormalizeEscapes_Authority) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri( |
+ "scheme://UsEr N%61%4de@h%4FsT.c%6fm:80/", &uri)); |
+ EXPECT_STREQ("scheme", uri.scheme); |
+ EXPECT_STREQ("UsEr%20NaMe", uri.userinfo); // Normalized, case preserved. |
+ EXPECT_STREQ("host.com", uri.host); // Normalized, lower-cased. |
+ EXPECT_STREQ("80", uri.port); |
+ EXPECT_STREQ("/", uri.path); |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ParseUri_BrokenEscapeSequence) { |
+ ParsedUri uri; |
+ EXPECT(ParseUri( |
+ "scheme:/%1g", &uri)); |
+ EXPECT_STREQ("scheme", uri.scheme); |
+ EXPECT(uri.userinfo == NULL); |
+ EXPECT(uri.host == NULL); |
+ EXPECT(uri.port == NULL); |
+ EXPECT_STREQ("/%1g", uri.path); // Broken sequence is unchanged. |
+ EXPECT(uri.query == NULL); |
+ EXPECT(uri.fragment == NULL); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_WithScheme_NoAuthorityNoQuery) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("rscheme:/ref/path", |
+ "bscheme://buser@bhost:11/base/path?baseQuery", |
+ &target_uri)); |
+ EXPECT_STREQ("rscheme:/ref/path", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_WithScheme_WithAuthorityWithQuery) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("rscheme://ruser@rhost:22/ref/path?refQuery", |
+ "bscheme://buser@bhost:11/base/path?baseQuery", |
+ &target_uri)); |
+ EXPECT_STREQ("rscheme://ruser@rhost:22/ref/path?refQuery", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NoScheme_WithAuthority) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("//ruser@rhost:22/ref/path", |
+ "bscheme://buser@bhost:11/base/path?baseQuery", |
+ &target_uri)); |
+ EXPECT_STREQ("bscheme://ruser@rhost:22/ref/path", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NoSchemeNoAuthority_AbsolutePath) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("/ref/path", |
+ "bscheme://buser@bhost:11/base/path?baseQuery", |
+ &target_uri)); |
+ EXPECT_STREQ("bscheme://buser@bhost:11/ref/path", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NoSchemeNoAuthority_RelativePath) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("ref/path", |
+ "bscheme://buser@bhost:11/base/path?baseQuery", |
+ &target_uri)); |
+ EXPECT_STREQ("bscheme://buser@bhost:11/base/ref/path", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NoSchemeNoAuthority_RelativePathEmptyBasePath) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("ref/path", |
+ "bscheme://buser@bhost:11", |
+ &target_uri)); |
+ EXPECT_STREQ("bscheme://buser@bhost:11/ref/path", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NoSchemeNoAuthority_RelativePathWeirdBasePath) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("ref/path", |
+ "bscheme:base", |
+ &target_uri)); |
+ EXPECT_STREQ("bscheme:ref/path", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NoSchemeNoAuthority_EmptyPath) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("", |
+ "bscheme://buser@bhost:11/base/path?baseQuery#bfragment", |
+ &target_uri)); |
+ // Note that we drop the base fragment from the resolved uri. |
+ EXPECT_STREQ("bscheme://buser@bhost:11/base/path?baseQuery", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NoSchemeNoAuthority_EmptyPathWithQuery) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("?refQuery", |
+ "bscheme://buser@bhost:11/base/path?baseQuery#bfragment", |
+ &target_uri)); |
+ EXPECT_STREQ("bscheme://buser@bhost:11/base/path?refQuery", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NoSchemeNoAuthority_EmptyPathWithFragment) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("#rfragment", |
+ "bscheme://buser@bhost:11/base/path?baseQuery#bfragment", |
+ &target_uri)); |
+ EXPECT_STREQ("bscheme://buser@bhost:11/base/path?baseQuery#rfragment", |
+ target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveOneDotSegment) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("./refpath", |
+ "scheme://auth/a/b/c/d", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/a/b/c/refpath", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveTwoDotSegments) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("././refpath", |
+ "scheme://auth/a/b/c/d", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/a/b/c/refpath", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveOneDotDotSegment) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("../refpath", |
+ "scheme://auth/a/b/c/d", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/a/b/refpath", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveTwoDotDotSegments) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("../../refpath", |
+ "scheme://auth/a/b/c/d", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/a/refpath", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveTooManyDotDotSegments) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("../../../../../../../../../refpath", |
+ "scheme://auth/a/b/c/d", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/refpath", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveDotSegmentsNothingLeft1) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("../../../../..", |
+ "scheme://auth/a/b/c/d", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/", target_uri); |
+} |
+ |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveDotSegmentsNothingLeft2) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri(".", |
+ "scheme://auth/", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/", target_uri); |
+} |
+ |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveDotSegmentsInitialPrefix) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("../../../../refpath", |
+ "scheme://auth", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/refpath", target_uri); |
+} |
+ |
+ |
+ |
+TEST_CASE(ResolveUri_RemoveDots_RemoveDotSegmentsMixed) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("../../1/./2/../3/4/../5/././6/../7", |
+ "scheme://auth/a/b/c/d/e", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme://auth/a/b/1/3/5/7", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NormalizeEscapes_PathQueryFragment) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("#A Fr%61gment", |
+ "scheme:/This%09Is A P%61th?This%09Is A Qu%65ry", |
+ &target_uri)); |
+ EXPECT_STREQ( |
+ "scheme:/This%09Is%20A%20Path?This%09Is%20A%20Query#A%20Fragment", |
+ target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NormalizeEscapes_UppercaseHexPreferred) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("", |
+ "scheme:/%1b%1B", |
+ &target_uri)); |
+ EXPECT_STREQ("scheme:/%1B%1B", |
+ target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NormalizeEscapes_Authority) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("", |
+ "scheme://UsEr N%61%4de@h%4FsT.c%6fm:80/", |
+ &target_uri)); |
+ // userinfo is normalized and case is preserved. host is normalized |
+ // and lower-cased. |
+ EXPECT_STREQ("scheme://UsEr%20NaMe@host.com:80/", target_uri); |
+} |
+ |
+ |
+TEST_CASE(ResolveUri_NormalizeEscapes_BrokenEscapeSequence) { |
+ const char* target_uri; |
+ EXPECT(ResolveUri("", |
+ "scheme:/%1g", |
+ &target_uri)); |
+ // We don't change broken escape sequences. |
+ EXPECT_STREQ("scheme:/%1g", |
+ target_uri); |
+} |
ahe
2016/05/25 12:44:52
I can't tell if this is a port of tests/corelib/ur
turnidge
2016/05/27 21:40:06
Well if it wasn't clear, it wasn't a port.
I have
|
+ |
+} // namespace dart |