| 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; |
| 95 uri = new Uri.file(test[1], windows: true); |
| 96 Expect.equals(test[0], uri.toString()); |
| 97 Expect.equals(test[2], uri.toFilePath(windows: true)); |
| 98 } |
| 99 } |
| 100 |
| 101 testFileUriWindowsWin32Namespace() { |
| 102 var tests = [ |
| 103 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], |
| 104 ["\\\\?\\C:\\", "file:///C:/", "C:\\"], |
| 105 ["\\\\?\\UNC\\server\\share\\file", |
| 106 "file://server/share/file", |
| 107 "\\\\server\\share\\file"], |
| 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( |
| 117 () => new Uri.file("\\\\?\\file", windows: true), |
| 118 (e) => e is ArgumentError); |
| 119 Expect.throws( |
| 120 () => new Uri.file("\\\\?\\UNX\\server\\share\\file", windows: true), |
| 121 (e) => e is ArgumentError); |
| 122 } |
| 123 |
| 124 testFileUriDriveLetter() { |
| 125 check(String s, String nonWindows, String windows) { |
| 126 Uri uri; |
| 127 uri = Uri.parse(s); |
| 128 Expect.equals(nonWindows, uri.toFilePath(windows: false)); |
| 129 if (windows != null) { |
| 130 Expect.equals(windows, uri.toFilePath(windows: true)); |
| 131 } else { |
| 132 Expect.throws(() => uri.toFilePath(windows: true), |
| 133 (e) => e is UnsupportedError); |
| 134 } |
| 135 } |
| 136 |
| 137 check("file:///C:", "/C:", "C:\\"); |
| 138 check("file:///C:/", "/C:/", "C:\\"); |
| 139 check("file:///C:a", "/C:a", null); |
| 140 check("file:///C:a/", "/C:a/", null); |
| 141 |
| 142 Expect.throws(() => new Uri.file("C:", windows: true), |
| 143 (e) => e is ArgumentError); |
| 144 Expect.throws(() => new Uri.file("C:a", windows: true), |
| 145 (e) => e is ArgumentError); |
| 146 Expect.throws(() => new Uri.file("C:a\b", windows: true), |
| 147 (e) => e is ArgumentError); |
| 148 } |
| 149 |
| 150 testFileUriResolve() { |
| 151 var tests = [ |
| 152 ["file:///a", "/a", "", "\\a", ""], |
| 153 ["file:///a/", "/a/", "", "\\a\\", ""], |
| 154 ["file:///b", "/a", "b", "\\a", "b"], |
| 155 ["file:///b/", "/a", "b/", "\\a", "b\\"], |
| 156 ["file:///a/b", "/a/", "b", "\\a\\", "b"], |
| 157 ["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"], |
| 158 ["file:///a/c/d", "/a/b", "c/d", "\\a\\b", "c\\d"], |
| 159 ["file:///a/c/d/", "/a/b", "c/d/", "\\a\\b", "c\\d\\"], |
| 160 ["file:///a/b/c/d", "/a/b/", "c/d", "\\a\\b\\", "c\\d"], |
| 161 ["file:///a/b/c/d/", "/a/b/", "c/d/", "\\a\\b\\", "c\\d\\"], |
| 162 ]; |
| 163 |
| 164 check(String s, String absolute, String relative, bool windows) { |
| 165 Uri absoluteUri = new Uri.file(absolute, windows: windows); |
| 166 Uri relativeUri = new Uri.file(relative, windows: windows); |
| 167 String relativeString = |
| 168 windows ? relative.replaceAll("\\", "/") : relative; |
| 169 Expect.equals(s, absoluteUri.resolve(relativeString).toString()); |
| 170 Expect.equals(s, absoluteUri.resolveUri(relativeUri).toString()); |
| 171 } |
| 172 |
| 173 for (var test in tests) { |
| 174 check(test[0], test[1], test[2], false); |
| 175 check(test[0], test[1], test[2], true); |
| 176 check(test[0], test[1], test[4], true); |
| 177 check(test[0], test[3], test[2], true); |
| 178 check(test[0], test[3], test[4], true); |
| 179 } |
| 180 } |
| 181 |
| 182 testFileUriIllegalCharacters() { |
| 183 // Slash is an invalid character in file names on both non-Windows |
| 184 // and Windows. |
| 185 Uri uri = Uri.parse("file:///a%2Fb"); |
| 186 Expect.throws(() => uri.toFilePath(windows: false), |
| 187 (e) => e is UnsupportedError); |
| 188 Expect.throws(() => uri.toFilePath(windows: true), |
| 189 (e) => e is UnsupportedError); |
| 190 |
| 191 // Illegal characters in windows file names. |
| 192 var illegalWindowsPaths = |
| 193 ["a<b", "a>b", "a:b", "a\"b", "a|b", "a?b", "a*b", "\\\\?\\c:\\a/b"]; |
| 194 |
| 195 for (var test in illegalWindowsPaths) { |
| 196 Expect.throws(() => new Uri.file(test, windows: true), |
| 197 (e) => e is ArgumentError); |
| 198 Expect.throws(() => new Uri.file("\\$test", windows: true), |
| 199 (e) => e is ArgumentError); |
| 200 |
| 201 // It is possible to create non-Windows URIs, but not Windows URIs. |
| 202 Uri uri = new Uri.file(test, windows: false); |
| 203 Uri absoluteUri = new Uri.file("/$test", windows: false); |
| 204 Expect.throws(() => new Uri.file(test, windows: true)); |
| 205 Expect.throws(() => new Uri.file("\\$test", windows: true)); |
| 206 |
| 207 // It is possible to extract non-Windows file path, but not |
| 208 // Windows file path. |
| 209 Expect.equals(test, uri.toFilePath(windows: false)); |
| 210 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); |
| 211 Expect.throws(() => uri.toFilePath(windows: true)); |
| 212 Expect.throws(() => absoluteUri.toFilePath(windows: true)); |
| 213 } |
| 214 |
| 215 // Backslash |
| 216 illegalWindowsPaths = ["a\\b", "a\\b\\"]; |
| 217 for (var test in illegalWindowsPaths) { |
| 218 // It is possible to create both non-Windows URIs, and Windows URIs. |
| 219 Uri uri = new Uri.file(test, windows: false); |
| 220 Uri absoluteUri = new Uri.file("/$test", windows: false); |
| 221 new Uri.file(test, windows: true); |
| 222 new Uri.file("\\$test", windows: true); |
| 223 |
| 224 // It is possible to extract non-Windows file path, but not |
| 225 // Windows file path from the non-Windows URI (it has a backslash |
| 226 // in a path segment). |
| 227 Expect.equals(test, uri.toFilePath(windows: false)); |
| 228 Expect.equals("/$test", absoluteUri.toFilePath(windows: false)); |
| 229 Expect.throws(() => uri.toFilePath(windows: true), |
| 230 (e) => e is UnsupportedError); |
| 231 Expect.throws(() => absoluteUri.toFilePath(windows: true)); |
| 232 } |
| 233 } |
| 234 |
| 235 testFileUriIllegalDriveLetter() { |
| 236 Expect.throws(() => new Uri.file("1:\\", windows: true), |
| 237 (e) => e is ArgumentError); |
| 238 Uri uri = new Uri.file("1:\\", windows: false); |
| 239 Expect.equals("1:\\", uri.toFilePath(windows: false)); |
| 240 Expect.throws(() => uri.toFilePath(windows: true), |
| 241 (e) => e is UnsupportedError); |
| 242 } |
| 243 |
| 244 testAdditionalComponents() { |
| 245 check(String s, {bool windowsOk: false}) { |
| 246 Uri uri = Uri.parse(s); |
| 247 Expect.throws(() => uri.toFilePath(windows: false), |
| 248 (e) => e is UnsupportedError); |
| 249 if (windowsOk) { |
| 250 Expect.isTrue(uri.toFilePath(windows: true) is String); |
| 251 } else { |
| 252 Expect.throws(() => uri.toFilePath(windows: true), |
| 253 (e) => e is UnsupportedError); |
| 254 } |
| 255 } |
| 256 |
| 257 check("file:///path?query"); |
| 258 check("file:///path#fragment"); |
| 259 check("file:///path?query#fragment"); |
| 260 check("file://host/path", windowsOk: true); |
| 261 check("file://user@password:host/path"); |
| 262 } |
| 263 |
| 264 main() { |
| 265 testFileUri(); |
| 266 testFileUriWindowsSlash(); |
| 267 testFileUriDriveLetter(); |
| 268 testFileUriWindowsWin32Namespace(); |
| 269 testFileUriResolve(); |
| 270 testFileUriIllegalCharacters(); |
| 271 testFileUriIllegalDriveLetter(); |
| 272 testAdditionalComponents(); |
| 273 } |
| OLD | NEW |