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'use strict'; 23const common = require('../common'); 24 25const assert = require('assert'); 26const fs = require('fs'); 27 28fs.stat('.', common.mustSucceed(function(stats) { 29 assert.ok(stats.mtime instanceof Date); 30 assert.ok(Object.hasOwn(stats, 'blksize')); 31 assert.ok(Object.hasOwn(stats, 'blocks')); 32 // Confirm that we are not running in the context of the internal binding 33 // layer. 34 // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1 35 assert.strictEqual(this, undefined); 36})); 37 38fs.lstat('.', common.mustSucceed(function(stats) { 39 assert.ok(stats.mtime instanceof Date); 40 // Confirm that we are not running in the context of the internal binding 41 // layer. 42 // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1 43 assert.strictEqual(this, undefined); 44})); 45 46// fstat 47fs.open('.', 'r', undefined, common.mustSucceed(function(fd) { 48 assert.ok(fd); 49 50 fs.fstat(-0, common.mustSucceed()); 51 52 fs.fstat(fd, common.mustSucceed(function(stats) { 53 assert.ok(stats.mtime instanceof Date); 54 fs.close(fd, assert.ifError); 55 // Confirm that we are not running in the context of the internal binding 56 // layer. 57 // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1 58 assert.strictEqual(this, undefined); 59 })); 60 61 // Confirm that we are not running in the context of the internal binding 62 // layer. 63 // Ref: https://github.com/nodejs/node/commit/463d6bac8b349acc462d345a6e298a76f7d06fb1 64 assert.strictEqual(this, undefined); 65})); 66 67// fstatSync 68fs.open('.', 'r', undefined, common.mustCall(function(err, fd) { 69 const stats = fs.fstatSync(fd); 70 assert.ok(stats.mtime instanceof Date); 71 fs.close(fd, common.mustSucceed()); 72})); 73 74fs.stat(__filename, common.mustSucceed((s) => { 75 assert.strictEqual(s.isDirectory(), false); 76 assert.strictEqual(s.isFile(), true); 77 assert.strictEqual(s.isSocket(), false); 78 assert.strictEqual(s.isBlockDevice(), false); 79 assert.strictEqual(s.isCharacterDevice(), false); 80 assert.strictEqual(s.isFIFO(), false); 81 assert.strictEqual(s.isSymbolicLink(), false); 82 83 const jsonString = JSON.stringify(s); 84 const parsed = JSON.parse(jsonString); 85 [ 86 'dev', 'mode', 'nlink', 'uid', 87 'gid', 'rdev', 'blksize', 'ino', 'size', 'blocks', 88 'atime', 'mtime', 'ctime', 'birthtime', 89 'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs', 90 ].forEach(function(k) { 91 assert.ok(k in s, `${k} should be in Stats`); 92 assert.notStrictEqual(s[k], undefined, `${k} should not be undefined`); 93 assert.notStrictEqual(s[k], null, `${k} should not be null`); 94 assert.notStrictEqual(parsed[k], undefined, `${k} should not be undefined`); 95 assert.notStrictEqual(parsed[k], null, `${k} should not be null`); 96 }); 97 [ 98 'dev', 'mode', 'nlink', 'uid', 'gid', 'rdev', 'blksize', 'ino', 'size', 99 'blocks', 'atimeMs', 'mtimeMs', 'ctimeMs', 'birthtimeMs', 100 ].forEach((k) => { 101 assert.strictEqual(typeof s[k], 'number', `${k} should be a number`); 102 assert.strictEqual(typeof parsed[k], 'number', `${k} should be a number`); 103 }); 104 ['atime', 'mtime', 'ctime', 'birthtime'].forEach((k) => { 105 assert.ok(s[k] instanceof Date, `${k} should be a Date`); 106 assert.strictEqual(typeof parsed[k], 'string', `${k} should be a string`); 107 }); 108})); 109 110['', false, null, undefined, {}, []].forEach((input) => { 111 ['fstat', 'fstatSync'].forEach((fnName) => { 112 assert.throws( 113 () => fs[fnName](input), 114 { 115 code: 'ERR_INVALID_ARG_TYPE', 116 name: 'TypeError' 117 } 118 ); 119 }); 120}); 121 122[false, 1, {}, [], null, undefined].forEach((input) => { 123 assert.throws( 124 () => fs.lstat(input, common.mustNotCall()), 125 { 126 code: 'ERR_INVALID_ARG_TYPE', 127 name: 'TypeError' 128 } 129 ); 130 assert.throws( 131 () => fs.lstatSync(input), 132 { 133 code: 'ERR_INVALID_ARG_TYPE', 134 name: 'TypeError' 135 } 136 ); 137 assert.throws( 138 () => fs.stat(input, common.mustNotCall()), 139 { 140 code: 'ERR_INVALID_ARG_TYPE', 141 name: 'TypeError' 142 } 143 ); 144 assert.throws( 145 () => fs.statSync(input), 146 { 147 code: 'ERR_INVALID_ARG_TYPE', 148 name: 'TypeError' 149 } 150 ); 151}); 152 153// Should not throw an error 154fs.stat(__filename, undefined, common.mustCall()); 155 156fs.open(__filename, 'r', undefined, common.mustCall((err, fd) => { 157 // Should not throw an error 158 fs.fstat(fd, undefined, common.mustCall()); 159})); 160 161// Should not throw an error 162fs.lstat(__filename, undefined, common.mustCall()); 163