11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto) common.skip('missing crypto');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Test --tls-keylog CLI flag.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst path = require('path');
101cb0ef41Sopenharmony_ciconst fs = require('fs');
111cb0ef41Sopenharmony_ciconst { fork } = require('child_process');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciif (process.argv[2] === 'test')
141cb0ef41Sopenharmony_ci  return test();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
171cb0ef41Sopenharmony_citmpdir.refresh();
181cb0ef41Sopenharmony_ciconst file = path.resolve(tmpdir.path, 'keylog.log');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst child = fork(__filename, ['test'], {
211cb0ef41Sopenharmony_ci  execArgv: ['--tls-keylog=' + file]
221cb0ef41Sopenharmony_ci});
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cichild.on('close', common.mustCall((code, signal) => {
251cb0ef41Sopenharmony_ci  assert.strictEqual(code, 0);
261cb0ef41Sopenharmony_ci  assert.strictEqual(signal, null);
271cb0ef41Sopenharmony_ci  const log = fs.readFileSync(file, 'utf8').trim().split('\n');
281cb0ef41Sopenharmony_ci  // Both client and server should log their secrets,
291cb0ef41Sopenharmony_ci  // so we should have two identical lines in the log
301cb0ef41Sopenharmony_ci  assert.strictEqual(log.length, 2);
311cb0ef41Sopenharmony_ci  assert.strictEqual(log[0], log[1]);
321cb0ef41Sopenharmony_ci}));
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cifunction test() {
351cb0ef41Sopenharmony_ci  const {
361cb0ef41Sopenharmony_ci    connect, keys
371cb0ef41Sopenharmony_ci  } = require(fixtures.path('tls-connect'));
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  connect({
401cb0ef41Sopenharmony_ci    client: {
411cb0ef41Sopenharmony_ci      checkServerIdentity: (servername, cert) => { },
421cb0ef41Sopenharmony_ci      ca: `${keys.agent1.cert}\n${keys.agent6.ca}`,
431cb0ef41Sopenharmony_ci    },
441cb0ef41Sopenharmony_ci    server: {
451cb0ef41Sopenharmony_ci      cert: keys.agent6.cert,
461cb0ef41Sopenharmony_ci      key: keys.agent6.key,
471cb0ef41Sopenharmony_ci      // Number of keylog events is dependent on protocol version
481cb0ef41Sopenharmony_ci      maxVersion: 'TLSv1.2',
491cb0ef41Sopenharmony_ci    },
501cb0ef41Sopenharmony_ci  }, common.mustCall((err, pair, cleanup) => {
511cb0ef41Sopenharmony_ci    if (pair.server.err) {
521cb0ef41Sopenharmony_ci      console.trace('server', pair.server.err);
531cb0ef41Sopenharmony_ci    }
541cb0ef41Sopenharmony_ci    if (pair.client.err) {
551cb0ef41Sopenharmony_ci      console.trace('client', pair.client.err);
561cb0ef41Sopenharmony_ci    }
571cb0ef41Sopenharmony_ci    assert.ifError(pair.server.err);
581cb0ef41Sopenharmony_ci    assert.ifError(pair.client.err);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    return cleanup();
611cb0ef41Sopenharmony_ci  }));
621cb0ef41Sopenharmony_ci}
63