-
-
Notifications
You must be signed in to change notification settings - Fork 667
Expand file tree
/
Copy pathtest-dynamic-ssl-options.test.mts
More file actions
127 lines (110 loc) · 3.16 KB
/
test-dynamic-ssl-options.test.mts
File metadata and controls
127 lines (110 loc) · 3.16 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
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
import EventEmitter from 'node:events';
import { describe, it, strict } from 'poku';
import BaseConnection from '../../../lib/base/connection.js';
import ConnectionConfig from '../../../lib/connection_config.js';
type SslFactoryResult = {
ca?: string;
cert?: string;
key?: string;
rejectUnauthorized?: boolean;
};
function createMockConnection(
ssl:
| false
| ((
config: ConnectionConfig
) => SslFactoryResult | Promise<SslFactoryResult>)
) {
const config = new ConnectionConfig({
host: 'localhost',
user: 'test',
password: 'test',
database: 'test',
connectTimeout: 0,
ssl,
});
const mockStream = Object.assign(new EventEmitter(), {
write: () => true,
end: () => {},
destroy() {
this.destroyed = true;
},
destroyed: false,
setKeepAlive: () => {},
setNoDelay: () => {},
removeAllListeners: EventEmitter.prototype.removeAllListeners,
});
config.stream = mockStream;
config.isServer = true;
return new BaseConnection({ config });
}
await describe('dynamic SSL options', async () => {
await it('should resolve SSL options from a synchronous function', async () => {
let capturedSslFactoryArg: ConnectionConfig | undefined;
const connection = createMockConnection((config) => {
capturedSslFactoryArg = config;
return {
ca: 'ca-data',
rejectUnauthorized: false,
};
});
let capturedSslConfig: SslFactoryResult | undefined;
connection._onSslConfig = (sslConfig, onSecure) => {
capturedSslConfig = sslConfig;
onSecure();
};
await new Promise<void>((resolve, reject) => {
connection.startTLS((err: unknown) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
strict.equal(capturedSslFactoryArg, connection.config);
strict.deepEqual(capturedSslConfig, {
ca: 'ca-data',
rejectUnauthorized: false,
});
});
await it('should resolve SSL options from an asynchronous function', async () => {
const connection = createMockConnection(async () => ({
ca: 'async-ca',
}));
let capturedSslConfig: SslFactoryResult | undefined;
connection._onSslConfig = (sslConfig, onSecure) => {
capturedSslConfig = sslConfig;
onSecure();
};
await new Promise<void>((resolve, reject) => {
connection.startTLS((err: unknown) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
strict.deepEqual(capturedSslConfig, {
ca: 'async-ca',
});
});
await it('should pass factory rejections to onSecure callback', async () => {
const connection = createMockConnection(async () => {
throw new Error('dynamic ssl failed');
});
let onSslConfigCalled = false;
connection._onSslConfig = (_sslConfig, _onSecure) => {
onSslConfigCalled = true;
};
await new Promise<void>((resolve) => {
connection.startTLS((err: unknown) => {
strict.ok(err instanceof Error);
strict.equal((err as Error).message, 'dynamic ssl failed');
resolve();
});
});
strict.equal(onSslConfigCalled, false);
});
});