Skip to content

Commit

Permalink
[CI] test_puma_localhost_authority.rb - use PumaSocket, remove net/ht…
Browse files Browse the repository at this point in the history
…tp (#3244)
  • Loading branch information
MSP-Greg committed Oct 2, 2023
1 parent 9bb65fa commit 87ad26a
Showing 1 changed file with 16 additions and 34 deletions.
50 changes: 16 additions & 34 deletions test/test_puma_localhost_authority.rb
Expand Up @@ -7,39 +7,30 @@

if ::Puma::HAS_SSL && !Puma::IS_JRUBY
require "puma/minissl"
require "net/http"

# net/http (loaded in helper) does not necessarily load OpenSSL
require_relative "helpers/test_puma/puma_socket"
require "openssl" unless Object.const_defined? :OpenSSL
end

class TestPumaLocalhostAuthority < Minitest::Test
parallelize_me!
include TestPuma
include TestPuma::PumaSocket

def setup
@http = nil
@server = nil
end

def teardown
@http.finish if @http&.started?
@server&.stop true
end

# yields ctx to block, use for ctx setup & configuration
def start_server
@host = "localhost"
app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }

@log_writer = SSLLogWriterHelper.new STDOUT, STDERR
@server = Puma::Server.new app, nil, {log_writer: @log_writer}
@server.add_ssl_listener @host, 0, nil
@http = Net::HTTP.new @host, @server.connected_ports[0]

@http.use_ssl = true
# Disabling verification since its self signed
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
# @http.verify_mode = OpenSSL::SSL::VERIFY_NONE

@server.add_ssl_listener LOCALHOST, 0, nil
@bind_port = @server.connected_ports[0]
@server.run
end

Expand All @@ -52,43 +43,34 @@ def test_localhost_authority_file_generated
assert_equal(File.exist?(File.join(Localhost::Authority.path,"localhost.crt")), true)
end

end if ::Puma::HAS_SSL && !Puma::IS_JRUBY
end if ::Puma::HAS_SSL && !Puma::IS_JRUBY

class TestPumaSSLLocalhostAuthority < Minitest::Test
def test_self_signed_by_localhost_authority
@host = "localhost"
include TestPuma
include TestPuma::PumaSocket

def test_self_signed_by_localhost_authority
app = lambda { |env| [200, {}, [env['rack.url_scheme']]] }

@log_writer = SSLLogWriterHelper.new STDOUT, STDERR

@server = Puma::Server.new app, nil, {log_writer: @log_writer}
@server.app = app

@server.add_ssl_listener @host, 0,nil

@http = Net::HTTP.new @host, @server.connected_ports[0]
@http.use_ssl = true
@server.add_ssl_listener LOCALHOST, 0, nil
@bind_port = @server.connected_ports[0]

OpenSSL::PKey::RSA.new File.read(File.join(Localhost::Authority.path,"localhost.key"))
local_authority_crt = OpenSSL::X509::Certificate.new File.read(File.join(Localhost::Authority.path,"localhost.crt"))

@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
@server.run
@cert = nil
cert = nil
begin
@http.start do
req = Net::HTTP::Get.new "/", {}
@http.request(req)
@cert = @http.peer_cert
end
cert = send_http(host: LOCALHOST, ctx: new_ctx).peer_cert
rescue OpenSSL::SSL::SSLError, EOFError, Errno::ECONNRESET
# Errno::ECONNRESET TruffleRuby
# closes socket if open, may not close on error
@http.send :do_finish
end
sleep 0.1

assert_equal(@cert.to_pem, local_authority_crt.to_pem)
assert_equal(cert.to_pem, local_authority_crt.to_pem)
end
end if ::Puma::HAS_SSL && !Puma::IS_JRUBY
end if ::Puma::HAS_SSL && !Puma::IS_JRUBY

0 comments on commit 87ad26a

Please sign in to comment.