aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2016-08-05 17:13:13 +0900
committerKazuki Yamaguchi <k@rhe.jp>2016-08-07 17:16:00 +0900
commit8b094404f29bef087794f365078cb7423c37172e (patch)
tree9f1b02731f45f8aeb19810016e6bedb8d1934ef3 /test
parent7c7469c34d65781c1575ebf3d1a7f9e032b35aad (diff)
downloadruby-openssl-8b094404f29bef087794f365078cb7423c37172e.tar.gz
test/envutil: port assert_raise_with_message from Ruby tree
Ruby's assert_raise doesn't allow the expected exception to be an instance of an exception.
Diffstat (limited to 'test')
-rw-r--r--test/envutil.rb50
-rw-r--r--test/test_ssl.rb12
2 files changed, 56 insertions, 6 deletions
diff --git a/test/envutil.rb b/test/envutil.rb
index a4964c2c..5cd8b511 100644
--- a/test/envutil.rb
+++ b/test/envutil.rb
@@ -576,6 +576,56 @@ eom
values
end
+ # :call-seq:
+ # assert_raise_with_message(exception, expected, msg = nil, &block)
+ #
+ #Tests if the given block raises an exception with the expected
+ #message.
+ #
+ # assert_raise_with_message(RuntimeError, "foo") do
+ # nil #Fails, no Exceptions are raised
+ # end
+ #
+ # assert_raise_with_message(RuntimeError, "foo") do
+ # raise ArgumentError, "foo" #Fails, different Exception is raised
+ # end
+ #
+ # assert_raise_with_message(RuntimeError, "foo") do
+ # raise "bar" #Fails, RuntimeError is raised but the message differs
+ # end
+ #
+ # assert_raise_with_message(RuntimeError, "foo") do
+ # raise "foo" #Raises RuntimeError with the message, so assertion succeeds
+ # end
+ def assert_raise_with_message(exception, expected, msg = nil, &block)
+ case expected
+ when String
+ assert = :assert_equal
+ when Regexp
+ assert = :assert_match
+ else
+ raise TypeError, "Expected #{expected.inspect} to be a kind of String or Regexp, not #{expected.class}"
+ end
+
+ ex = m = nil
+ EnvUtil.with_default_internal(expected.encoding) do
+ ex = assert_raise(exception, msg || "Exception(#{exception}) with message matches to #{expected.inspect}") do
+ yield
+ end
+ m = ex.message
+ end
+ msg = message(msg, "") {"Expected Exception(#{exception}) was raised, but the message doesn't match"}
+
+ if assert == :assert_equal
+ assert_equal(expected, m, msg)
+ else
+ msg = message(msg) { "Expected #{mu_pp expected} to match #{mu_pp m}" }
+ assert expected =~ m, msg
+ block.binding.eval("proc{|_|$~=_}").call($~)
+ end
+ ex
+ end
+
class << (AssertFile = Struct.new(:failure_message).new)
include Assertions
def assert_file_predicate(predicate, *args)
diff --git a/test/test_ssl.rb b/test/test_ssl.rb
index e7af8232..b821b2a2 100644
--- a/test/test_ssl.rb
+++ b/test/test_ssl.rb
@@ -13,10 +13,10 @@ class OpenSSL::TestSSL < OpenSSL::SSLTestCase
def test_ctx_setup_invalid
m = OpenSSL::SSL::SSLContext::METHODS.first
- assert_raise(ArgumentError.new("string contains null byte")) {
+ assert_raise_with_message(ArgumentError, /null/) {
OpenSSL::SSL::SSLContext.new("#{m}\0")
}
- assert_raise(ArgumentError.new("unknown SSL method `\u{ff33 ff33 ff2c}'.")) {
+ assert_raise_with_message(ArgumentError, /\u{ff33 ff33 ff2c}/) {
OpenSSL::SSL::SSLContext.new("\u{ff33 ff33 ff2c}")
}
end
@@ -443,9 +443,9 @@ class OpenSSL::TestSSL < OpenSSL::SSLTestCase
ctx.ciphers = "aNULL"
ctx.security_level = 0
server_connect(port, ctx) { |ssl|
- msg = "Peer verification enabled, but no certificate received. Anonymous cipher suite " \
- "ADH-AES256-GCM-SHA384 was negotiated. Anonymous suites must be disabled to use peer verification."
- assert_raise(sslerr.new(msg)){ssl.post_connection_check("localhost.localdomain")}
+ assert_raise_with_message(sslerr, /anonymous cipher suite/i){
+ ssl.post_connection_check("localhost.localdomain")
+ }
}
}
end if OpenSSL::ExtConfig::TLS_DH_anon_WITH_AES_256_GCM_SHA384
@@ -1130,7 +1130,7 @@ if OpenSSL::OPENSSL_VERSION_NUMBER >= 0x10002000
ctx_proc = Proc.new { |ctx|
ctx.alpn_select_cb = -> (protocols) { nil }
}
- assert_raises(MiniTest::Assertion) do # minitest/assertion comes from `assert_join_threads`
+ assert_raise(MiniTest::Assertion) do # minitest/assertion comes from `assert_join_threads`
start_server_version(:SSLv23, ctx_proc) { |server, port|
ctx = OpenSSL::SSL::SSLContext.new
ctx.alpn_protocols = ["http/1.1"]