|
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 |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 Components.utils.import("resource://gre/modules/Services.jsm", this); |
|
8 Services.prefs.setBoolPref("toolkit.osfile.test.syslib_necessary", false); |
|
9 // We don't need libc/kernel32.dll for this test |
|
10 |
|
11 let ImportWin = {}; |
|
12 let ImportUnix = {}; |
|
13 Components.utils.import("resource://gre/modules/osfile/ospath_win.jsm", ImportWin); |
|
14 Components.utils.import("resource://gre/modules/osfile/ospath_unix.jsm", ImportUnix); |
|
15 |
|
16 let Win = ImportWin; |
|
17 let Unix = ImportUnix; |
|
18 |
|
19 function do_check_fail(f) |
|
20 { |
|
21 try { |
|
22 let result = f(); |
|
23 do_print("Failed do_check_fail: " + result); |
|
24 do_check_true(false); |
|
25 } catch (ex) { |
|
26 do_check_true(true); |
|
27 } |
|
28 }; |
|
29 |
|
30 function run_test() |
|
31 { |
|
32 do_print("Testing Windows paths"); |
|
33 |
|
34 do_print("Backslash-separated, no drive"); |
|
35 do_check_eq(Win.basename("a\\b"), "b"); |
|
36 do_check_eq(Win.basename("a\\b\\"), ""); |
|
37 do_check_eq(Win.basename("abc"), "abc"); |
|
38 do_check_eq(Win.dirname("a\\b"), "a"); |
|
39 do_check_eq(Win.dirname("a\\b\\"), "a\\b"); |
|
40 do_check_eq(Win.dirname("a\\\\\\\\b"), "a"); |
|
41 do_check_eq(Win.dirname("abc"), "."); |
|
42 do_check_eq(Win.normalize("\\a\\b\\c"), "\\a\\b\\c"); |
|
43 do_check_eq(Win.normalize("\\a\\b\\\\\\\\c"), "\\a\\b\\c"); |
|
44 do_check_eq(Win.normalize("\\a\\b\\c\\\\\\"), "\\a\\b\\c"); |
|
45 do_check_eq(Win.normalize("\\a\\b\\c\\..\\..\\..\\d\\e\\f"), "\\d\\e\\f"); |
|
46 do_check_eq(Win.normalize("a\\b\\c\\..\\..\\..\\d\\e\\f"), "d\\e\\f"); |
|
47 do_check_fail(function() Win.normalize("\\a\\b\\c\\..\\..\\..\\..\\d\\e\\f")); |
|
48 |
|
49 do_check_eq(Win.join("\\tmp", "foo", "bar"), "\\tmp\\foo\\bar", "join \\tmp,foo,bar"); |
|
50 do_check_eq(Win.join("\\tmp", "\\foo", "bar"), "\\foo\\bar", "join \\tmp,\\foo,bar"); |
|
51 do_check_eq(Win.winGetDrive("\\tmp"), null); |
|
52 do_check_eq(Win.winGetDrive("\\tmp\\a\\b\\c\\d\\e"), null); |
|
53 do_check_eq(Win.winGetDrive("\\"), null); |
|
54 |
|
55 |
|
56 do_print("Backslash-separated, with a drive"); |
|
57 do_check_eq(Win.basename("c:a\\b"), "b"); |
|
58 do_check_eq(Win.basename("c:a\\b\\"), ""); |
|
59 do_check_eq(Win.basename("c:abc"), "abc"); |
|
60 do_check_eq(Win.dirname("c:a\\b"), "c:a"); |
|
61 do_check_eq(Win.dirname("c:a\\b\\"), "c:a\\b"); |
|
62 do_check_eq(Win.dirname("c:a\\\\\\\\b"), "c:a"); |
|
63 do_check_eq(Win.dirname("c:abc"), "c:"); |
|
64 let options = { |
|
65 winNoDrive: true |
|
66 }; |
|
67 do_check_eq(Win.dirname("c:a\\b", options), "a"); |
|
68 do_check_eq(Win.dirname("c:a\\b\\", options), "a\\b"); |
|
69 do_check_eq(Win.dirname("c:a\\\\\\\\b", options), "a"); |
|
70 do_check_eq(Win.dirname("c:abc", options), "."); |
|
71 do_check_eq(Win.join("c:", "abc"), "c:\\abc", "join c:,abc"); |
|
72 |
|
73 do_check_eq(Win.normalize("c:"), "c:\\"); |
|
74 do_check_eq(Win.normalize("c:\\"), "c:\\"); |
|
75 do_check_eq(Win.normalize("c:\\a\\b\\c"), "c:\\a\\b\\c"); |
|
76 do_check_eq(Win.normalize("c:\\a\\b\\\\\\\\c"), "c:\\a\\b\\c"); |
|
77 do_check_eq(Win.normalize("c:\\\\\\\\a\\b\\c"), "c:\\a\\b\\c"); |
|
78 do_check_eq(Win.normalize("c:\\a\\b\\c\\\\\\"), "c:\\a\\b\\c"); |
|
79 do_check_eq(Win.normalize("c:\\a\\b\\c\\..\\..\\..\\d\\e\\f"), "c:\\d\\e\\f"); |
|
80 do_check_eq(Win.normalize("c:a\\b\\c\\..\\..\\..\\d\\e\\f"), "c:\\d\\e\\f"); |
|
81 do_check_fail(function() Win.normalize("c:\\a\\b\\c\\..\\..\\..\\..\\d\\e\\f")); |
|
82 |
|
83 do_check_eq(Win.join("c:\\", "foo"), "c:\\foo", "join c:\,foo"); |
|
84 do_check_eq(Win.join("c:\\tmp", "foo", "bar"), "c:\\tmp\\foo\\bar", "join c:\\tmp,foo,bar"); |
|
85 do_check_eq(Win.join("c:\\tmp", "\\foo", "bar"), "c:\\foo\\bar", "join c:\\tmp,\\foo,bar"); |
|
86 do_check_eq(Win.join("c:\\tmp", "c:\\foo", "bar"), "c:\\foo\\bar", "join c:\\tmp,c:\\foo,bar"); |
|
87 do_check_eq(Win.join("c:\\tmp", "c:foo", "bar"), "c:\\foo\\bar", "join c:\\tmp,c:foo,bar"); |
|
88 do_check_eq(Win.winGetDrive("c:"), "c:"); |
|
89 do_check_eq(Win.winGetDrive("c:\\"), "c:"); |
|
90 do_check_eq(Win.winGetDrive("c:abc"), "c:"); |
|
91 do_check_eq(Win.winGetDrive("c:abc\\d\\e\\f\\g"), "c:"); |
|
92 do_check_eq(Win.winGetDrive("c:\\abc"), "c:"); |
|
93 do_check_eq(Win.winGetDrive("c:\\abc\\d\\e\\f\\g"), "c:"); |
|
94 |
|
95 do_print("Forwardslash-separated, no drive"); |
|
96 do_check_eq(Win.normalize("/a/b/c"), "\\a\\b\\c"); |
|
97 do_check_eq(Win.normalize("/a/b////c"), "\\a\\b\\c"); |
|
98 do_check_eq(Win.normalize("/a/b/c///"), "\\a\\b\\c"); |
|
99 do_check_eq(Win.normalize("/a/b/c/../../../d/e/f"), "\\d\\e\\f"); |
|
100 do_check_eq(Win.normalize("a/b/c/../../../d/e/f"), "d\\e\\f"); |
|
101 |
|
102 do_print("Forwardslash-separated, with a drive"); |
|
103 do_check_eq(Win.normalize("c:/"), "c:\\"); |
|
104 do_check_eq(Win.normalize("c:/a/b/c"), "c:\\a\\b\\c"); |
|
105 do_check_eq(Win.normalize("c:/a/b////c"), "c:\\a\\b\\c"); |
|
106 do_check_eq(Win.normalize("c:////a/b/c"), "c:\\a\\b\\c"); |
|
107 do_check_eq(Win.normalize("c:/a/b/c///"), "c:\\a\\b\\c"); |
|
108 do_check_eq(Win.normalize("c:/a/b/c/../../../d/e/f"), "c:\\d\\e\\f"); |
|
109 do_check_eq(Win.normalize("c:a/b/c/../../../d/e/f"), "c:\\d\\e\\f"); |
|
110 |
|
111 do_print("Backslash-separated, UNC-style"); |
|
112 do_check_eq(Win.basename("\\\\a\\b"), "b"); |
|
113 do_check_eq(Win.basename("\\\\a\\b\\"), ""); |
|
114 do_check_eq(Win.basename("\\\\abc"), ""); |
|
115 do_check_eq(Win.dirname("\\\\a\\b"), "\\\\a"); |
|
116 do_check_eq(Win.dirname("\\\\a\\b\\"), "\\\\a\\b"); |
|
117 do_check_eq(Win.dirname("\\\\a\\\\\\\\b"), "\\\\a"); |
|
118 do_check_eq(Win.dirname("\\\\abc"), "\\\\abc"); |
|
119 do_check_eq(Win.normalize("\\\\a\\b\\c"), "\\\\a\\b\\c"); |
|
120 do_check_eq(Win.normalize("\\\\a\\b\\\\\\\\c"), "\\\\a\\b\\c"); |
|
121 do_check_eq(Win.normalize("\\\\a\\b\\c\\\\\\"), "\\\\a\\b\\c"); |
|
122 do_check_eq(Win.normalize("\\\\a\\b\\c\\..\\..\\d\\e\\f"), "\\\\a\\d\\e\\f"); |
|
123 do_check_fail(function() Win.normalize("\\\\a\\b\\c\\..\\..\\..\\d\\e\\f")); |
|
124 |
|
125 do_check_eq(Win.join("\\\\a\\tmp", "foo", "bar"), "\\\\a\\tmp\\foo\\bar"); |
|
126 do_check_eq(Win.join("\\\\a\\tmp", "\\foo", "bar"), "\\\\a\\foo\\bar"); |
|
127 do_check_eq(Win.join("\\\\a\\tmp", "\\\\foo\\", "bar"), "\\\\foo\\bar"); |
|
128 do_check_eq(Win.winGetDrive("\\\\"), null); |
|
129 do_check_eq(Win.winGetDrive("\\\\c"), "\\\\c"); |
|
130 do_check_eq(Win.winGetDrive("\\\\c\\abc"), "\\\\c"); |
|
131 |
|
132 do_print("Testing unix paths"); |
|
133 do_check_eq(Unix.basename("a/b"), "b"); |
|
134 do_check_eq(Unix.basename("a/b/"), ""); |
|
135 do_check_eq(Unix.basename("abc"), "abc"); |
|
136 do_check_eq(Unix.dirname("a/b"), "a"); |
|
137 do_check_eq(Unix.dirname("a/b/"), "a/b"); |
|
138 do_check_eq(Unix.dirname("a////b"), "a"); |
|
139 do_check_eq(Unix.dirname("abc"), "."); |
|
140 do_check_eq(Unix.normalize("/a/b/c"), "/a/b/c"); |
|
141 do_check_eq(Unix.normalize("/a/b////c"), "/a/b/c"); |
|
142 do_check_eq(Unix.normalize("////a/b/c"), "/a/b/c"); |
|
143 do_check_eq(Unix.normalize("/a/b/c///"), "/a/b/c"); |
|
144 do_check_eq(Unix.normalize("/a/b/c/../../../d/e/f"), "/d/e/f"); |
|
145 do_check_eq(Unix.normalize("a/b/c/../../../d/e/f"), "d/e/f"); |
|
146 do_check_fail(function() Unix.normalize("/a/b/c/../../../../d/e/f")); |
|
147 |
|
148 do_check_eq(Unix.join("/tmp", "foo", "bar"), "/tmp/foo/bar", "join /tmp,foo,bar"); |
|
149 do_check_eq(Unix.join("/tmp", "/foo", "bar"), "/foo/bar", "join /tmp,/foo,bar"); |
|
150 |
|
151 do_print("Testing the presence of ospath.jsm"); |
|
152 let Scope = {}; |
|
153 try { |
|
154 Components.utils.import("resource://gre/modules/osfile/ospath.jsm", Scope); |
|
155 } catch (ex) { |
|
156 // Can't load ospath |
|
157 } |
|
158 do_check_true(!!Scope.basename); |
|
159 } |