| 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 ]; | |
| 84 | |
| 85 for (var test in tests) { | |
| 86 Uri uri = new Uri.file(test[1], windows: true); | |
| 87 Expect.equals(test[0], uri.toString()); | |
| 88 Expect.equals(test[2], uri.toFilePath(windows: true)); | |
| 89 bool couldBeDir = uri.path.isEmpty || uri.path.endsWith('\\'); | |
| 90 Uri dirUri = new Uri.directory(test[1], windows: true); | |
| 91 Expect.isTrue(dirUri.path.isEmpty || dirUri.path.endsWith('/')); | |
| 92 if (couldBeDir) { | |
| 93 Expect.equals(uri, dirUri); | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 testFileUriWindowsWin32Namespace() { | |
| 99 var tests = [ | |
| 100 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], | |
| 101 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], | |
| 102 [ | |
| 103 "\\\\?\\UNC\\server\\share\\file", | |
| 104 "file://server/share/file", | |
| 105 "\\\\server\\share\\file" | |
| 106 ], | |
| 107 ]; | |
| 108 | |
| 109 for (var test in tests) { | |
| 110 Uri uri = new Uri.file(test[0], windows: true); | |
| 111 Expect.equals(test[1], uri.toString()); | |
| 112 Expect.equals(test[2], uri.toFilePath(windows: true)); | |
| 113 } | |
| 114 | |
| 115 Expect.throws(() => new Uri.file("\\\\?\\file", windows: true), | |
| 116 (e) => e is ArgumentError); | |
| 117 Expect.throws( | |
| 118 () => new Uri.file("\\\\?\\UNX\\server\\share\\file", windows: true), | |
| 119 (e) => e is ArgumentError); | |
| 120 Expect.throws(() => new Uri.directory("\\\\?\\file", windows: true), | |
| 121 (e) => e is ArgumentError); | |
| 122 Expect.throws( | |
| 123 () => new Uri.directory("\\\\?\\UNX\\server\\share\\file", windows: true), | |
| 124 (e) => e is ArgumentError); | |
| 125 } | |
| 126 | |
| 127 testFileUriDriveLetter() { | |
| 128 check(String s, String nonWindows, String windows) { | |
| 129 Uri uri; | |
| 130 uri = Uri.parse(s); | |
| 131 Expect.equals(nonWindows, uri.toFilePath(windows: false)); | |
| 132 if (windows != null) { | |
| 133 Expect.equals(windows, uri.toFilePath(windows: true)); | |
| 134 } else { | |
| 135 Expect.throws( | |
| 136 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 check("file:///C:", "/C:", "C:\\"); | |
| 141 check("file:///C:/", "/C:/", "C:\\"); | |
| 142 check("file:///C:a", "/C:a", null); | |
| 143 check("file:///C:a/", "/C:a/", null); | |
| 144 | |
| 145 Expect.throws( | |
| 146 () => new Uri.file("C:", windows: true), (e) => e is ArgumentError); | |
| 147 Expect.throws( | |
| 148 () => new Uri.file("C:a", windows: true), (e) => e is ArgumentError); | |
| 149 Expect.throws( | |
| 150 () => new Uri.file("C:a\b", windows: true), (e) => e is ArgumentError); | |
| 151 Expect.throws( | |
| 152 () => new Uri.directory("C:", windows: true), (e) => e is ArgumentError); | |
| 153 Expect.throws( | |
| 154 () => new Uri.directory("C:a", windows: true), (e) => e is ArgumentError); | |
| 155 Expect.throws(() => new Uri.directory("C:a\b", windows: true), | |
| 156 (e) => e is ArgumentError); | |
| 157 } | |
| 158 | |
| 159 testFileUriResolve() { | |
| 160 var tests = [ | |
| 161 ["file:///a", "/a", "", "\\a", ""], | |
| 162 ["file:///a/", "/a/", "", "\\a\\", ""], | |
| 163 ["file:///b", "/a", "b", "\\a", "b"], | |
| 164 ["file:///b/", "/a", "b/", "\\a", "b\\"], | |
| 165 ["file:///a/b", "/a/", "b", "\\a\\", "b"], | |
| 166 ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"], | |
| 167 ["file:///a/c/d", "/a/b", "c/d", "\\a\\b", "c\\d"], | |
| 168 ["file:///a/c/d/", "/a/b", "c/d/", "\\a\\b", "c\\d\\"], | |
| 169 ["file:///a/b/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 ]; | |
| 172 | |
| 173 check(String s, String absolute, String relative, bool windows) { | |
| 174 Uri absoluteUri = new Uri.file(absolute, windows: windows); | |
| 175 Uri relativeUri = new Uri.file(relative, windows: windows); | |
| 176 String relativeString = windows ? relative.replaceAll("\\", "/") : relative; | |
| 177 Expect.equals(s, absoluteUri.resolve(relativeString).toString()); | |
| 178 Expect.equals(s, absoluteUri.resolveUri(relativeUri).toString()); | |
| 179 } | |
| 180 | |
| 181 for (var test in tests) { | |
| 182 check(test[0], test[1], test[2], false); | |
| 183 check(test[0], test[1], test[2], true); | |
| 184 check(test[0], test[1], test[4], true); | |
| 185 check(test[0], test[3], test[2], true); | |
| 186 check(test[0], test[3], test[4], true); | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 testFileUriIllegalCharacters() { | |
| 191 // Slash is an invalid character in file names on both non-Windows | |
| 192 // and Windows. | |
| 193 Uri uri = Uri.parse("file:///a%2Fb"); | |
| 194 Expect.throws( | |
| 195 () => uri.toFilePath(windows: false), (e) => e is UnsupportedError); | |
| 196 Expect.throws( | |
| 197 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 198 | |
| 199 // Illegal characters in windows file names. | |
| 200 var illegalWindowsPaths = [ | |
| 201 "a<b", | |
| 202 "a>b", | |
| 203 "a:b", | |
| 204 "a\"b", | |
| 205 "a|b", | |
| 206 "a?b", | |
| 207 "a*b", | |
| 208 "\\\\?\\c:\\a/b" | |
| 209 ]; | |
| 210 | |
| 211 for (var test in illegalWindowsPaths) { | |
| 212 Expect.throws( | |
| 213 () => new Uri.file(test, windows: true), (e) => e is ArgumentError); | |
| 214 Expect.throws(() => new Uri.file("\\$test", windows: true), | |
| 215 (e) => e is ArgumentError); | |
| 216 Expect.throws(() => new Uri.directory(test, windows: true), | |
| 217 (e) => e is ArgumentError); | |
| 218 Expect.throws(() => new Uri.directory("\\$test", windows: true), | |
| 219 (e) => e is ArgumentError); | |
| 220 | |
| 221 // It is possible to create non-Windows URIs, but not Windows URIs. | |
| 222 Uri uri = new Uri.file(test, windows: false); | |
| 223 Uri absoluteUri = new Uri.file("/$test", windows: false); | |
| 224 Uri dirUri = new Uri.directory(test, windows: false); | |
| 225 Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false); | |
| 226 Expect.throws( | |
| 227 () => new Uri.file(test, windows: true), (e) => e is ArgumentError); | |
| 228 Expect.throws(() => new Uri.file("\\$test", windows: true), | |
| 229 (e) => e is ArgumentError); | |
| 230 Expect.throws(() => new Uri.directory(test, windows: true), | |
| 231 (e) => e is ArgumentError); | |
| 232 Expect.throws(() => new Uri.directory("\\$test", windows: true), | |
| 233 (e) => e is ArgumentError); | |
| 234 | |
| 235 // It is possible to extract non-Windows file path, but not | |
| 236 // Windows file path. | |
| 237 Expect.equals(test, uri.toFilePath(windows: false)); | |
| 238 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); | |
| 239 Expect.equals("$test/", dirUri.toFilePath(windows: false)); | |
| 240 Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false)); | |
| 241 Expect.throws( | |
| 242 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 243 Expect.throws(() => absoluteUri.toFilePath(windows: true), | |
| 244 (e) => e is UnsupportedError); | |
| 245 Expect.throws( | |
| 246 () => dirUri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 247 Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true), | |
| 248 (e) => e is UnsupportedError); | |
| 249 } | |
| 250 | |
| 251 // Backslash | |
| 252 illegalWindowsPaths = ["a\\b", "a\\b\\"]; | |
| 253 for (var test in illegalWindowsPaths) { | |
| 254 // It is possible to create both non-Windows URIs, and Windows URIs. | |
| 255 Uri uri = new Uri.file(test, windows: false); | |
| 256 Uri absoluteUri = new Uri.file("/$test", windows: false); | |
| 257 Uri dirUri = new Uri.directory(test, windows: false); | |
| 258 Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false); | |
| 259 new Uri.file(test, windows: true); | |
| 260 new Uri.file("\\$test", windows: true); | |
| 261 | |
| 262 // It is possible to extract non-Windows file path, but not | |
| 263 // Windows file path from the non-Windows URI (it has a backslash | |
| 264 // in a path segment). | |
| 265 Expect.equals(test, uri.toFilePath(windows: false)); | |
| 266 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); | |
| 267 Expect.equals("$test/", dirUri.toFilePath(windows: false)); | |
| 268 Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false)); | |
| 269 Expect.throws( | |
| 270 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 271 Expect.throws(() => absoluteUri.toFilePath(windows: true), | |
| 272 (e) => e is UnsupportedError); | |
| 273 Expect.throws( | |
| 274 () => dirUri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 275 Expect.throws(() => dirAbsoluteUri.toFilePath(windows: true), | |
| 276 (e) => e is UnsupportedError); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 testFileUriIllegalDriveLetter() { | |
| 281 Expect.throws( | |
| 282 () => new Uri.file("1:\\", windows: true), (e) => e is ArgumentError); | |
| 283 Expect.throws(() => new Uri.directory("1:\\", windows: true), | |
| 284 (e) => e is ArgumentError); | |
| 285 Uri uri = new Uri.file("1:\\", windows: false); | |
| 286 Uri dirUri = new Uri.directory("1:\\", windows: false); | |
| 287 Expect.equals("1:\\", uri.toFilePath(windows: false)); | |
| 288 Expect.equals("1:\\/", dirUri.toFilePath(windows: false)); | |
| 289 Expect.throws( | |
| 290 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 291 Expect.throws( | |
| 292 () => dirUri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 293 } | |
| 294 | |
| 295 testAdditionalComponents() { | |
| 296 check(String s, {bool windowsOk: false}) { | |
| 297 Uri uri = Uri.parse(s); | |
| 298 Expect.throws( | |
| 299 () => uri.toFilePath(windows: false), (e) => e is UnsupportedError); | |
| 300 if (windowsOk) { | |
| 301 Expect.isTrue(uri.toFilePath(windows: true) is String); | |
| 302 } else { | |
| 303 Expect.throws( | |
| 304 () => uri.toFilePath(windows: true), (e) => e is UnsupportedError); | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 check("file:///path?query"); | |
| 309 check("file:///path#fragment"); | |
| 310 check("file:///path?query#fragment"); | |
| 311 check("file://host/path", windowsOk: true); | |
| 312 check("file://user:password@host/path", windowsOk: true); | |
| 313 } | |
| 314 | |
| 315 main() { | |
| 316 testFileUri(); | |
| 317 testFileUriWindowsSlash(); | |
| 318 testFileUriDriveLetter(); | |
| 319 testFileUriWindowsWin32Namespace(); | |
| 320 testFileUriResolve(); | |
| 321 testFileUriIllegalCharacters(); | |
| 322 testFileUriIllegalDriveLetter(); | |
| 323 testAdditionalComponents(); | |
| 324 } | |
| OLD | NEW |