Repository: puma/puma
File: test/test_puma_localhost_authority.rb (Ruby)

Code before:
#
# helper is required first since it loads Puma, which needs to be
# loaded so HAS_SSL is defined
require_relative "helper"

if ::Puma::HAS_SSL  && !Puma.jruby?
  require "puma/events"
  require "net/http"
  require "localhost/authority"

  class SSLEventsHelper < ::Puma::Events
    attr_accessor :addr, :cert, :error

    def ssl_error(error, ssl_socket)
      self.error = error
      self.addr = ssl_socket.peeraddr.last rescue "<unknown>"
      self.cert = ssl_socket.peercert
    end
  end

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

  puts "", RUBY_DESCRIPTION, "RUBYOPT: #{ENV['RUBYOPT']}",
    "                         OpenSSL",
    "OPENSSL_LIBRARY_VERSION: #{OpenSSL::OPENSSL_LIBRARY_VERSION}",
    "        OPENSSL_VERSION: #{OpenSSL::OPENSSL_VERSION}", ""
end

class TestPumaLocalhostAuthority < Minitest::Test
  parallelize_me!
  def setup
    @http = nil
    @server = nil
  end

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

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

    @events = SSLEventsHelper.new STDOUT, STDERR

    @server = Puma::Server.new app, @events
    @server.app = app
    @port = (@server.add_ssl_listener @host, 0,nil).addr[1]

Review comment (security):
Are these tests copied from another file?

I think for this feature we should be testing:

1. If the binder is SSL but there is no SSL cert provided, a self-signed one is generated (and used)
2. The self-signed cert is signed by the local authority
3. That's probably it. We know SSL works in Puma from other tests.

Code after:
#
# helper is required first since it loads Puma, which needs to be
# loaded so HAS_SSL is defined
require_relative "helper"
require "localhost/authority"

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

  class SSLEventsHelper < ::Puma::Events
    attr_accessor :addr, :cert, :error

    def ssl_error(error, ssl_socket)
      self.error = error
      self.addr = ssl_socket.peeraddr.last rescue "<unknown>"
      self.cert = ssl_socket.peercert
    end
  end

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

class TestPumaLocalhostAuthority < Minitest::Test
  parallelize_me!
  def setup
    @http = nil
    @server = nil
  end

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

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

    @events = SSLEventsHelper.new STDOUT, STDERR
    @server = Puma::Server.new app, @events
    @server.app = app
    @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