From 03571aaacd2a7879e3d55b6e5112565afd0734a2 Mon Sep 17 00:00:00 2001 From: Ouyang Yadong Date: Sat, 12 Oct 2019 20:26:39 +0800 Subject: [PATCH] test: add tests for quic Also fix some lint issues. --- doc/api/quic.md | 9 +- lib/internal/quic/core.js | 1 - test/parallel/test-quic-ipv6only.js | 132 ++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-quic-ipv6only.js diff --git a/doc/api/quic.md b/doc/api/quic.md index ecd008730e..d2d651a53b 100644 --- a/doc/api/quic.md +++ b/doc/api/quic.md @@ -84,6 +84,7 @@ added: REPLACEME recently validated addresses are remembered. Setting `validateAddressLRU` to `true`, will enable the `validateAddress` option as well. Default: `false`. + * `ipv6Only` {boolean} Creates a new `QuicSocket` instance. @@ -1118,8 +1119,8 @@ added: REPLACEME * `length` {number} The amount of data from the fd to send. Default: `-1`. -Instead of using a `Quicstream` as a writable stream, send data from a given file -descriptor. +Instead of using a `Quicstream` as a writable stream, send data from a given +file descriptor. If `offset` is set to a non-negative number, reading starts from that position and the file offset will not be advanced. @@ -1146,8 +1147,8 @@ added: REPLACEME * `length` {number} The amount of data from the fd to send. Default: `-1`. -Instead of using a `QuicStream` as a writable stream, send data from a given file -path. +Instead of using a `QuicStream` as a writable stream, send data from a given +file path. The `options.onError` callback will be called if the file could not be opened. If `offset` is set to a non-negative number, reading starts from that position. diff --git a/lib/internal/quic/core.js b/lib/internal/quic/core.js index ae75d82caf..c12dbd156e 100644 --- a/lib/internal/quic/core.js +++ b/lib/internal/quic/core.js @@ -21,7 +21,6 @@ const { validateQuicClientSessionOptions, validateQuicSocketOptions, } = require('internal/quic/util'); -const { validateNumber } = require('internal/validators'); const util = require('util'); const assert = require('internal/assert'); const EventEmitter = require('events'); diff --git a/test/parallel/test-quic-ipv6only.js b/test/parallel/test-quic-ipv6only.js new file mode 100644 index 0000000000..c6b93ac704 --- /dev/null +++ b/test/parallel/test-quic-ipv6only.js @@ -0,0 +1,132 @@ +'use strict'; + +// TODO support ipv6 +const common = require('../common'); +if (!common.hasQuic) + common.skip('missing quic'); + +const { createSocket } = require('quic'); +const fixtures = require('../common/fixtures'); +const key = fixtures.readKey('agent1-key.pem', 'binary'); +const cert = fixtures.readKey('agent1-cert.pem', 'binary'); +const ca = fixtures.readKey('ca1-cert.pem', 'binary'); + +const kServerName = 'agent2'; +const kALPN = 'zzz'; + +// Setting `type` to `udp4` while setting `ipv6Only` to `true` is possible +// and it will throw an error. +{ + const server = createSocket({ + type: 'udp4', + port: 0, + ipv6Only: true, + }); + + server.on('error', common.mustCall((err) => { + common.expectsError({ + code: 'EINVAL', + type: Error, + // TODO(@oyyd): Currently we can't know the exact "syscall" so that it's + // undefined here. + message: 'undefined EINVAL', + })(err); + })); + + server.listen({ + key, + cert, + ca, + alpn: kALPN, + }); +} + +// Connecting ipv6 server by "127.0.0.1" should work when `ipv6Only` +// is set to `false`. +{ + const server = createSocket({ + type: 'udp6', + port: 0, + ipv6Only: false, + }); + + server.listen({ + key, + cert, + ca, + alpn: kALPN, + }); + + server.on('session', common.mustCall((serverSession) => { + serverSession.on('stream', common.mustCall()); + })); + + server.on('ready', common.mustCall(() => { + const client = createSocket({ + port: 0, + }); + + const clientSession = client.connect({ + key, + cert, + ca, + address: common.localhostIPv4, + port: server.address.port, + servername: kServerName, + alpn: kALPN, + }); + + clientSession.on('secure', common.mustCall(() => { + const clientStream = clientSession.openStream({ + halfOpen: true, + }); + clientStream.end('hello'); + clientStream.on('close', common.mustCall(() => { + client.close(); + server.close(); + })); + })); + })); +} + +// When the `ipv6Only` set to `true`, a client cann't connect to it +// through "127.0.0.1". +{ + const server = createSocket({ + type: 'udp6', + port: 0, + ipv6Only: true, + }); + + server.listen({ + key, + cert, + ca, + alpn: kALPN, + }); + + server.on('ready', common.mustCall(() => { + const client = createSocket({ + port: 0, + }); + + client.on('ready', common.mustCall()); + + const clientSession = client.connect({ + key, + cert, + ca, + address: common.localhostIPv4, + port: server.address.port, + servername: kServerName, + alpn: kALPN, + idleTimeout: common.platformTimeout(500), + }); + + clientSession.on('secure', common.mustNotCall()); + clientSession.on('close', common.mustCall(() => { + client.close(); + server.close(); + })); + })); +}