Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions test/parallel/test-net-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

// Flags: --expose-internals

require('../common');
const common = require('../common');
const assert = require('assert');
const isLegalPort = require('internal/net').isLegalPort;
const { isLegalPort, makeSyncWrite } = require('internal/net');

for (let n = 0; n <= 0xFFFF; n++) {
assert(isLegalPort(n));
Expand All @@ -18,3 +18,38 @@ const bad = [-1, 'a', {}, [], false, true, 0xFFFF + 1, Infinity,
-Infinity, NaN, undefined, null, '', ' ', 1.1, '0x',
'-0x1', '-0o1', '-0b1', '0o', '0b'];
bad.forEach((i) => assert(!isLegalPort(i)));


function writeToStdout() {
const ctx = {
_handle: {
bytesWritten: 0
}
};
const syncWrite = makeSyncWrite(process.stdout.fd);
syncWrite.call(ctx, 'hello', 'utf-8', common.mustCall((ex) => {
assert.strictEqual(undefined, ex);
}));
syncWrite.call(ctx, Buffer.from('world'), 'buffer', common.mustCall((ex) => {
assert.strictEqual(undefined, ex);
}));
assert.strictEqual(ctx._handle.bytesWritten, 10);
}
function writeToInvalidFD() {
const ctx = {
_handle: {
bytesWritten: 0
}
};
const invalidFD = -1;
const syncWrite = makeSyncWrite(invalidFD);
syncWrite.call(ctx, Buffer.from('world'), 'buffer', common.mustCall((ex) => {
assert.strictEqual(ex.code, 'EBADF');
}));
assert.strictEqual(ctx._handle.bytesWritten, 5);
}
if (!common.isWindows) {
// This test will crash on windows, so skip it
writeToStdout();
writeToInvalidFD();
}