Skip to content

Commit 0ab8935

Browse files
DRIVERS-2769 Add test for unknown auth mechanism (#2858)
1 parent dc42fe8 commit 0ab8935

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

Diff for: lib/mongo/server/pending_connection.rb

+19-6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ def handshake_and_authenticate!
110110

111111
private
112112

113+
# Sends the hello command to the server, then receive and deserialize
114+
# the response.
115+
#
116+
# This method is extracted to be mocked in the tests.
117+
#
118+
# @param [ Protocol::Message ] Command that should be sent to a server
119+
# for handshake purposes.
120+
#
121+
# @return [ Mongo::Protocol::Reply ] Deserialized server response.
122+
def get_handshake_response(hello_command)
123+
@server.round_trip_time_averager.measure do
124+
add_server_diagnostics do
125+
socket.write(hello_command.serialize.to_s)
126+
Protocol::Message.deserialize(socket, Protocol::Message::MAX_MESSAGE_SIZE)
127+
end
128+
end
129+
end
130+
113131
# @param [ BSON::Document | nil ] speculative_auth_doc The document to
114132
# provide in speculativeAuthenticate field of handshake command.
115133
#
@@ -131,12 +149,7 @@ def handshake!(speculative_auth_doc: nil)
131149
doc = nil
132150
@server.handle_handshake_failure! do
133151
begin
134-
response = @server.round_trip_time_averager.measure do
135-
add_server_diagnostics do
136-
socket.write(hello_command.serialize.to_s)
137-
Protocol::Message.deserialize(socket, Protocol::Message::MAX_MESSAGE_SIZE)
138-
end
139-
end
152+
response = get_handshake_response(hello_command)
140153
result = Operation::Result.new([response])
141154
result.validate!
142155
doc = result.documents.first

Diff for: spec/mongo/server/connection_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,28 @@ class ConnectionSpecTestException < Exception; end
583583
end
584584
end
585585

586+
context 'when the server returns unknown saslSupportedMechs' do
587+
min_server_version '4.0'
588+
589+
let(:connection) do
590+
described_class.new(server, server.options.merge(connection_pool: pool))
591+
end
592+
593+
before do
594+
expect_any_instance_of(Mongo::Server::PendingConnection).to receive(:get_handshake_response).and_wrap_original do |original_method, *args|
595+
original_method.call(*args).tap do |result|
596+
if result.documents.first.fetch('saslSupportedMechs', nil).is_a?(Array)
597+
result.documents.first['saslSupportedMechs'].append('unknownMechanism')
598+
end
599+
end
600+
end
601+
end
602+
603+
it 'does not raise an error' do
604+
expect { connection.connect! }.not_to raise_error
605+
end
606+
end
607+
586608
end
587609

588610
describe '#disconnect!' do

0 commit comments

Comments
 (0)