@@ -5,182 +5,178 @@ import * as sinon from 'sinon';
55import { MongoAPIError , Server , ServerDescription , Topology } from '../../mongodb' ;
66import { topologyWithPlaceholderClient } from '../../tools/utils' ;
77
8- describe (
9- 'Initial DNS Seedlist Discovery (Prose Tests)' ,
10- { requires : { topology : 'single' } } ,
11- ( ) => {
12- context ( 'When running validation on an SRV string before DNS resolution' , function ( ) {
8+ describe ( 'Initial DNS Seedlist Discovery (Prose Tests)' , ( ) => {
9+ context ( '1) When running validation on an SRV string before DNS resolution' , function ( ) {
10+ beforeEach ( async function ( ) {
11+ // this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation
12+
13+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
14+ return [
15+ {
16+ name : 'resolved.mongodb.localhost' ,
17+ port : 27017 ,
18+ weight : 0 ,
19+ priority : 0
20+ }
21+ ] ;
22+ } ) ;
23+
24+ sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
25+ throw { code : 'ENODATA' } ;
26+ } ) ;
27+
28+ sinon . stub ( Topology . prototype , 'selectServer' ) . callsFake ( async ( ) => {
29+ return new Server (
30+ topologyWithPlaceholderClient ( [ ] , { } as any ) ,
31+ new ServerDescription ( 'a:1' ) ,
32+ { } as any
33+ ) ;
34+ } ) ;
35+ } ) ;
36+
37+ afterEach ( async function ( ) {
38+ sinon . restore ( ) ;
39+ } ) ;
40+
41+ 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+ } ) ;
46+
47+ 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 ( ) ;
51+ } ) ;
52+ } ) ;
53+
54+ context (
55+ '2) When given a host from DNS resolution that does NOT end with the original SRVs domain name' ,
56+ function ( ) {
1357 beforeEach ( async function ( ) {
14- // this fn stubs DNS resolution to always pass - so we are only checking pre-DNS validation
58+ sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
59+ throw { code : 'ENODATA' } ;
60+ } ) ;
61+ } ) ;
62+
63+ afterEach ( async function ( ) {
64+ sinon . restore ( ) ;
65+ } ) ;
1566
67+ it ( 'an SRV with one domain level causes a runtime error' , async function ( ) {
1668 sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
1769 return [
1870 {
19- name : 'resolved .mongodb.localhost' ,
71+ name : 'localhost .mongodb' , // this string contains the SRV but does not end with it
2072 port : 27017 ,
2173 weight : 0 ,
2274 priority : 0
2375 }
2476 ] ;
2577 } ) ;
78+ const err = await this . configuration
79+ . newClient ( 'mongodb+srv://localhost' , { } )
80+ . connect ( )
81+ . catch ( ( e : any ) => e ) ;
82+ expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
83+ expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
84+ } ) ;
2685
27- sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
28- throw { code : 'ENODATA' } ;
29- } ) ;
30-
31- sinon . stub ( Topology . prototype , 'selectServer' ) . callsFake ( async ( ) => {
32- return new Server (
33- topologyWithPlaceholderClient ( [ ] , { } as any ) ,
34- new ServerDescription ( 'a:1' ) ,
35- { } as any
36- ) ;
86+ it ( 'an SRV with two domain levels causes a runtime error' , async function ( ) {
87+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
88+ return [
89+ {
90+ name : 'evil.localhost' , // this string only ends with part of the domain, not all of it!
91+ port : 27017 ,
92+ weight : 0 ,
93+ priority : 0
94+ }
95+ ] ;
3796 } ) ;
97+ const err = await this . configuration
98+ . newClient ( 'mongodb+srv://mongodb.localhost' , { } )
99+ . connect ( )
100+ . catch ( e => e ) ;
101+ expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
102+ expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
38103 } ) ;
39104
40- afterEach ( async function ( ) {
41- sinon . restore ( ) ;
105+ it ( 'an SRV with three or more domain levels causes a runtime error' , async function ( ) {
106+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
107+ return [
108+ {
109+ name : 'blogs.evil.co.uk' ,
110+ port : 27017 ,
111+ weight : 0 ,
112+ priority : 0
113+ }
114+ ] ;
115+ } ) ;
116+ const err = await this . configuration
117+ . newClient ( 'mongodb+srv://blogs.mongodb.com' , { } )
118+ . connect ( )
119+ . catch ( e => e ) ;
120+ expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
121+ expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
42122 } ) ;
123+ }
124+ ) ;
43125
44- it ( 'do not error on an SRV because it has one domain level' , async function ( ) {
45- const client = await this . configuration . newClient ( 'mongodb+srv://localhost' , { } ) ;
46- client . connect ( ) ;
47- client . close ( ) ;
126+ context (
127+ '3) When given a host from DNS resolution that is identical to the original SRVs hostname' ,
128+ function ( ) {
129+ beforeEach ( async function ( ) {
130+ sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
131+ throw { code : 'ENODATA' } ;
132+ } ) ;
48133 } ) ;
49134
50- it ( 'do not error on an SRV because it has two domain levels' , async function ( ) {
51- const client = await this . configuration . newClient ( 'mongodb+srv://mongodb.localhost' , { } ) ;
52- client . connect ( ) ;
53- client . close ( ) ;
135+ afterEach ( async function ( ) {
136+ sinon . restore ( ) ;
54137 } ) ;
55- } ) ;
56-
57- context (
58- 'When given a host from DNS resolution that does NOT end with the original SRVs domain name' ,
59- function ( ) {
60- beforeEach ( async function ( ) {
61- sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
62- throw { code : 'ENODATA' } ;
63- } ) ;
64- } ) ;
65-
66- afterEach ( async function ( ) {
67- sinon . restore ( ) ;
68- } ) ;
69-
70- it ( 'an SRV with one domain level causes a runtime error' , async function ( ) {
71- sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
72- return [
73- {
74- name : 'localhost.mongodb' , // this string contains the SRV but does not end with it
75- port : 27017 ,
76- weight : 0 ,
77- priority : 0
78- }
79- ] ;
80- } ) ;
81- const err = await this . configuration
82- . newClient ( 'mongodb+srv://localhost' , { } )
83- . connect ( )
84- . catch ( e => e ) ;
85- expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
86- expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
87- } ) ;
88-
89- it ( 'an SRV with two domain levels causes a runtime error' , async function ( ) {
90- sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
91- return [
92- {
93- name : 'evil.localhost' , // this string only ends with part of the domain, not all of it!
94- port : 27017 ,
95- weight : 0 ,
96- priority : 0
97- }
98- ] ;
99- } ) ;
100- const err = await this . configuration
101- . newClient ( 'mongodb+srv://mongodb.localhost' , { } )
102- . connect ( )
103- . catch ( e => e ) ;
104- expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
105- expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
106- } ) ;
107-
108- it ( 'an SRV with three or more domain levels causes a runtime error' , async function ( ) {
109- sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
110- return [
111- {
112- name : 'blogs.evil.co.uk' ,
113- port : 27017 ,
114- weight : 0 ,
115- priority : 0
116- }
117- ] ;
118- } ) ;
119- const err = await this . configuration
120- . newClient ( 'mongodb+srv://blogs.mongodb.com' , { } )
121- . connect ( )
122- . catch ( e => e ) ;
123- expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
124- expect ( err . message ) . to . equal ( 'Server record does not share hostname with parent URI' ) ;
125- } ) ;
126- }
127- ) ;
128-
129- context (
130- 'When given a host from DNS resolution that is identical to the original SRVs hostname' ,
131- function ( ) {
132- beforeEach ( async function ( ) {
133- sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
134- throw { code : 'ENODATA' } ;
135- } ) ;
136- } ) ;
137138
138- afterEach ( async function ( ) {
139- sinon . restore ( ) ;
140- } ) ;
141-
142- it ( 'an SRV with one domain level causes a runtime error' , async function ( ) {
143- sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
144- return [
145- {
146- name : 'localhost' ,
147- port : 27017 ,
148- weight : 0 ,
149- priority : 0
150- }
151- ] ;
152- } ) ;
153- const err = await this . configuration
154- . newClient ( 'mongodb+srv://localhost' , { } )
155- . connect ( )
156- . catch ( e => e ) ;
157- expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
158- expect ( err . message ) . to . equal (
159- 'Server record does not have least one more domain than parent URI'
160- ) ;
139+ it ( 'an SRV with one domain level causes a runtime error' , async function ( ) {
140+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
141+ return [
142+ {
143+ name : 'localhost' ,
144+ port : 27017 ,
145+ weight : 0 ,
146+ priority : 0
147+ }
148+ ] ;
161149 } ) ;
150+ const err = await this . configuration
151+ . newClient ( 'mongodb+srv://localhost' , { } )
152+ . connect ( )
153+ . catch ( e => e ) ;
154+ expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
155+ expect ( err . message ) . to . equal (
156+ 'Server record does not have least one more domain than parent URI'
157+ ) ;
158+ } ) ;
162159
163- it ( 'an SRV with two domain levels causes a runtime error' , async function ( ) {
164- sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
165- return [
166- {
167- name : 'mongodb.localhost' ,
168- port : 27017 ,
169- weight : 0 ,
170- priority : 0
171- }
172- ] ;
173- } ) ;
174- const err = await this . configuration
175- . newClient ( 'mongodb+srv://mongodb.localhost' , { } )
176- . connect ( )
177- . catch ( e => e ) ;
178- expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
179- expect ( err . message ) . to . equal (
180- 'Server record does not have least one more domain than parent URI'
181- ) ;
160+ it ( 'an SRV with two domain levels causes a runtime error' , async function ( ) {
161+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
162+ return [
163+ {
164+ name : 'mongodb.localhost' ,
165+ port : 27017 ,
166+ weight : 0 ,
167+ priority : 0
168+ }
169+ ] ;
182170 } ) ;
183- }
184- ) ;
185- }
186- ) ;
171+ const err = await this . configuration
172+ . newClient ( 'mongodb+srv://mongodb.localhost' , { } )
173+ . connect ( )
174+ . catch ( e => e ) ;
175+ expect ( err ) . to . be . instanceOf ( MongoAPIError ) ;
176+ expect ( err . message ) . to . equal (
177+ 'Server record does not have least one more domain than parent URI'
178+ ) ;
179+ } ) ;
180+ }
181+ ) ;
182+ } ) ;
0 commit comments