summaryrefslogtreecommitdiff
path: root/test/parallel/test-quic-simple-client-migrate.js
blob: 2aed15704a1ac63f976556e4137d0a02e4072dd6 (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
// Flags: --expose-internals --no-warnings
'use strict';

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

const assert = require('assert');
const { key, cert, ca } = require('../common/quic');

const { once } = require('events');
const { createQuicSocket } = require('net');
const { pipeline } = require('stream');

const options = { key, cert, ca, alpn: 'zzz' };

let req;
const client = createQuicSocket({ client: options });
const client2 = createQuicSocket({ client: options });
const server = createQuicSocket({ server: options });

(async function() {
  server.on('session', common.mustCall((session) => {
    session.on('stream', common.mustCall(async (stream) => {
      pipeline(stream, stream, common.mustSucceed());
      (await session.openStream({ halfOpen: true }))
        .end('Hello from the server');
    }));
  }));

  await server.listen();

  req = await client.connect({
    address: common.localhostIPv4,
    port: server.endpoints[0].address.port,
  });

  req.on('close', () => {
    client2.close();
    server.close();
  });

  req.on('stream', common.mustCall((stream) => {
    let data = '';
    stream.setEncoding('utf8');
    stream.on('data', (chunk) => data += chunk);
    stream.on('end', common.mustCall(() => {
      assert.strictEqual(data, 'Hello from the server');
    }));
    stream.on('close', common.mustCall());
  }));

  let data = '';
  const stream = await req.openStream();
  stream.setEncoding('utf8');
  stream.on('data', (chunk) => data += chunk);
  stream.on('end', common.mustCall(() => {
    assert.strictEqual(data, 'Hello from the client');
  }));
  stream.on('close', common.mustCall(() => {
    req.close();
  }));
  // Send some data on one connection...
  stream.write('Hello ');

  // Wait just a bit, then migrate to a different
  // QuicSocket and continue sending.
  setTimeout(common.mustCall(async () => {
    const s1 = req.socket;
    const a1 = req.socket.endpoints[0].address;

    await Promise.all([1, {}, 'test', false, null, undefined].map((i) => {
      return assert.rejects(req.setSocket(i), {
        code: 'ERR_INVALID_ARG_TYPE'
      });
    }));
    await Promise.all([1, {}, 'test', null].map((i) => {
      return assert.rejects(req.setSocket(req.socket, i), {
        code: 'ERR_INVALID_ARG_TYPE'
      });
    }));

    await req.setSocket(client2);

    // Verify that it is using a different network endpoint
    assert.notStrictEqual(s1, req.socket);
    assert.notDeepStrictEqual(a1, req.socket.endpoints[0].address);
    client.close();
    stream.end('from the client');
  }), common.platformTimeout(100));

  await Promise.all([
    once(server, 'close'),
    once(client2, 'close')
  ]);
})().then(common.mustCall());