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

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: 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 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 void testValidCharacters() {
128 const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ";
129 const DIGIT = r"0123456789";
130 const PERCENT_ENCODED = "%00%ff";
131 const SUBDELIM = r"!$&'()*+,;=";
132
133 const SCHEMECHAR = "$ALPHA$DIGIT+-.";
134 const UNRESERVED = "$ALPHA$DIGIT-._~";
135 const REGNAMECHAR = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED";
136 const USERINFOCHAR = "$REGNAMECHAR:";
137
138 const PCHAR_NC = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED@";
139 const PCHAR = "$PCHAR_NC:";
140 const QUERYCHAR = "$PCHAR/?";
141
142 void checkInvalid(uri) {
143 try {
144 var result = Uri.parse(uri);
145 Expect.fail("Invalid URI `$uri` parsed to $result\n"
146 " Scheme: ${result.scheme}\n"
147 " User-info: ${result.userInfo}\n"
148 " Host: ${result.host}\n"
149 " Port: ${result.port}\n"
150 " Path: ${result.path}\n"
151 " Query: ${result.query}\n"
152 " Fragment: ${result.fragment}\n");
153 } on FormatException {
154 // Success.
155 }
156 }
157
158 // test that all valid characters are accepted.
kevmoo 2014/06/10 21:29:08 split valid test into its own test method?
159
160 for (var scheme in ["", "$SCHEMECHAR$SCHEMECHAR:"]) {
161 for (var userinfo in ["", "@", "$USERINFOCHAR$USERINFOCHAR@",
162 "$USERINFOCHAR:$DIGIT@"]) {
163 for (var host in ["", "$REGNAMECHAR$REGNAMECHAR",
164 "255.255.255.256", // valid reg-name.
165 "[ffff::ffff:ffff]", "[ffff::255.255.255.255]"]) {
166 for (var port in ["", ":$DIGIT$DIGIT"]) {
167 var auth = "$userinfo$host$port";
168 if (auth.isNotEmpty) auth = "//$auth";
169 var paths = ["/", "/$PCHAR", "/$PCHAR/"];
170 if (auth.isNotEmpty) {
171 // Initial segemnt may be empty.
172 paths.add("//$PCHAR");
173 } else {
174 // Path may begin with non-slash.
175 paths.add("");
176 if (scheme.isEmpty) {
177 // Initial segment must not contain colon.
178 paths..add("$PCHAR_NC/$PCHAR")..add("$PCHAR_NC/$PCHAR/");
179 } else {
180 paths..add("$PCHAR")..add("$PCHAR/");
181 }
182 }
183 for (var path in paths) {
184 for (var query in ["", "?", "?$QUERYCHAR"]) {
185 for (var fragment in ["", "#", "#$QUERYCHAR"]) {
186 var uri = "$scheme$auth$path$query$fragment";
187 // Should not throw.
188 var result = Uri.parse(uri);
189 }
190 }
191 }
192 }
193 }
194 }
195 }
196
197 // Invalid characters. The characters must be rejected, even if normalizing
198 // the input would cause them to be valid (normalization happens after
199 // validation).
200 var invalidChars = [
201 "\xe7", // Arbitrary non-ASCII letter
202 " ", // Space, not allowed anywhere.
203 '"', // Quote, not allowed anywhere
204 "\x7f", // DEL, not allowed anywhere
205 "%1g", // Invalid escape.
206 "\xdf", // German lower-case scharf-S. Becomes ASCII when upper-cased.
207 "\u0130" // Latin capital dotted I, becomes ASCII lower-case in Turkish.
208 "%\uFB03", // % + Ligature ffi, becomes ASCII when upper-cased,
209 // should not be read as "%FFI".
210 "\u212a", // Kelvin sign. Becomes ASCII when lower-cased.
211 ];
212 for (var invalid in invalidChars) {
213 checkInvalid("$SCHEMECHAR${invalid}$SCHEMECHAR:///");
214 checkInvalid("${invalid}$SCHEMECHAR:///");
215 checkInvalid("s://user${invalid}info@x.x/");
216 checkInvalid("s://reg${invalid}name/");
217 checkInvalid("s://regname:12${invalid}45/");
218 checkInvalid("s://regname/p${invalid}ath/");
219 checkInvalid("/p${invalid}ath/");
220 checkInvalid("p${invalid}ath/");
221 checkInvalid("s://regname/path/?x${invalid}x");
222 checkInvalid("s://regname/path/#x${invalid}x");
223 checkInvalid("s://regname/path/?#x${invalid}x");
224 }
225 checkInvalid("s%41://x.x/"); // No escapes in scheme,
226 // and no colon before slash in path.
227 checkInvalid("1a://x.x/"); // Scheme must start with letter,
228 // and no colon before slash in path.
229 checkInvalid(".a://x.x/"); // Scheme must start with letter,
230 // and no colon before slash in path.
231 checkInvalid(":"); // Scheme must start with letter,
232 // and no colon before slash in path.
233 checkInvalid("s://x@x@x.x/"); // At most one @ in userinfo.
234 checkInvalid("s://x@x:x/"); // No colon in host except before a port.
235 checkInvalid("s://x@x:9:9/"); // At most one port.
236 checkInvalid("s://x/x#foo#bar"); // At most one #.
237 checkInvalid("s://:/"); // Colon in host requires port,
238 // and path may not start with //.
239 checkInvalid("s@://x:9/x?x#x"); // @ not allowed in scheme.
240 }
kevmoo 2014/06/10 21:32:04 Include examples provided in the bug - https://cod
241
127 main() { 242 main() {
128 testUri("http:", true); 243 testUri("http:", true);
129 testUri("file://", true); 244 testUri("file://", true);
130 testUri("file", false); 245 testUri("file", false);
131 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); 246 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", 247 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment",
133 false); 248 false);
134 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment", 249 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment",
135 new Uri( 250 new Uri(
136 scheme: "http", 251 scheme: "http",
137 userInfo: "user", 252 userInfo: "user",
138 host: "example.com", 253 host: "example.com",
139 port: 80, 254 port: 80,
140 path: "/a/b/c", 255 path: "/a/b/c",
141 query: "query", 256 query: "query",
142 fragment: "fragment").toString()); 257 fragment: "fragment").toString());
143 Expect.stringEquals("//null@null/a/b/c/", 258 Expect.stringEquals("//null@null/a/b/c/",
144 new Uri( 259 new Uri(
145 scheme: null, 260 scheme: null,
146 userInfo: null, 261 userInfo: null,
147 host: null, 262 host: null,
148 port: 0, 263 port: 0,
149 path: "/a/b/c/", 264 path: "/a/b/c/",
150 query: null, 265 query: null,
151 fragment: null).toString()); 266 fragment: null).toString());
152 Expect.stringEquals("file://", Uri.parse("file:").toString()); 267 Expect.stringEquals("file://", Uri.parse("file:").toString());
153
154 testResolvePath("/a/g", "/a/b/c/./../../g"); 268 testResolvePath("/a/g", "/a/b/c/./../../g");
155 testResolvePath("/a/g", "/a/b/c/./../../g"); 269 testResolvePath("/a/g", "/a/b/c/./../../g");
156 testResolvePath("/mid/6", "mid/content=5/../6"); 270 testResolvePath("/mid/6", "mid/content=5/../6");
157 testResolvePath("/a/b/e", "a/b/c/d/../../e"); 271 testResolvePath("/a/b/e", "a/b/c/d/../../e");
158 testResolvePath("/a/b/e", "../a/b/c/d/../../e"); 272 testResolvePath("/a/b/e", "../a/b/c/d/../../e");
159 testResolvePath("/a/b/e", "./a/b/c/d/../../e"); 273 testResolvePath("/a/b/e", "./a/b/c/d/../../e");
160 testResolvePath("/a/b/e", "../a/b/./c/d/../../e"); 274 testResolvePath("/a/b/e", "../a/b/./c/d/../../e");
161 testResolvePath("/a/b/e", "./a/b/./c/d/../../e"); 275 testResolvePath("/a/b/e", "./a/b/./c/d/../../e");
162 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/."); 276 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/.");
163 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./."); 277 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"); 368 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24");
255 testEncodeDecodeComponent(s, "%F0%90%80%80"); 369 testEncodeDecodeComponent(s, "%F0%90%80%80");
256 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); 370 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B");
257 testEncodeDecodeQueryComponent( 371 testEncodeDecodeQueryComponent(
258 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); 372 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null);
259 373
260 // Invalid URI - : and @ is swapped, port ("host") should be numeric. 374 // Invalid URI - : and @ is swapped, port ("host") should be numeric.
261 Expect.throws( 375 Expect.throws(
262 () => Uri.parse("file://user@password:host/path"), 376 () => Uri.parse("file://user@password:host/path"),
263 (e) => e is FormatException); 377 (e) => e is FormatException);
378
379 testValidCharacters();
264 } 380 }
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