Skip to content

Commit 3026da1

Browse files
committed
Add tests to verify ssl_mode option.
Add tests to verify the following commmit. Can't enable SSL with MariaDB driver library.
1 parent 594b53a commit 3026da1

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

spec/mysql2/client_spec.rb

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,39 +126,58 @@ def connect(*args)
126126
expect(Mysql2::Client).to respond_to(:default_query_options)
127127
end
128128

129-
it "should be able to connect via SSL options" do
130-
ssl = @client.query "SHOW VARIABLES LIKE 'have_ssl'"
131-
ssl_uncompiled = ssl.any? { |x| x['Value'] == 'OFF' }
132-
pending("DON'T WORRY, THIS TEST PASSES - but SSL is not compiled into your MySQL daemon.") if ssl_uncompiled
133-
ssl_disabled = ssl.any? { |x| x['Value'] == 'DISABLED' }
134-
pending("DON'T WORRY, THIS TEST PASSES - but SSL is not enabled in your MySQL daemon.") if ssl_disabled
135-
136-
# You may need to adjust the lines below to match your SSL certificate paths
137-
ssl_client = nil
138-
option_overrides = {
139-
'host' => 'mysql2gem.example.com', # must match the certificates
140-
:sslkey => '/etc/mysql/client-key.pem',
141-
:sslcert => '/etc/mysql/client-cert.pem',
142-
:sslca => '/etc/mysql/ca-cert.pem',
143-
:sslcipher => 'DHE-RSA-AES256-SHA',
144-
:sslverify => true,
145-
}
146-
%i[sslkey sslcert sslca].each do |item|
147-
unless File.exist?(option_overrides[item])
148-
pending("DON'T WORRY, THIS TEST PASSES - but #{option_overrides[item]} does not exist.")
149-
break
129+
context "SSL" do
130+
before(:example) do
131+
ssl = @client.query "SHOW VARIABLES LIKE 'have_ssl'"
132+
ssl_uncompiled = ssl.any? { |x| x['Value'] == 'OFF' }
133+
pending("DON'T WORRY, THIS TEST PASSES - but SSL is not compiled into your MySQL daemon.") if ssl_uncompiled
134+
ssl_disabled = ssl.any? { |x| x['Value'] == 'DISABLED' }
135+
pending("DON'T WORRY, THIS TEST PASSES - but SSL is not enabled in your MySQL daemon.") if ssl_disabled
136+
137+
%i[sslkey sslcert sslca].each do |item|
138+
unless File.exist?(option_overrides[item])
139+
pending("DON'T WORRY, THIS TEST PASSES - but #{option_overrides[item]} does not exist.")
140+
break
141+
end
150142
end
151143
end
152-
expect do
153-
ssl_client = new_client(option_overrides)
154-
end.not_to raise_error
155144

156-
results = Hash[ssl_client.query('SHOW STATUS WHERE Variable_name LIKE "Ssl_%"').map { |x| x.values_at('Variable_name', 'Value') }]
157-
expect(results['Ssl_cipher']).not_to be_empty
158-
expect(results['Ssl_version']).not_to be_empty
145+
let(:option_overrides) do
146+
{
147+
'host' => 'mysql2gem.example.com', # must match the certificates
148+
:sslkey => '/etc/mysql/client-key.pem',
149+
:sslcert => '/etc/mysql/client-cert.pem',
150+
:sslca => '/etc/mysql/ca-cert.pem',
151+
:sslcipher => 'DHE-RSA-AES256-SHA',
152+
:sslverify => true,
153+
}
154+
end
155+
156+
let(:ssl_client) do
157+
new_client(option_overrides)
158+
end
159159

160-
expect(ssl_client.ssl_cipher).not_to be_empty
161-
expect(results['Ssl_cipher']).to eql(ssl_client.ssl_cipher)
160+
%i[disabled preferred required verify_ca verify_identity].each do |ssl_mode|
161+
it "should set ssl_mode option #{ssl_mode}" do
162+
options = {
163+
ssl_mode: ssl_mode,
164+
}
165+
options.merge!(option_overrides)
166+
expect do
167+
new_client(options)
168+
end.to_not output.to_stderr
169+
end
170+
end
171+
172+
it "should be able to connect via SSL options" do
173+
# You may need to adjust the lines below to match your SSL certificate paths
174+
results = Hash[ssl_client.query('SHOW STATUS WHERE Variable_name LIKE "Ssl_%"').map { |x| x.values_at('Variable_name', 'Value') }]
175+
expect(results['Ssl_cipher']).not_to be_empty
176+
expect(results['Ssl_version']).not_to be_empty
177+
178+
expect(ssl_client.ssl_cipher).not_to be_empty
179+
expect(results['Ssl_cipher']).to eql(ssl_client.ssl_cipher)
180+
end
162181
end
163182

164183
def run_gc

0 commit comments

Comments
 (0)