summaryrefslogtreecommitdiff
path: root/test/parallel/test-quic-quicsocket.js
blob: e4ee4895064c5c331be28b9f139766e9834b61e8 (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
// Flags: --no-warnings
'use strict';

// Test QuicSocket constructor option errors.

const common = require('../common');
if (!common.hasQuic)
  common.skip('missing quic');

const assert = require('assert');

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

const socket = createQuicSocket();
assert(socket);

// Before listen is called, serverSecureContext is always undefined.
assert.strictEqual(socket.serverSecureContext, undefined);

assert.deepStrictEqual(socket.endpoints.length, 1);

// Socket is not bound, so address should be empty
assert.deepStrictEqual(socket.endpoints[0].address, {});

// Socket is not bound
assert(!socket.bound);

// Socket is not pending
assert(!socket.pending);

// Socket is not destroyed
assert(!socket.destroyed);

assert.strictEqual(typeof socket.duration, 'number');
assert.strictEqual(typeof socket.boundDuration, 'number');
assert.strictEqual(typeof socket.listenDuration, 'number');
assert.strictEqual(typeof socket.bytesReceived, 'number');
assert.strictEqual(socket.bytesReceived, 0);
assert.strictEqual(socket.bytesSent, 0);
assert.strictEqual(socket.packetsReceived, 0);
assert.strictEqual(socket.packetsSent, 0);
assert.strictEqual(socket.serverSessions, 0);
assert.strictEqual(socket.clientSessions, 0);

const endpoint = socket.endpoints[0];
assert(endpoint);

// Will throw because the QuicSocket is not bound
{
  const err = { code: 'EBADF' };
  assert.throws(() => endpoint.setTTL(1), err);
  assert.throws(() => endpoint.setMulticastTTL(1), err);
  assert.throws(() => endpoint.setBroadcast(), err);
  assert.throws(() => endpoint.setMulticastLoopback(), err);
  assert.throws(() => endpoint.setMulticastInterface('0.0.0.0'), err);
  // TODO(@jasnell): Verify behavior of add/drop membership then test
  // assert.throws(() => endpoint.addMembership(
  //     '127.0.0.1', '127.0.0.1'), err);
  // assert.throws(() => endpoint.dropMembership(
  //     '127.0.0.1', '127.0.0.1'), err);
}

['test', null, {}, [], 1n, false].forEach((rx) => {
  assert.throws(() => socket.setDiagnosticPacketLoss({ rx }), {
    code: 'ERR_INVALID_ARG_TYPE'
  });
});

['test', null, {}, [], 1n, false].forEach((tx) => {
  assert.throws(() => socket.setDiagnosticPacketLoss({ tx }), {
    code: 'ERR_INVALID_ARG_TYPE'
  });
});

[
  { rx: -1 },
  { rx: 1.1 },
  { tx: -1 },
  { tx: 1.1 }
].forEach((options) => {
  assert.throws(() => socket.setDiagnosticPacketLoss(options), {
    code: 'ERR_OUT_OF_RANGE'
  });
});

[1, 1n, [], {}, null].forEach((arg) => {
  assert.throws(() => socket.serverBusy = arg, {
    code: 'ERR_INVALID_ARG_TYPE'
  });
});

(async function() {
  const p = socket.listen({ alpn: 'zzz' });
  assert(socket.pending);

  await p;

  assert(endpoint.bound);

  // QuicSocket is already listening.
  await assert.rejects(socket.listen(), {
    code: 'ERR_INVALID_STATE'
  });

  assert.strictEqual(typeof endpoint.address.address, 'string');
  assert.strictEqual(typeof endpoint.address.port, 'number');
  assert.strictEqual(typeof endpoint.address.family, 'string');

  if (!common.isWindows)
    assert.strictEqual(typeof endpoint.fd, 'number');

  endpoint.setTTL(1);
  endpoint.setMulticastTTL(1);
  endpoint.setBroadcast();
  endpoint.setBroadcast(true);
  endpoint.setBroadcast(false);

  endpoint.setMulticastLoopback();
  endpoint.setMulticastLoopback(true);
  endpoint.setMulticastLoopback(false);

  endpoint.setMulticastInterface('0.0.0.0');

  socket.setDiagnosticPacketLoss({ rx: 0.5, tx: 0.5 });

  socket.destroy();
  assert(socket.destroyed);
})().then(common.mustCall());

socket.on('close', common.mustCall(() => {
  [
    'ref',
    'unref',
    'setTTL',
    'setMulticastTTL',
    'setBroadcast',
    'setMulticastLoopback',
    'setMulticastInterface',
    'addMembership',
    'dropMembership'
  ].forEach((op) => {
    assert.throws(() => endpoint[op](), {
      code: 'ERR_INVALID_STATE'
    });
  });

  assert.throws(() => { socket.serverBusy = true; }, {
    code: 'ERR_INVALID_STATE'
  });
}));