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