|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as net from 'net'; |
| 3 | +import * as process from 'process'; |
| 4 | +import * as sinon from 'sinon'; |
| 5 | + |
| 6 | +import { ConnectionCreatedEvent, MongoClient, ReadPreference, TopologyType } from '../../../src'; |
| 7 | +import { byStrings, sorted } from '../../tools/utils'; |
| 8 | + |
| 9 | +describe('IPv6 Addresses', () => { |
| 10 | + let client: MongoClient; |
| 11 | + let ipv6Hosts: string[]; |
| 12 | + |
| 13 | + beforeEach(async function () { |
| 14 | + if ( |
| 15 | + process.platform === 'linux' || |
| 16 | + this.configuration.topologyType !== TopologyType.ReplicaSetWithPrimary |
| 17 | + ) { |
| 18 | + if (this.currentTest) { |
| 19 | + // Ubuntu 18 (linux) does not support localhost AAAA lookups (IPv6) |
| 20 | + // Windows (VS2019) has the AAAA lookup |
| 21 | + // We do not run a replica set on macos |
| 22 | + this.currentTest.skipReason = |
| 23 | + 'We are only running this on windows currently because it has the IPv6 translation for localhost'; |
| 24 | + } |
| 25 | + return this.skip(); |
| 26 | + } |
| 27 | + |
| 28 | + ipv6Hosts = this.configuration.options.hostAddresses.map(({ port }) => `[::1]:${port}`); |
| 29 | + client = this.configuration.newClient(`mongodb://${ipv6Hosts.join(',')}/test`, { |
| 30 | + [Symbol.for('@@mdb.skipPingOnConnect')]: true, |
| 31 | + maxPoolSize: 1 |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(async function () { |
| 36 | + sinon.restore(); |
| 37 | + await client?.close(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should have IPv6 loopback addresses set on the client', function () { |
| 41 | + const ipv6LocalhostAddresses = this.configuration.options.hostAddresses.map(({ port }) => ({ |
| 42 | + host: '::1', |
| 43 | + port, |
| 44 | + isIPv6: true, |
| 45 | + socketPath: undefined |
| 46 | + })); |
| 47 | + expect(client.options.hosts).to.deep.equal(ipv6LocalhostAddresses); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should successfully connect using IPv6 loopback addresses', async function () { |
| 51 | + const localhostHosts = this.configuration.options.hostAddresses.map( |
| 52 | + ({ port }) => `localhost:${port}` // ::1 will be swapped out for localhost |
| 53 | + ); |
| 54 | + await client.db().command({ ping: 1 }); |
| 55 | + // After running the first command we should receive the hosts back as reported by the mongod in a hello response |
| 56 | + // mongodb will report the bound host address, in this case "localhost" |
| 57 | + expect(client.topology).to.exist; |
| 58 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 59 | + expect(sorted(client.topology!.s.description.servers.keys(), byStrings)).to.deep.equal( |
| 60 | + localhostHosts |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should createConnection with IPv6 addresses initially then switch to mongodb bound addresses', async () => { |
| 65 | + const createConnectionSpy = sinon.spy(net, 'createConnection'); |
| 66 | + |
| 67 | + const connectionCreatedEvents: ConnectionCreatedEvent[] = []; |
| 68 | + client.on('connectionCreated', ev => connectionCreatedEvents.push(ev)); |
| 69 | + |
| 70 | + await client.db().command({ ping: 1 }, { readPreference: ReadPreference.primary }); |
| 71 | + |
| 72 | + const callArgs = createConnectionSpy.getCalls().map(({ args }) => args[0]); |
| 73 | + |
| 74 | + // This is 7 because we create 3 monitoring connections with ::1, then another 3 with localhost |
| 75 | + // and then 1 more in the connection pool for the operation, that is why we are checking for the connectionCreated event |
| 76 | + expect(callArgs).to.be.lengthOf(7); |
| 77 | + expect(connectionCreatedEvents).to.have.lengthOf(1); |
| 78 | + expect(connectionCreatedEvents[0]).to.have.property('address').that.includes('localhost'); |
| 79 | + |
| 80 | + for (let index = 0; index < 3; index++) { |
| 81 | + // The first 3 connections (monitoring) are made using the user provided addresses |
| 82 | + expect(callArgs[index]).to.have.property('host', '::1'); |
| 83 | + } |
| 84 | + |
| 85 | + for (let index = 3; index < 6; index++) { |
| 86 | + // MongoDB sends back hellos that have the bound address 'localhost' |
| 87 | + // We make new connection using that address instead |
| 88 | + expect(callArgs[index]).to.have.property('host', 'localhost'); |
| 89 | + } |
| 90 | + |
| 91 | + // Operation connection |
| 92 | + expect(callArgs[6]).to.have.property('host', 'localhost'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments