Skip to content

Commit d9d3c34

Browse files
DRIVERS-2769 Test unknown auth mechanism
1 parent dc42fe8 commit d9d3c34

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

lib/mongo/server/pending_connection.rb

Lines changed: 19 additions & 6 deletions
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

spec/mongo/server/connection_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,24 @@ class ConnectionSpecTestException < Exception; end
583583
end
584584
end
585585

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

588606
describe '#disconnect!' do

0 commit comments

Comments
 (0)