node/test/parallel/test-internal-os.js
Mudit Ameta 4a6b678070
os: add CIDR support
This patch adds support for CIDR notation to the output of the
`networkInterfaces()` method

PR-URL: https://github.com/nodejs/node/pull/14307
Fixes: https://github.com/nodejs/node/issues/14006
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
2017-08-14 15:43:10 -04:00

33 lines
893 B
JavaScript

// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const getCIDRSuffix = require('internal/os').getCIDRSuffix;
const specs = [
// valid
['128.0.0.0', 'ipv4', 1],
['255.0.0.0', 'ipv4', 8],
['255.255.255.128', 'ipv4', 25],
['255.255.255.255', 'ipv4', 32],
['ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'ipv6', 128],
['ffff:ffff:ffff:ffff::', 'ipv6', 64],
['ffff:ffff:ffff:ff80::', 'ipv6', 57],
// invalid
['255.0.0.1', 'ipv4', null],
['255.255.9.0', 'ipv4', null],
['255.255.1.0', 'ipv4', null],
['ffff:ffff:43::', 'ipv6', null],
['ffff:ffff:ffff:1::', 'ipv6', null]
];
specs.forEach(([mask, protocol, expectedSuffix]) => {
const actualSuffix = getCIDRSuffix(mask, protocol);
assert.strictEqual(
actualSuffix, expectedSuffix,
`Mask: ${mask}, expected: ${expectedSuffix}, actual: ${actualSuffix}`
);
});