toolkit/components/osfile/tests/xpcshell/test_osfile_async_setDates.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:3f682899ff15
1 "use strict";
2
3 Components.utils.import("resource://gre/modules/osfile.jsm");
4 Components.utils.import("resource://gre/modules/Task.jsm");
5
6 /**
7 * A test to ensure that OS.File.setDates and OS.File.prototype.setDates are
8 * working correctly.
9 * (see bug 924916)
10 */
11
12 function run_test() {
13 do_test_pending();
14 run_next_test();
15 }
16
17 // Non-prototypical tests, operating on path names.
18 add_task(function test_nonproto() {
19 // First, create a file we can mess with.
20 let path = OS.Path.join(OS.Constants.Path.tmpDir,
21 "test_osfile_async_setDates_nonproto.tmp");
22 yield OS.File.writeAtomic(path, new Uint8Array(1));
23
24 try {
25 // 1. Try to set some well known dates.
26 // We choose multiples of 2000ms, because the time stamp resolution of
27 // the underlying OS might not support something more precise.
28 const accDate = 2000;
29 const modDate = 4000;
30 {
31 yield OS.File.setDates(path, accDate, modDate);
32 let stat = yield OS.File.stat(path);
33 do_check_eq(accDate, stat.lastAccessDate.getTime());
34 do_check_eq(modDate, stat.lastModificationDate.getTime());
35 }
36
37 // 2.1 Try to omit modificationDate (which should then default to
38 // |Date.now()|, expect for resolution differences).
39 {
40 yield OS.File.setDates(path, accDate);
41 let stat = yield OS.File.stat(path);
42 do_check_eq(accDate, stat.lastAccessDate.getTime());
43 do_check_neq(modDate, stat.lastModificationDate.getTime());
44 }
45
46 // 2.2 Try to omit accessDate as well (which should then default to
47 // |Date.now()|, expect for resolution differences).
48 {
49 yield OS.File.setDates(path);
50 let stat = yield OS.File.stat(path);
51 do_check_neq(accDate, stat.lastAccessDate.getTime());
52 do_check_neq(modDate, stat.lastModificationDate.getTime());
53 }
54
55 // 3. Repeat 1., but with Date objects this time
56 {
57 yield OS.File.setDates(path, new Date(accDate), new Date(modDate));
58 let stat = yield OS.File.stat(path);
59 do_check_eq(accDate, stat.lastAccessDate.getTime());
60 do_check_eq(modDate, stat.lastModificationDate.getTime());
61 }
62
63 // 4. Check that invalid params will cause an exception/rejection.
64 {
65 for (let p of ["invalid", new Uint8Array(1), NaN]) {
66 try {
67 yield OS.File.setDates(path, p, modDate);
68 do_throw("Invalid access date should have thrown for: " + p);
69 } catch (ex) {
70 let stat = yield OS.File.stat(path);
71 do_check_eq(accDate, stat.lastAccessDate.getTime());
72 do_check_eq(modDate, stat.lastModificationDate.getTime());
73 }
74 try {
75 yield OS.File.setDates(path, accDate, p);
76 do_throw("Invalid modification date should have thrown for: " + p);
77 } catch (ex) {
78 let stat = yield OS.File.stat(path);
79 do_check_eq(accDate, stat.lastAccessDate.getTime());
80 do_check_eq(modDate, stat.lastModificationDate.getTime());
81 }
82 try {
83 yield OS.File.setDates(path, p, p);
84 do_throw("Invalid dates should have thrown for: " + p);
85 } catch (ex) {
86 let stat = yield OS.File.stat(path);
87 do_check_eq(accDate, stat.lastAccessDate.getTime());
88 do_check_eq(modDate, stat.lastModificationDate.getTime());
89 }
90 }
91 }
92 } finally {
93 // Remove the temp file again
94 yield OS.File.remove(path);
95 }
96 });
97
98 // Prototypical tests, operating on |File| handles.
99 add_task(function test_proto() {
100 // First, create a file we can mess with.
101 let path = OS.Path.join(OS.Constants.Path.tmpDir,
102 "test_osfile_async_setDates_proto.tmp");
103 yield OS.File.writeAtomic(path, new Uint8Array(1));
104
105 try {
106 let fd = yield OS.File.open(path, {write: true});
107
108 try {
109 // 1. Try to set some well known dates.
110 // We choose multiples of 2000ms, because the time stamp resolution of
111 // the underlying OS might not support something more precise.
112 const accDate = 2000;
113 const modDate = 4000;
114 {
115 yield fd.setDates(accDate, modDate);
116 let stat = yield fd.stat();
117 do_check_eq(accDate, stat.lastAccessDate.getTime());
118 do_check_eq(modDate, stat.lastModificationDate.getTime());
119 }
120
121 // 2.1 Try to omit modificationDate (which should then default to
122 // |Date.now()|, expect for resolution differences).
123 {
124 yield fd.setDates(accDate);
125 let stat = yield fd.stat();
126 do_check_eq(accDate, stat.lastAccessDate.getTime());
127 do_check_neq(modDate, stat.lastModificationDate.getTime());
128 }
129
130 // 2.2 Try to omit accessDate as well (which should then default to
131 // |Date.now()|, expect for resolution differences).
132 {
133 yield fd.setDates();
134 let stat = yield fd.stat();
135 do_check_neq(accDate, stat.lastAccessDate.getTime());
136 do_check_neq(modDate, stat.lastModificationDate.getTime());
137 }
138
139 // 3. Repeat 1., but with Date objects this time
140 {
141 yield fd.setDates(new Date(accDate), new Date(modDate));
142 let stat = yield fd.stat();
143 do_check_eq(accDate, stat.lastAccessDate.getTime());
144 do_check_eq(modDate, stat.lastModificationDate.getTime());
145 }
146
147 // 4. Check that invalid params will cause an exception/rejection.
148 {
149 for (let p of ["invalid", new Uint8Array(1), NaN]) {
150 try {
151 yield fd.setDates(p, modDate);
152 do_throw("Invalid access date should have thrown for: " + p);
153 } catch (ex) {
154 let stat = yield fd.stat();
155 do_check_eq(accDate, stat.lastAccessDate.getTime());
156 do_check_eq(modDate, stat.lastModificationDate.getTime());
157 }
158 try {
159 yield fd.setDates(accDate, p);
160 do_throw("Invalid modification date should have thrown for: " + p);
161 } catch (ex) {
162 let stat = yield fd.stat();
163 do_check_eq(accDate, stat.lastAccessDate.getTime());
164 do_check_eq(modDate, stat.lastModificationDate.getTime());
165 }
166 try {
167 yield fd.setDates(p, p);
168 do_throw("Invalid dates should have thrown for: " + p);
169 } catch (ex) {
170 let stat = yield fd.stat();
171 do_check_eq(accDate, stat.lastAccessDate.getTime());
172 do_check_eq(modDate, stat.lastModificationDate.getTime());
173 }
174 }
175 }
176 } finally {
177 yield fd.close();
178 }
179 } finally {
180 // Remove the temp file again
181 yield OS.File.remove(path);
182 }
183 });
184
185 // Tests setting dates on directories.
186 add_task(function test_dirs() {
187 let path = OS.Path.join(OS.Constants.Path.tmpDir,
188 "test_osfile_async_setDates_dir");
189 yield OS.File.makeDir(path);
190
191 try {
192 // 1. Try to set some well known dates.
193 // We choose multiples of 2000ms, because the time stamp resolution of
194 // the underlying OS might not support something more precise.
195 const accDate = 2000;
196 const modDate = 4000;
197 {
198 yield OS.File.setDates(path, accDate, modDate);
199 let stat = yield OS.File.stat(path);
200 do_check_eq(accDate, stat.lastAccessDate.getTime());
201 do_check_eq(modDate, stat.lastModificationDate.getTime());
202 }
203 } finally {
204 yield OS.File.removeEmptyDir(path);
205 }
206 });
207
208 add_task(do_test_finished);

mercurial