summaryrefslogtreecommitdiff
path: root/test/openssl/test_pkey_dh.rb
diff options
context:
space:
mode:
authoremboss <emboss@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-06-13 01:58:09 +0000
committeremboss <emboss@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-06-13 01:58:09 +0000
commita27b63d3fc1c491eda62a8a48527df53301f45ce (patch)
tree79f9a2aecb1e1e1f45044a87c31a99e715bee099 /test/openssl/test_pkey_dh.rb
parent149f35fc6ee42eb006300378daf002d998a9a07f (diff)
* ext/openssl/pkey_dh.c: corrected documentation.
* test/openssl/utils.rb: add test key for DH. * test/openssl/test_pkey_dh.rb: add tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32039 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/openssl/test_pkey_dh.rb')
-rw-r--r--test/openssl/test_pkey_dh.rb72
1 files changed, 72 insertions, 0 deletions
diff --git a/test/openssl/test_pkey_dh.rb b/test/openssl/test_pkey_dh.rb
new file mode 100644
index 0000000000..bcba400efe
--- /dev/null
+++ b/test/openssl/test_pkey_dh.rb
@@ -0,0 +1,72 @@
+require_relative 'utils'
+
+if defined?(OpenSSL)
+
+class OpenSSL::TestPKeyDH < Test::Unit::TestCase
+ def test_new
+ dh = OpenSSL::PKey::DH.new(256)
+ assert_key(dh)
+ end
+
+ def test_to_der
+ dh = OpenSSL::PKey::DH.new(256)
+ der = dh.to_der
+ dh2 = OpenSSL::PKey::DH.new(der)
+ assert_equal_params(dh, dh2)
+ assert_no_key(dh2)
+ end
+
+ def test_to_pem
+ dh = OpenSSL::PKey::DH.new(256)
+ pem = dh.to_pem
+ dh2 = OpenSSL::PKey::DH.new(pem)
+ assert_equal_params(dh, dh2)
+ assert_no_key(dh2)
+ end
+
+ def test_public_key
+ dh = OpenSSL::PKey::DH.new(256)
+ public_key = dh.public_key
+ assert_no_key(public_key) #implies public_key.public? is false!
+ assert_equal(dh.to_der, public_key.to_der)
+ assert_equal(dh.to_pem, public_key.to_pem)
+ end
+
+ def test_generate_key
+ dh = OpenSSL::TestUtils::TEST_KEY_DH512.public_key # creates a copy
+ assert_no_key(dh)
+ dh.generate_key!
+ assert_key(dh)
+ end
+
+ def test_key_exchange
+ dh = OpenSSL::TestUtils::TEST_KEY_DH512
+ dh2 = dh.public_key
+ dh.generate_key!
+ dh2.generate_key!
+ assert_equal(dh.compute_key(dh2.pub_key), dh2.compute_key(dh.pub_key))
+ end
+
+ private
+
+ def assert_equal_params(dh1, dh2)
+ assert_equal(dh1.g, dh2.g)
+ assert_equal(dh1.p, dh2.p)
+ end
+
+ def assert_no_key(dh)
+ assert_equal(false, dh.public?)
+ assert_equal(false, dh.private?)
+ assert_equal(nil, dh.pub_key)
+ assert_equal(nil, dh.priv_key)
+ end
+
+ def assert_key(dh)
+ assert(dh.public?)
+ assert(dh.private?)
+ assert(dh.pub_key)
+ assert(dh.priv_key)
+ end
+end
+
+end