Skip to content

Commit

Permalink
quic: add test-quic-openstream-errors
Browse files Browse the repository at this point in the history
PR-URL: nodejs#292
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
jasnell committed Feb 3, 2020
1 parent 86b2c14 commit efce2bd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/internal/quic/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ class QuicEndpoint {
(this.#reuseAddr ? UV_UDP_REUSEADDR : 0) |
(this.#ipv6Only ? UV_UDP_IPV6ONLY : 0);
const udpHandle = this.#udpSocket[internalDgram.kStateSymbol].handle;
if (udpHandle == null)
return;
const ret = udpHandle.bind(ip, this.#port || 0, flags);
if (ret) {
this.destroy(exceptionWithHostPort(ret, 'bind', ip, this.#port || 0));
Expand Down Expand Up @@ -1918,8 +1920,10 @@ class QuicSession extends EventEmitter {
halfOpen ?
_openUnidirectionalStream(this[kHandle]) :
_openBidirectionalStream(this[kHandle]);
if (handle === undefined)
this.emit('error', new ERR_QUICSTREAM_OPEN_FAILED());
if (handle === undefined) {
stream.destroy(new ERR_QUICSTREAM_OPEN_FAILED());
return;
}

stream[kSetHandle](handle);
this[kAddStream](stream.id, stream);
Expand Down
56 changes: 56 additions & 0 deletions test/parallel/test-quic-openstream-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Flags: --no-warnings
'use strict';
const common = require('../common');
if (!common.hasQuic)
common.skip('missing quic');

// Test errors thrown when openStream is called incorrectly
// or is not permitted

const assert = require('assert');
const quic = require('quic');

const Countdown = require('../common/countdown');
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 options = { key, cert, ca, alpn: 'zzz', maxStreamsUni: 0 };
const server = quic.createSocket({ server: options });
const client = quic.createSocket({ client: options });

const countdown = new Countdown(1, () => {
server.close();
client.close();
});

server.listen();
server.on('session', common.mustCall((session) => {
session.on('stream', common.mustNotCall());
}));

server.on('ready', common.mustCall(() => {
const req = client.connect({
address: 'localhost',
port: server.endpoints[0].address.port
});

['z', 1, {}, [], null, Infinity, 1n, new Date()].forEach((i) => {
assert.throws(
() => req.openStream({ halfOpen: i }),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});

// Unidirectional streams are not allowed. openStream will succeeed
// but the stream will be destroyed immediately.
req.openStream({ halfOpen: true })
.on('error', common.expectsError({
code: 'ERR_QUICSTREAM_OPEN_FAILED'
}))
.on('error', common.mustCall(() => countdown.dec()));

}));

server.on('close', common.mustCall());

0 comments on commit efce2bd

Please sign in to comment.