Skip to content

Commit c325c2f

Browse files
committed
RUBY-886 Add tests and adjust test suite for ssl verify default
1 parent a192625 commit c325c2f

File tree

5 files changed

+121
-30
lines changed

5 files changed

+121
-30
lines changed

spec/mongo/server/connection_pool_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
describe Mongo::Server::ConnectionPool do
44

5+
let(:options) do
6+
TEST_OPTIONS.merge(max_pool_size: 2)
7+
end
8+
59
let(:address) do
610
Mongo::Address.new('127.0.0.1:27017')
711
end
@@ -17,7 +21,7 @@
1721
describe '#checkin' do
1822

1923
let(:server) do
20-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, ssl: SSL)
24+
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
2125
end
2226

2327
let!(:pool) do
@@ -47,7 +51,7 @@
4751
describe '#checkout' do
4852

4953
let(:server) do
50-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, ssl: SSL)
54+
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
5155
end
5256

5357
let!(:pool) do
@@ -95,7 +99,7 @@
9599
describe '.get' do
96100

97101
let(:server) do
98-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, ssl: SSL)
102+
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
99103
end
100104

101105
let!(:pool) do
@@ -110,7 +114,7 @@
110114
describe '#inspect' do
111115

112116
let(:server) do
113-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, ssl: SSL)
117+
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
114118
end
115119

116120
let!(:pool) do
@@ -129,7 +133,7 @@
129133
describe '#with_connection' do
130134

131135
let(:server) do
132-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, ssl: SSL)
136+
Mongo::Server.new(address, double('cluster'), monitoring, listeners, options)
133137
end
134138

135139
let!(:pool) do

spec/mongo/server/connection_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
end
1616

1717
let(:server) do
18-
Mongo::Server.new(address, double('cluster'), monitoring, listeners, ssl: SSL)
18+
Mongo::Server.new(address, double('cluster'), monitoring, listeners, TEST_OPTIONS)
1919
end
2020

2121
describe '#connect!' do

spec/mongo/socket/ssl_spec.rb

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
let(:options) do
1414
{
15-
:ssl => true,
16-
:ssl_cert => CLIENT_PEM,
17-
:ssl_key => CLIENT_PEM
15+
:ssl => true,
16+
:ssl_cert => CLIENT_PEM,
17+
:ssl_key => CLIENT_PEM,
18+
:ssl_verify => false
1819
}
1920
end
2021

@@ -31,9 +32,10 @@
3132

3233
let(:options) do
3334
{
34-
:ssl => true,
35-
:ssl_cert => CLIENT_PEM,
36-
:ssl_key => CRL_PEM
35+
:ssl => true,
36+
:ssl_cert => CLIENT_PEM,
37+
:ssl_key => CRL_PEM,
38+
:ssl_verify => false
3739
}
3840
end
3941

@@ -51,7 +53,8 @@
5153
:ssl => true,
5254
:ssl_cert => CLIENT_PEM,
5355
:ssl_key => CLIENT_PEM,
54-
:ssl_ca_cert => CA_PEM
56+
:ssl_ca_cert => CA_PEM,
57+
:ssl_verify => true
5558
}
5659
end
5760

@@ -63,5 +66,67 @@
6366
expect(socket).to be_alive
6467
end
6568
end
69+
70+
context 'when ssl_verify is not specified' do
71+
72+
let(:options) do
73+
{
74+
:ssl => true,
75+
:ssl_cert => CLIENT_PEM,
76+
:ssl_key => CLIENT_PEM,
77+
:ssl_ca_cert => CA_PEM
78+
}
79+
end
80+
81+
before do
82+
socket.connect!
83+
end
84+
85+
it 'verifies the server certificate' do
86+
expect(socket).to be_alive
87+
end
88+
end
89+
90+
context 'when ssl_verify is true' do
91+
92+
let(:options) do
93+
{
94+
:ssl => true,
95+
:ssl_cert => CLIENT_PEM,
96+
:ssl_key => CLIENT_PEM,
97+
:ssl_ca_cert => CA_PEM,
98+
:ssl_verify => true
99+
}
100+
end
101+
102+
before do
103+
socket.connect!
104+
end
105+
106+
it 'verifies the server certificate' do
107+
expect(socket).to be_alive
108+
end
109+
end
110+
111+
context 'when ssl_verify is false' do
112+
113+
let(:options) do
114+
{
115+
:ssl => true,
116+
:ssl_cert => CLIENT_PEM,
117+
:ssl_key => CLIENT_PEM,
118+
:ssl_ca_cert => 'invalid',
119+
:ssl_verify => false
120+
}
121+
end
122+
123+
before do
124+
socket.connect!
125+
end
126+
127+
it 'does not verify the server certificate' do
128+
expect(socket).to be_alive
129+
end
130+
end
66131
end
67132
end

spec/spec_helper.rb

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@
1717
end
1818
end
1919

20+
TEST_SET = 'ruby-driver-rs'
21+
COVERAGE_MIN = 90
22+
CURRENT_PATH = File.expand_path(File.dirname(__FILE__))
23+
SERVER_DISCOVERY_TESTS = Dir.glob("#{CURRENT_PATH}/support/sdam/**/*.yml")
24+
SERVER_SELECTION_RTT_TESTS = Dir.glob("#{CURRENT_PATH}/support/server_selection/rtt/*.yml")
25+
SERVER_SELECTION_TESTS = Dir.glob("#{CURRENT_PATH}/support/server_selection/selection/**/*.yml")
26+
CRUD_TESTS = Dir.glob("#{CURRENT_PATH}/support/crud_tests/**/*.yml")
27+
28+
SSL_CERTS_DIR = "#{CURRENT_PATH}/support/certificates"
29+
CLIENT_PEM = "#{SSL_CERTS_DIR}/client.pem"
30+
CLIENT_PASSWORD_PEM = "#{SSL_CERTS_DIR}/password_protected.pem"
31+
CA_PEM = "#{SSL_CERTS_DIR}/ca.pem"
32+
CRL_PEM = "#{SSL_CERTS_DIR}/crl.pem"
33+
2034
require 'mongo'
2135

2236
require 'support/travis'
@@ -61,20 +75,6 @@
6175
end
6276
end
6377

64-
TEST_SET = 'ruby-driver-rs'
65-
COVERAGE_MIN = 90
66-
CURRENT_PATH = File.expand_path(File.dirname(__FILE__))
67-
SERVER_DISCOVERY_TESTS = Dir.glob("#{CURRENT_PATH}/support/sdam/**/*.yml")
68-
SERVER_SELECTION_RTT_TESTS = Dir.glob("#{CURRENT_PATH}/support/server_selection/rtt/*.yml")
69-
SERVER_SELECTION_TESTS = Dir.glob("#{CURRENT_PATH}/support/server_selection/selection/**/*.yml")
70-
CRUD_TESTS = Dir.glob("#{CURRENT_PATH}/support/crud_tests/**/*.yml")
71-
72-
SSL_CERTS_DIR = "#{CURRENT_PATH}/support/certificates"
73-
CLIENT_PEM = "#{SSL_CERTS_DIR}/client.pem"
74-
CLIENT_PASSWORD_PEM = "#{SSL_CERTS_DIR}/password_protected.pem"
75-
CA_PEM = "#{SSL_CERTS_DIR}/ca.pem"
76-
CRL_PEM = "#{SSL_CERTS_DIR}/crl.pem"
77-
7878
# Determine whether the test clients are connecting to a standalone.
7979
#
8080
# @since 2.0.0
@@ -149,10 +149,16 @@ def list_command_enabled?
149149
$list_command_enabled ||= $mongo_client.cluster.servers.first.features.list_indexes_enabled?
150150
end
151151

152+
# Is the test suite running locallly (not on Travis or Jenkins).
153+
#
154+
# @since 2.1.0
152155
def testing_locally?
153156
!(ENV['CI'] || ENV['JENKINS_HOME'])
154157
end
155158

159+
# Is the test suite running on SSL.
160+
#
161+
# @since 2.0.2
156162
def running_ssl?
157163
SSL
158164
end

spec/support/authorization.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,28 @@
5050
# @since 2.0.3
5151
SSL = ENV['SSL_ENABLED'] == 'true'
5252

53+
# SSL options.
54+
#
55+
# @since 2.1.0
56+
SSL_OPTIONS = {
57+
ssl: SSL,
58+
ssl_verify: false,
59+
ssl_cert: CLIENT_PEM,
60+
ssl_key: CLIENT_PEM
61+
}
62+
63+
# Base test options.
64+
#
65+
# @since 2.1.0
66+
BASE_OPTIONS = {
67+
max_pool_size: 1,
68+
write: WRITE_CONCERN
69+
}
70+
5371
# Options for test suite clients.
5472
#
5573
# @since 2.0.3
56-
TEST_OPTIONS = CONNECT.merge(max_pool_size: 1,
57-
write: WRITE_CONCERN,
58-
ssl: SSL)
74+
TEST_OPTIONS = BASE_OPTIONS.merge(CONNECT).merge(SSL_OPTIONS)
5975

6076
# The root user name.
6177
#

0 commit comments

Comments
 (0)