Skip to content

Commit d6544ac

Browse files
committed
test: fix test-fs-utimes on non-Y2K38 file systems
Move Y2K38-specific parts of test-fs-utimes to test-fs-utimes-y2K38.js. On non-Windows, check for Y2K38 support and skip if it is unsupported. Fixes: #36591
1 parent da4b233 commit d6544ac

File tree

2 files changed

+55
-31
lines changed

2 files changed

+55
-31
lines changed

test/parallel/test-fs-utimes-y2K38.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const common = require('../common');
3+
const tmpdir = require('../common/tmpdir');
4+
5+
const assert = require('assert');
6+
const fs = require('fs');
7+
8+
tmpdir.refresh();
9+
10+
// Check for Y2K38 support on POSIX. For Windows, assume it's there.
11+
if (!common.isWindows) {
12+
const testFilePath = `${tmpdir.path}/y2k38-test`;
13+
const testFileDate = '203801020304';
14+
const { spawnSync } = require('child_process');
15+
const touchResult = spawnSync('touch',
16+
['-t', testFileDate, testFilePath],
17+
{ encoding: 'utf8' });
18+
assert.strictEqual(touchResult.status, 0);
19+
20+
const dateResult = spawnSync('date',
21+
['-r', testFilePath, '+%Y%m%d%H%M'],
22+
{ encoding: 'utf8' });
23+
assert.strictEqual(dateResult.status, 0);
24+
25+
if (dateResult.stdout.trim() !== testFileDate) {
26+
common.skip('File system appears to lack Y2k38 support');
27+
}
28+
}
29+
30+
// Ref: https://github.com/nodejs/node/issues/13255
31+
const path = `${tmpdir.path}/test-utimes-precision`;
32+
fs.writeFileSync(path, '');
33+
34+
const Y2K38_mtime = 2 ** 31;
35+
fs.utimesSync(path, Y2K38_mtime, Y2K38_mtime);
36+
const Y2K38_stats = fs.statSync(path);
37+
assert.strictEqual(Y2K38_stats.mtime.getTime() / 1000, Y2K38_mtime);
38+
39+
if (common.isWindows) {
40+
// This value would get converted to (double)1713037251359.9998
41+
const truncate_mtime = 1713037251360;
42+
fs.utimesSync(path, truncate_mtime / 1000, truncate_mtime / 1000);
43+
const truncate_stats = fs.statSync(path);
44+
assert.strictEqual(truncate_stats.mtime.getTime(), truncate_mtime);
45+
46+
// test Y2K38 for windows
47+
// This value if treaded as a `signed long` gets converted to -2135622133469.
48+
// POSIX systems stores timestamps in {long t_sec, long t_usec}.
49+
// NTFS stores times in nanoseconds in a single `uint64_t`, so when libuv
50+
// calculates (long)`uv_timespec_t.tv_sec` we get 2's complement.
51+
const overflow_mtime = 2159345162531;
52+
fs.utimesSync(path, overflow_mtime / 1000, overflow_mtime / 1000);
53+
const overflow_stats = fs.statSync(path);
54+
assert.strictEqual(overflow_stats.mtime.getTime(), overflow_mtime);
55+
}

test/parallel/test-fs-utimes.js

-31
Original file line numberDiff line numberDiff line change
@@ -156,37 +156,6 @@ function runTests(iter) {
156156
}
157157
}
158158

159-
// Ref: https://github.com/nodejs/node/issues/13255
160-
const path = `${tmpdir.path}/test-utimes-precision`;
161-
fs.writeFileSync(path, '');
162-
163-
// Test Y2K38 for all platforms [except 'arm', 'OpenBSD', 'SunOS' and 'IBMi']
164-
if (!process.arch.includes('arm') &&
165-
!common.isOpenBSD && !common.isSunOS && !common.isIBMi) {
166-
const Y2K38_mtime = 2 ** 31;
167-
fs.utimesSync(path, Y2K38_mtime, Y2K38_mtime);
168-
const Y2K38_stats = fs.statSync(path);
169-
assert.strictEqual(Y2K38_stats.mtime.getTime() / 1000, Y2K38_mtime);
170-
}
171-
172-
if (common.isWindows) {
173-
// This value would get converted to (double)1713037251359.9998
174-
const truncate_mtime = 1713037251360;
175-
fs.utimesSync(path, truncate_mtime / 1000, truncate_mtime / 1000);
176-
const truncate_stats = fs.statSync(path);
177-
assert.strictEqual(truncate_stats.mtime.getTime(), truncate_mtime);
178-
179-
// test Y2K38 for windows
180-
// This value if treaded as a `signed long` gets converted to -2135622133469.
181-
// POSIX systems stores timestamps in {long t_sec, long t_usec}.
182-
// NTFS stores times in nanoseconds in a single `uint64_t`, so when libuv
183-
// calculates (long)`uv_timespec_t.tv_sec` we get 2's complement.
184-
const overflow_mtime = 2159345162531;
185-
fs.utimesSync(path, overflow_mtime / 1000, overflow_mtime / 1000);
186-
const overflow_stats = fs.statSync(path);
187-
assert.strictEqual(overflow_stats.mtime.getTime(), overflow_mtime);
188-
}
189-
190159
const expectTypeError = {
191160
code: 'ERR_INVALID_ARG_TYPE',
192161
name: 'TypeError'

0 commit comments

Comments
 (0)