summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_osfile_async_setDates.js
blob: 17d3afa7c1c9482a37a78bb8e43f2c6c4726e362 (plain)
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
"use strict";

Components.utils.import("resource://gre/modules/osfile.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");

/**
 * A test to ensure that OS.File.setDates and OS.File.prototype.setDates are
 * working correctly.
 * (see bug 924916)
 */

function run_test() {
  run_next_test();
}

// Non-prototypical tests, operating on path names.
add_task(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");
  yield 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;
    {
      yield OS.File.setDates(path, accDate, modDate);
      let stat = yield OS.File.stat(path);
      do_check_eq(accDate, stat.lastAccessDate.getTime());
      do_check_eq(modDate, stat.lastModificationDate.getTime());
    }

    // 2.1 Try to omit modificationDate (which should then default to
    // |Date.now()|, expect for resolution differences).
    {
      yield OS.File.setDates(path, accDate);
      let stat = yield OS.File.stat(path);
      do_check_eq(accDate, stat.lastAccessDate.getTime());
      do_check_neq(modDate, stat.lastModificationDate.getTime());
    }

    // 2.2 Try to omit accessDate as well (which should then default to
    // |Date.now()|, expect for resolution differences).
    {
      yield OS.File.setDates(path);
      let stat = yield OS.File.stat(path);
      do_check_neq(accDate, stat.lastAccessDate.getTime());
      do_check_neq(modDate, stat.lastModificationDate.getTime());
    }

    // 3. Repeat 1., but with Date objects this time
    {
      yield OS.File.setDates(path, new Date(accDate), new Date(modDate));
      let stat = yield OS.File.stat(path);
      do_check_eq(accDate, stat.lastAccessDate.getTime());
      do_check_eq(modDate, stat.lastModificationDate.getTime());
    }

    // 4. Check that invalid params will cause an exception/rejection.
    {
      for (let p of ["invalid", new Uint8Array(1), NaN]) {
        try {
          yield OS.File.setDates(path, p, modDate);
          do_throw("Invalid access date should have thrown for: " + p);
        } catch (ex) {
          let stat = yield OS.File.stat(path);
          do_check_eq(accDate, stat.lastAccessDate.getTime());
          do_check_eq(modDate, stat.lastModificationDate.getTime());
        }
        try {
          yield OS.File.setDates(path, accDate, p);
          do_throw("Invalid modification date should have thrown for: " + p);
        } catch (ex) {
          let stat = yield OS.File.stat(path);
          do_check_eq(accDate, stat.lastAccessDate.getTime());
          do_check_eq(modDate, stat.lastModificationDate.getTime());
        }
        try {
          yield OS.File.setDates(path, p, p);
          do_throw("Invalid dates should have thrown for: " + p);
        } catch (ex) {
          let stat = yield OS.File.stat(path);
          do_check_eq(accDate, stat.lastAccessDate.getTime());
          do_check_eq(modDate, stat.lastModificationDate.getTime());
        }
      }
    }
  } finally {
    // Remove the temp file again
    yield OS.File.remove(path);
  }
});

// Prototypical tests, operating on |File| handles.
add_task(function* test_proto() {
  if (OS.Constants.Sys.Name == "Android" || OS.Constants.Sys.Name == "Gonk") {
    do_print("File.prototype.setDates is not implemented for Android/B2G");
    do_check_eq(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");
  yield OS.File.writeAtomic(path, new Uint8Array(1));

  try {
    let fd = yield 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;
      {
        yield fd.setDates(accDate, modDate);
        let stat = yield fd.stat();
        do_check_eq(accDate, stat.lastAccessDate.getTime());
        do_check_eq(modDate, stat.lastModificationDate.getTime());
      }

      // 2.1 Try to omit modificationDate (which should then default to
      // |Date.now()|, expect for resolution differences).
      {
        yield fd.setDates(accDate);
        let stat = yield fd.stat();
        do_check_eq(accDate, stat.lastAccessDate.getTime());
        do_check_neq(modDate, stat.lastModificationDate.getTime());
      }

      // 2.2 Try to omit accessDate as well (which should then default to
      // |Date.now()|, expect for resolution differences).
      {
        yield fd.setDates();
        let stat = yield fd.stat();
        do_check_neq(accDate, stat.lastAccessDate.getTime());
        do_check_neq(modDate, stat.lastModificationDate.getTime());
      }

      // 3. Repeat 1., but with Date objects this time
      {
        yield fd.setDates(new Date(accDate), new Date(modDate));
        let stat = yield fd.stat();
        do_check_eq(accDate, stat.lastAccessDate.getTime());
        do_check_eq(modDate, stat.lastModificationDate.getTime());
      }

      // 4. Check that invalid params will cause an exception/rejection.
      {
        for (let p of ["invalid", new Uint8Array(1), NaN]) {
          try {
            yield fd.setDates(p, modDate);
            do_throw("Invalid access date should have thrown for: " + p);
          } catch (ex) {
            let stat = yield fd.stat();
            do_check_eq(accDate, stat.lastAccessDate.getTime());
            do_check_eq(modDate, stat.lastModificationDate.getTime());
          }
          try {
            yield fd.setDates(accDate, p);
            do_throw("Invalid modification date should have thrown for: " + p);
          } catch (ex) {
            let stat = yield fd.stat();
            do_check_eq(accDate, stat.lastAccessDate.getTime());
            do_check_eq(modDate, stat.lastModificationDate.getTime());
          }
          try {
            yield fd.setDates(p, p);
            do_throw("Invalid dates should have thrown for: " + p);
          } catch (ex) {
            let stat = yield fd.stat();
            do_check_eq(accDate, stat.lastAccessDate.getTime());
            do_check_eq(modDate, stat.lastModificationDate.getTime());
          }
        }
      }
    } finally {
      yield fd.close();
    }
  } finally {
    // Remove the temp file again
    yield OS.File.remove(path);
  }
});

// Tests setting dates on directories.
add_task(function* test_dirs() {
  let path = OS.Path.join(OS.Constants.Path.tmpDir,
                              "test_osfile_async_setDates_dir");
  yield 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;
    {
      yield OS.File.setDates(path, accDate, modDate);
      let stat = yield OS.File.stat(path);
      do_check_eq(accDate, stat.lastAccessDate.getTime());
      do_check_eq(modDate, stat.lastModificationDate.getTime());
    }
  } finally {
    yield OS.File.removeEmptyDir(path);
  }
});