2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2011-07-21 20:13:07 -04:00
|
|
|
require_relative 'utils'
|
|
|
|
|
|
|
|
class OpenSSL::TestEngine < Test::Unit::TestCase
|
|
|
|
|
2011-11-26 09:04:52 -05:00
|
|
|
def teardown
|
|
|
|
OpenSSL::Engine.cleanup # [ruby-core:40669]
|
|
|
|
assert_equal(0, OpenSSL::Engine.engines.size)
|
|
|
|
end
|
|
|
|
|
2011-07-21 20:13:07 -04:00
|
|
|
def test_engines_free # [ruby-dev:44173]
|
2011-11-26 09:04:52 -05:00
|
|
|
OpenSSL::Engine.load("openssl")
|
2011-07-21 20:13:07 -04:00
|
|
|
OpenSSL::Engine.engines
|
|
|
|
OpenSSL::Engine.engines
|
|
|
|
end
|
|
|
|
|
2011-11-03 20:33:11 -04:00
|
|
|
def test_openssl_engine_builtin
|
|
|
|
engine = OpenSSL::Engine.load("openssl")
|
|
|
|
assert_equal(true, engine)
|
|
|
|
assert_equal(1, OpenSSL::Engine.engines.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_engine_by_id_string
|
2011-11-26 09:04:52 -05:00
|
|
|
engine = get_engine
|
2011-11-03 20:33:11 -04:00
|
|
|
assert_not_nil(engine)
|
|
|
|
assert_equal(1, OpenSSL::Engine.engines.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_engine_id_name_inspect
|
2011-11-26 09:04:52 -05:00
|
|
|
engine = get_engine
|
2011-11-03 20:33:11 -04:00
|
|
|
assert_equal("openssl", engine.id)
|
|
|
|
assert_not_nil(engine.name)
|
|
|
|
assert_not_nil(engine.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_engine_digest_sha1
|
2011-11-26 09:04:52 -05:00
|
|
|
engine = get_engine
|
2011-11-03 20:33:11 -04:00
|
|
|
digest = engine.digest("SHA1")
|
|
|
|
assert_not_nil(digest)
|
|
|
|
data = "test"
|
|
|
|
assert_equal(OpenSSL::Digest::SHA1.digest(data), digest.digest(data))
|
2011-11-05 14:33:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_openssl_engine_cipher_rc4
|
2011-11-26 09:04:52 -05:00
|
|
|
engine = get_engine
|
2011-11-05 14:33:37 -04:00
|
|
|
algo = "RC4" #AES is not supported by openssl Engine (<=1.0.0e)
|
|
|
|
data = "a" * 1000
|
|
|
|
key = OpenSSL::Random.random_bytes(16)
|
2011-11-23 20:09:55 -05:00
|
|
|
# suppress message from openssl Engine's RC4 cipher [ruby-core:41026]
|
|
|
|
err_back = $stderr.dup
|
|
|
|
$stderr.reopen(IO::NULL)
|
|
|
|
encrypted = crypt_data(data, key, :encrypt) { engine.cipher(algo) }
|
|
|
|
decrypted = crypt_data(encrypted, key, :decrypt) { OpenSSL::Cipher.new(algo) }
|
2011-11-05 14:33:37 -04:00
|
|
|
assert_equal(data, decrypted)
|
2011-11-23 20:09:55 -05:00
|
|
|
ensure
|
2011-11-24 06:07:50 -05:00
|
|
|
if err_back
|
|
|
|
$stderr.reopen(err_back)
|
|
|
|
err_back.close
|
|
|
|
end
|
2012-08-21 00:50:18 -04:00
|
|
|
end
|
2011-11-03 20:33:11 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-11-26 09:04:52 -05:00
|
|
|
def get_engine
|
|
|
|
OpenSSL::Engine.by_id("openssl")
|
|
|
|
end
|
|
|
|
|
2011-11-23 20:09:55 -05:00
|
|
|
def crypt_data(data, key, mode)
|
|
|
|
cipher = yield
|
|
|
|
cipher.send mode
|
|
|
|
cipher.key = key
|
|
|
|
cipher.update(data) + cipher.final
|
|
|
|
end
|
|
|
|
|
2014-12-12 22:05:43 -05:00
|
|
|
end if defined?(OpenSSL::TestUtils)
|
2011-07-21 20:13:07 -04:00
|
|
|
|