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