|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { promisify } from 'util'; |
| 3 | + |
| 4 | +import { CancellationToken } from '../../../src'; |
| 5 | +import { MongoCredentials } from '../../../src/cmap/auth/mongo_credentials'; |
| 6 | +import { |
| 7 | + connect, |
| 8 | + prepareHandshakeDocument as prepareHandshakeDocumentCb |
| 9 | +} from '../../../src/cmap/connect'; |
| 10 | +import { Connection, ConnectionOptions } from '../../../src/cmap/connection'; |
| 11 | +import { LEGACY_HELLO_COMMAND } from '../../../src/constants'; |
| 12 | +import { MongoNetworkError } from '../../../src/error'; |
| 13 | +import { ClientMetadata, HostAddress, isHello } from '../../../src/utils'; |
| 14 | +import { genClusterTime } from '../../tools/common'; |
| 15 | +import * as mock from '../../tools/mongodb-mock/index'; |
| 16 | + |
| 17 | +const CONNECT_DEFAULTS = { |
| 18 | + id: 1, |
| 19 | + tls: false, |
| 20 | + generation: 1, |
| 21 | + monitorCommands: false, |
| 22 | + metadata: {} as ClientMetadata, |
| 23 | + loadBalanced: false |
| 24 | +}; |
| 25 | + |
| 26 | +describe('Connect Tests', function () { |
| 27 | + context('when PLAIN auth enabled', () => { |
| 28 | + const test: { |
| 29 | + server?: any; |
| 30 | + connectOptions?: ConnectionOptions; |
| 31 | + } = {}; |
| 32 | + |
| 33 | + beforeEach(async () => { |
| 34 | + const mockServer = await mock.createServer(); |
| 35 | + test.server = mockServer; |
| 36 | + test.connectOptions = { |
| 37 | + ...CONNECT_DEFAULTS, |
| 38 | + hostAddress: test.server.hostAddress() as HostAddress, |
| 39 | + credentials: new MongoCredentials({ |
| 40 | + username: 'testUser', |
| 41 | + password: 'pencil', |
| 42 | + source: 'admin', |
| 43 | + mechanism: 'PLAIN', |
| 44 | + mechanismProperties: {} |
| 45 | + }) |
| 46 | + }; |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => mock.cleanup()); |
| 50 | + |
| 51 | + it('should auth against a non-arbiter', function (done) { |
| 52 | + const whatHappened = {}; |
| 53 | + |
| 54 | + test.server.setMessageHandler(request => { |
| 55 | + const doc = request.document; |
| 56 | + const $clusterTime = genClusterTime(Date.now()); |
| 57 | + |
| 58 | + if (isHello(doc)) { |
| 59 | + whatHappened[LEGACY_HELLO_COMMAND] = true; |
| 60 | + request.reply( |
| 61 | + Object.assign({}, mock.HELLO, { |
| 62 | + $clusterTime |
| 63 | + }) |
| 64 | + ); |
| 65 | + } else if (doc.saslStart) { |
| 66 | + whatHappened.saslStart = true; |
| 67 | + request.reply({ ok: 1 }); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + connect(test.connectOptions, err => { |
| 72 | + try { |
| 73 | + expect(whatHappened).to.have.property(LEGACY_HELLO_COMMAND, true); |
| 74 | + expect(whatHappened).to.have.property('saslStart', true); |
| 75 | + } catch (_err) { |
| 76 | + err = _err; |
| 77 | + } |
| 78 | + |
| 79 | + done(err); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should not auth against an arbiter', function (done) { |
| 84 | + const whatHappened = {}; |
| 85 | + test.server.setMessageHandler(request => { |
| 86 | + const doc = request.document; |
| 87 | + const $clusterTime = genClusterTime(Date.now()); |
| 88 | + if (isHello(doc)) { |
| 89 | + whatHappened[LEGACY_HELLO_COMMAND] = true; |
| 90 | + request.reply( |
| 91 | + Object.assign({}, mock.HELLO, { |
| 92 | + $clusterTime, |
| 93 | + arbiterOnly: true |
| 94 | + }) |
| 95 | + ); |
| 96 | + } else if (doc.saslStart) { |
| 97 | + whatHappened.saslStart = true; |
| 98 | + request.reply({ ok: 0 }); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + connect(test.connectOptions, err => { |
| 103 | + try { |
| 104 | + expect(whatHappened).to.have.property(LEGACY_HELLO_COMMAND, true); |
| 105 | + expect(whatHappened).to.not.have.property('saslStart'); |
| 106 | + } catch (_err) { |
| 107 | + err = _err; |
| 108 | + } |
| 109 | + |
| 110 | + done(err); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + context('when creating a connection', () => { |
| 116 | + let server; |
| 117 | + let connectOptions; |
| 118 | + let connection: Connection; |
| 119 | + |
| 120 | + beforeEach(async () => { |
| 121 | + server = await mock.createServer(); |
| 122 | + server.setMessageHandler(request => { |
| 123 | + if (isHello(request.document)) { |
| 124 | + request.reply(mock.HELLO); |
| 125 | + } |
| 126 | + }); |
| 127 | + connectOptions = { |
| 128 | + ...CONNECT_DEFAULTS, |
| 129 | + hostAddress: server.hostAddress() as HostAddress, |
| 130 | + socketTimeoutMS: 15000 |
| 131 | + }; |
| 132 | + |
| 133 | + connection = await promisify<Connection>(callback => |
| 134 | + //@ts-expect-error: Callbacks do not have mutual exclusion for error/result existence |
| 135 | + connect(connectOptions, callback) |
| 136 | + )(); |
| 137 | + }); |
| 138 | + |
| 139 | + afterEach(async () => { |
| 140 | + connection.destroy({ force: true }); |
| 141 | + await mock.cleanup(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('creates a connection with an infinite timeout', async () => { |
| 145 | + expect(connection.stream).to.have.property('timeout', 0); |
| 146 | + }); |
| 147 | + |
| 148 | + it('connection instance has property socketTimeoutMS equal to the value passed in the connectOptions', async () => { |
| 149 | + expect(connection).to.have.property('socketTimeoutMS', 15000); |
| 150 | + }); |
| 151 | + |
| 152 | + context('when the provided cancellation token emits cancel', () => { |
| 153 | + it('interrupts the connection with an error', async () => { |
| 154 | + // set no response handler for mock server, effectively black hole requests |
| 155 | + server.setMessageHandler(() => null); |
| 156 | + |
| 157 | + const cancellationToken = new CancellationToken(); |
| 158 | + // Make sure the cancel listener is added before emitting cancel |
| 159 | + cancellationToken.addListener('newListener', () => { |
| 160 | + process.nextTick(() => { |
| 161 | + cancellationToken.emit('cancel'); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + const error = await promisify<Connection>(callback => |
| 166 | + connect( |
| 167 | + { |
| 168 | + ...connectOptions, |
| 169 | + // Ensure these timeouts do not fire first |
| 170 | + socketTimeoutMS: 5000, |
| 171 | + connectTimeoutMS: 5000, |
| 172 | + cancellationToken |
| 173 | + }, |
| 174 | + //@ts-expect-error: Callbacks do not have mutual exclusion for error/result existence |
| 175 | + callback |
| 176 | + ) |
| 177 | + )().catch(error => error); |
| 178 | + |
| 179 | + expect(error, error.stack).to.match(/connection establishment was cancelled/); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + context('when connecting takes longer than connectTimeoutMS', () => { |
| 184 | + it('interrupts the connection with an error', async () => { |
| 185 | + // set no response handler for mock server, effectively black hole requests |
| 186 | + server.setMessageHandler(() => null); |
| 187 | + |
| 188 | + const error = await promisify<Connection>(callback => |
| 189 | + //@ts-expect-error: Callbacks do not have mutual exclusion for error/result existence |
| 190 | + connect({ ...connectOptions, connectTimeoutMS: 5 }, callback) |
| 191 | + )().catch(error => error); |
| 192 | + |
| 193 | + expect(error).to.match(/timed out/); |
| 194 | + }); |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should emit `MongoNetworkError` for network errors', function (done) { |
| 199 | + connect({ hostAddress: new HostAddress('non-existent:27018') }, err => { |
| 200 | + expect(err).to.be.instanceOf(MongoNetworkError); |
| 201 | + done(); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + context('prepareHandshakeDocument', () => { |
| 206 | + const prepareHandshakeDocument = promisify(prepareHandshakeDocumentCb); |
| 207 | + |
| 208 | + context('when serverApi.version is present', () => { |
| 209 | + const options = {}; |
| 210 | + const authContext = { |
| 211 | + connection: { serverApi: { version: '1' } }, |
| 212 | + options |
| 213 | + }; |
| 214 | + |
| 215 | + it('sets the hello parameter to true', async () => { |
| 216 | + const handshakeDocument = await prepareHandshakeDocument(authContext); |
| 217 | + expect(handshakeDocument).to.have.property('hello', true); |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + context('when serverApi is not present', () => { |
| 222 | + const options = {}; |
| 223 | + const authContext = { |
| 224 | + connection: {}, |
| 225 | + options |
| 226 | + }; |
| 227 | + |
| 228 | + it('sets the legacy hello parameter to true', async () => { |
| 229 | + const handshakeDocument = await prepareHandshakeDocument(authContext); |
| 230 | + expect(handshakeDocument).to.have.property(LEGACY_HELLO_COMMAND, true); |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + context('loadBalanced option', () => { |
| 235 | + context('when loadBalanced is not set as an option', () => { |
| 236 | + it('does not set loadBalanced on the handshake document', async () => { |
| 237 | + const options = {}; |
| 238 | + const authContext = { |
| 239 | + connection: {}, |
| 240 | + options |
| 241 | + }; |
| 242 | + const handshakeDocument = await prepareHandshakeDocument(authContext); |
| 243 | + expect(handshakeDocument).not.to.have.property('loadBalanced'); |
| 244 | + }); |
| 245 | + }); |
| 246 | + |
| 247 | + context('when loadBalanced is set to false', () => { |
| 248 | + it('does not set loadBalanced on the handshake document', async () => { |
| 249 | + const options = { |
| 250 | + loadBalanced: false |
| 251 | + }; |
| 252 | + const authContext = { |
| 253 | + connection: {}, |
| 254 | + options |
| 255 | + }; |
| 256 | + const handshakeDocument = await prepareHandshakeDocument(authContext); |
| 257 | + expect(handshakeDocument).not.to.have.property('loadBalanced'); |
| 258 | + }); |
| 259 | + }); |
| 260 | + |
| 261 | + context('when loadBalanced is set to true', () => { |
| 262 | + it('does set loadBalanced on the handshake document', async () => { |
| 263 | + const options = { |
| 264 | + loadBalanced: true |
| 265 | + }; |
| 266 | + const authContext = { |
| 267 | + connection: {}, |
| 268 | + options |
| 269 | + }; |
| 270 | + const handshakeDocument = await prepareHandshakeDocument(authContext); |
| 271 | + expect(handshakeDocument).to.have.property('loadBalanced', true); |
| 272 | + }); |
| 273 | + }); |
| 274 | + }); |
| 275 | + }); |
| 276 | +}); |
0 commit comments