-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathinitial_dns_seedlist_discovery.prose.test.ts
More file actions
314 lines (283 loc) · 11.1 KB
/
initial_dns_seedlist_discovery.prose.test.ts
File metadata and controls
314 lines (283 loc) · 11.1 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { expect } from 'chai';
import * as dns from 'dns';
import * as sinon from 'sinon';
import { ConnectionPool } from '../../../src/cmap/connection_pool';
import { MongoAPIError } from '../../../src/error';
import { Server } from '../../../src/sdam/server';
import { ServerDescription } from '../../../src/sdam/server_description';
import { Topology } from '../../../src/sdam/topology';
import { topologyWithPlaceholderClient } from '../../tools/utils';
describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
describe('1. Allow SRVs with fewer than 3 . separated parts', function () {
context('when running validation on an SRV string before DNS resolution', function () {
/**
* When running validation on an SRV string before DNS resolution, do not throw a error due to number of SRV parts.
* - mongodb+srv://localhost
* - mongodb+srv://mongo.localhost
*/
let client;
beforeEach(async function () {
// this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation
const stub = sinon.stub(dns.promises, 'resolve');
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'resolved.mongo.localhost',
port: 27017,
weight: 0,
priority: 0
}
];
});
stub.withArgs(sinon.match.string, 'TXT').callsFake(async () => {
throw { code: 'ENODATA' };
});
sinon.stub(Topology.prototype, 'selectServer').callsFake(async () => {
return new Server(
topologyWithPlaceholderClient([], {} as any),
new ServerDescription('a:1'),
{} as any
);
});
sinon.stub(ConnectionPool.prototype, 'checkOut').callsFake(async function () {
return {};
});
sinon.stub(ConnectionPool.prototype, 'checkIn').callsFake(function () {
return;
});
});
afterEach(async function () {
sinon.restore();
await client.close();
});
it('does not error on an SRV because it has one domain level', async function () {
client = await this.configuration.newClient('mongodb+srv://localhost', {});
await client.connect();
});
it('does not error on an SRV because it has two domain levels', async function () {
client = await this.configuration.newClient('mongodb+srv://mongo.localhost', {});
await client.connect();
});
});
});
describe('2. Throw when return address does not end with SRV domain', function () {
context(
'when given a host from DNS resolution that does NOT end with the original SRVs domain name',
function () {
/**
* When given a returned address that does NOT end with the original SRV's domain name, throw a runtime error.
* For this test, run each of the following cases:
* - the SRV mongodb+srv://localhost resolving to localhost.mongodb
* - the SRV mongodb+srv://mongo.local resolving to test_1.evil.local
* - the SRV mongodb+srv://blogs.mongodb.com resolving to blogs.evil.com
* Remember, the domain of an SRV with one or two . separated parts is the SRVs entire hostname.
*/
let stub;
beforeEach(async function () {
stub = sinon.stub(dns.promises, 'resolve');
stub.withArgs(sinon.match.string, 'TXT').callsFake(async () => {
throw { code: 'ENODATA' };
});
});
afterEach(async function () {
sinon.restore();
});
it('an SRV with one domain level causes a runtime error', async function () {
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'localhost.mongodb',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://localhost', {})
.connect()
.catch((e: any) => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});
it('an SRV with two domain levels causes a runtime error', async function () {
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'test_1.evil.local', // this string only ends with part of the domain, not all of it!
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://mongo.local', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});
it('an SRV with three or more domain levels causes a runtime error', async function () {
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'blogs.evil.com',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://blogs.mongodb.com', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});
}
);
});
describe('3. Throw when return address is identical to SRV hostname', function () {
/**
* When given a returned address that is identical to the SRV hostname and the SRV hostname has fewer than three . separated parts, throw a runtime error.
* For this test, run each of the following cases:
* - the SRV mongodb+srv://localhost resolving to localhost
* - the SRV mongodb+srv://mongo.local resolving to mongo.local
*/
context(
'when given a host from DNS resolution that is identical to the original SRVs hostname',
function () {
let stub;
beforeEach(async function () {
stub = sinon.stub(dns.promises, 'resolve');
stub.withArgs(sinon.match.string, 'TXT').callsFake(async () => {
throw { code: 'ENODATA' };
});
});
afterEach(async function () {
sinon.restore();
});
it('an SRV with one domain level causes a runtime error', async function () {
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'localhost',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://localhost', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal(
'Server record does not have at least one more domain level than parent URI'
);
});
it('an SRV with two domain levels causes a runtime error', async function () {
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'mongo.local',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://mongo.local', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal(
'Server record does not have at least one more domain level than parent URI'
);
});
}
);
});
describe('4. Throw when return address does not contain . separating shared part of domain', function () {
/**
* When given a returned address that does NOT share the domain name of the SRV record because it's missing a ., throw a runtime error.
* For this test, run each of the following cases:
* - the SRV mongodb+srv://localhost resolving to test_1.cluster_1localhost
* - the SRV mongodb+srv://mongo.local resolving to test_1.my_hostmongo.local
* - the SRV mongodb+srv://blogs.mongodb.com resolving to cluster.testmongodb.com
*/
context(
'when given a returned address that does NOT share the domain name of the SRV record because its missing a `.`',
function () {
let stub;
beforeEach(async function () {
stub = sinon.stub(dns.promises, 'resolve');
stub.withArgs(sinon.match.string, 'TXT').callsFake(async () => {
throw { code: 'ENODATA' };
});
});
afterEach(async function () {
sinon.restore();
});
it('an SRV with one domain level causes a runtime error', async function () {
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'test_1.cluster_1localhost',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://localhost', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});
it('an SRV with two domain levels causes a runtime error', async function () {
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'test_1.my_hostmongo.local',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://mongo.local', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});
it('an SRV with three domain levels causes a runtime error', async function () {
stub.withArgs(sinon.match.string, 'SRV').callsFake(async () => {
return [
{
name: 'cluster.testmongodb.com',
port: 27017,
weight: 0,
priority: 0
}
];
});
const err = await this.configuration
.newClient('mongodb+srv://blogs.mongodb.com', {})
.connect()
.catch(e => e);
expect(err).to.be.instanceOf(MongoAPIError);
expect(err.message).to.equal('Server record does not share hostname with parent URI');
});
}
);
});
});