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