-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
96 lines (90 loc) · 3.06 KB
/
test.js
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
const tls = require('tls');
const fs = require('fs');
const child_process = require('child_process');
const assert = require('assert');
const {
exportCertificateAndPrivateKey,
exportCertificateAndPrivateKeyAsync,
exportSystemCertificates,
exportSystemCertificatesAsync
} = require('./');
describe('exportCertificateAndPrivateKey', () => {
let tlsServer;
let authorized;
let resolveAuthorized;
let tlsServerConnectOptions;
before((done) => {
const serverOpts = {
key: fs.readFileSync(__dirname + '/testkeys/testserver-privkey.pem'),
cert: fs.readFileSync(__dirname + '/testkeys/testserver-certificate.pem'),
requestCert: true,
ca: [fs.readFileSync(__dirname + '/testkeys/certificate.pem')]
};
tlsServer = tls.createServer(serverOpts, (socket) => {
resolveAuthorized(socket.authorized);
socket.end();
});
tlsServer.listen(0, () => {
tlsServerConnectOptions = {
host: 'localhost',
port: tlsServer.address().port,
rejectUnauthorized: false
};
done();
});
})
beforeEach(() => {
authorized = new Promise(resolve => resolveAuthorized = resolve);
});
after(() => {
tlsServer.close();
});
for (const method of ['sync', 'async']) {
const fn = {
sync: exportCertificateAndPrivateKey,
async: exportCertificateAndPrivateKeyAsync
}[method];
context(method, () => {
it('throws when no cert can be found', async() => {
await assert.rejects(async() => {
await fn({ subject: 'Banana Corp '});
}, /Could not find a matching certificate/);
});
it('loads a certificate based on its thumbprint', async() => {
const { passphrase, pfx } = await fn({
thumbprint: Buffer.from('d755afda2bbad2509d39eca5968553b9103305af', 'hex')
});
tls.connect({ ...tlsServerConnectOptions, passphrase, pfx });
assert.strictEqual(await authorized, true);
});
it('loads a certificate based on its subject', async() => {
const { passphrase, pfx } = await fn({
subject: 'Internet Widgits Pty Ltd'
});
tls.connect({ ...tlsServerConnectOptions, passphrase, pfx });
assert.strictEqual(await authorized, true);
});
});
}
});
describe('exportSystemCertificates', () => {
for (const method of ['sync', 'async']) {
const fn = {
sync: exportSystemCertificates,
async: exportSystemCertificatesAsync
}[method];
context(method, () => {
it('exports all system certificates', async() => {
const certsFromSecurity = child_process.execSync(
'security find-certificate -a -p && security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain', {
encoding: 'utf8'
})
.match(/^-----BEGIN\sCERTIFICATE-----[\s\S]+?-----END\sCERTIFICATE-----$/mg)
.map(str => str.trim());
const certsFromAddon = (await fn())
.map(str => str.trim());
assert.deepStrictEqual(new Set(certsFromAddon), new Set(certsFromSecurity));
});
});
}
});