node/test/parallel/test-tls-startcom-wosign-whitelist.js
James M Snell 7535a94c8a test: begin normalizing fixtures use
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

PR-URL: https://github.com/nodejs/node/pull/14332
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2017-08-07 18:00:57 -07:00

85 lines
2.0 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const tls = require('tls');
const fixtures = require('../common/fixtures');
let finished = 0;
function loadPEM(n) {
return fixtures.readKey(`${n}.pem`);
}
const testCases = [
{ // agent8 is signed by fake-startcom-root with notBefore of
// Oct 20 23:59:59 2016 GMT. It passes StartCom/WoSign check.
serverOpts: {
key: loadPEM('agent8-key'),
cert: loadPEM('agent8-cert')
},
clientOpts: {
ca: loadPEM('fake-startcom-root-cert'),
port: undefined,
rejectUnauthorized: true
},
errorCode: 'CERT_OK'
},
{ // agent9 is signed by fake-startcom-root with notBefore of
// Oct 21 00:00:01 2016 GMT. It fails StartCom/WoSign check.
serverOpts: {
key: loadPEM('agent9-key'),
cert: loadPEM('agent9-cert')
},
clientOpts: {
ca: loadPEM('fake-startcom-root-cert'),
port: undefined,
rejectUnauthorized: true
},
errorCode: 'CERT_REVOKED'
}
];
function runNextTest(server, tindex) {
server.close(function() {
finished++;
runTest(tindex + 1);
});
}
function runTest(tindex) {
const tcase = testCases[tindex];
if (!tcase) return;
const server = tls.createServer(tcase.serverOpts, function(s) {
s.resume();
}).listen(0, function() {
tcase.clientOpts.port = this.address().port;
const client = tls.connect(tcase.clientOpts);
client.on('error', function(e) {
assert.strictEqual(e.code, tcase.errorCode);
runNextTest(server, tindex);
});
client.on('secureConnect', function() {
// agent8 can pass StartCom/WoSign check so that the secureConnect
// is established.
assert.strictEqual(tcase.errorCode, 'CERT_OK');
client.end();
runNextTest(server, tindex);
});
});
}
runTest(0);
process.on('exit', function() {
assert.strictEqual(finished, testCases.length);
});