11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../../common');
31cb0ef41Sopenharmony_ciconst fixture = require('../../common/fixtures');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst fs = require('fs');
91cb0ef41Sopenharmony_ciconst path = require('path');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst engine = path.join(__dirname,
121cb0ef41Sopenharmony_ci                         `/build/${common.buildType}/testkeyengine.engine`);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciif (!fs.existsSync(engine))
151cb0ef41Sopenharmony_ci  common.skip('no client cert engine');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst assert = require('assert');
181cb0ef41Sopenharmony_ciconst https = require('https');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst agentKey = fs.readFileSync(fixture.path('/keys/agent1-key.pem'));
211cb0ef41Sopenharmony_ciconst agentCert = fs.readFileSync(fixture.path('/keys/agent1-cert.pem'));
221cb0ef41Sopenharmony_ciconst agentCa = fs.readFileSync(fixture.path('/keys/ca1-cert.pem'));
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst serverOptions = {
251cb0ef41Sopenharmony_ci  key: agentKey,
261cb0ef41Sopenharmony_ci  cert: agentCert,
271cb0ef41Sopenharmony_ci  ca: agentCa,
281cb0ef41Sopenharmony_ci  requestCert: true,
291cb0ef41Sopenharmony_ci  rejectUnauthorized: true,
301cb0ef41Sopenharmony_ci};
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst server = https.createServer(serverOptions, common.mustCall((req, res) => {
331cb0ef41Sopenharmony_ci  res.writeHead(200);
341cb0ef41Sopenharmony_ci  res.end('hello world');
351cb0ef41Sopenharmony_ci})).listen(0, common.localhostIPv4, common.mustCall(() => {
361cb0ef41Sopenharmony_ci  const clientOptions = {
371cb0ef41Sopenharmony_ci    method: 'GET',
381cb0ef41Sopenharmony_ci    host: common.localhostIPv4,
391cb0ef41Sopenharmony_ci    port: server.address().port,
401cb0ef41Sopenharmony_ci    path: '/test',
411cb0ef41Sopenharmony_ci    privateKeyEngine: engine,
421cb0ef41Sopenharmony_ci    privateKeyIdentifier: 'dummykey',
431cb0ef41Sopenharmony_ci    cert: agentCert,
441cb0ef41Sopenharmony_ci    rejectUnauthorized: false, // Prevent failing on self-signed certificates
451cb0ef41Sopenharmony_ci    headers: {},
461cb0ef41Sopenharmony_ci  };
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  const req = https.request(clientOptions, common.mustCall((response) => {
491cb0ef41Sopenharmony_ci    let body = '';
501cb0ef41Sopenharmony_ci    response.setEncoding('utf8');
511cb0ef41Sopenharmony_ci    response.on('data', (chunk) => {
521cb0ef41Sopenharmony_ci      body += chunk;
531cb0ef41Sopenharmony_ci    });
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    response.on('end', common.mustCall(() => {
561cb0ef41Sopenharmony_ci      assert.strictEqual(body, 'hello world');
571cb0ef41Sopenharmony_ci      server.close();
581cb0ef41Sopenharmony_ci    }));
591cb0ef41Sopenharmony_ci  }));
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  req.end();
621cb0ef41Sopenharmony_ci}));
63