Skip to content

Commit

Permalink
pkey: add tests for PKey::{RSA,DSA,DH}#params
Browse files Browse the repository at this point in the history
Add a test case to verify the current behavior.
  • Loading branch information
rhenium committed Jun 14, 2024
1 parent c737234 commit bc61a93
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/openssl/test_pkey_dh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ def test_params_ok?
assert_equal(false, dh2.params_ok?)
end

def test_params
dh = Fixtures.pkey("dh2048_ffdhe2048")
assert_not_nil dh.p
assert_equal dh.p, dh.params["p"]
assert_equal dh.q, dh.params["q"]
assert_not_nil dh.g
assert_equal dh.g, dh.params["g"]
assert_nil dh.pub_key
assert_equal 0, dh.params["pub_key"]
assert_nil dh.priv_key
assert_equal 0, dh.params["priv_key"]

dhkey = OpenSSL::PKey.generate_key(dh)
assert_equal dh.params["p"], dhkey.params["p"]
assert_not_nil dhkey.pub_key
assert_equal dhkey.pub_key, dhkey.params["pub_key"]
assert_not_nil dhkey.priv_key
assert_equal dhkey.priv_key, dhkey.params["priv_key"]
end

def test_dup
# Parameters only
dh1 = Fixtures.pkey("dh2048_ffdhe2048")
Expand Down
21 changes: 21 additions & 0 deletions test/openssl/test_pkey_dsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,27 @@ def test_read_DSAPublicKey_pem
assert_equal(nil, key.priv_key)
end

def test_params
key = Fixtures.pkey("dsa2048")
assert_not_nil key.p
assert_equal key.p, key.params["p"]
assert_not_nil key.q
assert_equal key.q, key.params["q"]
assert_not_nil key.g
assert_equal key.g, key.params["g"]
assert_not_nil key.pub_key
assert_equal key.pub_key, key.params["pub_key"]
assert_not_nil key.priv_key
assert_equal key.priv_key, key.params["priv_key"]

pubkey = OpenSSL::PKey.read(key.public_to_der)
assert_equal key.params["p"], pubkey.params["p"]
assert_equal key.pub_key, pubkey.pub_key
assert_equal key.pub_key, pubkey.params["pub_key"]
assert_nil pubkey.priv_key
assert_equal 0, pubkey.params["priv_key"]
end

def test_dup
key = Fixtures.pkey("dsa1024")
key2 = key.dup
Expand Down
18 changes: 18 additions & 0 deletions test/openssl/test_pkey_rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,24 @@ def test_private_encoding_encrypted
assert_same_rsa rsa1024, OpenSSL::PKey.read(pem, "abcdef")
end

def test_params
key = Fixtures.pkey("rsa2048")
assert_equal 2048, key.n.num_bits
assert_equal key.n, key.params["n"]
assert_equal 65537, key.e
assert_equal key.e, key.params["e"]
[:d, :p, :q, :dmp1, :dmq1, :iqmp].each do |name|
assert_not_nil key.send(name)
assert_equal key.send(name), key.params[name.to_s]
end

pubkey = OpenSSL::PKey.read(key.public_to_der)
[:d, :p, :q, :dmp1, :dmq1, :iqmp].each do |name|
assert_nil pubkey.send(name)
assert_equal 0, pubkey.params[name.to_s]
end
end

def test_dup
key = Fixtures.pkey("rsa1024")
key2 = key.dup
Expand Down

0 comments on commit bc61a93

Please sign in to comment.