forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-tls-socket-default-options.js
More file actions
76 lines (66 loc) · 2.54 KB
/
Copy pathtest-tls-socket-default-options.js
File metadata and controls
76 lines (66 loc) · 2.54 KB
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
'use strict';
const common = require('../common');
// Test directly created TLS sockets and options.
const assert = require('assert');
const join = require('path').join;
const {
connect, keys, tls
} = require(join(common.fixturesDir, 'tls-connect'));
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
test(undefined, (err) => {
assert.strictEqual(err.message, 'unable to verify the first certificate');
});
test({}, (err) => {
assert.strictEqual(err.message, 'unable to verify the first certificate');
// Properties never set on success or failure when tls.connect not used.
assert(!('authorized' in this), 'only supported for tls.connect');
assert(!('authorizationError' in this), 'only supported for tls.connect');
});
test({secureContext: tls.createSecureContext({ca: keys.agent1.ca})}, (err) => {
assert.ifError(err);
// Properties never set on success or failure when tls.connect not used.
assert(!('authorized' in this), 'only supported for tls.connect');
assert(!('authorizationError' in this), 'only supported for tls.connect');
});
test({ca: keys.agent1.ca}, (err) => {
assert.ifError(err);
});
// Secure context options, like ca, are ignored if a sec ctx is explicitly
// provided.
test({secureContext: tls.createSecureContext(), ca: keys.agent1.ca}, (err) => {
assert.strictEqual(err.message, 'unable to verify the first certificate');
});
function test(client, callback) {
callback = common.mustCall(callback);
connect({
server: {
key: keys.agent1.key,
cert: keys.agent1.cert,
},
}, function(err, pair, cleanup) {
assert.strictEqual(err.message, 'unable to verify the first certificate');
let recv = '';
pair.server.server.once('secureConnection', common.mustCall((conn) => {
conn.on('data', (data) => recv += data);
conn.on('end', common.mustCall(() => {
// Server sees nothing wrong with connection, even though the client's
// authentication of the server cert failed.
assert.strictEqual(recv, 'hello');
cleanup();
}));
}));
// Client doesn't support the 'secureConnect' event, and doesn't error if
// authentication failed. Caller must explicitly check for failure.
(new tls.TLSSocket(null, client)).connect(pair.server.server.address().port)
.on('connect', common.mustCall(function() {
this.end('hello');
}))
.on('secureConnect', () => assert(0, 'only supported for tls.connect'))
.on('secure', common.mustCall(function() {
callback.call(this, this.ssl.verifyError());
}));
});
}