Skip to content

Commit ebbed60

Browse files
sam-githubtargos
authored andcommitted
test: rework to remove flakiness, and be parallel
Let .end() propogate from client, to server, and back, before considering the test complete. Also, remove the test vector and exit handling in favour of running all the tests in parallel and using common.must/mustNotCall(). PR-URL: #27300 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 8f34428 commit ebbed60

File tree

1 file changed

+65
-73
lines changed

1 file changed

+65
-73
lines changed

test/parallel/test-tls-sni-option.js

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -69,114 +69,106 @@ const SNIContexts = {
6969
}
7070
};
7171

72-
const clientsOptions = [{
72+
test({
7373
port: undefined,
7474
key: loadPEM('agent1-key'),
7575
cert: loadPEM('agent1-cert'),
7676
ca: [loadPEM('ca1-cert')],
7777
servername: 'a.example.com',
7878
rejectUnauthorized: false
79-
}, {
79+
},
80+
true,
81+
{ sni: 'a.example.com', authorized: false },
82+
null,
83+
null);
84+
85+
test({
8086
port: undefined,
8187
key: loadPEM('agent4-key'),
8288
cert: loadPEM('agent4-cert'),
8389
ca: [loadPEM('ca1-cert')],
8490
servername: 'a.example.com',
8591
rejectUnauthorized: false
86-
}, {
92+
},
93+
true,
94+
{ sni: 'a.example.com', authorized: true },
95+
null,
96+
null);
97+
98+
test({
8799
port: undefined,
88100
key: loadPEM('agent2-key'),
89101
cert: loadPEM('agent2-cert'),
90102
ca: [loadPEM('ca2-cert')],
91103
servername: 'b.example.com',
92104
rejectUnauthorized: false
93-
}, {
105+
},
106+
true,
107+
{ sni: 'b.example.com', authorized: false },
108+
null,
109+
null);
110+
111+
test({
94112
port: undefined,
95113
key: loadPEM('agent3-key'),
96114
cert: loadPEM('agent3-cert'),
97115
ca: [loadPEM('ca1-cert')],
98116
servername: 'c.wrong.com',
99117
rejectUnauthorized: false
100-
}, {
118+
},
119+
false,
120+
{ sni: 'c.wrong.com', authorized: false },
121+
null,
122+
null);
123+
124+
test({
101125
port: undefined,
102126
key: loadPEM('agent3-key'),
103127
cert: loadPEM('agent3-cert'),
104128
ca: [loadPEM('ca1-cert')],
105129
servername: 'c.another.com',
106130
rejectUnauthorized: false
107-
}];
108-
109-
const serverResults = [];
110-
const clientResults = [];
111-
const serverErrors = [];
112-
const clientErrors = [];
113-
let serverError;
114-
let clientError;
115-
116-
const server = tls.createServer(serverOptions, function(c) {
117-
serverResults.push({ sni: c.servername, authorized: c.authorized });
118-
c.end();
119-
});
120-
121-
server.on('tlsClientError', function(err) {
122-
serverResults.push(null);
123-
serverError = err.message;
124-
});
125-
126-
server.listen(0, startTest);
131+
},
132+
false,
133+
null,
134+
'Client network socket disconnected before secure TLS ' +
135+
'connection was established',
136+
'Invalid SNI context');
137+
138+
function test(options, clientResult, serverResult, clientError, serverError) {
139+
const server = tls.createServer(serverOptions, (c) => {
140+
assert.deepStrictEqual(
141+
{ sni: c.servername, authorized: c.authorized },
142+
serverResult
143+
);
144+
});
127145

128-
function startTest() {
129-
function connectClient(i, callback) {
130-
const options = clientsOptions[i];
131-
clientError = null;
132-
serverError = null;
146+
if (serverResult) {
147+
assert(!serverError);
148+
server.on('tlsClientError', common.mustNotCall());
149+
} else {
150+
assert(serverError);
151+
server.on('tlsClientError', common.mustCall((err) => {
152+
assert.strictEqual(err.message, serverError);
153+
}));
154+
}
133155

156+
server.listen(0, () => {
134157
options.port = server.address().port;
135-
const client = tls.connect(options, function() {
136-
clientResults.push(
137-
client.authorizationError &&
138-
(client.authorizationError === 'ERR_TLS_CERT_ALTNAME_INVALID'));
139-
140-
next();
141-
});
142-
143-
client.on('error', function(err) {
144-
clientResults.push(false);
145-
clientError = err.message;
146-
next();
158+
const client = tls.connect(options, () => {
159+
const result = client.authorizationError &&
160+
(client.authorizationError === 'ERR_TLS_CERT_ALTNAME_INVALID');
161+
assert.strictEqual(result, clientResult);
162+
client.end();
147163
});
148164

149-
function next() {
150-
clientErrors.push(clientError);
151-
serverErrors.push(serverError);
152-
153-
if (i === clientsOptions.length - 1)
154-
callback();
155-
else
156-
connectClient(i + 1, callback);
157-
}
158-
}
165+
client.on('close', common.mustCall(() => server.close()));
159166

160-
connectClient(0, function() {
161-
server.close();
167+
if (clientError)
168+
client.on('error', common.mustCall((err) => {
169+
assert.strictEqual(err.message, clientError);
170+
}));
171+
else
172+
client.on('error', common.mustNotCall());
162173
});
163174
}
164-
165-
process.on('exit', function() {
166-
assert.deepStrictEqual(serverResults, [
167-
{ sni: 'a.example.com', authorized: false },
168-
{ sni: 'a.example.com', authorized: true },
169-
{ sni: 'b.example.com', authorized: false },
170-
{ sni: 'c.wrong.com', authorized: false },
171-
null
172-
]);
173-
assert.deepStrictEqual(clientResults, [true, true, true, false, false]);
174-
assert.deepStrictEqual(clientErrors, [
175-
null, null, null, null,
176-
'Client network socket disconnected before secure TLS ' +
177-
'connection was established'
178-
]);
179-
assert.deepStrictEqual(serverErrors, [
180-
null, null, null, null, 'Invalid SNI context'
181-
]);
182-
});

0 commit comments

Comments
 (0)