michael@0: "use strict"; michael@0: michael@0: Components.utils.import("resource://gre/modules/osfile.jsm"); michael@0: Components.utils.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: /** michael@0: * A test to ensure that OS.File.setDates and OS.File.prototype.setDates are michael@0: * working correctly. michael@0: * (see bug 924916) michael@0: */ michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: run_next_test(); michael@0: } michael@0: michael@0: // Non-prototypical tests, operating on path names. michael@0: add_task(function test_nonproto() { michael@0: // First, create a file we can mess with. michael@0: let path = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_setDates_nonproto.tmp"); michael@0: yield OS.File.writeAtomic(path, new Uint8Array(1)); michael@0: michael@0: try { michael@0: // 1. Try to set some well known dates. michael@0: // We choose multiples of 2000ms, because the time stamp resolution of michael@0: // the underlying OS might not support something more precise. michael@0: const accDate = 2000; michael@0: const modDate = 4000; michael@0: { michael@0: yield OS.File.setDates(path, accDate, modDate); michael@0: let stat = yield OS.File.stat(path); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: michael@0: // 2.1 Try to omit modificationDate (which should then default to michael@0: // |Date.now()|, expect for resolution differences). michael@0: { michael@0: yield OS.File.setDates(path, accDate); michael@0: let stat = yield OS.File.stat(path); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_neq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: michael@0: // 2.2 Try to omit accessDate as well (which should then default to michael@0: // |Date.now()|, expect for resolution differences). michael@0: { michael@0: yield OS.File.setDates(path); michael@0: let stat = yield OS.File.stat(path); michael@0: do_check_neq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_neq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: michael@0: // 3. Repeat 1., but with Date objects this time michael@0: { michael@0: yield OS.File.setDates(path, new Date(accDate), new Date(modDate)); michael@0: let stat = yield OS.File.stat(path); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: michael@0: // 4. Check that invalid params will cause an exception/rejection. michael@0: { michael@0: for (let p of ["invalid", new Uint8Array(1), NaN]) { michael@0: try { michael@0: yield OS.File.setDates(path, p, modDate); michael@0: do_throw("Invalid access date should have thrown for: " + p); michael@0: } catch (ex) { michael@0: let stat = yield OS.File.stat(path); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: try { michael@0: yield OS.File.setDates(path, accDate, p); michael@0: do_throw("Invalid modification date should have thrown for: " + p); michael@0: } catch (ex) { michael@0: let stat = yield OS.File.stat(path); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: try { michael@0: yield OS.File.setDates(path, p, p); michael@0: do_throw("Invalid dates should have thrown for: " + p); michael@0: } catch (ex) { michael@0: let stat = yield OS.File.stat(path); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: } michael@0: } michael@0: } finally { michael@0: // Remove the temp file again michael@0: yield OS.File.remove(path); michael@0: } michael@0: }); michael@0: michael@0: // Prototypical tests, operating on |File| handles. michael@0: add_task(function test_proto() { michael@0: // First, create a file we can mess with. michael@0: let path = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_setDates_proto.tmp"); michael@0: yield OS.File.writeAtomic(path, new Uint8Array(1)); michael@0: michael@0: try { michael@0: let fd = yield OS.File.open(path, {write: true}); michael@0: michael@0: try { michael@0: // 1. Try to set some well known dates. michael@0: // We choose multiples of 2000ms, because the time stamp resolution of michael@0: // the underlying OS might not support something more precise. michael@0: const accDate = 2000; michael@0: const modDate = 4000; michael@0: { michael@0: yield fd.setDates(accDate, modDate); michael@0: let stat = yield fd.stat(); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: michael@0: // 2.1 Try to omit modificationDate (which should then default to michael@0: // |Date.now()|, expect for resolution differences). michael@0: { michael@0: yield fd.setDates(accDate); michael@0: let stat = yield fd.stat(); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_neq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: michael@0: // 2.2 Try to omit accessDate as well (which should then default to michael@0: // |Date.now()|, expect for resolution differences). michael@0: { michael@0: yield fd.setDates(); michael@0: let stat = yield fd.stat(); michael@0: do_check_neq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_neq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: michael@0: // 3. Repeat 1., but with Date objects this time michael@0: { michael@0: yield fd.setDates(new Date(accDate), new Date(modDate)); michael@0: let stat = yield fd.stat(); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: michael@0: // 4. Check that invalid params will cause an exception/rejection. michael@0: { michael@0: for (let p of ["invalid", new Uint8Array(1), NaN]) { michael@0: try { michael@0: yield fd.setDates(p, modDate); michael@0: do_throw("Invalid access date should have thrown for: " + p); michael@0: } catch (ex) { michael@0: let stat = yield fd.stat(); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: try { michael@0: yield fd.setDates(accDate, p); michael@0: do_throw("Invalid modification date should have thrown for: " + p); michael@0: } catch (ex) { michael@0: let stat = yield fd.stat(); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: try { michael@0: yield fd.setDates(p, p); michael@0: do_throw("Invalid dates should have thrown for: " + p); michael@0: } catch (ex) { michael@0: let stat = yield fd.stat(); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: } michael@0: } michael@0: } finally { michael@0: yield fd.close(); michael@0: } michael@0: } finally { michael@0: // Remove the temp file again michael@0: yield OS.File.remove(path); michael@0: } michael@0: }); michael@0: michael@0: // Tests setting dates on directories. michael@0: add_task(function test_dirs() { michael@0: let path = OS.Path.join(OS.Constants.Path.tmpDir, michael@0: "test_osfile_async_setDates_dir"); michael@0: yield OS.File.makeDir(path); michael@0: michael@0: try { michael@0: // 1. Try to set some well known dates. michael@0: // We choose multiples of 2000ms, because the time stamp resolution of michael@0: // the underlying OS might not support something more precise. michael@0: const accDate = 2000; michael@0: const modDate = 4000; michael@0: { michael@0: yield OS.File.setDates(path, accDate, modDate); michael@0: let stat = yield OS.File.stat(path); michael@0: do_check_eq(accDate, stat.lastAccessDate.getTime()); michael@0: do_check_eq(modDate, stat.lastModificationDate.getTime()); michael@0: } michael@0: } finally { michael@0: yield OS.File.removeEmptyDir(path); michael@0: } michael@0: }); michael@0: michael@0: add_task(do_test_finished);