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 ["file:///absolute", "/absolute", "\\absolute"], | |
21 ["file:///absolute", "/absolute", "\\absolute"], | |
22 ["file:///a/b", "/a/b", "\\a\\b"], | |
23 ["file:///a/b", "/a/b", "\\a\\b"], | |
24 ["file://server/a/b", unsupported, "\\\\server\\a\\b"], | |
25 ["file://server/a/b/", unsupported, "\\\\server\\a\\b\\"], | |
26 ["file:///C:/", "/C:/", "C:\\"], | |
27 ["file:///C:/a/b", "/C:/a/b", "C:\\a\\b"], | |
28 ["file:///C:/a/b/", "/C:/a/b/", "C:\\a\\b\\"], | |
29 ["http:/a/b", unsupported, unsupported], | |
30 ["https:/a/b", unsupported, unsupported], | |
31 ["urn:a:b", unsupported, unsupported], | |
32 ]; | |
33 | |
34 void check(String s, filePath, bool windows) { | |
35 Uri uri = Uri.parse(s); | |
36 if (filePath is Error) { | |
37 if (filePath is UnsupportedError) { | |
38 Expect.throws(() => uri.toFilePath(windows: windows), | |
39 (e) => e is UnsupportedError); | |
40 } else { | |
41 Expect.throws(() => uri.toFilePath(windows: windows)); | |
42 } | |
43 } else { | |
44 Expect.equals(filePath, uri.toFilePath(windows: windows)); | |
45 Expect.equals(s, new Uri.file(filePath, windows: windows).toString()); | |
46 } | |
47 } | |
48 | |
49 for (var test in tests) { | |
50 check(test[0], test[1], false); | |
51 check(test[0], test[2], true); | |
52 } | |
53 | |
54 Uri uri; | |
55 uri = Uri.parse("file:a"); | |
56 Expect.equals("/a", uri.toFilePath(windows: false)); | |
57 Expect.equals("\\a", uri.toFilePath(windows: true)); | |
58 uri = Uri.parse("file:a/"); | |
59 Expect.equals("/a/", uri.toFilePath(windows: false)); | |
60 Expect.equals("\\a\\", uri.toFilePath(windows: true)); | |
61 } | |
62 | |
63 testFileUriWindowsSlash() { | |
64 var tests = [ | |
65 ["", "", ""], | |
66 ["relative", "relative", "relative"], | |
67 ["relative/", "relative/", "relative\\"], | |
68 ["a%20b", "a b", "a b"], | |
69 ["a%20b/", "a b/", "a b\\"], | |
70 ["a/b", "a/b", "a\\b"], | |
71 ["a/b/", "a/b/", "a\\b\\"], | |
72 ["a%20b/c%20d", "a b/c d", "a b\\c d"], | |
73 ["a%20b/c%20d/", "a b/c d/", "a b\\c d\\"], | |
74 ["file:///absolute", "/absolute", "\\absolute"], | |
75 ["file:///absolute", "/absolute", "\\absolute"], | |
76 ["file:///a/b", "/a/b", "\\a\\b"], | |
77 ["file:///a/b", "/a/b", "\\a\\b"], | |
78 ["file://server/a/b", "//server/a/b", "\\\\server\\a\\b"], | |
79 ["file://server/a/b/", "//server/a/b/", "\\\\server\\a\\b\\"], | |
80 ["file:///C:/", "C:/", "C:\\"], | |
81 ["file:///C:/a/b", "C:/a/b", "C:\\a\\b"], | |
82 ["file:///C:/a/b/", "C:/a/b/", "C:\\a\\b\\"], | |
83 ["file:///C:/xxx/yyy", "C:\\xxx\\yyy", "C:\\xxx\\yyy"], | |
84 ]; | |
85 | |
86 for (var test in tests) { | |
87 Uri uri = new Uri.file(test[1], windows: true); | |
88 Expect.equals(test[0], uri.toString()); | |
89 Expect.equals(test[2], uri.toFilePath(windows: true)); | |
90 bool couldBeDir = uri.path.isEmpty || uri.path.endsWith('\\'); | |
91 Uri dirUri = new Uri.directory(test[1], windows: true); | |
92 Expect.isTrue(dirUri.path.isEmpty || dirUri.path.endsWith('/')); | |
93 if (couldBeDir) { | |
94 Expect.equals(uri, dirUri); | |
95 } | |
96 } | |
97 } | |
98 | |
99 testFileUriWindowsWin32Namespace() { | |
100 var tests = [ | |
101 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], | |
102 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], | |
103 [ | |
104 "\\\\?\\UNC\\server\\share\\file", | |
105 "file://server/share/file", | |
106 "\\\\server\\share\\file" | |
107 ], | |
108 ]; | |
109 | |
110 for (var test in tests) { | |
111 Uri uri = new Uri.file(test[0], windows: true); | |
112 Expect.equals(test[1], uri.toString()); | |
113 Expect.equals(test[2], uri.toFilePath(windows: true)); | |
114 } | |
115 | |
116 Expect.throws(() => new Uri.file("\\\\?\\file", windows: true), | |
117 (e) => e is ArgumentError); | |
118 Expect.throws( | |
119 () => new Uri.file("\\\\?\\UNX\\server\\share\\file", windows: true), | |
120 (e) => e is ArgumentError); | |
121 Expect.throws(() => new Uri.directory("\\\\?\\file", windows: true), | |
122 (e) => e is ArgumentError); | |
123 Expect.throws( | |
124 () => new Uri.directory("\\\\?\\UNX\\server\\share\\file", windows: true), | |
125 (e) => e is ArgumentError); | |
126 } | |
127 | |
128 testFileUriDriveLetter() { | |
129 check(String s, String nonWindows, String windows) { | |
130 Uri uri; | |
131 uri = Uri.parse(s); | |
132 Expect.equals(nonWindows, uri.toFilePath(windows: false)); | |
133 if (windows != null) { | |
134 Expect.equals(windows, uri.toFilePath(windows: true)); | |
135 } else { | |
136 Expect.throws( | |
137 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
138 } | |
139 } | |
140 | |
141 check("file:///C:", "/C:", "C:\\"); | |
142 check("file:///C:/", "/C:/", "C:\\"); | |
143 check("file:///C:a", "/C:a", null); | |
144 check("file:///C:a/", "/C:a/", null); | |
145 | |
146 Expect.throws( | |
147 () => new Uri.file("C:", windows: true), (e) => e is ArgumentError); | |
148 Expect.throws( | |
149 () => new Uri.file("C:a", windows: true), (e) => e is ArgumentError); | |
150 Expect.throws( | |
151 () => new Uri.file("C:a\b", windows: true), (e) => e is ArgumentError); | |
152 Expect.throws( | |
153 () => new Uri.directory("C:", windows: true), (e) => e is ArgumentError); | |
154 Expect.throws( | |
155 () => new Uri.directory("C:a", windows: true), (e) => e is ArgumentError); | |
156 Expect.throws(() => new Uri.directory("C:a\b", windows: true), | |
157 (e) => e is ArgumentError); | |
158 } | |
159 | |
160 testFileUriResolve() { | |
161 var tests = [ | |
162 ["file:///a", "/a", "", "\\a", ""], | |
163 ["file:///a/", "/a/", "", "\\a\\", ""], | |
164 ["file:///b", "/a", "b", "\\a", "b"], | |
165 ["file:///b/", "/a", "b/", "\\a", "b\\"], | |
166 ["file:///a/b", "/a/", "b", "\\a\\", "b"], | |
167 ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"], | |
168 ["file:///a/c/d", "/a/b", "c/d", "\\a\\b", "c\\d"], | |
169 ["file:///a/c/d/", "/a/b", "c/d/", "\\a\\b", "c\\d\\"], | |
170 ["file:///a/b/c/d", "/a/b/", "c/d", "\\a\\b\\", "c\\d"], | |
171 ["file:///a/b/c/d/", "/a/b/", "c/d/", "\\a\\b\\", "c\\d\\"], | |
172 ]; | |
173 | |
174 check(String s, String absolute, String relative, bool windows) { | |
175 Uri absoluteUri = new Uri.file(absolute, windows: windows); | |
176 Uri relativeUri = new Uri.file(relative, windows: windows); | |
177 String relativeString = windows ? relative.replaceAll("\\", "/") : relative; | |
178 Expect.equals(s, absoluteUri.resolve(relativeString).toString()); | |
179 Expect.equals(s, absoluteUri.resolveUri(relativeUri).toString()); | |
180 } | |
181 | |
182 for (var test in tests) { | |
183 check(test[0], test[1], test[2], false); | |
184 check(test[0], test[1], test[2], true); | |
185 check(test[0], test[1], test[4], true); | |
186 check(test[0], test[3], test[2], true); | |
187 check(test[0], test[3], test[4], true); | |
188 } | |
189 } | |
190 | |
191 testFileUriIllegalCharacters() { | |
192 // Slash is an invalid character in file names on both non-Windows | |
193 // and Windows. | |
194 Uri uri = Uri.parse("file:///a%2Fb"); | |
195 Expect.throws( | |
196 () => uri.toFilePath(windows: false), (e) => e is UnsupportedError); | |
197 Expect.throws( | |
198 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
199 | |
200 // Illegal characters in windows file names. | |
201 var illegalWindowsPaths = [ | |
202 "a<b", | |
203 "a>b", | |
204 "a:b", | |
205 "a\"b", | |
206 "a|b", | |
207 "a?b", | |
208 "a*b", | |
209 "\\\\?\\c:\\a/b" | |
210 ]; | |
211 | |
212 for (var test in illegalWindowsPaths) { | |
213 Expect.throws( | |
214 () => new Uri.file(test, windows: true), (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( | |
228 () => new Uri.file(test, windows: true), (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( | |
243 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
244 Expect.throws(() => absoluteUri.toFilePath(windows: true), | |
245 (e) => e is UnsupportedError); | |
246 Expect.throws( | |
247 () => dirUri.toFilePath(windows: true), (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( | |
271 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
272 Expect.throws(() => absoluteUri.toFilePath(windows: true), | |
273 (e) => e is UnsupportedError); | |
274 Expect.throws( | |
275 () => dirUri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
276 Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true), | |
277 (e) => e is UnsupportedError); | |
278 } | |
279 } | |
280 | |
281 testFileUriIllegalDriveLetter() { | |
282 Expect.throws( | |
283 () => new Uri.file("1:\\", windows: true), (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( | |
291 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
292 Expect.throws( | |
293 () => dirUri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
294 } | |
295 | |
296 testAdditionalComponents() { | |
297 check(String s, {bool windowsOk: false}) { | |
298 Uri uri = Uri.parse(s); | |
299 Expect.throws( | |
300 () => uri.toFilePath(windows: false), (e) => e is UnsupportedError); | |
301 if (windowsOk) { | |
302 Expect.isTrue(uri.toFilePath(windows: true) is String); | |
303 } else { | |
304 Expect.throws( | |
305 () => uri.toFilePath(windows: true), (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 |