xref: /third_party/node/test/parallel/test-os.js (revision 1cb0ef41)
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.match(endianness, /[BL]E/);
79
80const hostname = os.hostname();
81is.string(hostname);
82assert.ok(hostname.length > 0);
83
84// IBMi process priority is different.
85if (!common.isIBMi) {
86  const DUMMY_PRIORITY = 10;
87  os.setPriority(DUMMY_PRIORITY);
88  const priority = os.getPriority();
89  is.number(priority);
90  assert.strictEqual(priority, DUMMY_PRIORITY);
91}
92
93// On IBMi, os.uptime() returns 'undefined'
94if (!common.isIBMi) {
95  const uptime = os.uptime();
96  is.number(uptime);
97  assert.ok(uptime > 0);
98}
99
100const cpus = os.cpus();
101is.array(cpus);
102assert.ok(cpus.length > 0);
103for (const cpu of cpus) {
104  assert.strictEqual(typeof cpu.model, 'string');
105  assert.strictEqual(typeof cpu.speed, 'number');
106  assert.strictEqual(typeof cpu.times.user, 'number');
107  assert.strictEqual(typeof cpu.times.nice, 'number');
108  assert.strictEqual(typeof cpu.times.sys, 'number');
109  assert.strictEqual(typeof cpu.times.idle, 'number');
110  assert.strictEqual(typeof cpu.times.irq, 'number');
111}
112
113const type = os.type();
114is.string(type);
115assert.ok(type.length > 0);
116
117const release = os.release();
118is.string(release);
119assert.ok(release.length > 0);
120// TODO: Check format on more than just AIX
121if (common.isAIX)
122  assert.match(release, /^\d+\.\d+$/);
123
124const platform = os.platform();
125is.string(platform);
126assert.ok(platform.length > 0);
127
128const arch = os.arch();
129is.string(arch);
130assert.ok(arch.length > 0);
131
132if (!common.isSunOS) {
133  // not implemented yet
134  assert.ok(os.loadavg().length > 0);
135  assert.ok(os.freemem() > 0);
136  assert.ok(os.totalmem() > 0);
137}
138
139const interfaces = os.networkInterfaces();
140switch (platform) {
141  case 'linux': {
142    const filter = (e) =>
143      e.address === '127.0.0.1' &&
144      e.netmask === '255.0.0.0';
145
146    const actual = interfaces.lo.filter(filter);
147    const expected = [{
148      address: '127.0.0.1',
149      netmask: '255.0.0.0',
150      family: 'IPv4',
151      mac: '00:00:00:00:00:00',
152      internal: true,
153      cidr: '127.0.0.1/8'
154    }];
155    assert.deepStrictEqual(actual, expected);
156    break;
157  }
158  case 'win32': {
159    const filter = (e) =>
160      e.address === '127.0.0.1';
161
162    const actual = interfaces['Loopback Pseudo-Interface 1'].filter(filter);
163    const expected = [{
164      address: '127.0.0.1',
165      netmask: '255.0.0.0',
166      family: 'IPv4',
167      mac: '00:00:00:00:00:00',
168      internal: true,
169      cidr: '127.0.0.1/8'
170    }];
171    assert.deepStrictEqual(actual, expected);
172    break;
173  }
174}
175const netmaskToCIDRSuffixMap = new Map(Object.entries({
176  '255.0.0.0': 8,
177  '255.255.255.0': 24,
178  'ffff:ffff:ffff:ffff::': 64,
179  'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff': 128
180}));
181
182Object.values(interfaces)
183  .flat(Infinity)
184  .map((v) => ({ v, mask: netmaskToCIDRSuffixMap.get(v.netmask) }))
185  .forEach(({ v, mask }) => {
186    assert.ok('cidr' in v, `"cidr" prop not found in ${inspect(v)}`);
187    if (mask) {
188      assert.strictEqual(v.cidr, `${v.address}/${mask}`);
189    }
190  });
191
192const EOL = os.EOL;
193if (common.isWindows) {
194  assert.strictEqual(EOL, '\r\n');
195} else {
196  assert.strictEqual(EOL, '\n');
197}
198
199const home = os.homedir();
200is.string(home);
201assert.ok(home.includes(path.sep));
202
203const version = os.version();
204assert.strictEqual(typeof version, 'string');
205assert(version);
206
207if (common.isWindows && process.env.USERPROFILE) {
208  assert.strictEqual(home, process.env.USERPROFILE);
209  delete process.env.USERPROFILE;
210  assert.ok(os.homedir().includes(path.sep));
211  process.env.USERPROFILE = home;
212} else if (!common.isWindows && process.env.HOME) {
213  assert.strictEqual(home, process.env.HOME);
214  delete process.env.HOME;
215  assert.ok(os.homedir().includes(path.sep));
216  process.env.HOME = home;
217}
218
219const pwd = os.userInfo();
220is.object(pwd);
221const pwdBuf = os.userInfo({ encoding: 'buffer' });
222
223if (common.isWindows) {
224  assert.strictEqual(pwd.uid, -1);
225  assert.strictEqual(pwd.gid, -1);
226  assert.strictEqual(pwd.shell, null);
227  assert.strictEqual(pwdBuf.uid, -1);
228  assert.strictEqual(pwdBuf.gid, -1);
229  assert.strictEqual(pwdBuf.shell, null);
230} else {
231  is.number(pwd.uid);
232  is.number(pwd.gid);
233  assert.strictEqual(typeof pwd.shell, 'string');
234  // It's possible for /etc/passwd to leave the user's shell blank.
235  if (pwd.shell.length > 0) {
236    assert(pwd.shell.includes(path.sep));
237  }
238  assert.strictEqual(pwd.uid, pwdBuf.uid);
239  assert.strictEqual(pwd.gid, pwdBuf.gid);
240  assert.strictEqual(pwd.shell, pwdBuf.shell.toString('utf8'));
241}
242
243is.string(pwd.username);
244assert.ok(pwd.homedir.includes(path.sep));
245assert.strictEqual(pwd.username, pwdBuf.username.toString('utf8'));
246assert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8'));
247
248assert.strictEqual(`${os.hostname}`, os.hostname());
249assert.strictEqual(`${os.homedir}`, os.homedir());
250assert.strictEqual(`${os.release}`, os.release());
251assert.strictEqual(`${os.type}`, os.type());
252assert.strictEqual(`${os.endianness}`, os.endianness());
253assert.strictEqual(`${os.tmpdir}`, os.tmpdir());
254assert.strictEqual(`${os.arch}`, os.arch());
255assert.strictEqual(`${os.platform}`, os.platform());
256assert.strictEqual(`${os.version}`, os.version());
257assert.strictEqual(`${os.machine}`, os.machine());
258assert.strictEqual(+os.totalmem, os.totalmem());
259
260// Assert that the following values are coercible to numbers.
261// On IBMi, os.uptime() returns 'undefined'
262if (!common.isIBMi) {
263  is.number(+os.uptime, 'uptime');
264  is.number(os.uptime(), 'uptime');
265}
266
267is.number(+os.availableParallelism, 'availableParallelism');
268is.number(os.availableParallelism(), 'availableParallelism');
269is.number(+os.freemem, 'freemem');
270is.number(os.freemem(), 'freemem');
271
272const devNull = os.devNull;
273if (common.isWindows) {
274  assert.strictEqual(devNull, '\\\\.\\nul');
275} else {
276  assert.strictEqual(devNull, '/dev/null');
277}
278
279assert.ok(os.availableParallelism() > 0);
280