@@ -131,39 +131,63 @@ def connect(*args)
131
131
expect ( Mysql2 ::Client ) . to respond_to ( :default_query_options )
132
132
end
133
133
134
- it "should be able to connect via SSL options" do
135
- ssl = @client . query "SHOW VARIABLES LIKE 'have_ssl'"
136
- ssl_uncompiled = ssl . any? { |x | x [ 'Value' ] == 'OFF' }
137
- pending ( "DON'T WORRY, THIS TEST PASSES - but SSL is not compiled into your MySQL daemon." ) if ssl_uncompiled
138
- ssl_disabled = ssl . any? { |x | x [ 'Value' ] == 'DISABLED' }
139
- pending ( "DON'T WORRY, THIS TEST PASSES - but SSL is not enabled in your MySQL daemon." ) if ssl_disabled
140
-
141
- # You may need to adjust the lines below to match your SSL certificate paths
142
- ssl_client = nil
143
- option_overrides = {
144
- 'host' => 'mysql2gem.example.com' , # must match the certificates
145
- :sslkey => '/etc/mysql/client-key.pem' ,
146
- :sslcert => '/etc/mysql/client-cert.pem' ,
147
- :sslca => '/etc/mysql/ca-cert.pem' ,
148
- :sslcipher => 'DHE-RSA-AES256-SHA' ,
149
- :sslverify => true ,
150
- }
151
- %i[ sslkey sslcert sslca ] . each do |item |
152
- unless File . exist? ( option_overrides [ item ] )
153
- pending ( "DON'T WORRY, THIS TEST PASSES - but #{ option_overrides [ item ] } does not exist." )
154
- break
134
+ context "SSL" do
135
+ before ( :example ) do
136
+ ssl = @client . query "SHOW VARIABLES LIKE 'have_ssl'"
137
+ ssl_uncompiled = ssl . any? { |x | x [ 'Value' ] == 'OFF' }
138
+ ssl_disabled = ssl . any? { |x | x [ 'Value' ] == 'DISABLED' }
139
+ if ssl_uncompiled
140
+ skip ( "DON'T WORRY, THIS TEST PASSES - but SSL is not compiled into your MySQL daemon." )
141
+ elsif ssl_disabled
142
+ skip ( "DON'T WORRY, THIS TEST PASSES - but SSL is not enabled in your MySQL daemon." )
143
+ else
144
+ %i[ sslkey sslcert sslca ] . each do |item |
145
+ unless File . exist? ( option_overrides [ item ] )
146
+ skip ( "DON'T WORRY, THIS TEST PASSES - but #{ option_overrides [ item ] } does not exist." )
147
+ break
148
+ end
149
+ end
155
150
end
156
151
end
157
- expect do
158
- ssl_client = new_client ( option_overrides )
159
- end . not_to raise_error
160
152
161
- results = Hash [ ssl_client . query ( 'SHOW STATUS WHERE Variable_name LIKE "Ssl_%"' ) . map { |x | x . values_at ( 'Variable_name' , 'Value' ) } ]
162
- expect ( results [ 'Ssl_cipher' ] ) . not_to be_empty
163
- expect ( results [ 'Ssl_version' ] ) . not_to be_empty
153
+ let ( :option_overrides ) do
154
+ {
155
+ 'host' => 'mysql2gem.example.com' , # must match the certificates
156
+ :sslkey => '/etc/mysql/client-key.pem' ,
157
+ :sslcert => '/etc/mysql/client-cert.pem' ,
158
+ :sslca => '/etc/mysql/ca-cert.pem' ,
159
+ :sslcipher => 'DHE-RSA-AES256-SHA' ,
160
+ :sslverify => true ,
161
+ }
162
+ end
163
+
164
+ let ( :ssl_client ) do
165
+ new_client ( option_overrides )
166
+ end
167
+
168
+ %i[ disabled preferred required verify_ca verify_identity ] . each do |ssl_mode |
169
+ it "should set ssl_mode option #{ ssl_mode } " do
170
+ options = {
171
+ ssl_mode : ssl_mode ,
172
+ }
173
+ options . merge! ( option_overrides )
174
+ # Relax the matching condition by checking if an error is not raised.
175
+ # TODO: Verify warnings by checking stderr.
176
+ expect do
177
+ new_client ( options )
178
+ end . not_to raise_error
179
+ end
180
+ end
164
181
165
- expect ( ssl_client . ssl_cipher ) . not_to be_empty
166
- expect ( results [ 'Ssl_cipher' ] ) . to eql ( ssl_client . ssl_cipher )
182
+ it "should be able to connect via SSL options" do
183
+ # You may need to adjust the lines below to match your SSL certificate paths
184
+ results = Hash [ ssl_client . query ( 'SHOW STATUS WHERE Variable_name LIKE "Ssl_%"' ) . map { |x | x . values_at ( 'Variable_name' , 'Value' ) } ]
185
+ expect ( results [ 'Ssl_cipher' ] ) . not_to be_empty
186
+ expect ( results [ 'Ssl_version' ] ) . not_to be_empty
187
+
188
+ expect ( ssl_client . ssl_cipher ) . not_to be_empty
189
+ expect ( results [ 'Ssl_cipher' ] ) . to eql ( ssl_client . ssl_cipher )
190
+ end
167
191
end
168
192
169
193
def run_gc
0 commit comments