1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 var common = require('../common');
23 var assert = require('assert');
24 var util = require('util');
25 var fs = require('fs');
27 var is_windows = process.platform === 'win32';
32 function stat_resource(resource) {
33 if (typeof resource == 'string') {
34 return fs.statSync(resource);
36 // ensure mtime has been written to disk
37 fs.fsyncSync(resource);
38 return fs.fstatSync(resource);
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);
51 function expect_errno(syscall, resource, err, errno) {
52 if (err && (err.code === errno || err.code === 'ENOSYS')) {
55 console.log('FAILED:', arguments.callee.name, util.inspect(arguments));
59 function expect_ok(syscall, resource, err, atime, mtime) {
60 if (!err && check_mtime(resource, mtime) ||
61 err && err.code === 'ENOSYS') {
64 console.log('FAILED:', arguments.callee.name, util.inspect(arguments));
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) {
75 // test synchronized code paths, these functions throw on failure
77 function syncTests() {
78 fs.utimesSync(__filename, atime, mtime);
79 expect_ok('utimesSync', __filename, undefined, atime, mtime);
82 // some systems don't have futimes
83 // if there's an error, it should be ENOSYS
86 fs.futimesSync(fd, atime, mtime);
87 expect_ok('futimesSync', fd, undefined, atime, mtime);
89 expect_errno('futimesSync', fd, ex, 'ENOSYS');
95 fs.utimesSync('foobarbaz', atime, mtime);
99 expect_errno('utimesSync', 'foobarbaz', err, 'ENOENT');
104 fs.futimesSync(-1, atime, mtime);
108 expect_errno('futimesSync', -1, err, 'EBADF');
113 // test async code paths
115 fs.utimes(__filename, atime, mtime, function(err) {
116 expect_ok('utimes', __filename, err, atime, mtime);
118 fs.utimes('foobarbaz', atime, mtime, function(err) {
119 expect_errno('utimes', 'foobarbaz', err, 'ENOENT');
121 // don't close this fd
123 fd = fs.openSync(__filename, 'r+');
125 fd = fs.openSync(__filename, 'r');
128 fs.futimes(fd, atime, mtime, function(err) {
129 expect_ok('futimes', fd, err, atime, mtime);
131 fs.futimes(-1, atime, mtime, function(err) {
132 expect_errno('futimes', -1, err, 'EBADF');
145 var stats = fs.statSync(__filename);
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() {
157 process.on('exit', function() {
158 console.log('Tests run / ok:', tests_run, '/', tests_ok);
159 assert.equal(tests_ok, tests_run);