summaryrefslogtreecommitdiff
path: root/test/parallel/test-quic-errors-quicsocket-listen.js
blob: 19c14fb14fdc9b8978be0f9deca4b7479742c028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Flags: --no-warnings
'use strict';

// Tests error and input validation checks for QuicSocket.connect()

const common = require('../common');

if (!common.hasQuic)
  common.skip('missing quic');

const assert = require('assert');
const { createQuicSocket } = require('net');

async function testAlreadyListening() {
  const server = createQuicSocket();
  // Can be called multiple times while pending...
  await Promise.all([server.listen(), server.listen()]);
  // But fails if called again after resolving
  await assert.rejects(server.listen(), {
    code: 'ERR_INVALID_STATE'
  });
  server.close();
}

async function testListenAfterClose() {
  const server = createQuicSocket();
  server.close();
  await assert.rejects(server.listen(), {
    code: 'ERR_INVALID_STATE'
  });
}

async function rejectsValue(
  server,
  name,
  values,
  code = 'ERR_INVALID_ARG_TYPE') {
  for (const v of values) {
    await assert.rejects(server.listen({ [name]: v }), { code });
  }
}

async function testInvalidOptions() {
  const server = createQuicSocket();

  await rejectsValue(
    server,
    'alpn',
    [1, 1n, true, {}, [], null]);

  for (const prop of [
    'idleTimeout',
    'activeConnectionIdLimit',
    'maxAckDelay',
    'maxData',
    'maxUdpPayloadSize',
    'maxStreamDataBidiLocal',
    'maxStreamDataBidiRemote',
    'maxStreamDataUni',
    'maxStreamsBidi',
    'maxStreamsUni',
    'highWaterMark',
  ]) {
    await rejectsValue(
      server,
      prop,
      [-1],
      'ERR_OUT_OF_RANGE');
    await rejectsValue(
      server,
      prop,
      [Number.MAX_SAFE_INTEGER + 1],
      'ERR_OUT_OF_RANGE');
    await rejectsValue(
      server,
      prop,
      ['a', 1n, [], {}, false]);
  }

  await rejectsValue(
    server,
    'rejectUnauthorized',
    [1, 1n, 'test', {}, []]);

  await rejectsValue(
    server,
    'requestCert',
    [1, 1n, 'test', {}, []]);

  await rejectsValue(
    server,
    'ciphers',
    [1, 1n, false, {}, [], null]);

  await rejectsValue(
    server,
    'groups',
    [1, 1n, false, {}, [], null]);

  await rejectsValue(
    server,
    'defaultEncoding',
    [1, 1n, false, {}, [], 'zebra'],
    'ERR_INVALID_ARG_VALUE');

  await rejectsValue(
    server,
    'preferredAddress',
    [1, 1n, 'test', false]
  );

  await assert.rejects(
    server.listen({ preferredAddress: { port: -1 } }), {
      code: 'ERR_INVALID_ARG_TYPE'
    });

  for (const address of [1, 1n, null, false, [], {}]) {
    await assert.rejects(
      server.listen({ preferredAddress: { address } }), {
        code: 'ERR_INVALID_ARG_TYPE'
      });
  }

  for (const type of [1, 'test', false, null, [], {}]) {
    await assert.rejects(
      server.listen({ preferredAddress: { type } }), {
        code: 'ERR_INVALID_ARG_TYPE'
      });
  }

  // Make sure that after all of the validation checks, the socket
  // is not actually marked as listening at all.
  assert.strictEqual(typeof server.listening, 'boolean');
  assert(!server.listening);
}

(async function() {
  await testAlreadyListening();
  await testListenAfterClose();
  await testInvalidOptions();
})().then(common.mustCall());

// Options to check
// * [x] alpn
// * [x] idleTimeout
// * [x] activeConnectionIdLimit
// * [x] maxAckDelay
// * [x] maxData
// * [x] maxUdpPayloadSize
// * [x] maxStreamsBidi
// * [x] maxStreamsUni
// * [x] maxStreamDataBidiLocal
// * [x] maxStreamDataBidiRemote
// * [x] maxStreamDataUni
// * [x] preferredAddress
// * [x] requestCert
// * [x] rejectUnauthorized

// SecureContext Options
// * [ ] ca
// * [ ] cert
// * [x] ciphers
// * [ ] clientCertEngine
// * [ ] crl
// * [ ] dhparam
// * [ ] groups
// * [ ] ecdhCurve
// * [ ] honorCipherOrder
// * [ ] key
// * [ ] passphrase
// * [ ] pfx
// * [ ] secureOptions
// * [ ] sessionIdContext