Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making parse_config accepts a String like Ruby MRI #303

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/openssl/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def parse(string)
# Raises a ConfigError on invalid configuration data.
def parse_config(io)
begin
parse_config_lines(io)
if io.instance_of?(String)
parse(io)
else
parse_config_lines(io)
end
rescue => error
raise ConfigError, "error in line #{io.lineno}: " + error.message
end
Expand Down Expand Up @@ -414,6 +418,8 @@ def sections
@data.keys
end

alias keys sections

##
# Get the parsable form of the current configuration
#
Expand Down
50 changes: 50 additions & 0 deletions src/test/ruby/test_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# coding: US-ASCII
require File.expand_path('test_helper', File.dirname(__FILE__))
require "tempfile"

class TestSSL < TestCase
def setup
super
file = Tempfile.open("openssl.cnf")
file << <<__EOD__
HOME = .
[ ca ]
default_ca = CA_default
[ CA_default ]
dir = ./demoCA
certs = ./certs
__EOD__
file.close
@tmpfile = file
@it = OpenSSL::Config.new(file.path)
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_config
ret = OpenSSL::Config.parse_config(@it.to_s)

assert_equal(@it.sections.sort, ret.keys.sort)
assert_equal(@it["default"], ret["default"])
end

def test_sections
assert_equal(['CA_default', 'ca', 'default'], @it.sections.sort)
Tempfile.create("openssl.cnf") { |f|
f.write File.read(@tmpfile.path)
f.puts "[ new_section ]"
f.puts "foo = bar"
f.puts "[ empty_section ]"
f.close

c = OpenSSL::Config.new(f.path)
assert_equal(['CA_default', 'ca', 'default', 'empty_section', 'new_section'],
c.sections.sort)
}
end
end
Loading