forked from redis/node-redis
/
tls.spec.js
155 lines (136 loc) · 5.68 KB
/
tls.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'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 = require('tls');
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) return done();
helper.stopStunnel(function () {
helper.startStunnel(done);
});
});
after(function (done) {
if (skip) return done();
helper.stopStunnel(done);
});
var client;
afterEach(function () {
if (skip) return;
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;
assert.strictEqual(client.address, '127.0.0.1:' + tls_port);
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)) {
process.nextTick(function () {
assert.strictEqual(time, connect_timeout);
assert.strictEqual(client.emitted_end, true);
assert.strictEqual(client.connected, false);
assert.strictEqual(client.ready, false);
assert.strictEqual(client.closing, true);
assert.strictEqual(time, connect_timeout);
done();
});
}
});
});
});
describe('when not connected', function () {
it('connect with host and port provided in the tls object', function (done) {
if (skip) this.skip();
var tls_opts = utils.clone(tls_options);
tls_opts.port = tls_port;
tls_opts.host = 'localhost';
client = redis.createClient({
connect_timeout: 1000,
tls: tls_opts
});
// verify connection is using TCP, not UNIX socket
assert.strictEqual(client.connection_options.host, 'localhost');
assert.strictEqual(client.connection_options.port, tls_port);
assert.strictEqual(client.address, 'localhost:' + tls_port);
assert(client.stream.encrypted);
client.set('foo', 'bar');
client.get('foo', helper.isString('bar', done));
});
describe('using rediss as url protocol', function (done) {
var tls_connect = tls.connect
beforeEach(function () {
tls.connect = function (options) {
options = utils.clone(options)
options.ca = tls_options.ca;
return tls_connect.call(tls, options);
}
})
afterEach(function () {
tls.connect = tls_connect;
})
it('connect with tls when rediss is used as the protocol', function (done) {
if (skip) this.skip();
client = redis.createClient('rediss://localhost:' + tls_port);
// verify connection is using TCP, not UNIX socket
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
});
assert.strictEqual(client.address, 'localhost:' + tls_port);
client.on('error', function (err) {
assert(/DEPTH_ZERO_SELF_SIGNED_CERT/.test(err.code || err.message), err);
client.end(true);
});
client.set('foo', 'bar', function (err, res) {
done(res);
});
});
});
});