11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciif (!common.hasCrypto) 51cb0ef41Sopenharmony_ci common.skip('missing crypto'); 61cb0ef41Sopenharmony_ciif (!common.opensslCli) 71cb0ef41Sopenharmony_ci common.skip('missing openssl cli'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst assert = require('assert'); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciconst tls = require('tls'); 121cb0ef41Sopenharmony_ciconst spawn = require('child_process').spawn; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst CIPHERS = 'PSK+HIGH'; 151cb0ef41Sopenharmony_ciconst KEY = 'd731ef57be09e5204f0b205b60627028'; 161cb0ef41Sopenharmony_ciconst IDENTITY = 'TestUser'; 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ciconst server = tls.createServer({ 191cb0ef41Sopenharmony_ci ciphers: CIPHERS, 201cb0ef41Sopenharmony_ci pskIdentityHint: IDENTITY, 211cb0ef41Sopenharmony_ci pskCallback(socket, identity) { 221cb0ef41Sopenharmony_ci assert.ok(socket instanceof tls.TLSSocket); 231cb0ef41Sopenharmony_ci assert.ok(typeof identity === 'string'); 241cb0ef41Sopenharmony_ci if (identity === IDENTITY) 251cb0ef41Sopenharmony_ci return Buffer.from(KEY, 'hex'); 261cb0ef41Sopenharmony_ci } 271cb0ef41Sopenharmony_ci}); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciserver.on('connection', common.mustCall()); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciserver.on('secureConnection', (socket) => { 321cb0ef41Sopenharmony_ci socket.write('hello\r\n'); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci socket.on('data', (data) => { 351cb0ef41Sopenharmony_ci socket.write(data); 361cb0ef41Sopenharmony_ci }); 371cb0ef41Sopenharmony_ci}); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cilet gotHello = false; 401cb0ef41Sopenharmony_cilet sentWorld = false; 411cb0ef41Sopenharmony_cilet gotWorld = false; 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ciserver.listen(0, () => { 441cb0ef41Sopenharmony_ci const client = spawn(common.opensslCli, [ 451cb0ef41Sopenharmony_ci 's_client', 461cb0ef41Sopenharmony_ci '-connect', `127.0.0.1:${server.address().port}`, 471cb0ef41Sopenharmony_ci '-cipher', CIPHERS, 481cb0ef41Sopenharmony_ci '-psk', KEY, 491cb0ef41Sopenharmony_ci '-psk_identity', IDENTITY, 501cb0ef41Sopenharmony_ci ]); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci let out = ''; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci client.stdout.setEncoding('utf8'); 551cb0ef41Sopenharmony_ci client.stdout.on('data', (d) => { 561cb0ef41Sopenharmony_ci out += d; 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci if (!gotHello && /hello/.test(out)) { 591cb0ef41Sopenharmony_ci gotHello = true; 601cb0ef41Sopenharmony_ci client.stdin.write('world\r\n'); 611cb0ef41Sopenharmony_ci sentWorld = true; 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci if (!gotWorld && /world/.test(out)) { 651cb0ef41Sopenharmony_ci gotWorld = true; 661cb0ef41Sopenharmony_ci client.stdin.end(); 671cb0ef41Sopenharmony_ci } 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci client.on('exit', common.mustCall((code) => { 711cb0ef41Sopenharmony_ci assert.ok(gotHello); 721cb0ef41Sopenharmony_ci assert.ok(sentWorld); 731cb0ef41Sopenharmony_ci assert.ok(gotWorld); 741cb0ef41Sopenharmony_ci assert.strictEqual(code, 0); 751cb0ef41Sopenharmony_ci server.close(); 761cb0ef41Sopenharmony_ci })); 771cb0ef41Sopenharmony_ci}); 78