eae80238bc14697358173171f324554fc282ee00
[platform/upstream/nodejs.git] / test / parallel / test-fs-utimes.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 var common = require('../common');
23 var assert = require('assert');
24 var util = require('util');
25 var fs = require('fs');
26
27 var is_windows = process.platform === 'win32';
28
29 var tests_ok = 0;
30 var tests_run = 0;
31
32 function stat_resource(resource) {
33   if (typeof resource == 'string') {
34     return fs.statSync(resource);
35   } else {
36     // ensure mtime has been written to disk
37     fs.fsyncSync(resource);
38     return fs.fstatSync(resource);
39   }
40 }
41
42 function check_mtime(resource, mtime) {
43   var mtime = fs._toUnixTimestamp(mtime);
44   var stats = stat_resource(resource);
45   var real_mtime = fs._toUnixTimestamp(stats.mtime);
46   // check up to single-second precision
47   // sub-second precision is OS and fs dependant
48   return Math.floor(mtime) == Math.floor(real_mtime);
49 }
50
51 function expect_errno(syscall, resource, err, errno) {
52   if (err && (err.code === errno || err.code === 'ENOSYS')) {
53     tests_ok++;
54   } else {
55     console.log('FAILED:', arguments.callee.name, util.inspect(arguments));
56   }
57 }
58
59 function expect_ok(syscall, resource, err, atime, mtime) {
60   if (!err && check_mtime(resource, mtime) ||
61       err && err.code === 'ENOSYS') {
62     tests_ok++;
63   } else {
64     console.log('FAILED:', arguments.callee.name, util.inspect(arguments));
65   }
66 }
67
68 // the tests assume that __filename belongs to the user running the tests
69 // this should be a fairly safe assumption; testing against a temp file
70 // would be even better though (node doesn't have such functionality yet)
71 function runTest(atime, mtime, callback) {
72
73   var fd, err;
74   //
75   // test synchronized code paths, these functions throw on failure
76   //
77   function syncTests() {
78     fs.utimesSync(__filename, atime, mtime);
79     expect_ok('utimesSync', __filename, undefined, atime, mtime);
80     tests_run++;
81
82     // some systems don't have futimes
83     // if there's an error, it should be ENOSYS
84     try {
85       tests_run++;
86       fs.futimesSync(fd, atime, mtime);
87       expect_ok('futimesSync', fd, undefined, atime, mtime);
88     } catch (ex) {
89       expect_errno('futimesSync', fd, ex, 'ENOSYS');
90     }
91
92     var err;
93     err = undefined;
94     try {
95       fs.utimesSync('foobarbaz', atime, mtime);
96     } catch (ex) {
97       err = ex;
98     }
99     expect_errno('utimesSync', 'foobarbaz', err, 'ENOENT');
100     tests_run++;
101
102     err = undefined;
103     try {
104       fs.futimesSync(-1, atime, mtime);
105     } catch (ex) {
106       err = ex;
107     }
108     expect_errno('futimesSync', -1, err, 'EBADF');
109     tests_run++;
110   }
111
112   //
113   // test async code paths
114   //
115   fs.utimes(__filename, atime, mtime, function(err) {
116     expect_ok('utimes', __filename, err, atime, mtime);
117
118     fs.utimes('foobarbaz', atime, mtime, function(err) {
119       expect_errno('utimes', 'foobarbaz', err, 'ENOENT');
120
121       // don't close this fd
122       if (is_windows) {
123         fd = fs.openSync(__filename, 'r+');
124       } else {
125         fd = fs.openSync(__filename, 'r');
126       }
127
128       fs.futimes(fd, atime, mtime, function(err) {
129         expect_ok('futimes', fd, err, atime, mtime);
130
131         fs.futimes(-1, atime, mtime, function(err) {
132           expect_errno('futimes', -1, err, 'EBADF');
133           syncTests();
134           callback();
135         });
136         tests_run++;
137       });
138       tests_run++;
139     });
140     tests_run++;
141   });
142   tests_run++;
143 }
144
145 var stats = fs.statSync(__filename);
146
147 runTest(new Date('1982-09-10 13:37'), new Date('1982-09-10 13:37'), function() {
148   runTest(new Date(), new Date(), function() {
149     runTest(123456.789, 123456.789, function() {
150       runTest(stats.mtime, stats.mtime, function() {
151         // done
152       });
153     });
154   });
155 });
156
157 process.on('exit', function() {
158   console.log('Tests run / ok:', tests_run, '/', tests_ok);
159   assert.equal(tests_ok, tests_run);
160 });