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