-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node:net):
node:net
compatability
- Loading branch information
Showing
6 changed files
with
177 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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. | ||
|
||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
|
||
const server = net.createServer(common.mustCall((s) => { | ||
console.error('SERVER: got connection'); | ||
s.end(); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const c = net.createConnection(server.address().port); | ||
c.on('close', common.mustCall(() => { | ||
/* eslint-disable no-unused-expressions */ | ||
console.error('connection closed'); | ||
assert.strictEqual(c._handle, null); | ||
// Calling functions / accessing properties of a closed socket should not | ||
// throw. | ||
c.setNoDelay(); | ||
c.setKeepAlive(); | ||
c.bufferSize; | ||
c.pause(); | ||
c.resume(); | ||
c.address(); | ||
c.remoteAddress; | ||
c.remotePort; | ||
server.close(); | ||
/* eslint-enable no-unused-expressions */ | ||
})); | ||
})); |
44 changes: 44 additions & 0 deletions
44
test/js/node/test/parallel/test-net-listen-invalid-port.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
// This test ensures that port numbers are validated in *all* kinds of `listen` | ||
// calls. If an invalid port is supplied, ensures a `RangeError` is thrown. | ||
// https://github.com/nodejs/node/issues/5727 | ||
|
||
const assert = require('assert'); | ||
const net = require('net'); | ||
|
||
const invalidPort = -1 >>> 0; | ||
|
||
const s = net.Server(); | ||
s.listen(0, function() { | ||
const address = this.address(); | ||
const key = `${address.family.slice(-1)}:${address.address}:0`; | ||
|
||
assert.strictEqual(this._connectionKey, key); | ||
this.close(); | ||
}); | ||
|
||
// The first argument is a configuration object | ||
assert.throws(() => { | ||
net.Server().listen({ port: invalidPort }, common.mustNotCall()); | ||
}, { | ||
code: 'ERR_SOCKET_BAD_PORT', | ||
name: 'RangeError' | ||
}); | ||
|
||
// The first argument is the port, no IP given. | ||
assert.throws(() => { | ||
net.Server().listen(invalidPort, common.mustNotCall()); | ||
}, { | ||
code: 'ERR_SOCKET_BAD_PORT', | ||
name: 'RangeError' | ||
}); | ||
|
||
// The first argument is the port, the second an IP. | ||
assert.throws(() => { | ||
net.Server().listen(invalidPort, '0.0.0.0', common.mustNotCall()); | ||
}, { | ||
code: 'ERR_SOCKET_BAD_PORT', | ||
name: 'RangeError' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasIPv6) | ||
common.skip('no IPv6 support'); | ||
|
||
// This test ensures that dual-stack support is disabled when | ||
// we specify the `ipv6Only` option in `net.Server.listen()`. | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
|
||
const host = '::'; | ||
const server = net.createServer(); | ||
server.listen({ | ||
host, | ||
port: 0, | ||
ipv6Only: true, | ||
}, common.mustCall(() => { | ||
const { port } = server.address(); | ||
const socket = net.connect({ | ||
host: '0.0.0.0', | ||
port, | ||
}); | ||
|
||
socket.on('connect', common.mustNotCall()); | ||
socket.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err.code, 'ECONNREFUSED'); | ||
server.close(); | ||
})); | ||
})); |