2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2016-09-17 11:44:41 +02:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const os = require('os');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const is = {
|
|
|
|
string: (value) => { assert.strictEqual(typeof value, 'string'); },
|
|
|
|
number: (value) => { assert.strictEqual(typeof value, 'number'); },
|
|
|
|
array: (value) => { assert.ok(Array.isArray(value)); },
|
|
|
|
object: (value) => {
|
|
|
|
assert.strictEqual(typeof value, 'object');
|
|
|
|
assert.notStrictEqual(value, null);
|
|
|
|
}
|
|
|
|
};
|
2011-04-14 03:11:21 +02:00
|
|
|
|
2012-06-13 04:05:51 +02:00
|
|
|
process.env.TMPDIR = '/tmpdir';
|
|
|
|
process.env.TMP = '/tmp';
|
|
|
|
process.env.TEMP = '/temp';
|
2015-05-25 17:01:42 +02:00
|
|
|
if (common.isWindows) {
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/temp');
|
2013-12-10 15:52:27 +01:00
|
|
|
process.env.TEMP = '';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/tmp');
|
2013-12-10 15:52:27 +01:00
|
|
|
process.env.TMP = '';
|
2017-04-28 03:06:42 +02:00
|
|
|
const expected = `${process.env.SystemRoot || process.env.windir}\\temp`;
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), expected);
|
2015-02-06 23:27:22 +01:00
|
|
|
process.env.TEMP = '\\temp\\';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '\\temp');
|
2015-05-11 18:11:01 +02:00
|
|
|
process.env.TEMP = '\\tmpdir/';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '\\tmpdir/');
|
2015-05-11 18:11:01 +02:00
|
|
|
process.env.TEMP = '\\';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '\\');
|
2015-05-11 18:11:01 +02:00
|
|
|
process.env.TEMP = 'C:\\';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), 'C:\\');
|
2013-12-10 15:52:27 +01:00
|
|
|
} else {
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/tmpdir');
|
2013-12-10 15:52:27 +01:00
|
|
|
process.env.TMPDIR = '';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/tmp');
|
2013-12-10 15:52:27 +01:00
|
|
|
process.env.TMP = '';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/temp');
|
2013-12-10 15:52:27 +01:00
|
|
|
process.env.TEMP = '';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/tmp');
|
2015-02-06 23:27:22 +01:00
|
|
|
process.env.TMPDIR = '/tmpdir/';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/tmpdir');
|
2015-05-11 18:11:01 +02:00
|
|
|
process.env.TMPDIR = '/tmpdir\\';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/tmpdir\\');
|
2015-05-11 18:11:01 +02:00
|
|
|
process.env.TMPDIR = '/';
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(os.tmpdir(), '/');
|
2013-12-10 15:52:27 +01:00
|
|
|
}
|
2012-06-13 04:05:51 +02:00
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const endianness = os.endianness();
|
|
|
|
is.string(endianness);
|
2012-11-08 21:31:45 +01:00
|
|
|
assert.ok(/[BL]E/.test(endianness));
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const hostname = os.hostname();
|
|
|
|
is.string(hostname);
|
2011-03-27 12:14:34 +02:00
|
|
|
assert.ok(hostname.length > 0);
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const uptime = os.uptime();
|
|
|
|
is.number(uptime);
|
2011-03-27 12:14:34 +02:00
|
|
|
assert.ok(uptime > 0);
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const cpus = os.cpus();
|
|
|
|
is.array(cpus);
|
2011-03-27 12:14:34 +02:00
|
|
|
assert.ok(cpus.length > 0);
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const type = os.type();
|
|
|
|
is.string(type);
|
2011-03-27 12:14:34 +02:00
|
|
|
assert.ok(type.length > 0);
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const release = os.release();
|
|
|
|
is.string(release);
|
2011-03-27 12:14:34 +02:00
|
|
|
assert.ok(release.length > 0);
|
2016-12-13 05:56:24 +01:00
|
|
|
//TODO: Check format on more than just AIX
|
|
|
|
if (common.isAix)
|
|
|
|
assert.ok(/^\d+\.\d+$/.test(release));
|
2011-03-27 12:14:34 +02:00
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const platform = os.platform();
|
|
|
|
is.string(platform);
|
2011-04-27 05:02:54 +02:00
|
|
|
assert.ok(platform.length > 0);
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const arch = os.arch();
|
|
|
|
is.string(arch);
|
2011-04-27 05:02:54 +02:00
|
|
|
assert.ok(arch.length > 0);
|
|
|
|
|
2016-05-29 17:53:40 +02:00
|
|
|
if (!common.isSunOS) {
|
2011-03-27 12:14:34 +02:00
|
|
|
// not implemeneted yet
|
|
|
|
assert.ok(os.loadavg().length > 0);
|
|
|
|
assert.ok(os.freemem() > 0);
|
|
|
|
assert.ok(os.totalmem() > 0);
|
|
|
|
}
|
2011-03-17 00:31:38 +01:00
|
|
|
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const interfaces = os.networkInterfaces();
|
2011-04-27 05:02:54 +02:00
|
|
|
switch (platform) {
|
2011-03-17 00:31:38 +01:00
|
|
|
case 'linux':
|
2016-01-31 06:20:37 +01:00
|
|
|
{
|
2017-04-28 02:27:05 +02:00
|
|
|
const filter = (e) => e.address === '127.0.0.1';
|
2016-01-31 06:20:37 +01:00
|
|
|
const actual = interfaces.lo.filter(filter);
|
|
|
|
const expected = [{ address: '127.0.0.1', netmask: '255.0.0.0',
|
2017-01-01 06:39:57 +01:00
|
|
|
mac: '00:00:00:00:00:00', family: 'IPv4',
|
|
|
|
internal: true }];
|
2016-04-20 00:37:45 +02:00
|
|
|
assert.deepStrictEqual(actual, expected);
|
2016-01-31 06:20:37 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-12-15 21:36:05 +01:00
|
|
|
case 'win32':
|
2016-01-31 06:20:37 +01:00
|
|
|
{
|
2017-04-28 02:27:05 +02:00
|
|
|
const filter = (e) => e.address === '127.0.0.1';
|
2016-01-31 06:20:37 +01:00
|
|
|
const actual = interfaces['Loopback Pseudo-Interface 1'].filter(filter);
|
|
|
|
const expected = [{ address: '127.0.0.1', netmask: '255.0.0.0',
|
2017-01-01 06:39:57 +01:00
|
|
|
mac: '00:00:00:00:00:00', family: 'IPv4',
|
|
|
|
internal: true }];
|
2016-04-20 00:37:45 +02:00
|
|
|
assert.deepStrictEqual(actual, expected);
|
2016-01-31 06:20:37 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-03-17 00:31:38 +01:00
|
|
|
}
|
2012-04-12 10:29:15 +02:00
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const EOL = os.EOL;
|
2012-04-12 10:29:15 +02:00
|
|
|
assert.ok(EOL.length > 0);
|
2015-05-25 17:01:42 +02:00
|
|
|
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
const home = os.homedir();
|
2015-05-25 17:01:42 +02:00
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
is.string(home);
|
2016-09-24 23:11:47 +02:00
|
|
|
assert.ok(home.includes(path.sep));
|
2015-05-25 17:01:42 +02:00
|
|
|
|
|
|
|
if (common.isWindows && process.env.USERPROFILE) {
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(home, process.env.USERPROFILE);
|
2015-05-25 17:01:42 +02:00
|
|
|
delete process.env.USERPROFILE;
|
2016-09-24 23:11:47 +02:00
|
|
|
assert.ok(os.homedir().includes(path.sep));
|
2015-05-25 17:01:42 +02:00
|
|
|
process.env.USERPROFILE = home;
|
|
|
|
} else if (!common.isWindows && process.env.HOME) {
|
2016-09-17 11:44:41 +02:00
|
|
|
assert.strictEqual(home, process.env.HOME);
|
2015-05-25 17:01:42 +02:00
|
|
|
delete process.env.HOME;
|
2016-09-24 23:11:47 +02:00
|
|
|
assert.ok(os.homedir().includes(path.sep));
|
2015-05-25 17:01:42 +02:00
|
|
|
process.env.HOME = home;
|
|
|
|
}
|
2016-04-07 19:45:40 +02:00
|
|
|
|
|
|
|
const pwd = os.userInfo();
|
2016-09-17 11:44:41 +02:00
|
|
|
is.object(pwd);
|
2016-04-07 19:45:40 +02:00
|
|
|
const pwdBuf = os.userInfo({ encoding: 'buffer' });
|
|
|
|
|
|
|
|
if (common.isWindows) {
|
|
|
|
assert.strictEqual(pwd.uid, -1);
|
|
|
|
assert.strictEqual(pwd.gid, -1);
|
|
|
|
assert.strictEqual(pwd.shell, null);
|
|
|
|
assert.strictEqual(pwdBuf.uid, -1);
|
|
|
|
assert.strictEqual(pwdBuf.gid, -1);
|
|
|
|
assert.strictEqual(pwdBuf.shell, null);
|
|
|
|
} else {
|
2016-09-17 11:44:41 +02:00
|
|
|
is.number(pwd.uid);
|
|
|
|
is.number(pwd.gid);
|
2016-09-24 23:11:47 +02:00
|
|
|
assert.ok(pwd.shell.includes(path.sep));
|
2016-04-07 19:45:40 +02:00
|
|
|
assert.strictEqual(pwd.uid, pwdBuf.uid);
|
|
|
|
assert.strictEqual(pwd.gid, pwdBuf.gid);
|
|
|
|
assert.strictEqual(pwd.shell, pwdBuf.shell.toString('utf8'));
|
|
|
|
}
|
|
|
|
|
2016-09-17 11:44:41 +02:00
|
|
|
is.string(pwd.username);
|
2016-09-24 23:11:47 +02:00
|
|
|
assert.ok(pwd.homedir.includes(path.sep));
|
2016-04-07 19:45:40 +02:00
|
|
|
assert.strictEqual(pwd.username, pwdBuf.username.toString('utf8'));
|
|
|
|
assert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8'));
|
2017-04-25 23:48:32 +02:00
|
|
|
|
|
|
|
// Test that the Symbol.toPrimitive functions work correctly
|
|
|
|
[
|
|
|
|
[`${os.hostname}`, os.hostname()],
|
|
|
|
[`${os.homedir}`, os.homedir()],
|
|
|
|
[`${os.release}`, os.release()],
|
|
|
|
[`${os.type}`, os.type()],
|
|
|
|
[`${os.endianness}`, os.endianness()]
|
|
|
|
].forEach((set) => assert.strictEqual(set[0], set[1]));
|