node/test/parallel/test-tls-interleave.js
Johan Bergström 671fbd5a9d test: refactor all tests that depends on crypto
we had a few ways versions of looking for support before executing a test. this
commit unifies them as well as add the check for all tests that previously
lacked them. found by running `./configure --without-ssl && make test`. also,
produce tap skip output if the test is skipped.

PR-URL: https://github.com/iojs/io.js/pull/1049
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Shigeki Ohtsu <ohtsu@iij.ad.jp>
2015-03-05 10:31:41 +09:00

53 lines
1.3 KiB
JavaScript

var common = require('../common');
var assert = require('assert');
if (!common.hasCrypto) {
console.log('1..0 # Skipped: missing crypto');
process.exit();
}
var tls = require('tls');
var fs = require('fs');
var PORT = common.PORT;
var dir = common.fixturesDir;
var options = { key: fs.readFileSync(dir + '/test_key.pem'),
cert: fs.readFileSync(dir + '/test_cert.pem'),
ca: [ fs.readFileSync(dir + '/test_ca.pem') ] };
var writes = [
'some server data',
'and a separate packet',
'and one more',
];
var receivedWrites = 0;
var server = tls.createServer(options, function(c) {
writes.forEach(function(str) {
c.write(str);
});
}).listen(PORT, function() {
var c = tls.connect(PORT, { rejectUnauthorized: false }, function() {
c.write('some client data');
c.on('readable', function() {
var data = c.read();
if (data === null)
return;
data = data.toString();
while (data.length !== 0) {
assert.strictEqual(data.indexOf(writes[receivedWrites]), 0);
data = data.slice(writes[receivedWrites].length);
if (++receivedWrites === writes.length) {
c.end();
server.close();
}
}
});
});
});
process.on('exit', function() {
assert.equal(receivedWrites, writes.length);
});