summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2020-04-21 02:12:29 +0900
committerKazuki Yamaguchi <k@rhe.jp>2020-05-13 15:47:51 +0900
commit6f008c9d2fec52f2c2d39c04ad83e87c7975105c (patch)
treeb9f95b1bfe44d38db386df64bbf6cd016447786e /test
parenta7145c3de4ddbe9f90bd6f087afac44a764fae04 (diff)
[ruby/openssl] pkey: add PKey#inspect and #oid
Implement OpenSSL::PKey::PKey#oid as a wrapper around EVP_PKEY_id(). This allows user code to check the type of a PKey object. EVP_PKEY can have a pkey type for which we do not provide a dedicated subclass. In other words, an EVP_PKEY that is not any of {RSA,DSA,DH,EC} can exist. It is currently not possible to distinguish such a pkey. Also, implement PKey#inspect to include the key type for convenience. https://github.com/ruby/openssl/commit/dafbb1b3e6
Diffstat (limited to 'test')
-rw-r--r--test/openssl/test_pkey.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/test/openssl/test_pkey.rb b/test/openssl/test_pkey.rb
new file mode 100644
index 0000000000..0bdc9795cc
--- /dev/null
+++ b/test/openssl/test_pkey.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+require_relative "utils"
+
+class OpenSSL::TestPKey < OpenSSL::PKeyTestCase
+ def test_generic_oid_inspect
+ # RSA private key
+ rsa = Fixtures.pkey("rsa-1")
+ assert_instance_of OpenSSL::PKey::RSA, rsa
+ assert_equal "rsaEncryption", rsa.oid
+ assert_match %r{oid=rsaEncryption}, rsa.inspect
+
+ # X25519 private key
+ x25519_pem = <<~EOF
+ -----BEGIN PRIVATE KEY-----
+ MC4CAQAwBQYDK2VuBCIEIHcHbQpzGKV9PBbBclGyZkXfTC+H68CZKrF3+6UduSwq
+ -----END PRIVATE KEY-----
+ EOF
+ begin
+ x25519 = OpenSSL::PKey.read(x25519_pem)
+ rescue OpenSSL::PKey::PKeyError
+ # OpenSSL < 1.1.0
+ pend "X25519 is not implemented"
+ end
+ assert_instance_of OpenSSL::PKey::PKey, x25519
+ assert_equal "X25519", x25519.oid
+ assert_match %r{oid=X25519}, x25519.inspect
+ end
+end