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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial