2020-02-16 01:21:29 -05:00
|
|
|
# frozen_string_literal: true
|
2010-12-15 14:50:00 -05:00
|
|
|
require_relative 'utils'
|
2009-11-26 07:18:04 -05:00
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
if defined?(OpenSSL)
|
|
|
|
|
2016-05-18 00:07:47 -04:00
|
|
|
class OpenSSL::TestConfig < OpenSSL::TestCase
|
2010-08-19 04:22:31 -04:00
|
|
|
def setup
|
2016-12-10 03:12:02 -05:00
|
|
|
super
|
2010-08-19 04:22:31 -04:00
|
|
|
file = Tempfile.open("openssl.cnf")
|
|
|
|
file << <<__EOD__
|
|
|
|
HOME = .
|
|
|
|
[ ca ]
|
|
|
|
default_ca = CA_default
|
|
|
|
[ CA_default ]
|
|
|
|
dir = ./demoCA
|
|
|
|
certs = ./certs
|
|
|
|
__EOD__
|
|
|
|
file.close
|
2012-07-18 06:42:40 -04:00
|
|
|
@tmpfile = file
|
2010-08-19 04:22:31 -04:00
|
|
|
@it = OpenSSL::Config.new(file.path)
|
|
|
|
end
|
|
|
|
|
2012-07-18 06:42:40 -04:00
|
|
|
def teardown
|
2016-05-18 00:07:47 -04:00
|
|
|
super
|
2014-05-27 11:49:13 -04:00
|
|
|
@tmpfile.close!
|
2012-07-18 06:42:40 -04:00
|
|
|
end
|
|
|
|
|
2010-08-19 04:22:31 -04:00
|
|
|
def test_constants
|
|
|
|
assert(defined?(OpenSSL::Config::DEFAULT_CONFIG_FILE))
|
2012-10-06 10:26:29 -04:00
|
|
|
config_file = OpenSSL::Config::DEFAULT_CONFIG_FILE
|
2016-08-29 01:47:09 -04:00
|
|
|
pend "DEFAULT_CONFIG_FILE may return a wrong path on your platforms. [Bug #6830]" unless File.readable?(config_file)
|
2010-08-19 04:22:31 -04:00
|
|
|
assert_nothing_raised do
|
2012-10-06 10:26:29 -04:00
|
|
|
OpenSSL::Config.load(config_file)
|
2010-08-19 04:22:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_parse
|
|
|
|
c = OpenSSL::Config.parse('')
|
|
|
|
assert_equal("[ default ]\n\n", c.to_s)
|
|
|
|
c = OpenSSL::Config.parse(@it.to_s)
|
|
|
|
assert_equal(['CA_default', 'ca', 'default'], c.sections.sort)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_parse_format
|
|
|
|
c = OpenSSL::Config.parse(<<__EOC__)
|
|
|
|
baz =qx\t # "baz = qx"
|
|
|
|
|
|
|
|
foo::bar = baz # shortcut section::key format
|
|
|
|
default::bar = baz # ditto
|
|
|
|
a=\t \t # "a = ": trailing spaces are ignored
|
|
|
|
=b # " = b": empty key
|
|
|
|
=c # " = c": empty key (override the above line)
|
|
|
|
d= # "c = ": trailing comment is ignored
|
|
|
|
|
|
|
|
sq = 'foo''b\\'ar'
|
|
|
|
dq ="foo""''\\""
|
|
|
|
dq2 = foo""bar
|
|
|
|
esc=a\\r\\n\\b\\tb
|
|
|
|
foo\\bar = foo\\b\\\\ar
|
|
|
|
foo\\bar::foo\\bar = baz
|
|
|
|
[default1 default2]\t\t # space is allowed in section name
|
|
|
|
fo =b ar # space allowed in value
|
|
|
|
[emptysection]
|
2020-02-16 01:21:29 -05:00
|
|
|
[doller ]
|
2010-08-19 04:22:31 -04:00
|
|
|
foo=bar
|
|
|
|
bar = $(foo)
|
|
|
|
baz = 123$(default::bar)456${foo}798
|
|
|
|
qux = ${baz}
|
|
|
|
quxx = $qux.$qux
|
|
|
|
__EOC__
|
2020-02-16 01:21:29 -05:00
|
|
|
assert_equal(['default', 'default1 default2', 'doller', 'emptysection', 'foo', 'foo\\bar'], c.sections.sort)
|
2010-08-19 04:22:31 -04:00
|
|
|
assert_equal(['', 'a', 'bar', 'baz', 'd', 'dq', 'dq2', 'esc', 'foo\\bar', 'sq'], c['default'].keys.sort)
|
|
|
|
assert_equal('c', c['default'][''])
|
|
|
|
assert_equal('', c['default']['a'])
|
|
|
|
assert_equal('qx', c['default']['baz'])
|
|
|
|
assert_equal('', c['default']['d'])
|
|
|
|
assert_equal('baz', c['default']['bar'])
|
|
|
|
assert_equal("foob'ar", c['default']['sq'])
|
|
|
|
assert_equal("foo''\"", c['default']['dq'])
|
|
|
|
assert_equal("foobar", c['default']['dq2'])
|
|
|
|
assert_equal("a\r\n\b\tb", c['default']['esc'])
|
|
|
|
assert_equal("foo\b\\ar", c['default']['foo\\bar'])
|
|
|
|
assert_equal('baz', c['foo']['bar'])
|
|
|
|
assert_equal('baz', c['foo\\bar']['foo\\bar'])
|
|
|
|
assert_equal('b ar', c['default1 default2']['fo'])
|
|
|
|
|
2020-02-16 01:21:29 -05:00
|
|
|
# dolloer
|
|
|
|
assert_equal('bar', c['doller']['foo'])
|
|
|
|
assert_equal('bar', c['doller']['bar'])
|
|
|
|
assert_equal('123baz456bar798', c['doller']['baz'])
|
|
|
|
assert_equal('123baz456bar798', c['doller']['qux'])
|
|
|
|
assert_equal('123baz456bar798.123baz456bar798', c['doller']['quxx'])
|
2010-08-19 04:22:31 -04:00
|
|
|
|
|
|
|
excn = assert_raise(OpenSSL::ConfigError) do
|
|
|
|
OpenSSL::Config.parse("foo = $bar")
|
|
|
|
end
|
|
|
|
assert_equal("error in line 1: variable has no value", excn.message)
|
|
|
|
|
|
|
|
excn = assert_raise(OpenSSL::ConfigError) do
|
|
|
|
OpenSSL::Config.parse("foo = $(bar")
|
|
|
|
end
|
|
|
|
assert_equal("error in line 1: no close brace", excn.message)
|
|
|
|
|
|
|
|
excn = assert_raise(OpenSSL::ConfigError) do
|
|
|
|
OpenSSL::Config.parse("f o =b ar # no space in key")
|
|
|
|
end
|
|
|
|
assert_equal("error in line 1: missing equal sign", excn.message)
|
|
|
|
|
|
|
|
excn = assert_raise(OpenSSL::ConfigError) do
|
|
|
|
OpenSSL::Config.parse(<<__EOC__)
|
|
|
|
# comment 1 # comments
|
|
|
|
|
|
|
|
#
|
|
|
|
# comment 2
|
|
|
|
\t#comment 3
|
|
|
|
[second ]\t
|
|
|
|
[third # section not terminated
|
|
|
|
__EOC__
|
|
|
|
end
|
|
|
|
assert_equal("error in line 7: missing close square bracket", excn.message)
|
|
|
|
end
|
|
|
|
|
2020-03-09 05:26:19 -04:00
|
|
|
def test_s_parse_include
|
|
|
|
in_tmpdir("ossl-config-include-test") do |dir|
|
|
|
|
Dir.mkdir("child")
|
|
|
|
File.write("child/a.conf", <<~__EOC__)
|
|
|
|
[default]
|
|
|
|
file-a = a.conf
|
|
|
|
[sec-a]
|
|
|
|
a = 123
|
|
|
|
__EOC__
|
|
|
|
File.write("child/b.cnf", <<~__EOC__)
|
|
|
|
[default]
|
|
|
|
file-b = b.cnf
|
|
|
|
[sec-b]
|
|
|
|
b = 123
|
|
|
|
__EOC__
|
|
|
|
File.write("include-child.conf", <<~__EOC__)
|
|
|
|
key_outside_section = value_a
|
|
|
|
.include child
|
|
|
|
__EOC__
|
|
|
|
|
|
|
|
include_file = <<~__EOC__
|
|
|
|
[default]
|
|
|
|
file-main = unnamed
|
|
|
|
[sec-main]
|
|
|
|
main = 123
|
|
|
|
.include = include-child.conf
|
|
|
|
__EOC__
|
|
|
|
|
|
|
|
# Include a file by relative path
|
|
|
|
c1 = OpenSSL::Config.parse(include_file)
|
|
|
|
assert_equal(["default", "sec-a", "sec-b", "sec-main"], c1.sections.sort)
|
|
|
|
assert_equal(["file-main", "file-a", "file-b"], c1["default"].keys)
|
|
|
|
assert_equal({"a" => "123"}, c1["sec-a"])
|
|
|
|
assert_equal({"b" => "123"}, c1["sec-b"])
|
|
|
|
assert_equal({"main" => "123", "key_outside_section" => "value_a"}, c1["sec-main"])
|
|
|
|
|
|
|
|
# Relative paths are from the working directory
|
|
|
|
assert_raise(OpenSSL::ConfigError) do
|
|
|
|
Dir.chdir("child") { OpenSSL::Config.parse(include_file) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-19 04:22:31 -04:00
|
|
|
def test_s_load
|
|
|
|
# alias of new
|
|
|
|
c = OpenSSL::Config.load
|
|
|
|
assert_equal("", c.to_s)
|
|
|
|
assert_equal([], c.sections)
|
|
|
|
#
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("openssl.cnf") {|file|
|
|
|
|
file.close
|
|
|
|
c = OpenSSL::Config.load(file.path)
|
|
|
|
assert_equal("[ default ]\n\n", c.to_s)
|
|
|
|
assert_equal(['default'], c.sections)
|
|
|
|
}
|
2010-08-19 04:22:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize
|
|
|
|
c = OpenSSL::Config.new
|
|
|
|
assert_equal("", c.to_s)
|
|
|
|
assert_equal([], c.sections)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_with_empty_file
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("openssl.cnf") {|file|
|
|
|
|
file.close
|
|
|
|
c = OpenSSL::Config.new(file.path)
|
|
|
|
assert_equal("[ default ]\n\n", c.to_s)
|
|
|
|
assert_equal(['default'], c.sections)
|
|
|
|
}
|
2010-08-19 04:22:31 -04:00
|
|
|
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
|
|
|
|
# fallback to 'default' ugly...
|
|
|
|
assert_equal('.', @it.get_value('unknown', 'HOME'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_value_ENV
|
|
|
|
key = ENV.keys.first
|
|
|
|
assert_not_nil(key) # make sure we have at least one ENV var.
|
|
|
|
assert_equal(ENV[key], @it.get_value('ENV', key))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_value
|
2015-12-24 18:23:46 -05:00
|
|
|
# suppress deprecation warnings
|
2017-09-03 08:35:27 -04:00
|
|
|
EnvUtil.suppress_warning do
|
2010-08-19 04:22:31 -04:00
|
|
|
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'))
|
|
|
|
# fallback to 'default' ugly...
|
|
|
|
assert_equal('.', @it.value('unknown', 'HOME'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_value_ENV
|
2017-09-03 08:35:27 -04:00
|
|
|
EnvUtil.suppress_warning do
|
2010-08-19 04:22:31 -04:00
|
|
|
key = ENV.keys.first
|
|
|
|
assert_not_nil(key) # make sure we have at least one ENV var.
|
|
|
|
assert_equal(ENV[key], @it.value('ENV', key))
|
|
|
|
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
|
2017-09-03 08:35:27 -04:00
|
|
|
EnvUtil.suppress_warning do
|
2010-08-19 04:22:31 -04:00
|
|
|
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)
|
2020-03-09 05:26:19 -04:00
|
|
|
# OpenSSL::Config#[]= is deprecated
|
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
@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
|
2010-08-19 04:22:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_value
|
2020-03-09 05:26:19 -04:00
|
|
|
# OpenSSL::Config#add_value is deprecated
|
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
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('bar', c['default']['foo'])
|
|
|
|
assert_equal('qux', c['default']['baz'])
|
|
|
|
# update the value
|
|
|
|
c.add_value('default', 'baz', 'quxxx')
|
|
|
|
assert_equal('bar', c['default']['foo'])
|
|
|
|
assert_equal('quxxx', c['default']['baz'])
|
|
|
|
# add section and key
|
|
|
|
c.add_value('section', 'foo', 'bar')
|
|
|
|
assert_equal('bar', c['default']['foo'])
|
|
|
|
assert_equal('quxxx', c['default']['baz'])
|
|
|
|
assert_equal('bar', c['section']['foo'])
|
|
|
|
end
|
2010-08-19 04:22:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_aset
|
2020-03-09 05:26:19 -04:00
|
|
|
# OpenSSL::Config#[]= is deprecated
|
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
@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
|
2010-08-19 04:22:31 -04:00
|
|
|
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_to_s
|
|
|
|
c = OpenSSL::Config.parse("[empty]\n")
|
|
|
|
assert_equal("[ default ]\n\n[ empty ]\n\n", c.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect
|
|
|
|
assert_match(/#<OpenSSL::Config sections=\[.*\]>/, @it.inspect)
|
|
|
|
end
|
|
|
|
|
2009-11-26 07:18:04 -05:00
|
|
|
def test_freeze
|
2020-03-09 05:26:19 -04:00
|
|
|
@it.freeze
|
2009-11-26 07:18:04 -05:00
|
|
|
|
2020-03-09 05:26:19 -04:00
|
|
|
# Modifying OpenSSL::Config produces a warning
|
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
bug = '[ruby-core:18377]'
|
|
|
|
# RuntimeError for 1.9, TypeError for 1.8
|
|
|
|
e = assert_raise(TypeError, bug) do
|
|
|
|
@it['foo'] = [['key', 'wrong']]
|
|
|
|
end
|
|
|
|
assert_match(/can't modify/, e.message, bug)
|
2009-11-26 07:18:04 -05:00
|
|
|
end
|
|
|
|
end
|
2010-08-19 04:22:31 -04:00
|
|
|
|
|
|
|
def test_dup
|
|
|
|
assert(!@it.sections.empty?)
|
|
|
|
c = @it.dup
|
|
|
|
assert_equal(@it.sections.sort, c.sections.sort)
|
2020-03-09 05:26:19 -04:00
|
|
|
# OpenSSL::Config#[]= is deprecated
|
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
@it['newsection'] = {'a' => 'b'}
|
|
|
|
assert_not_equal(@it.sections.sort, c.sections.sort)
|
|
|
|
end
|
2010-08-19 04:22:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clone
|
|
|
|
assert(!@it.sections.empty?)
|
|
|
|
c = @it.clone
|
|
|
|
assert_equal(@it.sections.sort, c.sections.sort)
|
2020-03-09 05:26:19 -04:00
|
|
|
# OpenSSL::Config#[]= is deprecated
|
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
@it['newsection'] = {'a' => 'b'}
|
|
|
|
assert_not_equal(@it.sections.sort, c.sections.sort)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def in_tmpdir(*args)
|
|
|
|
Dir.mktmpdir(*args) do |dir|
|
|
|
|
dir = File.realpath(dir)
|
|
|
|
Dir.chdir(dir) do
|
|
|
|
yield dir
|
|
|
|
end
|
|
|
|
end
|
2010-08-19 04:22:31 -04:00
|
|
|
end
|
2017-09-03 08:35:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|