11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ciconst os = require('os'); 261cb0ef41Sopenharmony_ciconst path = require('path'); 271cb0ef41Sopenharmony_ciconst { inspect } = require('util'); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciconst is = { 301cb0ef41Sopenharmony_ci number: (value, key) => { 311cb0ef41Sopenharmony_ci assert(!Number.isNaN(value), `${key} should not be NaN`); 321cb0ef41Sopenharmony_ci assert.strictEqual(typeof value, 'number'); 331cb0ef41Sopenharmony_ci }, 341cb0ef41Sopenharmony_ci string: (value) => { assert.strictEqual(typeof value, 'string'); }, 351cb0ef41Sopenharmony_ci array: (value) => { assert.ok(Array.isArray(value)); }, 361cb0ef41Sopenharmony_ci object: (value) => { 371cb0ef41Sopenharmony_ci assert.strictEqual(typeof value, 'object'); 381cb0ef41Sopenharmony_ci assert.notStrictEqual(value, null); 391cb0ef41Sopenharmony_ci } 401cb0ef41Sopenharmony_ci}; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciprocess.env.TMPDIR = '/tmpdir'; 431cb0ef41Sopenharmony_ciprocess.env.TMP = '/tmp'; 441cb0ef41Sopenharmony_ciprocess.env.TEMP = '/temp'; 451cb0ef41Sopenharmony_ciif (common.isWindows) { 461cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/temp'); 471cb0ef41Sopenharmony_ci process.env.TEMP = ''; 481cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/tmp'); 491cb0ef41Sopenharmony_ci process.env.TMP = ''; 501cb0ef41Sopenharmony_ci const expected = `${process.env.SystemRoot || process.env.windir}\\temp`; 511cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), expected); 521cb0ef41Sopenharmony_ci process.env.TEMP = '\\temp\\'; 531cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '\\temp'); 541cb0ef41Sopenharmony_ci process.env.TEMP = '\\tmpdir/'; 551cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '\\tmpdir/'); 561cb0ef41Sopenharmony_ci process.env.TEMP = '\\'; 571cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '\\'); 581cb0ef41Sopenharmony_ci process.env.TEMP = 'C:\\'; 591cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), 'C:\\'); 601cb0ef41Sopenharmony_ci} else { 611cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/tmpdir'); 621cb0ef41Sopenharmony_ci process.env.TMPDIR = ''; 631cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/tmp'); 641cb0ef41Sopenharmony_ci process.env.TMP = ''; 651cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/temp'); 661cb0ef41Sopenharmony_ci process.env.TEMP = ''; 671cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/tmp'); 681cb0ef41Sopenharmony_ci process.env.TMPDIR = '/tmpdir/'; 691cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/tmpdir'); 701cb0ef41Sopenharmony_ci process.env.TMPDIR = '/tmpdir\\'; 711cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/tmpdir\\'); 721cb0ef41Sopenharmony_ci process.env.TMPDIR = '/'; 731cb0ef41Sopenharmony_ci assert.strictEqual(os.tmpdir(), '/'); 741cb0ef41Sopenharmony_ci} 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ciconst endianness = os.endianness(); 771cb0ef41Sopenharmony_ciis.string(endianness); 781cb0ef41Sopenharmony_ciassert.match(endianness, /[BL]E/); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ciconst hostname = os.hostname(); 811cb0ef41Sopenharmony_ciis.string(hostname); 821cb0ef41Sopenharmony_ciassert.ok(hostname.length > 0); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci// IBMi process priority is different. 851cb0ef41Sopenharmony_ciif (!common.isIBMi) { 861cb0ef41Sopenharmony_ci const DUMMY_PRIORITY = 10; 871cb0ef41Sopenharmony_ci os.setPriority(DUMMY_PRIORITY); 881cb0ef41Sopenharmony_ci const priority = os.getPriority(); 891cb0ef41Sopenharmony_ci is.number(priority); 901cb0ef41Sopenharmony_ci assert.strictEqual(priority, DUMMY_PRIORITY); 911cb0ef41Sopenharmony_ci} 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci// On IBMi, os.uptime() returns 'undefined' 941cb0ef41Sopenharmony_ciif (!common.isIBMi) { 951cb0ef41Sopenharmony_ci const uptime = os.uptime(); 961cb0ef41Sopenharmony_ci is.number(uptime); 971cb0ef41Sopenharmony_ci assert.ok(uptime > 0); 981cb0ef41Sopenharmony_ci} 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ciconst cpus = os.cpus(); 1011cb0ef41Sopenharmony_ciis.array(cpus); 1021cb0ef41Sopenharmony_ciassert.ok(cpus.length > 0); 1031cb0ef41Sopenharmony_cifor (const cpu of cpus) { 1041cb0ef41Sopenharmony_ci assert.strictEqual(typeof cpu.model, 'string'); 1051cb0ef41Sopenharmony_ci assert.strictEqual(typeof cpu.speed, 'number'); 1061cb0ef41Sopenharmony_ci assert.strictEqual(typeof cpu.times.user, 'number'); 1071cb0ef41Sopenharmony_ci assert.strictEqual(typeof cpu.times.nice, 'number'); 1081cb0ef41Sopenharmony_ci assert.strictEqual(typeof cpu.times.sys, 'number'); 1091cb0ef41Sopenharmony_ci assert.strictEqual(typeof cpu.times.idle, 'number'); 1101cb0ef41Sopenharmony_ci assert.strictEqual(typeof cpu.times.irq, 'number'); 1111cb0ef41Sopenharmony_ci} 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ciconst type = os.type(); 1141cb0ef41Sopenharmony_ciis.string(type); 1151cb0ef41Sopenharmony_ciassert.ok(type.length > 0); 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ciconst release = os.release(); 1181cb0ef41Sopenharmony_ciis.string(release); 1191cb0ef41Sopenharmony_ciassert.ok(release.length > 0); 1201cb0ef41Sopenharmony_ci// TODO: Check format on more than just AIX 1211cb0ef41Sopenharmony_ciif (common.isAIX) 1221cb0ef41Sopenharmony_ci assert.match(release, /^\d+\.\d+$/); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ciconst platform = os.platform(); 1251cb0ef41Sopenharmony_ciis.string(platform); 1261cb0ef41Sopenharmony_ciassert.ok(platform.length > 0); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ciconst arch = os.arch(); 1291cb0ef41Sopenharmony_ciis.string(arch); 1301cb0ef41Sopenharmony_ciassert.ok(arch.length > 0); 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ciif (!common.isSunOS) { 1331cb0ef41Sopenharmony_ci // not implemented yet 1341cb0ef41Sopenharmony_ci assert.ok(os.loadavg().length > 0); 1351cb0ef41Sopenharmony_ci assert.ok(os.freemem() > 0); 1361cb0ef41Sopenharmony_ci assert.ok(os.totalmem() > 0); 1371cb0ef41Sopenharmony_ci} 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ciconst interfaces = os.networkInterfaces(); 1401cb0ef41Sopenharmony_ciswitch (platform) { 1411cb0ef41Sopenharmony_ci case 'linux': { 1421cb0ef41Sopenharmony_ci const filter = (e) => 1431cb0ef41Sopenharmony_ci e.address === '127.0.0.1' && 1441cb0ef41Sopenharmony_ci e.netmask === '255.0.0.0'; 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci const actual = interfaces.lo.filter(filter); 1471cb0ef41Sopenharmony_ci const expected = [{ 1481cb0ef41Sopenharmony_ci address: '127.0.0.1', 1491cb0ef41Sopenharmony_ci netmask: '255.0.0.0', 1501cb0ef41Sopenharmony_ci family: 'IPv4', 1511cb0ef41Sopenharmony_ci mac: '00:00:00:00:00:00', 1521cb0ef41Sopenharmony_ci internal: true, 1531cb0ef41Sopenharmony_ci cidr: '127.0.0.1/8' 1541cb0ef41Sopenharmony_ci }]; 1551cb0ef41Sopenharmony_ci assert.deepStrictEqual(actual, expected); 1561cb0ef41Sopenharmony_ci break; 1571cb0ef41Sopenharmony_ci } 1581cb0ef41Sopenharmony_ci case 'win32': { 1591cb0ef41Sopenharmony_ci const filter = (e) => 1601cb0ef41Sopenharmony_ci e.address === '127.0.0.1'; 1611cb0ef41Sopenharmony_ci 1621cb0ef41Sopenharmony_ci const actual = interfaces['Loopback Pseudo-Interface 1'].filter(filter); 1631cb0ef41Sopenharmony_ci const expected = [{ 1641cb0ef41Sopenharmony_ci address: '127.0.0.1', 1651cb0ef41Sopenharmony_ci netmask: '255.0.0.0', 1661cb0ef41Sopenharmony_ci family: 'IPv4', 1671cb0ef41Sopenharmony_ci mac: '00:00:00:00:00:00', 1681cb0ef41Sopenharmony_ci internal: true, 1691cb0ef41Sopenharmony_ci cidr: '127.0.0.1/8' 1701cb0ef41Sopenharmony_ci }]; 1711cb0ef41Sopenharmony_ci assert.deepStrictEqual(actual, expected); 1721cb0ef41Sopenharmony_ci break; 1731cb0ef41Sopenharmony_ci } 1741cb0ef41Sopenharmony_ci} 1751cb0ef41Sopenharmony_ciconst netmaskToCIDRSuffixMap = new Map(Object.entries({ 1761cb0ef41Sopenharmony_ci '255.0.0.0': 8, 1771cb0ef41Sopenharmony_ci '255.255.255.0': 24, 1781cb0ef41Sopenharmony_ci 'ffff:ffff:ffff:ffff::': 64, 1791cb0ef41Sopenharmony_ci 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff': 128 1801cb0ef41Sopenharmony_ci})); 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ciObject.values(interfaces) 1831cb0ef41Sopenharmony_ci .flat(Infinity) 1841cb0ef41Sopenharmony_ci .map((v) => ({ v, mask: netmaskToCIDRSuffixMap.get(v.netmask) })) 1851cb0ef41Sopenharmony_ci .forEach(({ v, mask }) => { 1861cb0ef41Sopenharmony_ci assert.ok('cidr' in v, `"cidr" prop not found in ${inspect(v)}`); 1871cb0ef41Sopenharmony_ci if (mask) { 1881cb0ef41Sopenharmony_ci assert.strictEqual(v.cidr, `${v.address}/${mask}`); 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci }); 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ciconst EOL = os.EOL; 1931cb0ef41Sopenharmony_ciif (common.isWindows) { 1941cb0ef41Sopenharmony_ci assert.strictEqual(EOL, '\r\n'); 1951cb0ef41Sopenharmony_ci} else { 1961cb0ef41Sopenharmony_ci assert.strictEqual(EOL, '\n'); 1971cb0ef41Sopenharmony_ci} 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ciconst home = os.homedir(); 2001cb0ef41Sopenharmony_ciis.string(home); 2011cb0ef41Sopenharmony_ciassert.ok(home.includes(path.sep)); 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ciconst version = os.version(); 2041cb0ef41Sopenharmony_ciassert.strictEqual(typeof version, 'string'); 2051cb0ef41Sopenharmony_ciassert(version); 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ciif (common.isWindows && process.env.USERPROFILE) { 2081cb0ef41Sopenharmony_ci assert.strictEqual(home, process.env.USERPROFILE); 2091cb0ef41Sopenharmony_ci delete process.env.USERPROFILE; 2101cb0ef41Sopenharmony_ci assert.ok(os.homedir().includes(path.sep)); 2111cb0ef41Sopenharmony_ci process.env.USERPROFILE = home; 2121cb0ef41Sopenharmony_ci} else if (!common.isWindows && process.env.HOME) { 2131cb0ef41Sopenharmony_ci assert.strictEqual(home, process.env.HOME); 2141cb0ef41Sopenharmony_ci delete process.env.HOME; 2151cb0ef41Sopenharmony_ci assert.ok(os.homedir().includes(path.sep)); 2161cb0ef41Sopenharmony_ci process.env.HOME = home; 2171cb0ef41Sopenharmony_ci} 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ciconst pwd = os.userInfo(); 2201cb0ef41Sopenharmony_ciis.object(pwd); 2211cb0ef41Sopenharmony_ciconst pwdBuf = os.userInfo({ encoding: 'buffer' }); 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ciif (common.isWindows) { 2241cb0ef41Sopenharmony_ci assert.strictEqual(pwd.uid, -1); 2251cb0ef41Sopenharmony_ci assert.strictEqual(pwd.gid, -1); 2261cb0ef41Sopenharmony_ci assert.strictEqual(pwd.shell, null); 2271cb0ef41Sopenharmony_ci assert.strictEqual(pwdBuf.uid, -1); 2281cb0ef41Sopenharmony_ci assert.strictEqual(pwdBuf.gid, -1); 2291cb0ef41Sopenharmony_ci assert.strictEqual(pwdBuf.shell, null); 2301cb0ef41Sopenharmony_ci} else { 2311cb0ef41Sopenharmony_ci is.number(pwd.uid); 2321cb0ef41Sopenharmony_ci is.number(pwd.gid); 2331cb0ef41Sopenharmony_ci assert.strictEqual(typeof pwd.shell, 'string'); 2341cb0ef41Sopenharmony_ci // It's possible for /etc/passwd to leave the user's shell blank. 2351cb0ef41Sopenharmony_ci if (pwd.shell.length > 0) { 2361cb0ef41Sopenharmony_ci assert(pwd.shell.includes(path.sep)); 2371cb0ef41Sopenharmony_ci } 2381cb0ef41Sopenharmony_ci assert.strictEqual(pwd.uid, pwdBuf.uid); 2391cb0ef41Sopenharmony_ci assert.strictEqual(pwd.gid, pwdBuf.gid); 2401cb0ef41Sopenharmony_ci assert.strictEqual(pwd.shell, pwdBuf.shell.toString('utf8')); 2411cb0ef41Sopenharmony_ci} 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ciis.string(pwd.username); 2441cb0ef41Sopenharmony_ciassert.ok(pwd.homedir.includes(path.sep)); 2451cb0ef41Sopenharmony_ciassert.strictEqual(pwd.username, pwdBuf.username.toString('utf8')); 2461cb0ef41Sopenharmony_ciassert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8')); 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ciassert.strictEqual(`${os.hostname}`, os.hostname()); 2491cb0ef41Sopenharmony_ciassert.strictEqual(`${os.homedir}`, os.homedir()); 2501cb0ef41Sopenharmony_ciassert.strictEqual(`${os.release}`, os.release()); 2511cb0ef41Sopenharmony_ciassert.strictEqual(`${os.type}`, os.type()); 2521cb0ef41Sopenharmony_ciassert.strictEqual(`${os.endianness}`, os.endianness()); 2531cb0ef41Sopenharmony_ciassert.strictEqual(`${os.tmpdir}`, os.tmpdir()); 2541cb0ef41Sopenharmony_ciassert.strictEqual(`${os.arch}`, os.arch()); 2551cb0ef41Sopenharmony_ciassert.strictEqual(`${os.platform}`, os.platform()); 2561cb0ef41Sopenharmony_ciassert.strictEqual(`${os.version}`, os.version()); 2571cb0ef41Sopenharmony_ciassert.strictEqual(`${os.machine}`, os.machine()); 2581cb0ef41Sopenharmony_ciassert.strictEqual(+os.totalmem, os.totalmem()); 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci// Assert that the following values are coercible to numbers. 2611cb0ef41Sopenharmony_ci// On IBMi, os.uptime() returns 'undefined' 2621cb0ef41Sopenharmony_ciif (!common.isIBMi) { 2631cb0ef41Sopenharmony_ci is.number(+os.uptime, 'uptime'); 2641cb0ef41Sopenharmony_ci is.number(os.uptime(), 'uptime'); 2651cb0ef41Sopenharmony_ci} 2661cb0ef41Sopenharmony_ci 2671cb0ef41Sopenharmony_ciis.number(+os.availableParallelism, 'availableParallelism'); 2681cb0ef41Sopenharmony_ciis.number(os.availableParallelism(), 'availableParallelism'); 2691cb0ef41Sopenharmony_ciis.number(+os.freemem, 'freemem'); 2701cb0ef41Sopenharmony_ciis.number(os.freemem(), 'freemem'); 2711cb0ef41Sopenharmony_ci 2721cb0ef41Sopenharmony_ciconst devNull = os.devNull; 2731cb0ef41Sopenharmony_ciif (common.isWindows) { 2741cb0ef41Sopenharmony_ci assert.strictEqual(devNull, '\\\\.\\nul'); 2751cb0ef41Sopenharmony_ci} else { 2761cb0ef41Sopenharmony_ci assert.strictEqual(devNull, '/dev/null'); 2771cb0ef41Sopenharmony_ci} 2781cb0ef41Sopenharmony_ci 2791cb0ef41Sopenharmony_ciassert.ok(os.availableParallelism() > 0); 280