11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (common.opensslCli === false)
71cb0ef41Sopenharmony_ci  common.skip('node compiled without OpenSSL CLI.');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst tls = require('tls');
111cb0ef41Sopenharmony_ciconst spawn = require('child_process').spawn;
121cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst cert = fixtures.readKey('rsa_cert.crt');
151cb0ef41Sopenharmony_ciconst key = fixtures.readKey('rsa_private.pem');
161cb0ef41Sopenharmony_ciconst server = tls.createServer({ cert, key }, common.mustNotCall());
171cb0ef41Sopenharmony_ciconst errors = [];
181cb0ef41Sopenharmony_cilet stderr = '';
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciserver.listen(0, '127.0.0.1', function() {
211cb0ef41Sopenharmony_ci  const address = `${this.address().address}:${this.address().port}`;
221cb0ef41Sopenharmony_ci  const args = ['s_client',
231cb0ef41Sopenharmony_ci                '-ssl3',
241cb0ef41Sopenharmony_ci                '-connect', address];
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  const client = spawn(common.opensslCli, args, { stdio: 'pipe' });
271cb0ef41Sopenharmony_ci  client.stdout.pipe(process.stdout);
281cb0ef41Sopenharmony_ci  client.stderr.pipe(process.stderr);
291cb0ef41Sopenharmony_ci  client.stderr.setEncoding('utf8');
301cb0ef41Sopenharmony_ci  client.stderr.on('data', (data) => stderr += data);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  client.once('exit', common.mustCall(function(exitCode) {
331cb0ef41Sopenharmony_ci    assert.strictEqual(exitCode, 1);
341cb0ef41Sopenharmony_ci    server.close();
351cb0ef41Sopenharmony_ci  }));
361cb0ef41Sopenharmony_ci});
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciserver.on('tlsClientError', (err) => errors.push(err));
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciprocess.on('exit', function() {
411cb0ef41Sopenharmony_ci  if (/[Uu]nknown option:? -ssl3/.test(stderr)) {
421cb0ef41Sopenharmony_ci    common.printSkipMessage('`openssl s_client -ssl3` not supported.');
431cb0ef41Sopenharmony_ci  } else {
441cb0ef41Sopenharmony_ci    assert.strictEqual(errors.length, 1);
451cb0ef41Sopenharmony_ci    assert(/:version too low/.test(errors[0].message));
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci});
48