OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 | |
7 testFileUri() { | |
8 final unsupported = new UnsupportedError(""); | |
9 | |
10 var tests = [ | |
11 ["", "", ""], | |
12 ["relative", "relative", "relative"], | |
13 ["relative/", "relative/", "relative\\"], | |
14 ["a%20b", "a b", "a b"], | |
15 ["a%20b/", "a b/", "a b\\"], | |
16 ["a/b", "a/b", "a\\b"], | |
17 ["a/b/", "a/b/", "a\\b\\"], | |
18 ["a%20b/c%20d", "a b/c d", "a b\\c d"], | |
19 ["a%20b/c%20d/", "a b/c d/", "a b\\c d\\"], | |
20 | |
21 ["file:///absolute", "/absolute", "\\absolute"], | |
22 ["file:///absolute", "/absolute", "\\absolute"], | |
23 ["file:///a/b", "/a/b", "\\a\\b"], | |
24 ["file:///a/b", "/a/b", "\\a\\b"], | |
25 | |
26 ["file://server/a/b", unsupported, "\\\\server\\a\\b"], | |
27 ["file://server/a/b/", unsupported, "\\\\server\\a\\b\\"], | |
28 | |
29 ["file:///C:/", "/C:/", "C:\\"], | |
30 ["file:///C:/a/b", "/C:/a/b", "C:\\a\\b"], | |
31 ["file:///C:/a/b/", "/C:/a/b/", "C:\\a\\b\\"], | |
32 | |
33 ["http:/a/b", unsupported, unsupported], | |
34 ["https:/a/b", unsupported, unsupported], | |
35 ["urn:a:b", unsupported, unsupported], | |
36 ]; | |
37 | |
38 void check(String s, filePath, bool windows) { | |
39 Uri uri = Uri.parse(s); | |
40 if (filePath is Error) { | |
41 if (filePath is UnsupportedError) { | |
42 Expect.throws(() => uri.toFilePath(windows: windows), | |
43 (e) => e is UnsupportedError); | |
44 } else { | |
45 Expect.throws(() => uri.toFilePath(windows: windows)); | |
46 } | |
47 } else { | |
48 Expect.equals(filePath, uri.toFilePath(windows: windows)); | |
49 Expect.equals( | |
50 s, new Uri.file(filePath, windows: windows).toString()); | |
51 } | |
52 } | |
53 | |
54 for (var test in tests) { | |
55 check(test[0], test[1], false); | |
56 check(test[0], test[2], true); | |
57 } | |
58 | |
59 Uri uri; | |
60 uri = Uri.parse("file:a"); | |
61 Expect.equals("/a", uri.toFilePath(windows: false)); | |
62 Expect.equals("\\a", uri.toFilePath(windows: true)); | |
63 uri = Uri.parse("file:a/"); | |
64 Expect.equals("/a/", uri.toFilePath(windows: false)); | |
65 Expect.equals("\\a\\", uri.toFilePath(windows: true)); | |
66 } | |
67 | |
68 testFileUriWindowsSlash() { | |
69 var tests = [ | |
70 ["", "", ""], | |
71 ["relative", "relative", "relative"], | |
72 ["relative/", "relative/", "relative\\"], | |
73 ["a%20b", "a b", "a b"], | |
74 ["a%20b/", "a b/", "a b\\"], | |
75 ["a/b", "a/b", "a\\b"], | |
76 ["a/b/", "a/b/", "a\\b\\"], | |
77 ["a%20b/c%20d", "a b/c d", "a b\\c d"], | |
78 ["a%20b/c%20d/", "a b/c d/", "a b\\c d\\"], | |
79 | |
80 ["file:///absolute", "/absolute", "\\absolute"], | |
81 ["file:///absolute", "/absolute", "\\absolute"], | |
82 ["file:///a/b", "/a/b", "\\a\\b"], | |
83 ["file:///a/b", "/a/b", "\\a\\b"], | |
84 | |
85 ["file://server/a/b", "//server/a/b", "\\\\server\\a\\b"], | |
86 ["file://server/a/b/", "//server/a/b/", "\\\\server\\a\\b\\"], | |
87 | |
88 ["file:///C:/", "C:/", "C:\\"], | |
89 ["file:///C:/a/b", "C:/a/b", "C:\\a\\b"], | |
90 ["file:///C:/a/b/", "C:/a/b/", "C:\\a\\b\\"], | |
91 ]; | |
92 | |
93 for (var test in tests) { | |
94 Uri uri = new Uri.file(test[1], windows: true); | |
95 Expect.equals(test[0], uri.toString()); | |
96 Expect.equals(test[2], uri.toFilePath(windows: true)); | |
97 bool couldBeDir = uri.path.isEmpty || uri.path.endsWith('\\'); | |
98 Uri dirUri = new Uri.directory(test[1], windows: true); | |
99 Expect.isTrue(dirUri.path.isEmpty || dirUri.path.endsWith('/')); | |
100 if (couldBeDir) { | |
101 Expect.equals(uri, dirUri); | |
102 } | |
103 } | |
104 } | |
105 | |
106 testFileUriWindowsWin32Namespace() { | |
107 var tests = [ | |
108 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], | |
109 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], | |
110 ["\\\\?\\UNC\\server\\share\\file", | |
111 "file://server/share/file", | |
112 "\\\\server\\share\\file"], | |
113 ]; | |
114 | |
115 for (var test in tests) { | |
116 Uri uri = new Uri.file(test[0], windows: true); | |
117 Expect.equals(test[1], uri.toString()); | |
118 Expect.equals(test[2], uri.toFilePath(windows: true)); | |
119 } | |
120 | |
121 Expect.throws( | |
122 () => new Uri.file("\\\\?\\file", windows: true), | |
123 (e) => e is ArgumentError); | |
124 Expect.throws( | |
125 () => new Uri.file("\\\\?\\UNX\\server\\share\\file", windows: true), | |
126 (e) => e is ArgumentError); | |
127 Expect.throws( | |
128 () => new Uri.directory("\\\\?\\file", windows: true), | |
129 (e) => e is ArgumentError); | |
130 Expect.throws( | |
131 () => new Uri.directory("\\\\?\\UNX\\server\\share\\file", windows: true), | |
132 (e) => e is ArgumentError); | |
133 } | |
134 | |
135 testFileUriDriveLetter() { | |
136 check(String s, String nonWindows, String windows) { | |
137 Uri uri; | |
138 uri = Uri.parse(s); | |
139 Expect.equals(nonWindows, uri.toFilePath(windows: false)); | |
140 if (windows != null) { | |
141 Expect.equals(windows, uri.toFilePath(windows: true)); | |
142 } else { | |
143 Expect.throws(() => uri.toFilePath(windows: true), | |
144 (e) => e is UnsupportedError); | |
145 } | |
146 } | |
147 | |
148 check("file:///C:", "/C:", "C:\\"); | |
149 check("file:///C:/", "/C:/", "C:\\"); | |
150 check("file:///C:a", "/C:a", null); | |
151 check("file:///C:a/", "/C:a/", null); | |
152 | |
153 Expect.throws(() => new Uri.file("C:", windows: true), | |
154 (e) => e is ArgumentError); | |
155 Expect.throws(() => new Uri.file("C:a", windows: true), | |
156 (e) => e is ArgumentError); | |
157 Expect.throws(() => new Uri.file("C:a\b", windows: true), | |
158 (e) => e is ArgumentError); | |
159 Expect.throws(() => new Uri.directory("C:", windows: true), | |
160 (e) => e is ArgumentError); | |
161 Expect.throws(() => new Uri.directory("C:a", windows: true), | |
162 (e) => e is ArgumentError); | |
163 Expect.throws(() => new Uri.directory("C:a\b", windows: true), | |
164 (e) => e is ArgumentError); | |
165 } | |
166 | |
167 testFileUriResolve() { | |
168 var tests = [ | |
169 ["file:///a", "/a", "", "\\a", ""], | |
170 ["file:///a/", "/a/", "", "\\a\\", ""], | |
171 ["file:///b", "/a", "b", "\\a", "b"], | |
172 ["file:///b/", "/a", "b/", "\\a", "b\\"], | |
173 ["file:///a/b", "/a/", "b", "\\a\\", "b"], | |
174 ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"], | |
175 ["file:///a/c/d", "/a/b", "c/d", "\\a\\b", "c\\d"], | |
176 ["file:///a/c/d/", "/a/b", "c/d/", "\\a\\b", "c\\d\\"], | |
177 ["file:///a/b/c/d", "/a/b/", "c/d", "\\a\\b\\", "c\\d"], | |
178 ["file:///a/b/c/d/", "/a/b/", "c/d/", "\\a\\b\\", "c\\d\\"], | |
179 ]; | |
180 | |
181 check(String s, String absolute, String relative, bool windows) { | |
182 Uri absoluteUri = new Uri.file(absolute, windows: windows); | |
183 Uri relativeUri = new Uri.file(relative, windows: windows); | |
184 String relativeString = | |
185 windows ? relative.replaceAll("\\", "/") : relative; | |
186 Expect.equals(s, absoluteUri.resolve(relativeString).toString()); | |
187 Expect.equals(s, absoluteUri.resolveUri(relativeUri).toString()); | |
188 } | |
189 | |
190 for (var test in tests) { | |
191 check(test[0], test[1], test[2], false); | |
192 check(test[0], test[1], test[2], true); | |
193 check(test[0], test[1], test[4], true); | |
194 check(test[0], test[3], test[2], true); | |
195 check(test[0], test[3], test[4], true); | |
196 } | |
197 } | |
198 | |
199 testFileUriIllegalCharacters() { | |
200 // Slash is an invalid character in file names on both non-Windows | |
201 // and Windows. | |
202 Uri uri = Uri.parse("file:///a%2Fb"); | |
203 Expect.throws(() => uri.toFilePath(windows: false), | |
204 (e) => e is UnsupportedError); | |
205 Expect.throws(() => uri.toFilePath(windows: true), | |
206 (e) => e is UnsupportedError); | |
207 | |
208 // Illegal characters in windows file names. | |
209 var illegalWindowsPaths = | |
210 ["a<b", "a>b", "a:b", "a\"b", "a|b", "a?b", "a*b", "\\\\?\\c:\\a/b"]; | |
211 | |
212 for (var test in illegalWindowsPaths) { | |
213 Expect.throws(() => new Uri.file(test, windows: true), | |
214 (e) => e is ArgumentError); | |
215 Expect.throws(() => new Uri.file("\\$test", windows: true), | |
216 (e) => e is ArgumentError); | |
217 Expect.throws(() => new Uri.directory(test, windows: true), | |
218 (e) => e is ArgumentError); | |
219 Expect.throws(() => new Uri.directory("\\$test", windows: true), | |
220 (e) => e is ArgumentError); | |
221 | |
222 // It is possible to create non-Windows URIs, but not Windows URIs. | |
223 Uri uri = new Uri.file(test, windows: false); | |
224 Uri absoluteUri = new Uri.file("/$test", windows: false); | |
225 Uri dirUri = new Uri.directory(test, windows: false); | |
226 Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false); | |
227 Expect.throws(() => new Uri.file(test, windows: true), | |
228 (e) => e is ArgumentError); | |
229 Expect.throws(() => new Uri.file("\\$test", windows: true), | |
230 (e) => e is ArgumentError); | |
231 Expect.throws(() => new Uri.directory(test, windows: true), | |
232 (e) => e is ArgumentError); | |
233 Expect.throws(() => new Uri.directory("\\$test", windows: true), | |
234 (e) => e is ArgumentError); | |
235 | |
236 // It is possible to extract non-Windows file path, but not | |
237 // Windows file path. | |
238 Expect.equals(test, uri.toFilePath(windows: false)); | |
239 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); | |
240 Expect.equals("$test/", dirUri.toFilePath(windows: false)); | |
241 Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false)); | |
242 Expect.throws(() => uri.toFilePath(windows: true), | |
243 (e) => e is UnsupportedError); | |
244 Expect.throws(() => absoluteUri.toFilePath(windows: true), | |
245 (e) => e is UnsupportedError); | |
246 Expect.throws(() => dirUri.toFilePath(windows: true), | |
247 (e) => e is UnsupportedError); | |
248 Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true), | |
249 (e) => e is UnsupportedError); | |
250 } | |
251 | |
252 // Backslash | |
253 illegalWindowsPaths = ["a\\b", "a\\b\\"]; | |
254 for (var test in illegalWindowsPaths) { | |
255 // It is possible to create both non-Windows URIs, and Windows URIs. | |
256 Uri uri = new Uri.file(test, windows: false); | |
257 Uri absoluteUri = new Uri.file("/$test", windows: false); | |
258 Uri dirUri = new Uri.directory(test, windows: false); | |
259 Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false); | |
260 new Uri.file(test, windows: true); | |
261 new Uri.file("\\$test", windows: true); | |
262 | |
263 // It is possible to extract non-Windows file path, but not | |
264 // Windows file path from the non-Windows URI (it has a backslash | |
265 // in a path segment). | |
266 Expect.equals(test, uri.toFilePath(windows: false)); | |
267 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); | |
268 Expect.equals("$test/", dirUri.toFilePath(windows: false)); | |
269 Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false)); | |
270 Expect.throws(() => uri.toFilePath(windows: true), | |
271 (e) => e is UnsupportedError); | |
272 Expect.throws(() => absoluteUri.toFilePath(windows: true), | |
273 (e) => e is UnsupportedError); | |
274 Expect.throws(() => dirUri.toFilePath(windows: true), | |
275 (e) => e is UnsupportedError); | |
276 Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true), | |
277 (e) => e is UnsupportedError); | |
278 } | |
279 } | |
280 | |
281 testFileUriIllegalDriveLetter() { | |
282 Expect.throws(() => new Uri.file("1:\\", windows: true), | |
283 (e) => e is ArgumentError); | |
284 Expect.throws(() => new Uri.directory("1:\\", windows: true), | |
285 (e) => e is ArgumentError); | |
286 Uri uri = new Uri.file("1:\\", windows: false); | |
287 Uri dirUri = new Uri.directory("1:\\", windows: false); | |
288 Expect.equals("1:\\", uri.toFilePath(windows: false)); | |
289 Expect.equals("1:\\/", dirUri.toFilePath(windows: false)); | |
290 Expect.throws(() => uri.toFilePath(windows: true), | |
291 (e) => e is UnsupportedError); | |
292 Expect.throws(() => dirUri.toFilePath(windows: true), | |
293 (e) => e is UnsupportedError); | |
294 } | |
295 | |
296 testAdditionalComponents() { | |
297 check(String s, {bool windowsOk: false}) { | |
298 Uri uri = Uri.parse(s); | |
299 Expect.throws(() => uri.toFilePath(windows: false), | |
300 (e) => e is UnsupportedError); | |
301 if (windowsOk) { | |
302 Expect.isTrue(uri.toFilePath(windows: true) is String); | |
303 } else { | |
304 Expect.throws(() => uri.toFilePath(windows: true), | |
305 (e) => e is UnsupportedError); | |
306 } | |
307 } | |
308 | |
309 check("file:///path?query"); | |
310 check("file:///path#fragment"); | |
311 check("file:///path?query#fragment"); | |
312 check("file://host/path", windowsOk: true); | |
313 check("file://user:password@host/path", windowsOk: true); | |
314 } | |
315 | |
316 main() { | |
317 testFileUri(); | |
318 testFileUriWindowsSlash(); | |
319 testFileUriDriveLetter(); | |
320 testFileUriWindowsWin32Namespace(); | |
321 testFileUriResolve(); | |
322 testFileUriIllegalCharacters(); | |
323 testFileUriIllegalDriveLetter(); | |
324 testAdditionalComponents(); | |
325 } | |
OLD | NEW |