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
174
175
176
177
178
179
180
181
182
183
184
185
|
'use strict'
const { readFileSync } = require('fs')
const { join } = require('path')
const https = require('https')
const crypto = require('crypto')
const { test, teardown } = require('tap')
const { Client, Pool } = require('..')
const { kSocket } = require('../lib/core/symbols')
const { nodeMajor } = require('../lib/core/util')
const options = {
key: readFileSync(join(__dirname, 'fixtures', 'key.pem'), 'utf8'),
cert: readFileSync(join(__dirname, 'fixtures', 'cert.pem'), 'utf8')
}
const ca = readFileSync(join(__dirname, 'fixtures', 'ca.pem'), 'utf8')
test('A client should disable session caching', {
skip: nodeMajor < 11 // tls socket session event has been added in Node 11. Cf. https://nodejs.org/api/tls.html#tls_event_session
}, t => {
const clientSessions = {}
let serverRequests = 0
t.test('Prepare request', t => {
t.plan(3)
const server = https.createServer(options, (req, res) => {
if (req.url === '/drop-key') {
server.setTicketKeys(crypto.randomBytes(48))
}
serverRequests++
res.end()
})
server.listen(0, function () {
const tls = {
ca,
rejectUnauthorized: false,
servername: 'agent1'
}
const client = new Client(`https://localhost:${server.address().port}`, {
pipelining: 0,
tls,
maxCachedSessions: 0
})
t.teardown(() => {
client.close()
server.close()
})
const queue = [{
name: 'first',
method: 'GET',
path: '/'
}, {
name: 'second',
method: 'GET',
path: '/'
}]
function request () {
const options = queue.shift()
if (options.ciphers) {
// Choose different cipher to use different cache entry
tls.ciphers = options.ciphers
} else {
delete tls.ciphers
}
client.request(options, (err, data) => {
t.error(err)
clientSessions[options.name] = client[kSocket].getSession()
data.body.resume().on('end', () => {
if (queue.length !== 0) {
return request()
}
t.pass()
})
})
}
request()
})
})
t.test('Verify cached sessions', t => {
t.plan(2)
t.equal(serverRequests, 2)
t.not(
clientSessions.first.toString('hex'),
clientSessions.second.toString('hex')
)
})
t.end()
})
test('A pool should be able to reuse TLS sessions between clients', {
skip: nodeMajor < 11 // tls socket session event has been added in Node 11. Cf. https://nodejs.org/api/tls.html#tls_event_session
}, t => {
let serverRequests = 0
const REQ_COUNT = 10
const ASSERT_PERFORMANCE_GAIN = false
t.test('Prepare request', t => {
t.plan(2 + 1 + (ASSERT_PERFORMANCE_GAIN ? 1 : 0))
const server = https.createServer(options, (req, res) => {
serverRequests++
res.end()
})
let numSessions = 0
const sessions = []
server.listen(0, async () => {
const poolWithSessionReuse = new Pool(`https://localhost:${server.address().port}`, {
pipelining: 0,
connections: 100,
maxCachedSessions: 1,
tls: {
ca,
rejectUnauthorized: false,
servername: 'agent1'
}
})
const poolWithoutSessionReuse = new Pool(`https://localhost:${server.address().port}`, {
pipelining: 0,
connections: 100,
maxCachedSessions: 0,
tls: {
ca,
rejectUnauthorized: false,
servername: 'agent1'
}
})
poolWithSessionReuse.on('connect', (url, targets) => {
const y = targets[1][kSocket].getSession()
if (sessions.some(x => x.equals(y))) {
return
}
sessions.push(y)
numSessions++
})
t.teardown(() => {
poolWithSessionReuse.close()
poolWithoutSessionReuse.close()
server.close()
})
function request (pool, expectTLSSessionCache) {
return new Promise((resolve, reject) => {
pool.request({
method: 'GET',
path: '/'
}, (err, data) => {
if (err) return reject(err)
data.body.resume().on('end', resolve)
})
})
}
async function runRequests (pool, numIterations, expectTLSSessionCache) {
const requests = []
// For the session reuse, we first need one client to connect to receive a valid tls session to reuse
await request(pool, false)
while (numIterations--) {
requests.push(request(pool, expectTLSSessionCache))
}
return await Promise.all(requests)
}
await runRequests(poolWithoutSessionReuse, REQ_COUNT, false)
await runRequests(poolWithSessionReuse, REQ_COUNT, true)
t.equal(numSessions, 2)
t.equal(serverRequests, 2 + REQ_COUNT * 2)
t.pass()
})
})
t.end()
})
teardown(() => process.exit())
|