From 9319b74664ccf5e8173b14b0fa51068567ed1750 Mon Sep 17 00:00:00 2001 From: Edipo Federle Date: Mon, 20 May 2024 11:26:22 -0300 Subject: [PATCH 1/4] Making parse_config accepts a String like Ruby MRI --- lib/openssl/config.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/openssl/config.rb b/lib/openssl/config.rb index 9a0b7874..cea19ba8 100644 --- a/lib/openssl/config.rb +++ b/lib/openssl/config.rb @@ -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 From a6823c9abceeb5a112b617e6708b359ab469500e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89dipo=20F=C3=A9derle?= Date: Mon, 10 Jun 2024 12:53:04 -0300 Subject: [PATCH 2/4] Update lib/openssl/config.rb Co-authored-by: Karol Bucek --- lib/openssl/config.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/openssl/config.rb b/lib/openssl/config.rb index cea19ba8..191ee8e3 100644 --- a/lib/openssl/config.rb +++ b/lib/openssl/config.rb @@ -52,7 +52,7 @@ def parse(string) # Raises a ConfigError on invalid configuration data. def parse_config(io) begin - if(io.instance_of?(String)) + if io.instance_of?(String) parse(io) else parse_config_lines(io) From 633fa12e7b0ee647373bfc085d7a2f42ec39080b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89dipo=20F=C3=A9derle?= Date: Mon, 10 Jun 2024 13:42:10 -0300 Subject: [PATCH 3/4] Alias method Config.keys to Config.sections --- lib/openssl/config.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/openssl/config.rb b/lib/openssl/config.rb index 191ee8e3..5e4c51f7 100644 --- a/lib/openssl/config.rb +++ b/lib/openssl/config.rb @@ -418,6 +418,8 @@ def sections @data.keys end + alias keys sections + ## # Get the parsable form of the current configuration # From 3ecfd4f9ca778a5bcfd6e4855e8089bd82f7dc71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89dipo=20F=C3=A9derle?= Date: Mon, 10 Jun 2024 13:42:44 -0300 Subject: [PATCH 4/4] Add some testsfor config parsing --- src/test/ruby/test_config.rb | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/ruby/test_config.rb diff --git a/src/test/ruby/test_config.rb b/src/test/ruby/test_config.rb new file mode 100644 index 00000000..94868ed8 --- /dev/null +++ b/src/test/ruby/test_config.rb @@ -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 \ No newline at end of file