|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Test that TLSSocket can take arrays of strings for ALPNProtocols and |
| 4 | +// NPNProtocols. |
| 5 | + |
| 6 | +const common = require('../common'); |
| 7 | + |
| 8 | +if (!common.hasCrypto) |
| 9 | + common.skip('missing crypto'); |
| 10 | + |
| 11 | +const tls = require('tls'); |
| 12 | + |
| 13 | +new tls.TLSSocket(null, { |
| 14 | + ALPNProtocols: ['http/1.1'], |
| 15 | + NPNProtocols: ['http/1.1'] |
| 16 | +}); |
| 17 | + |
| 18 | +if (!process.features.tls_npn) |
| 19 | + common.skip('node compiled without NPN feature of OpenSSL'); |
| 20 | + |
| 21 | +if (!process.features.tls_alpn) |
| 22 | + common.skip('node compiled without ALPN feature of OpenSSL'); |
| 23 | + |
| 24 | +const assert = require('assert'); |
| 25 | +const net = require('net'); |
| 26 | +const fixtures = require('../common/fixtures'); |
| 27 | + |
| 28 | +const key = fixtures.readKey('agent1-key.pem'); |
| 29 | +const cert = fixtures.readKey('agent1-cert.pem'); |
| 30 | + |
| 31 | +const protocols = []; |
| 32 | + |
| 33 | +const server = net.createServer(common.mustCall((s) => { |
| 34 | + const tlsSocket = new tls.TLSSocket(s, { |
| 35 | + isServer: true, |
| 36 | + server, |
| 37 | + key, |
| 38 | + cert, |
| 39 | + ALPNProtocols: ['http/1.1'], |
| 40 | + NPNProtocols: ['http/1.1'] |
| 41 | + }); |
| 42 | + |
| 43 | + tlsSocket.on('secure', common.mustCall(() => { |
| 44 | + protocols.push({ |
| 45 | + alpnProtocol: tlsSocket.alpnProtocol, |
| 46 | + npnProtocol: tlsSocket.npnProtocol |
| 47 | + }); |
| 48 | + tlsSocket.end(); |
| 49 | + })); |
| 50 | +}, 2)); |
| 51 | + |
| 52 | +server.listen(0, common.mustCall(() => { |
| 53 | + const alpnOpts = { |
| 54 | + port: server.address().port, |
| 55 | + rejectUnauthorized: false, |
| 56 | + ALPNProtocols: ['h2', 'http/1.1'] |
| 57 | + }; |
| 58 | + const npnOpts = { |
| 59 | + port: server.address().port, |
| 60 | + rejectUnauthorized: false, |
| 61 | + NPNProtocols: ['h2', 'http/1.1'] |
| 62 | + }; |
| 63 | + |
| 64 | + tls.connect(alpnOpts, function() { |
| 65 | + this.end(); |
| 66 | + |
| 67 | + tls.connect(npnOpts, function() { |
| 68 | + this.end(); |
| 69 | + |
| 70 | + server.close(); |
| 71 | + |
| 72 | + assert.deepStrictEqual(protocols, [ |
| 73 | + { alpnProtocol: 'http/1.1', npnProtocol: false }, |
| 74 | + { alpnProtocol: false, npnProtocol: 'http/1.1' } |
| 75 | + ]); |
| 76 | + }); |
| 77 | + }); |
| 78 | +})); |
0 commit comments