diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-07-07 13:04:11 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-08-18 01:59:09 +0900 |
commit | 20a88ace0778282d5ee564842023c78e4a2e9399 (patch) | |
tree | 1e0e8c88f99e30485ce82ebccdce41b5a36d0151 /test | |
parent | 675e2906764a141040144b8586a5f3d32e228f6f (diff) | |
download | ruby-openssl-20a88ace0778282d5ee564842023c78e4a2e9399.tar.gz |
test: refactor PKey::PKey#{sign,verify} tests
RSA, DSA and EC should work in the same way, so extract to a new file,
test/test_pkey.rb.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_pkey.rb | 49 | ||||
-rw-r--r-- | test/test_pkey_dsa.rb | 33 | ||||
-rw-r--r-- | test/test_pkey_rsa.rb | 8 |
3 files changed, 49 insertions, 41 deletions
diff --git a/test/test_pkey.rb b/test/test_pkey.rb new file mode 100644 index 00000000..7e960926 --- /dev/null +++ b/test/test_pkey.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: false +require_relative "utils" + +if defined?(OpenSSL::TestUtils) + +class OpenSSL::TestPKey < OpenSSL::PKeyTestCase + PKEYS = { + OpenSSL::PKey::RSA => { + key: OpenSSL::TestUtils::TEST_KEY_RSA1024, + digest: OpenSSL::Digest::SHA1, + }, + OpenSSL::PKey::DSA => { + key: OpenSSL::TestUtils::TEST_KEY_DSA512, + digest: OpenSSL::Digest::SHA1, + }, + } + if defined?(OpenSSL::PKey::EC) + PKEYS[OpenSSL::PKey::EC] = { + key: OpenSSL::TestUtils::TEST_KEY_EC_P256V1, + digest: OpenSSL::TestUtils::DSA_SIGNATURE_DIGEST, + } + end + + def test_sign_verify + data = "Sign me!" + invalid_data = "Sign me?" + PKEYS.each do |klass, prop| + key = prop[:key] + pub_key = dup_public(prop[:key]) + digest = prop[:digest].new + signature = key.sign(digest, data) + assert_equal(true, pub_key.verify(digest, signature, data)) + assert_equal(false, pub_key.verify(digest, signature, invalid_data)) + # digest state is irrelevant + digest << "unya" + assert_equal(true, pub_key.verify(digest, signature, data)) + assert_equal(false, pub_key.verify(digest, signature, invalid_data)) + + if OpenSSL::OPENSSL_VERSION_NUMBER > 0x10000000 + digest = OpenSSL::Digest::SHA256.new + signature = key.sign(digest, data) + assert_equal(true, pub_key.verify(digest, signature, data)) + assert_equal(false, pub_key.verify(digest, signature, invalid_data)) + end + end + end +end + +end diff --git a/test/test_pkey_dsa.rb b/test/test_pkey_dsa.rb index a65fc222..d0ba8ec0 100644 --- a/test/test_pkey_dsa.rb +++ b/test/test_pkey_dsa.rb @@ -44,32 +44,6 @@ class OpenSSL::TestPKeyDSA < OpenSSL::PKeyTestCase assert(key.sysverify(digest, sig)) end - def test_sign_verify - check_sign_verify(OpenSSL::Digest::DSS1.new) - end if defined?(OpenSSL::Digest::DSS1) - -if (OpenSSL::OPENSSL_VERSION_NUMBER > 0x10000000) - def test_sign_verify_sha1 - check_sign_verify(OpenSSL::Digest::SHA1.new) - end - - def test_sign_verify_sha256 - check_sign_verify(OpenSSL::Digest::SHA256.new) - end -end - - def test_digest_state_irrelevant_verify - key = OpenSSL::TestUtils::TEST_KEY_DSA256 - digest1 = OpenSSL::TestUtils::DSA_SIGNATURE_DIGEST.new - digest2 = OpenSSL::TestUtils::DSA_SIGNATURE_DIGEST.new - data = 'Sign me!' - sig = key.sign(digest1, data) - digest1.reset - digest1 << 'Change state of digest1' - assert(key.verify(digest1, sig, data)) - assert(key.verify(digest2, sig, data)) - end - def test_DSAPrivateKey # OpenSSL DSAPrivateKey format; similar to RSAPrivateKey asn1 = OpenSSL::ASN1::Sequence([ @@ -198,13 +172,6 @@ fWLOqqkzFeRrYMDzUpl36XktY6Yq8EJYlW9pCMmBVNy/dQ== end private - def check_sign_verify(digest) - key = OpenSSL::TestUtils::TEST_KEY_DSA256 - data = 'Sign me!' - sig = key.sign(digest, data) - assert(key.verify(digest, sig, data)) - end - def assert_same_dsa(expected, key) check_component(expected, key, [:p, :q, :g, :pub_key, :priv_key]) end diff --git a/test/test_pkey_rsa.rb b/test/test_pkey_rsa.rb index 4215b77a..e211faa6 100644 --- a/test/test_pkey_rsa.rb +++ b/test/test_pkey_rsa.rb @@ -70,14 +70,6 @@ class OpenSSL::TestPKeyRSA < OpenSSL::PKeyTestCase end end - def test_sign_verify - key = RSA1024 - digest = OpenSSL::Digest::SHA1.new - data = 'Sign me!' - sig = key.sign(digest, data) - assert(key.verify(digest, sig, data)) - end - def test_digest_state_irrelevant_sign key = RSA1024 digest1 = OpenSSL::Digest::SHA1.new |