1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/openssl/test_config.c: added tests for all Config methods.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@28575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nahi 2010-07-08 04:44:58 +00:00
parent d4d1388ad8
commit 6b225d142d
4 changed files with 143 additions and 12 deletions

View file

@ -1,3 +1,7 @@
Thu Jul 8 13:43:13 2010 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
* test/openssl/test_config.c: added tests for all Config methods.
Wed Jul 7 13:24:24 2010 NAKAMURA Usaku <usa@ruby-lang.org>
* file.c (ruby_find_basename): set correct baselen.

View file

@ -1,7 +1,134 @@
require 'openssl'
require "test/unit"
require 'tempfile'
require File.join(File.dirname(__FILE__), "utils.rb")
class OpenSSL::TestConfig < Test::Unit::TestCase
def setup
file = Tempfile.open("openssl.cnf")
file << <<__EOD__
HOME = .
[ ca ]
default_ca = CA_default
[ CA_default ]
dir = ./demoCA
certs = ./certs
__EOD__
file.close
@it = OpenSSL::Config.new(file.path)
end
def test_initialize
c = OpenSSL::Config.new
assert_equal("", c.to_s)
assert_equal([], c.sections)
end
def test_initialize_with_empty_file
file = Tempfile.open("openssl.cnf")
file.close
c = OpenSSL::Config.new(file.path)
assert_equal("[ default ]\n\n", c.to_s)
assert_equal(['default'], c.sections)
end
def test_initialize_with_example_file
assert_equal(['CA_default', 'ca', 'default'], @it.sections.sort)
end
def test_get_value
assert_equal('CA_default', @it.get_value('ca', 'default_ca'))
assert_equal(nil, @it.get_value('ca', 'no such key'))
assert_equal(nil, @it.get_value('no such section', 'no such key'))
assert_equal('.', @it.get_value('', 'HOME'))
assert_raise(TypeError) do
@it.get_value(nil, 'HOME') # not allowed unlike Config#value
end
end
def test_value
# supress deprecation warnings
OpenSSL::TestUtils.silent do
assert_equal('CA_default', @it.value('ca', 'default_ca'))
assert_equal(nil, @it.value('ca', 'no such key'))
assert_equal(nil, @it.value('no such section', 'no such key'))
assert_equal('.', @it.value('', 'HOME'))
assert_equal('.', @it.value(nil, 'HOME'))
assert_equal('.', @it.value('HOME'))
end
end
def test_aref
assert_equal({'HOME' => '.'}, @it['default'])
assert_equal({'dir' => './demoCA', 'certs' => './certs'}, @it['CA_default'])
assert_equal({}, @it['no_such_section'])
assert_equal({}, @it[''])
end
def test_section
OpenSSL::TestUtils.silent do
assert_equal({'HOME' => '.'}, @it.section('default'))
assert_equal({'dir' => './demoCA', 'certs' => './certs'}, @it.section('CA_default'))
assert_equal({}, @it.section('no_such_section'))
assert_equal({}, @it.section(''))
end
end
def test_sections
assert_equal(['CA_default', 'ca', 'default'], @it.sections.sort)
@it['new_section'] = {'foo' => 'bar'}
assert_equal(['CA_default', 'ca', 'default', 'new_section'], @it.sections.sort)
@it['new_section'] = {}
assert_equal(['CA_default', 'ca', 'default', 'new_section'], @it.sections.sort)
end
def test_add_value
c = OpenSSL::Config.new
assert_equal("", c.to_s)
# add key
c.add_value('default', 'foo', 'bar')
assert_equal("[ default ]\nfoo=bar\n\n", c.to_s)
# add another key
c.add_value('default', 'baz', 'qux')
assert_equal("[ default ]\nfoo=bar\nbaz=qux\n\n", c.to_s)
# update the value
c.add_value('default', 'baz', 'quxxx')
assert_equal("[ default ]\nfoo=bar\nbaz=quxxx\n\n", c.to_s)
# add section and key
c.add_value('section', 'foo', 'bar')
assert_equal("[ default ]\nfoo=bar\nbaz=quxxx\n\n[ section ]\nfoo=bar\n\n", c.to_s)
end
def test_aset
@it['foo'] = {'bar' => 'baz'}
assert_equal({'bar' => 'baz'}, @it['foo'])
@it['foo'] = {'bar' => 'qux', 'baz' => 'quxx'}
assert_equal({'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
# OpenSSL::Config is add only for now.
@it['foo'] = {'foo' => 'foo'}
assert_equal({'foo' => 'foo', 'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
# you cannot override or remove any section and key.
@it['foo'] = {}
assert_equal({'foo' => 'foo', 'bar' => 'qux', 'baz' => 'quxx'}, @it['foo'])
end
def test_each
# each returns [section, key, value] array.
ary = @it.map { |e| e }.sort { |a, b| a[0] <=> b[0] }
assert_equal(4, ary.size)
assert_equal('CA_default', ary[0][0])
assert_equal('CA_default', ary[1][0])
assert_equal(["ca", "default_ca", "CA_default"], ary[2])
assert_equal(["default", "HOME", "."], ary[3])
end
def test_inspect
assert_nothing_raised do
@it.inspect
end
end
def test_freeze
c = OpenSSL::Config.new
c['foo'] = [['key', 'value']]

View file

@ -151,17 +151,8 @@ class OpenSSL::TestPKCS7 < Test::Unit::TestCase
assert_equal(data, p7.decrypt(@rsa1024, @ee2_cert))
end
def silent
begin
back, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = back if back
end
end
def test_signed_pkcs7_pkcs7
silent do
OpenSSL::TestUtils.silent do
store = OpenSSL::X509::Store.new
store.add_cert(@ca_cert)
ca_certs = [@ca_cert]
@ -224,7 +215,7 @@ class OpenSSL::TestPKCS7 < Test::Unit::TestCase
end
def test_detached_sign_pkcs7_pkcs7
silent do
OpenSSL::TestUtils.silent do
store = OpenSSL::X509::Store.new
store.add_cert(@ca_cert)
ca_certs = [@ca_cert]
@ -250,7 +241,7 @@ class OpenSSL::TestPKCS7 < Test::Unit::TestCase
end
def test_enveloped_pkcs7_pkcs7
silent do
OpenSSL::TestUtils.silent do
if OpenSSL::OPENSSL_VERSION_NUMBER <= 0x0090704f
# PKCS7_encrypt() of OpenSSL-0.9.7d goes to SEGV.
# http://www.mail-archive.com/openssl-dev@openssl.org/msg17376.html

View file

@ -132,4 +132,13 @@ Q1VB8qkJN7rA7/2HrCR3gTsWNb1YhAsnFsoeRscC+LxXoXi9OAIUBG98h4tilg6S
pkvalue = publickey.value
OpenSSL::Digest::SHA1.hexdigest(pkvalue).scan(/../).join(":").upcase
end
def silent
begin
back, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = back if back
end
end
end