@@ -6,14 +6,16 @@ import { MongoAPIError, Server, ServerDescription, Topology } from '../../mongod
6
6
import { topologyWithPlaceholderClient } from '../../tools/utils' ;
7
7
8
8
describe ( 'Initial DNS Seedlist Discovery (Prose Tests)' , ( ) => {
9
- context ( '1) When running validation on an SRV string before DNS resolution' , function ( ) {
9
+ context ( '1. When running validation on an SRV string before DNS resolution' , function ( ) {
10
+ let client ;
11
+
10
12
beforeEach ( async function ( ) {
11
13
// this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation
12
14
13
15
sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
14
16
return [
15
17
{
16
- name : 'resolved.mongodb .localhost' ,
18
+ name : 'resolved.mongo .localhost' ,
17
19
port : 27017 ,
18
20
weight : 0 ,
19
21
priority : 0
@@ -36,23 +38,22 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
36
38
37
39
afterEach ( async function ( ) {
38
40
sinon . restore ( ) ;
41
+ client . close ( ) ;
39
42
} ) ;
40
43
41
44
it ( 'does not error on an SRV because it has one domain level' , async function ( ) {
42
- const client = await this . configuration . newClient ( 'mongodb+srv://localhost' , { } ) ;
43
- client . connect ( ) ;
44
- client . close ( ) ;
45
+ client = await this . configuration . newClient ( 'mongodb+srv://localhost' , { } ) ;
46
+ await client . connect ( ) ;
45
47
} ) ;
46
48
47
49
it ( 'does not error on an SRV because it has two domain levels' , async function ( ) {
48
- const client = await this . configuration . newClient ( 'mongodb+srv://mongodb.localhost' , { } ) ;
49
- client . connect ( ) ;
50
- client . close ( ) ;
50
+ client = await this . configuration . newClient ( 'mongodb+srv://mongo.localhost' , { } ) ;
51
+ await client . connect ( ) ;
51
52
} ) ;
52
53
} ) ;
53
54
54
55
context (
55
- '2) When given a host from DNS resolution that does NOT end with the original SRVs domain name' ,
56
+ '2. When given a host from DNS resolution that does NOT end with the original SRVs domain name' ,
56
57
function ( ) {
57
58
beforeEach ( async function ( ) {
58
59
sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
@@ -68,7 +69,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
68
69
sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
69
70
return [
70
71
{
71
- name : 'localhost.mongodb' , // this string contains the SRV but does not end with it
72
+ name : 'localhost.mongodb' ,
72
73
port : 27017 ,
73
74
weight : 0 ,
74
75
priority : 0
@@ -87,15 +88,15 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
87
88
sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
88
89
return [
89
90
{
90
- name : 'evil.localhost ' , // this string only ends with part of the domain, not all of it!
91
+ name : 'test_1. evil.local ' , // this string only ends with part of the domain, not all of it!
91
92
port : 27017 ,
92
93
weight : 0 ,
93
94
priority : 0
94
95
}
95
96
] ;
96
97
} ) ;
97
98
const err = await this . configuration
98
- . newClient ( 'mongodb+srv://mongodb.localhost ' , { } )
99
+ . newClient ( 'mongodb+srv://mongo.local ' , { } )
99
100
. connect ( )
100
101
. catch ( e => e ) ;
101
102
expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
@@ -106,7 +107,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
106
107
sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
107
108
return [
108
109
{
109
- name : 'blogs.evil.co.uk ' ,
110
+ name : 'blogs.evil.com ' ,
110
111
port : 27017 ,
111
112
weight : 0 ,
112
113
priority : 0
@@ -124,7 +125,7 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
124
125
) ;
125
126
126
127
context (
127
- '3) When given a host from DNS resolution that is identical to the original SRVs hostname' ,
128
+ '3. When given a host from DNS resolution that is identical to the original SRVs hostname' ,
128
129
function ( ) {
129
130
beforeEach ( async function ( ) {
130
131
sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
@@ -161,15 +162,15 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
161
162
sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
162
163
return [
163
164
{
164
- name : 'mongodb.localhost ' ,
165
+ name : 'mongo.local ' ,
165
166
port : 27017 ,
166
167
weight : 0 ,
167
168
priority : 0
168
169
}
169
170
] ;
170
171
} ) ;
171
172
const err = await this . configuration
172
- . newClient ( 'mongodb+srv://mongodb.localhost ' , { } )
173
+ . newClient ( 'mongodb+srv://mongo.local ' , { } )
173
174
. connect ( )
174
175
. catch ( e => e ) ;
175
176
expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
@@ -179,4 +180,76 @@ describe('Initial DNS Seedlist Discovery (Prose Tests)', () => {
179
180
} ) ;
180
181
}
181
182
) ;
183
+
184
+ context (
185
+ '4. When given a returned address that does NOT share the domain name of the SRV record because its missing a `.`' ,
186
+ function ( ) {
187
+ beforeEach ( async function ( ) {
188
+ sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
189
+ throw { code : 'ENODATA' } ;
190
+ } ) ;
191
+ } ) ;
192
+
193
+ afterEach ( async function ( ) {
194
+ sinon . restore ( ) ;
195
+ } ) ;
196
+
197
+ it ( 'an SRV with one domain level causes a runtime error' , async function ( ) {
198
+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
199
+ return [
200
+ {
201
+ name : 'test_1.cluster_1localhost' ,
202
+ port : 27017 ,
203
+ weight : 0 ,
204
+ priority : 0
205
+ }
206
+ ] ;
207
+ } ) ;
208
+ const err = await this . configuration
209
+ . newClient ( 'mongodb+srv://localhost' , { } )
210
+ . connect ( )
211
+ . catch ( e => e ) ;
212
+ expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
213
+ expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
214
+ } ) ;
215
+
216
+ it ( 'an SRV with two domain levels causes a runtime error' , async function ( ) {
217
+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
218
+ return [
219
+ {
220
+ name : 'test_1.my_hostmongo.local' ,
221
+ port : 27017 ,
222
+ weight : 0 ,
223
+ priority : 0
224
+ }
225
+ ] ;
226
+ } ) ;
227
+ const err = await this . configuration
228
+ . newClient ( 'mongodb+srv://mongo.local' , { } )
229
+ . connect ( )
230
+ . catch ( e => e ) ;
231
+ expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
232
+ expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
233
+ } ) ;
234
+
235
+ it ( 'an SRV with three domain levels causes a runtime error' , async function ( ) {
236
+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
237
+ return [
238
+ {
239
+ name : 'cluster.testmongodb.com' ,
240
+ port : 27017 ,
241
+ weight : 0 ,
242
+ priority : 0
243
+ }
244
+ ] ;
245
+ } ) ;
246
+ const err = await this . configuration
247
+ . newClient ( 'mongodb+srv://blogs.mongodb.com' , { } )
248
+ . connect ( )
249
+ . catch ( e => e ) ;
250
+ expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
251
+ expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
252
+ } ) ;
253
+ }
254
+ ) ;
182
255
} ) ;
0 commit comments