summaryrefslogtreecommitdiff
path: root/test/addons/openssl-client-cert-engine/test.js
blob: e843e4bf433bff7cd3faa500dedd3f6c9bac7370 (plain)
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
'use strict';
const common = require('../../common');
const fixture = require('../../common/fixtures');

if (!common.hasCrypto)
  common.skip('missing crypto');

const fs = require('fs');
const path = require('path');

const engine = path.join(__dirname,
                         `/build/${common.buildType}/testengine.engine`);

if (!fs.existsSync(engine))
  common.skip('no client cert engine');

const assert = require('assert');
const https = require('https');

const agentKey = fs.readFileSync(fixture.path('/keys/agent1-key.pem'));
const agentCert = fs.readFileSync(fixture.path('/keys/agent1-cert.pem'));
const agentCa = fs.readFileSync(fixture.path('/keys/ca1-cert.pem'));

const serverOptions = {
  key: agentKey,
  cert: agentCert,
  ca: agentCa,
  requestCert: true,
  rejectUnauthorized: true
};

const server = https.createServer(serverOptions, common.mustCall((req, res) => {
  res.writeHead(200);
  res.end('hello world');
})).listen(0, common.localhostIPv4, common.mustCall(() => {
  const clientOptions = {
    method: 'GET',
    host: common.localhostIPv4,
    port: server.address().port,
    path: '/test',
    clientCertEngine: engine,  // `engine` will provide key+cert
    rejectUnauthorized: false, // Prevent failing on self-signed certificates
    headers: {}
  };

  const req = https.request(clientOptions, common.mustCall((response) => {
    let body = '';
    response.setEncoding('utf8');
    response.on('data', (chunk) => {
      body += chunk;
    });

    response.on('end', common.mustCall(() => {
      assert.strictEqual(body, 'hello world');
      server.close();
    }));
  }));

  req.end();
}));