|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 function run_test() { |
|
6 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
7 Components.utils.import("resource://gre/modules/osfile.jsm"); |
|
8 Components.utils.import("resource://gre/modules/FileUtils.jsm"); |
|
9 |
|
10 let isWindows = ("@mozilla.org/windows-registry-key;1" in Components.classes); |
|
11 |
|
12 // Test cases for filePathToURI |
|
13 let paths = isWindows ? [ |
|
14 'C:\\', |
|
15 'C:\\test', |
|
16 'C:\\test\\', |
|
17 'C:\\test%2f', |
|
18 'C:\\test\\test\\test', |
|
19 'C:\\test;+%', |
|
20 'C:\\test?action=index\\', |
|
21 'C:\\test\ test', |
|
22 '\\\\C:\\a\\b\\c', |
|
23 '\\\\Server\\a\\b\\c', |
|
24 |
|
25 // note that per http://support.microsoft.com/kb/177506 (under more info), |
|
26 // the following characters are allowed on Windows: |
|
27 'C:\\char^', |
|
28 'C:\\char&', |
|
29 'C:\\char\'', |
|
30 'C:\\char@', |
|
31 'C:\\char{', |
|
32 'C:\\char}', |
|
33 'C:\\char[', |
|
34 'C:\\char]', |
|
35 'C:\\char,', |
|
36 'C:\\char$', |
|
37 'C:\\char=', |
|
38 'C:\\char!', |
|
39 'C:\\char-', |
|
40 'C:\\char#', |
|
41 'C:\\char(', |
|
42 'C:\\char)', |
|
43 'C:\\char%', |
|
44 'C:\\char.', |
|
45 'C:\\char+', |
|
46 'C:\\char~', |
|
47 'C:\\char_' |
|
48 ] : [ |
|
49 '/', |
|
50 '/test', |
|
51 '/test/', |
|
52 '/test%2f', |
|
53 '/test/test/test', |
|
54 '/test;+%', |
|
55 '/test?action=index/', |
|
56 '/test\ test', |
|
57 '/punctuation/;,/?:@&=+$-_.!~*\'()"#', |
|
58 '/CasePreserving' |
|
59 ]; |
|
60 |
|
61 // some additional URIs to test, beyond those generated from paths |
|
62 let uris = isWindows ? [ |
|
63 'file:///C:/test/', |
|
64 'file://localhost/C:/test', |
|
65 'file:///c:/test/test.txt', |
|
66 //'file:///C:/foo%2f', // trailing, encoded slash |
|
67 'file:///C:/%3f%3F', |
|
68 'file:///C:/%3b%3B', |
|
69 'file:///C:/%3c%3C', // not one of the special-cased ? or ; |
|
70 'file:///C:/%78', // 'x', not usually uri encoded |
|
71 'file:///C:/test#frag', // a fragment identifier |
|
72 'file:///C:/test?action=index' // an actual query component |
|
73 ] : [ |
|
74 'file:///test/', |
|
75 'file://localhost/test', |
|
76 'file:///test/test.txt', |
|
77 'file:///foo%2f', // trailing, encoded slash |
|
78 'file:///%3f%3F', |
|
79 'file:///%3b%3B', |
|
80 'file:///%3c%3C', // not one of the special-cased ? or ; |
|
81 'file:///%78', // 'x', not usually uri encoded |
|
82 'file:///test#frag', // a fragment identifier |
|
83 'file:///test?action=index' // an actual query component |
|
84 ]; |
|
85 |
|
86 for (let path of paths) { |
|
87 // convert that to a uri using FileUtils and Services, which toFileURI is trying to model |
|
88 let file = FileUtils.File(path); |
|
89 let uri = Services.io.newFileURI(file).spec; |
|
90 do_check_eq(uri, OS.Path.toFileURI(path)); |
|
91 |
|
92 // keep the resulting URI to try the reverse, except for "C:\" for which the |
|
93 // behavior of nsIFileURL and OS.File is inconsistent |
|
94 if (path != "C:\\") { |
|
95 uris.push(uri); |
|
96 } |
|
97 } |
|
98 |
|
99 for (let uri of uris) { |
|
100 // convert URIs to paths with nsIFileURI, which fromFileURI is trying to model |
|
101 let path = Services.io.newURI(uri, null, null).QueryInterface(Components.interfaces.nsIFileURL).file.path; |
|
102 do_check_eq(path, OS.Path.fromFileURI(uri)); |
|
103 } |
|
104 |
|
105 // check that non-file URLs aren't allowed |
|
106 let thrown = false; |
|
107 try { |
|
108 OS.Path.fromFileURI('http://test.com') |
|
109 } catch (e) { |
|
110 do_check_eq(e.message, "fromFileURI expects a file URI"); |
|
111 thrown = true; |
|
112 } |
|
113 do_check_true(thrown); |
|
114 } |