1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-30 02:59:11 -05:00
denoland-deno/tests/node_compat/test/parallel/test-tls-psk-server.js
Yoshiya Hinosawa fb24fd37c9
test: add node compat test cases (#27134)
This PR enables node compat test cases found passing by using the tool
added in #27122

The percentage of passing test case increases from 16.16% to 30.43% by
this change.
2024-12-04 11:37:20 +09:00

84 lines
1.9 KiB
JavaScript

// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 20.11.1
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
if (!common.opensslCli)
common.skip('missing openssl cli');
const assert = require('assert');
const tls = require('tls');
const spawn = require('child_process').spawn;
const CIPHERS = 'PSK+HIGH';
const KEY = 'd731ef57be09e5204f0b205b60627028';
const IDENTITY = 'TestUser';
const server = tls.createServer({
ciphers: CIPHERS,
pskIdentityHint: IDENTITY,
pskCallback(socket, identity) {
assert.ok(socket instanceof tls.TLSSocket);
assert.ok(typeof identity === 'string');
if (identity === IDENTITY)
return Buffer.from(KEY, 'hex');
}
});
server.on('connection', common.mustCall());
server.on('secureConnection', (socket) => {
socket.write('hello\r\n');
socket.on('data', (data) => {
socket.write(data);
});
});
let gotHello = false;
let sentWorld = false;
let gotWorld = false;
server.listen(0, () => {
const client = spawn(common.opensslCli, [
's_client',
'-connect', `127.0.0.1:${server.address().port}`,
'-cipher', CIPHERS,
'-psk', KEY,
'-psk_identity', IDENTITY,
]);
let out = '';
client.stdout.setEncoding('utf8');
client.stdout.on('data', (d) => {
out += d;
if (!gotHello && /hello/.test(out)) {
gotHello = true;
client.stdin.write('world\r\n');
sentWorld = true;
}
if (!gotWorld && /world/.test(out)) {
gotWorld = true;
client.stdin.end();
}
});
client.on('exit', common.mustCall((code) => {
assert.ok(gotHello);
assert.ok(sentWorld);
assert.ok(gotWorld);
assert.strictEqual(code, 0);
server.close();
}));
});