1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_osfile_async_setDates.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,208 @@ 1.4 +"use strict"; 1.5 + 1.6 +Components.utils.import("resource://gre/modules/osfile.jsm"); 1.7 +Components.utils.import("resource://gre/modules/Task.jsm"); 1.8 + 1.9 +/** 1.10 + * A test to ensure that OS.File.setDates and OS.File.prototype.setDates are 1.11 + * working correctly. 1.12 + * (see bug 924916) 1.13 + */ 1.14 + 1.15 +function run_test() { 1.16 + do_test_pending(); 1.17 + run_next_test(); 1.18 +} 1.19 + 1.20 +// Non-prototypical tests, operating on path names. 1.21 +add_task(function test_nonproto() { 1.22 + // First, create a file we can mess with. 1.23 + let path = OS.Path.join(OS.Constants.Path.tmpDir, 1.24 + "test_osfile_async_setDates_nonproto.tmp"); 1.25 + yield OS.File.writeAtomic(path, new Uint8Array(1)); 1.26 + 1.27 + try { 1.28 + // 1. Try to set some well known dates. 1.29 + // We choose multiples of 2000ms, because the time stamp resolution of 1.30 + // the underlying OS might not support something more precise. 1.31 + const accDate = 2000; 1.32 + const modDate = 4000; 1.33 + { 1.34 + yield OS.File.setDates(path, accDate, modDate); 1.35 + let stat = yield OS.File.stat(path); 1.36 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.37 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.38 + } 1.39 + 1.40 + // 2.1 Try to omit modificationDate (which should then default to 1.41 + // |Date.now()|, expect for resolution differences). 1.42 + { 1.43 + yield OS.File.setDates(path, accDate); 1.44 + let stat = yield OS.File.stat(path); 1.45 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.46 + do_check_neq(modDate, stat.lastModificationDate.getTime()); 1.47 + } 1.48 + 1.49 + // 2.2 Try to omit accessDate as well (which should then default to 1.50 + // |Date.now()|, expect for resolution differences). 1.51 + { 1.52 + yield OS.File.setDates(path); 1.53 + let stat = yield OS.File.stat(path); 1.54 + do_check_neq(accDate, stat.lastAccessDate.getTime()); 1.55 + do_check_neq(modDate, stat.lastModificationDate.getTime()); 1.56 + } 1.57 + 1.58 + // 3. Repeat 1., but with Date objects this time 1.59 + { 1.60 + yield OS.File.setDates(path, new Date(accDate), new Date(modDate)); 1.61 + let stat = yield OS.File.stat(path); 1.62 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.63 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.64 + } 1.65 + 1.66 + // 4. Check that invalid params will cause an exception/rejection. 1.67 + { 1.68 + for (let p of ["invalid", new Uint8Array(1), NaN]) { 1.69 + try { 1.70 + yield OS.File.setDates(path, p, modDate); 1.71 + do_throw("Invalid access date should have thrown for: " + p); 1.72 + } catch (ex) { 1.73 + let stat = yield OS.File.stat(path); 1.74 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.75 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.76 + } 1.77 + try { 1.78 + yield OS.File.setDates(path, accDate, p); 1.79 + do_throw("Invalid modification date should have thrown for: " + p); 1.80 + } catch (ex) { 1.81 + let stat = yield OS.File.stat(path); 1.82 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.83 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.84 + } 1.85 + try { 1.86 + yield OS.File.setDates(path, p, p); 1.87 + do_throw("Invalid dates should have thrown for: " + p); 1.88 + } catch (ex) { 1.89 + let stat = yield OS.File.stat(path); 1.90 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.91 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.92 + } 1.93 + } 1.94 + } 1.95 + } finally { 1.96 + // Remove the temp file again 1.97 + yield OS.File.remove(path); 1.98 + } 1.99 +}); 1.100 + 1.101 +// Prototypical tests, operating on |File| handles. 1.102 +add_task(function test_proto() { 1.103 + // First, create a file we can mess with. 1.104 + let path = OS.Path.join(OS.Constants.Path.tmpDir, 1.105 + "test_osfile_async_setDates_proto.tmp"); 1.106 + yield OS.File.writeAtomic(path, new Uint8Array(1)); 1.107 + 1.108 + try { 1.109 + let fd = yield OS.File.open(path, {write: true}); 1.110 + 1.111 + try { 1.112 + // 1. Try to set some well known dates. 1.113 + // We choose multiples of 2000ms, because the time stamp resolution of 1.114 + // the underlying OS might not support something more precise. 1.115 + const accDate = 2000; 1.116 + const modDate = 4000; 1.117 + { 1.118 + yield fd.setDates(accDate, modDate); 1.119 + let stat = yield fd.stat(); 1.120 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.121 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.122 + } 1.123 + 1.124 + // 2.1 Try to omit modificationDate (which should then default to 1.125 + // |Date.now()|, expect for resolution differences). 1.126 + { 1.127 + yield fd.setDates(accDate); 1.128 + let stat = yield fd.stat(); 1.129 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.130 + do_check_neq(modDate, stat.lastModificationDate.getTime()); 1.131 + } 1.132 + 1.133 + // 2.2 Try to omit accessDate as well (which should then default to 1.134 + // |Date.now()|, expect for resolution differences). 1.135 + { 1.136 + yield fd.setDates(); 1.137 + let stat = yield fd.stat(); 1.138 + do_check_neq(accDate, stat.lastAccessDate.getTime()); 1.139 + do_check_neq(modDate, stat.lastModificationDate.getTime()); 1.140 + } 1.141 + 1.142 + // 3. Repeat 1., but with Date objects this time 1.143 + { 1.144 + yield fd.setDates(new Date(accDate), new Date(modDate)); 1.145 + let stat = yield fd.stat(); 1.146 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.147 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.148 + } 1.149 + 1.150 + // 4. Check that invalid params will cause an exception/rejection. 1.151 + { 1.152 + for (let p of ["invalid", new Uint8Array(1), NaN]) { 1.153 + try { 1.154 + yield fd.setDates(p, modDate); 1.155 + do_throw("Invalid access date should have thrown for: " + p); 1.156 + } catch (ex) { 1.157 + let stat = yield fd.stat(); 1.158 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.159 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.160 + } 1.161 + try { 1.162 + yield fd.setDates(accDate, p); 1.163 + do_throw("Invalid modification date should have thrown for: " + p); 1.164 + } catch (ex) { 1.165 + let stat = yield fd.stat(); 1.166 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.167 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.168 + } 1.169 + try { 1.170 + yield fd.setDates(p, p); 1.171 + do_throw("Invalid dates should have thrown for: " + p); 1.172 + } catch (ex) { 1.173 + let stat = yield fd.stat(); 1.174 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.175 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.176 + } 1.177 + } 1.178 + } 1.179 + } finally { 1.180 + yield fd.close(); 1.181 + } 1.182 + } finally { 1.183 + // Remove the temp file again 1.184 + yield OS.File.remove(path); 1.185 + } 1.186 +}); 1.187 + 1.188 +// Tests setting dates on directories. 1.189 +add_task(function test_dirs() { 1.190 + let path = OS.Path.join(OS.Constants.Path.tmpDir, 1.191 + "test_osfile_async_setDates_dir"); 1.192 + yield OS.File.makeDir(path); 1.193 + 1.194 + try { 1.195 + // 1. Try to set some well known dates. 1.196 + // We choose multiples of 2000ms, because the time stamp resolution of 1.197 + // the underlying OS might not support something more precise. 1.198 + const accDate = 2000; 1.199 + const modDate = 4000; 1.200 + { 1.201 + yield OS.File.setDates(path, accDate, modDate); 1.202 + let stat = yield OS.File.stat(path); 1.203 + do_check_eq(accDate, stat.lastAccessDate.getTime()); 1.204 + do_check_eq(modDate, stat.lastModificationDate.getTime()); 1.205 + } 1.206 + } finally { 1.207 + yield OS.File.removeEmptyDir(path); 1.208 + } 1.209 +}); 1.210 + 1.211 +add_task(do_test_finished);