1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00
Files
node-redis/test/tls.spec.js
Ruben Bridgewater 1a8a72ddf3 Improve tls tests
Removed redundant tests.
Skip the tests on windows instead of not even showing them.
Add a faulty cert to check proper cert validation.
Reject unauthorized certs
2016-03-07 01:57:56 +01:00

129 lines
4.2 KiB
JavaScript

'use strict';
var assert = require("assert");
var config = require("./lib/config");
var fs = require('fs');
var helper = require('./helper');
var path = require('path');
var redis = config.redis;
var utils = require('../lib/utils');
var tls_options = {
servername: "redis.js.org",
rejectUnauthorized: true,
ca: [ String(fs.readFileSync(path.resolve(__dirname, "./conf/redis.js.org.cert"))) ]
};
var tls_port = 6380;
// Use skip instead of returning to indicate what tests really got skipped
var skip = false;
// Wait until stunnel4 is in the travis whitelist
// Check: https://github.com/travis-ci/apt-package-whitelist/issues/403
// If this is merged, remove the travis env checks
describe("TLS connection tests", function () {
before(function (done) {
// Print the warning when the tests run instead of while starting mocha
if (process.platform === 'win32') {
skip = true;
console.warn('\nStunnel tests do not work on windows atm. If you think you can fix that, it would be warmly welcome.\n');
} else if (process.env.TRAVIS === 'true') {
skip = true;
console.warn('\nTravis does not support stunnel right now. Skipping tests.\nCheck: https://github.com/travis-ci/apt-package-whitelist/issues/403\n');
}
if (skip) {
done();
return;
}
helper.stopStunnel(function () {
helper.startStunnel(done);
});
});
after(function (done) {
if (skip) {
done();
return;
}
helper.stopStunnel(done);
});
var client;
afterEach(function () {
client.end(true);
});
describe("on lost connection", function () {
it("emit an error after max retry timeout and do not try to reconnect afterwards", function (done) {
if (skip) this.skip();
var connect_timeout = 500; // in ms
client = redis.createClient({
connect_timeout: connect_timeout,
port: tls_port,
tls: tls_options
});
var time = 0;
client.once('ready', function() {
helper.killConnection(client);
});
client.on("reconnecting", function (params) {
time += params.delay;
});
client.on('error', function(err) {
if (/Redis connection in broken state: connection timeout.*?exceeded./.test(err.message)) {
setTimeout(function () {
assert(time === connect_timeout);
done();
}, 100);
}
});
});
});
describe("when not connected", function () {
it("connect with host and port provided in the options object", function (done) {
if (skip) this.skip();
client = redis.createClient({
host: 'localhost',
connect_timeout: 1000,
port: tls_port,
tls: tls_options
});
// verify connection is using TCP, not UNIX socket
assert.strictEqual(client.connection_options.host, 'localhost');
assert.strictEqual(client.connection_options.port, tls_port);
assert(client.stream.encrypted);
client.set('foo', 'bar');
client.get('foo', helper.isString('bar', done));
});
it('fails to connect because the cert is not correct', function (done) {
if (skip) this.skip();
var faulty_cert = utils.clone(tls_options);
faulty_cert.ca = [ String(fs.readFileSync(path.resolve(__dirname, "./conf/faulty.cert"))) ];
client = redis.createClient({
host: 'localhost',
connect_timeout: 1000,
port: tls_port,
tls: faulty_cert
});
client.on('error', function (err) {
assert.strictEqual(err.code, 'DEPTH_ZERO_SELF_SIGNED_CERT');
client.end(true);
});
client.set('foo', 'bar', function (err, res) {
done(res);
});
});
});
});