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'); 24const assert = require('assert'); 25const os = require('os'); 26const path = require('path'); 27const { inspect } = require('util'); 28 29const is = { 30 number: (value, key) => { 31 assert(!Number.isNaN(value), `${key} should not be NaN`); 32 assert.strictEqual(typeof value, 'number'); 33 }, 34 string: (value) => { assert.strictEqual(typeof value, 'string'); }, 35 array: (value) => { assert.ok(Array.isArray(value)); }, 36 object: (value) => { 37 assert.strictEqual(typeof value, 'object'); 38 assert.notStrictEqual(value, null); 39 } 40}; 41 42process.env.TMPDIR = '/tmpdir'; 43process.env.TMP = '/tmp'; 44process.env.TEMP = '/temp'; 45if (common.isWindows) { 46 assert.strictEqual(os.tmpdir(), '/temp'); 47 process.env.TEMP = ''; 48 assert.strictEqual(os.tmpdir(), '/tmp'); 49 process.env.TMP = ''; 50 const expected = `${process.env.SystemRoot || process.env.windir}\\temp`; 51 assert.strictEqual(os.tmpdir(), expected); 52 process.env.TEMP = '\\temp\\'; 53 assert.strictEqual(os.tmpdir(), '\\temp'); 54 process.env.TEMP = '\\tmpdir/'; 55 assert.strictEqual(os.tmpdir(), '\\tmpdir/'); 56 process.env.TEMP = '\\'; 57 assert.strictEqual(os.tmpdir(), '\\'); 58 process.env.TEMP = 'C:\\'; 59 assert.strictEqual(os.tmpdir(), 'C:\\'); 60} else { 61 assert.strictEqual(os.tmpdir(), '/tmpdir'); 62 process.env.TMPDIR = ''; 63 assert.strictEqual(os.tmpdir(), '/tmp'); 64 process.env.TMP = ''; 65 assert.strictEqual(os.tmpdir(), '/temp'); 66 process.env.TEMP = ''; 67 assert.strictEqual(os.tmpdir(), '/tmp'); 68 process.env.TMPDIR = '/tmpdir/'; 69 assert.strictEqual(os.tmpdir(), '/tmpdir'); 70 process.env.TMPDIR = '/tmpdir\\'; 71 assert.strictEqual(os.tmpdir(), '/tmpdir\\'); 72 process.env.TMPDIR = '/'; 73 assert.strictEqual(os.tmpdir(), '/'); 74} 75 76const endianness = os.endianness(); 77is.string(endianness); 78assert.ok(/[BL]E/.test(endianness)); 79 80const hostname = os.hostname(); 81is.string(hostname); 82assert.ok(hostname.length > 0); 83 84// On IBMi, os.uptime() returns 'undefined' 85if (!common.isIBMi) { 86 const uptime = os.uptime(); 87 is.number(uptime); 88 assert.ok(uptime > 0); 89} 90 91const cpus = os.cpus(); 92is.array(cpus); 93assert.ok(cpus.length > 0); 94for (const cpu of cpus) { 95 assert.strictEqual(typeof cpu.model, 'string'); 96 assert.strictEqual(typeof cpu.speed, 'number'); 97 assert.strictEqual(typeof cpu.times.user, 'number'); 98 assert.strictEqual(typeof cpu.times.nice, 'number'); 99 assert.strictEqual(typeof cpu.times.sys, 'number'); 100 assert.strictEqual(typeof cpu.times.idle, 'number'); 101 assert.strictEqual(typeof cpu.times.irq, 'number'); 102} 103 104const type = os.type(); 105is.string(type); 106assert.ok(type.length > 0); 107 108const release = os.release(); 109is.string(release); 110assert.ok(release.length > 0); 111// TODO: Check format on more than just AIX 112if (common.isAIX) 113 assert.ok(/^\d+\.\d+$/.test(release)); 114 115const platform = os.platform(); 116is.string(platform); 117assert.ok(platform.length > 0); 118 119const arch = os.arch(); 120is.string(arch); 121assert.ok(arch.length > 0); 122 123if (!common.isSunOS) { 124 // not implemented yet 125 assert.ok(os.loadavg().length > 0); 126 assert.ok(os.freemem() > 0); 127 assert.ok(os.totalmem() > 0); 128} 129 130const interfaces = os.networkInterfaces(); 131switch (platform) { 132 case 'linux': { 133 const filter = (e) => 134 e.address === '127.0.0.1' && 135 e.netmask === '255.0.0.0'; 136 137 const actual = interfaces.lo.filter(filter); 138 const expected = [{ 139 address: '127.0.0.1', 140 netmask: '255.0.0.0', 141 family: 'IPv4', 142 mac: '00:00:00:00:00:00', 143 internal: true, 144 cidr: '127.0.0.1/8' 145 }]; 146 assert.deepStrictEqual(actual, expected); 147 break; 148 } 149 case 'win32': { 150 const filter = (e) => 151 e.address === '127.0.0.1'; 152 153 const actual = interfaces['Loopback Pseudo-Interface 1'].filter(filter); 154 const expected = [{ 155 address: '127.0.0.1', 156 netmask: '255.0.0.0', 157 family: 'IPv4', 158 mac: '00:00:00:00:00:00', 159 internal: true, 160 cidr: '127.0.0.1/8' 161 }]; 162 assert.deepStrictEqual(actual, expected); 163 break; 164 } 165} 166const netmaskToCIDRSuffixMap = new Map(Object.entries({ 167 '255.0.0.0': 8, 168 '255.255.255.0': 24, 169 'ffff:ffff:ffff:ffff::': 64, 170 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff': 128 171})); 172 173Object.values(interfaces) 174 .flat(Infinity) 175 .map((v) => ({ v, mask: netmaskToCIDRSuffixMap.get(v.netmask) })) 176 .forEach(({ v, mask }) => { 177 assert.ok('cidr' in v, `"cidr" prop not found in ${inspect(v)}`); 178 if (mask) { 179 assert.strictEqual(v.cidr, `${v.address}/${mask}`); 180 } 181 }); 182 183const EOL = os.EOL; 184if (common.isWindows) { 185 assert.strictEqual(EOL, '\r\n'); 186} else { 187 assert.strictEqual(EOL, '\n'); 188} 189 190const home = os.homedir(); 191is.string(home); 192assert.ok(home.includes(path.sep)); 193 194const version = os.version(); 195assert.strictEqual(typeof version, 'string'); 196assert(version); 197 198if (common.isWindows && process.env.USERPROFILE) { 199 assert.strictEqual(home, process.env.USERPROFILE); 200 delete process.env.USERPROFILE; 201 assert.ok(os.homedir().includes(path.sep)); 202 process.env.USERPROFILE = home; 203} else if (!common.isWindows && process.env.HOME) { 204 assert.strictEqual(home, process.env.HOME); 205 delete process.env.HOME; 206 assert.ok(os.homedir().includes(path.sep)); 207 process.env.HOME = home; 208} 209 210const pwd = os.userInfo(); 211is.object(pwd); 212const pwdBuf = os.userInfo({ encoding: 'buffer' }); 213 214if (common.isWindows) { 215 assert.strictEqual(pwd.uid, -1); 216 assert.strictEqual(pwd.gid, -1); 217 assert.strictEqual(pwd.shell, null); 218 assert.strictEqual(pwdBuf.uid, -1); 219 assert.strictEqual(pwdBuf.gid, -1); 220 assert.strictEqual(pwdBuf.shell, null); 221} else { 222 is.number(pwd.uid); 223 is.number(pwd.gid); 224 assert.strictEqual(typeof pwd.shell, 'string'); 225 // It's possible for /etc/passwd to leave the user's shell blank. 226 if (pwd.shell.length > 0) { 227 assert(pwd.shell.includes(path.sep)); 228 } 229 assert.strictEqual(pwd.uid, pwdBuf.uid); 230 assert.strictEqual(pwd.gid, pwdBuf.gid); 231 assert.strictEqual(pwd.shell, pwdBuf.shell.toString('utf8')); 232} 233 234is.string(pwd.username); 235assert.ok(pwd.homedir.includes(path.sep)); 236assert.strictEqual(pwd.username, pwdBuf.username.toString('utf8')); 237assert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8')); 238 239assert.strictEqual(`${os.hostname}`, os.hostname()); 240assert.strictEqual(`${os.homedir}`, os.homedir()); 241assert.strictEqual(`${os.release}`, os.release()); 242assert.strictEqual(`${os.type}`, os.type()); 243assert.strictEqual(`${os.endianness}`, os.endianness()); 244assert.strictEqual(`${os.tmpdir}`, os.tmpdir()); 245assert.strictEqual(`${os.arch}`, os.arch()); 246assert.strictEqual(`${os.platform}`, os.platform()); 247assert.strictEqual(`${os.version}`, os.version()); 248 249assert.strictEqual(+os.totalmem, os.totalmem()); 250 251// Assert that the following values are coercible to numbers. 252// On IBMi, os.uptime() returns 'undefined' 253if (!common.isIBMi) { 254 is.number(+os.uptime, 'uptime'); 255 is.number(os.uptime(), 'uptime'); 256} 257 258is.number(+os.freemem, 'freemem'); 259is.number(os.freemem(), 'freemem'); 260 261const devNull = os.devNull; 262if (common.isWindows) { 263 assert.strictEqual(devNull, '\\\\.\\nul'); 264} else { 265 assert.strictEqual(devNull, '/dev/null'); 266} 267