From 2b2ae322cd9b001f3c568e2b5cb9d03717f987e5 Mon Sep 17 00:00:00 2001 From: Herwin Date: Thu, 15 May 2025 14:53:44 +0200 Subject: [PATCH 1/4] Various layout cleanups in test_webrick_server These remarks originate from the review of the tests of the CGI server, but since most of that code was copied from this file, clean this up here as well. If we ever want to use shared logic for these files, it's preferable to keep the differences to a minimum. --- test/test_webrick_server.rb | 62 ++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/test/test_webrick_server.rb b/test/test_webrick_server.rb index 98a300b..65aa838 100644 --- a/test/test_webrick_server.rb +++ b/test/test_webrick_server.rb @@ -24,11 +24,11 @@ class << s; self end.send(:define_method, :service) {|req, res| super(req, res) } - s.add_handler("test.add") do |a,b| + s.add_handler("test.add") do |a, b| a + b end - s.add_handler("test.div") do |a,b| + s.add_handler("test.div") do |a, b| if b == 0 raise XMLRPC::FaultException.new(1, "division by zero") else @@ -65,24 +65,23 @@ def setup_http_server_option(use_ssl) def test_client_server # NOTE: I don't enable SSL testing as this hangs - [false].each do |use_ssl| - option = setup_http_server_option(use_ssl) - with_server(option, method(:create_servlet)) {|addr| - @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) - @s.user = 'admin' - @s.password = 'admin' - silent do - do_test - end - @s.http.finish - @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) - @s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789' - @s.password = 'guest' - silent do - do_test - end - @s.http.finish - } + use_ssl = false + option = setup_http_server_option(use_ssl) + with_server(option, method(:create_servlet)) do |addr| + @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) + @s.user = 'admin' + @s.password = 'admin' + silent do + do_test + end + @s.http.finish + @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) + @s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789' + @s.password = 'guest' + silent do + do_test + end + @s.http.finish end end @@ -97,37 +96,38 @@ def silent def do_test # simple call - assert_equal 9, @s.call('test.add', 4, 5) + assert_equal(9, @s.call('test.add', 4, 5)) # fault exception assert_raise(XMLRPC::FaultException) { @s.call('test.div', 1, 0) } # fault exception via call2 ok, param = @s.call2('test.div', 1, 0) - assert_equal false, ok - assert_instance_of XMLRPC::FaultException, param - assert_equal 1, param.faultCode - assert_equal 'division by zero', param.faultString + assert_equal(false, ok) + assert_instance_of(XMLRPC::FaultException, param) + assert_equal(1, param.faultCode) + assert_equal('division by zero', param.faultString) # call2 without fault exception ok, param = @s.call2('test.div', 10, 5) - assert_equal true, ok - assert_equal param, 2 + assert_equal(true, ok) + assert_equal(param, 2) # introspection - assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods") + assert_equal(["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], + @s.call("system.listMethods")) # default handler (missing handler) ok, param = @s.call2('test.nonexisting') - assert_equal false, ok + assert_equal(false, ok) assert_equal(-99, param.faultCode) # default handler (wrong number of arguments) ok, param = @s.call2('test.add', 1, 2, 3) - assert_equal false, ok + assert_equal(false, ok) assert_equal(-99, param.faultCode) # multibyte characters - assert_equal "あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ") + assert_equal("あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ")) end end From 2cf80da555d9a708544a114b373b23305d92887a Mon Sep 17 00:00:00 2001 From: Herwin Date: Tue, 29 Apr 2025 15:36:12 +0200 Subject: [PATCH 2/4] Add unit tests for XMLRPC::CGIServer --- test/test_cgi_server.rb | 138 ++++++++++++++++++++++++++++++++++++++++ test/webrick_testing.rb | 4 +- 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 test/test_cgi_server.rb diff --git a/test/test_cgi_server.rb b/test/test_cgi_server.rb new file mode 100644 index 0000000..914c09b --- /dev/null +++ b/test/test_cgi_server.rb @@ -0,0 +1,138 @@ +# coding: utf-8 +# frozen_string_literal: false + +require 'test/unit' +require 'webrick' +require_relative 'webrick_testing' +require 'tempfile' +require "xmlrpc/server" +require 'xmlrpc/client' + +class Test_CGIServer < Test::Unit::TestCase + include WEBrick_Testing + + def setup_http_server_option(use_ssl) + option = { + :BindAddress => "localhost", + :Port => 0, + :SSLEnable => use_ssl, + } + if use_ssl + require 'webrick/https' + option.update( + :SSLVerifyClient => ::OpenSSL::SSL::VERIFY_NONE, + :SSLCertName => [] + ) + end + + option + end + + def test_client_server + # NOTE: I don't enable SSL testing as this hangs + Tempfile.create("cgi-bin") do |tempfile| + tempfile.write(cgi_bin_script) + tempfile.close + File.chmod(0755, tempfile.path) + + [false].each do |use_ssl| + option = setup_http_server_option(use_ssl) + with_server(option, WEBrick::HTTPServlet::CGIHandler, tempfile.path) {|addr| + @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) + @s.user = 'admin' + @s.password = 'admin' + silent do + do_test + end + @s.http.finish + @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) + @s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789' + @s.password = 'guest' + silent do + do_test + end + @s.http.finish + } + end + end + end + + def silent + begin + back, $VERBOSE = $VERBOSE, nil + yield + ensure + $VERBOSE = back + end + end + + def do_test + # simple call + assert_equal 9, @s.call('test.add', 4, 5) + + # fault exception + assert_raise(XMLRPC::FaultException) { @s.call('test.div', 1, 0) } + + # fault exception via call2 + ok, param = @s.call2('test.div', 1, 0) + assert_equal false, ok + assert_instance_of XMLRPC::FaultException, param + assert_equal 1, param.faultCode + assert_equal 'division by zero', param.faultString + + # call2 without fault exception + ok, param = @s.call2('test.div', 10, 5) + assert_equal true, ok + assert_equal param, 2 + + # introspection + assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods") + + # default handler (missing handler) + ok, param = @s.call2('test.nonexisting') + assert_equal false, ok + assert_equal(-99, param.faultCode) + + # default handler (wrong number of arguments) + ok, param = @s.call2('test.add', 1, 2, 3) + assert_equal false, ok + assert_equal(-99, param.faultCode) + + # multibyte characters + assert_equal "あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ") + end + + def cgi_bin_script + <<~RUBY + #!/usr/bin/env ruby + # frozen_string_literal: true + + $LOAD_PATH << #{File.expand_path("../lib", __dir__).inspect} + + require "xmlrpc/server" + + s = XMLRPC::CGIServer.new + + s.add_handler("test.add") do |a,b| + a + b + end + + s.add_handler("test.div") do |a,b| + if b == 0 + raise XMLRPC::FaultException.new(1, "division by zero") + else + a / b + end + end + + s.set_default_handler do |name, *args| + raise XMLRPC::FaultException.new(-99, "Method #{name} missing" + + " or wrong number of parameters!") + end + + s.add_introspection + + s.serve + RUBY + end +end diff --git a/test/webrick_testing.rb b/test/webrick_testing.rb index 2d6d198..48ce4a5 100644 --- a/test/webrick_testing.rb +++ b/test/webrick_testing.rb @@ -35,12 +35,12 @@ def start_server(logger, config={}) addr end - def with_server(config, servlet) + def with_server(config, servlet, *args) log = [] logger = WEBrick::Log.new(log, WEBrick::BasicLog::WARN) addr = start_server(logger, config) {|w| servlet = servlet.call(w) if servlet.respond_to? :call - w.mount('/RPC2', servlet) + w.mount('/RPC2', servlet, *args) } begin From cb644da8dd5b22238ce40f5b48f8b954cd79ee08 Mon Sep 17 00:00:00 2001 From: Herwin Date: Tue, 29 Apr 2025 16:01:08 +0200 Subject: [PATCH 3/4] Skip tests on Windows The do not work out of the box, I'm sure this can be fixed but I have no idea how to do that. --- test/test_cgi_server.rb | 70 +++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/test/test_cgi_server.rb b/test/test_cgi_server.rb index 914c09b..316051c 100644 --- a/test/test_cgi_server.rb +++ b/test/test_cgi_server.rb @@ -29,30 +29,31 @@ def setup_http_server_option(use_ssl) end def test_client_server - # NOTE: I don't enable SSL testing as this hangs + omit("The CGI file does not work on Windows") if Gem.win_platform? + Tempfile.create("cgi-bin") do |tempfile| tempfile.write(cgi_bin_script) tempfile.close File.chmod(0755, tempfile.path) - [false].each do |use_ssl| - option = setup_http_server_option(use_ssl) - with_server(option, WEBrick::HTTPServlet::CGIHandler, tempfile.path) {|addr| - @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) - @s.user = 'admin' - @s.password = 'admin' - silent do - do_test - end - @s.http.finish - @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) - @s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789' - @s.password = 'guest' - silent do - do_test - end - @s.http.finish - } + # NOTE: I don't enable SSL testing as this hangs + use_ssl = false + option = setup_http_server_option(use_ssl) + with_server(option, WEBrick::HTTPServlet::CGIHandler, tempfile.path) do |addr| + @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) + @s.user = 'admin' + @s.password = 'admin' + silent do + do_test + end + @s.http.finish + @s = XMLRPC::Client.new3(:host => addr.ip_address, :port => addr.ip_port, :use_ssl => use_ssl) + @s.user = '01234567890123456789012345678901234567890123456789012345678901234567890123456789' + @s.password = 'guest' + silent do + do_test + end + @s.http.finish end end end @@ -68,43 +69,44 @@ def silent def do_test # simple call - assert_equal 9, @s.call('test.add', 4, 5) + assert_equal(9, @s.call('test.add', 4, 5)) # fault exception assert_raise(XMLRPC::FaultException) { @s.call('test.div', 1, 0) } # fault exception via call2 ok, param = @s.call2('test.div', 1, 0) - assert_equal false, ok - assert_instance_of XMLRPC::FaultException, param - assert_equal 1, param.faultCode - assert_equal 'division by zero', param.faultString + assert_equal(false, ok) + assert_instance_of(XMLRPC::FaultException, param) + assert_equal(1, param.faultCode) + assert_equal('division by zero', param.faultString) # call2 without fault exception ok, param = @s.call2('test.div', 10, 5) - assert_equal true, ok - assert_equal param, 2 + assert_equal(true, ok) + assert_equal(param, 2) # introspection - assert_equal ["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], @s.call("system.listMethods") + assert_equal(["test.add", "test.div", "system.listMethods", "system.methodSignature", "system.methodHelp"], + @s.call("system.listMethods")) # default handler (missing handler) ok, param = @s.call2('test.nonexisting') - assert_equal false, ok + assert_equal(false, ok) assert_equal(-99, param.faultCode) # default handler (wrong number of arguments) ok, param = @s.call2('test.add', 1, 2, 3) - assert_equal false, ok + assert_equal(false, ok) assert_equal(-99, param.faultCode) # multibyte characters - assert_equal "あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ") + assert_equal("あいうえおかきくけこ", @s.call('test.add', "あいうえお", "かきくけこ")) end def cgi_bin_script <<~RUBY - #!/usr/bin/env ruby + #!#{Gem.ruby} # frozen_string_literal: true $LOAD_PATH << #{File.expand_path("../lib", __dir__).inspect} @@ -113,11 +115,11 @@ def cgi_bin_script s = XMLRPC::CGIServer.new - s.add_handler("test.add") do |a,b| + s.add_handler("test.add") do |a, b| a + b end - s.add_handler("test.div") do |a,b| + s.add_handler("test.div") do |a, b| if b == 0 raise XMLRPC::FaultException.new(1, "division by zero") else @@ -126,7 +128,7 @@ def cgi_bin_script end s.set_default_handler do |name, *args| - raise XMLRPC::FaultException.new(-99, "Method #{name} missing" + + raise XMLRPC::FaultException.new(-99, "Method \#{name} missing" + " or wrong number of parameters!") end From f172d4bf002a69387414fc79382bc66d5089ecce Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Fri, 16 May 2025 09:45:47 +0900 Subject: [PATCH 4/4] Remove needless _ --- test/test_cgi_server.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_cgi_server.rb b/test/test_cgi_server.rb index 316051c..2cbd37f 100644 --- a/test/test_cgi_server.rb +++ b/test/test_cgi_server.rb @@ -8,7 +8,7 @@ require "xmlrpc/server" require 'xmlrpc/client' -class Test_CGIServer < Test::Unit::TestCase +class TestCGIServer < Test::Unit::TestCase include WEBrick_Testing def setup_http_server_option(use_ssl)