2019-07-16 18:53:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-12 15:16:55 -04:00
|
|
|
require_relative "helper"
|
2016-11-21 09:40:56 -05:00
|
|
|
|
2016-11-22 10:05:49 -05:00
|
|
|
require "puma/binder"
|
|
|
|
require "puma/puma_http11"
|
2016-11-21 09:40:56 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
class TestBinderBase < Minitest::Test
|
2016-11-21 09:40:56 -05:00
|
|
|
def setup
|
2019-09-11 08:01:24 -04:00
|
|
|
@events = Puma::Events.strings
|
2016-11-21 09:40:56 -05:00
|
|
|
@binder = Puma::Binder.new(@events)
|
|
|
|
end
|
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
private
|
|
|
|
|
2019-09-11 08:01:24 -04:00
|
|
|
def key
|
|
|
|
@key ||= File.expand_path "../../examples/puma/puma_keypair.pem", __FILE__
|
|
|
|
end
|
|
|
|
|
|
|
|
def cert
|
|
|
|
@cert ||= File.expand_path "../../examples/puma/cert_puma.pem", __FILE__
|
|
|
|
end
|
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
def ssl_context_for_binder(binder)
|
2019-10-11 05:00:58 -04:00
|
|
|
binder.ios[0].instance_variable_get(:@ctx)
|
2019-07-16 18:53:28 -04:00
|
|
|
end
|
|
|
|
end
|
2016-11-21 09:40:56 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
class TestBinder < TestBinderBase
|
|
|
|
def test_localhost_addresses_dont_alter_listeners_for_tcp_addresses
|
2016-11-21 09:40:56 -05:00
|
|
|
@binder.parse(["tcp://localhost:10001"], @events)
|
|
|
|
|
2019-09-11 06:49:35 -04:00
|
|
|
assert_equal [], @binder.instance_variable_get(:@listeners)
|
2016-11-21 09:40:56 -05:00
|
|
|
end
|
2019-09-11 08:01:24 -04:00
|
|
|
|
|
|
|
def test_correct_zero_port
|
|
|
|
@binder.parse(["tcp://localhost:0"], @events)
|
|
|
|
|
|
|
|
m = %r!tcp://127.0.0.1:(\d+)!.match(@events.stdout.string)
|
|
|
|
port = m[1].to_i
|
|
|
|
|
|
|
|
refute_equal 0, port
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_logs_all_localhost_bindings
|
|
|
|
@binder.parse(["tcp://localhost:0"], @events)
|
|
|
|
|
|
|
|
assert_match %r!tcp://127.0.0.1:(\d+)!, @events.stdout.string
|
|
|
|
if @binder.loopback_addresses.include?("::1")
|
|
|
|
assert_match %r!tcp://\[::1\]:(\d+)!, @events.stdout.string
|
|
|
|
end
|
|
|
|
end
|
2019-09-26 06:54:15 -04:00
|
|
|
|
|
|
|
def test_correct_zero_port_ssl
|
2019-10-01 05:08:28 -04:00
|
|
|
skip("Implement in 4.3")
|
2019-09-26 06:54:15 -04:00
|
|
|
@binder.parse(["ssl://localhost:0?key=#{key}&cert=#{cert}"], @events)
|
|
|
|
|
|
|
|
stdout = @events.stdout.string
|
|
|
|
m = %r!tcp://127.0.0.1:(\d+)!.match(stdout)
|
|
|
|
port = m[1].to_i
|
|
|
|
|
|
|
|
refute_equal 0, port
|
|
|
|
assert_match %r!ssl://127.0.0.1:(\d+)!, stdout
|
|
|
|
if @binder.loopback_addresses.include?("::1")
|
|
|
|
assert_match %r!ssl://\[::1\]:(\d+)!, stdout
|
|
|
|
end
|
|
|
|
end
|
2019-09-26 06:55:23 -04:00
|
|
|
|
2019-10-01 05:33:00 -04:00
|
|
|
def test_allows_both_ssl_and_tcp
|
|
|
|
assert_parsing_logs_uri [:ssl, :tcp]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_allows_both_unix_and_tcp
|
|
|
|
assert_parsing_logs_uri [:unix, :tcp]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_allows_both_tcp_and_unix
|
|
|
|
assert_parsing_logs_uri [:tcp, :unix]
|
|
|
|
end
|
|
|
|
|
2019-10-01 10:01:45 -04:00
|
|
|
def test_pre_existing_unix
|
|
|
|
skip UNIX_SKT_MSG unless UNIX_SKT_EXIST
|
|
|
|
unix_path = "test/#{name}_server.sock"
|
|
|
|
|
2019-10-01 12:28:33 -04:00
|
|
|
File.open(unix_path, mode: 'wb') { |f| f.puts 'pre existing' }
|
2019-10-01 10:01:45 -04:00
|
|
|
@binder.parse ["unix://#{unix_path}"], @events
|
|
|
|
|
|
|
|
assert_match %r!unix://#{unix_path}!, @events.stdout.string
|
|
|
|
|
|
|
|
refute_includes @binder.instance_variable_get(:@unix_paths), unix_path
|
|
|
|
|
2019-10-01 12:28:33 -04:00
|
|
|
@binder.close_unix_paths
|
|
|
|
|
|
|
|
assert File.exist?(unix_path)
|
|
|
|
|
2019-10-01 10:01:45 -04:00
|
|
|
ensure
|
|
|
|
if UNIX_SKT_EXIST
|
|
|
|
File.unlink unix_path if File.exist? unix_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-01 05:33:00 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def assert_parsing_logs_uri(order = [:unix, :tcp])
|
|
|
|
skip UNIX_SKT_MSG if order.include?(:unix) && !UNIX_SKT_EXIST
|
|
|
|
|
|
|
|
prepared_paths = {
|
|
|
|
ssl: "ssl://127.0.0.1:#{UniquePort.call}?key=#{key}&cert=#{cert}",
|
|
|
|
tcp: "tcp://127.0.0.1:#{UniquePort.call}",
|
|
|
|
unix: "unix://test/#{name}_server.sock"
|
|
|
|
}
|
|
|
|
|
|
|
|
tested_paths = [prepared_paths[order[0]], prepared_paths[order[1]]]
|
2019-09-26 06:55:23 -04:00
|
|
|
|
2019-10-01 05:33:00 -04:00
|
|
|
@binder.parse(tested_paths, @events)
|
2019-09-26 06:55:23 -04:00
|
|
|
stdout = @events.stdout.string
|
|
|
|
|
2019-10-01 05:33:00 -04:00
|
|
|
assert stdout.include?(prepared_paths[order[0]]), "\n#{stdout}\n"
|
|
|
|
assert stdout.include?(prepared_paths[order[1]]), "\n#{stdout}\n"
|
|
|
|
ensure
|
|
|
|
@binder.close_unix_paths if order.include?(:unix) && UNIX_SKT_EXIST
|
2019-09-26 06:55:23 -04:00
|
|
|
end
|
2019-07-16 18:53:28 -04:00
|
|
|
end
|
2016-11-21 09:40:56 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
class TestBinderJRuby < TestBinderBase
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
skip_unless :jruby
|
|
|
|
end
|
2016-11-21 09:40:56 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
def test_binder_parses_jruby_ssl_options
|
|
|
|
keystore = File.expand_path "../../examples/puma/keystore.jks", __FILE__
|
|
|
|
ssl_cipher_list = "TLS_DHE_RSA_WITH_DES_CBC_SHA,TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA"
|
2016-11-21 09:40:56 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
@binder.parse(["ssl://0.0.0.0:8080?keystore=#{keystore}&keystore-pass=&ssl_cipher_list=#{ssl_cipher_list}"], @events)
|
2016-11-21 09:40:56 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
assert_equal keystore, ssl_context_for_binder(@binder).keystore
|
|
|
|
assert_equal ssl_cipher_list, ssl_context_for_binder(@binder).ssl_cipher_list
|
2016-11-21 09:40:56 -05:00
|
|
|
end
|
2019-07-16 18:53:28 -04:00
|
|
|
end
|
2017-12-01 13:56:48 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
class TestBinderMRI < TestBinderBase
|
|
|
|
def setup
|
|
|
|
super
|
2018-08-21 21:08:06 -04:00
|
|
|
skip_on :jruby
|
2019-07-16 18:53:28 -04:00
|
|
|
end
|
2017-12-01 13:56:48 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
def test_localhost_addresses_dont_alter_listeners_for_ssl_addresses
|
2019-09-11 08:01:24 -04:00
|
|
|
@binder.parse(["ssl://localhost:10002?key=#{key}&cert=#{cert}"], @events)
|
2017-12-01 13:56:48 -05:00
|
|
|
|
2019-09-11 06:49:35 -04:00
|
|
|
assert_equal [], @binder.instance_variable_get(:@listeners)
|
2017-12-01 13:56:48 -05:00
|
|
|
end
|
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
def test_binder_parses_ssl_cipher_filter
|
|
|
|
ssl_cipher_filter = "AES@STRENGTH"
|
2017-12-01 14:06:33 -05:00
|
|
|
|
2019-09-11 08:01:24 -04:00
|
|
|
@binder.parse(["ssl://0.0.0.0?key=#{key}&cert=#{cert}&ssl_cipher_filter=#{ssl_cipher_filter}"], @events)
|
2017-12-01 13:56:48 -05:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
assert_equal ssl_cipher_filter, ssl_context_for_binder(@binder).ssl_cipher_filter
|
2017-12-01 13:56:48 -05:00
|
|
|
end
|
2018-04-17 09:25:22 -04:00
|
|
|
|
|
|
|
def test_binder_parses_tlsv1_disabled
|
2019-09-11 08:01:24 -04:00
|
|
|
@binder.parse(["ssl://0.0.0.0?key=#{key}&cert=#{cert}&no_tlsv1=true"], @events)
|
2018-04-17 09:25:22 -04:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
assert ssl_context_for_binder(@binder).no_tlsv1
|
2018-04-17 09:25:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_binder_parses_tlsv1_enabled
|
2019-09-11 08:01:24 -04:00
|
|
|
@binder.parse(["ssl://0.0.0.0?key=#{key}&cert=#{cert}&no_tlsv1=false"], @events)
|
2018-04-17 09:25:22 -04:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
refute ssl_context_for_binder(@binder).no_tlsv1
|
2018-04-17 09:25:22 -04:00
|
|
|
end
|
|
|
|
|
2019-08-03 15:56:30 -04:00
|
|
|
def test_binder_parses_tlsv1_tlsv1_1_unspecified_defaults_to_enabled
|
2019-09-11 08:01:24 -04:00
|
|
|
@binder.parse(["ssl://0.0.0.0?key=#{key}&cert=#{cert}"], @events)
|
2018-04-17 09:25:22 -04:00
|
|
|
|
2019-07-16 18:53:28 -04:00
|
|
|
refute ssl_context_for_binder(@binder).no_tlsv1
|
2019-08-03 15:56:30 -04:00
|
|
|
refute ssl_context_for_binder(@binder).no_tlsv1_1
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_binder_parses_tlsv1_1_disabled
|
2019-09-11 08:01:24 -04:00
|
|
|
@binder.parse(["ssl://0.0.0.0?key=#{key}&cert=#{cert}&no_tlsv1_1=true"], @events)
|
2019-08-03 15:56:30 -04:00
|
|
|
|
|
|
|
assert ssl_context_for_binder(@binder).no_tlsv1_1
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_binder_parses_tlsv1_1_enabled
|
2019-09-11 08:01:24 -04:00
|
|
|
@binder.parse(["ssl://0.0.0.0?key=#{key}&cert=#{cert}&no_tlsv1_1=false"], @events)
|
2019-08-03 15:56:30 -04:00
|
|
|
|
|
|
|
refute ssl_context_for_binder(@binder).no_tlsv1_1
|
2018-04-17 09:25:22 -04:00
|
|
|
end
|
2017-12-01 14:06:33 -05:00
|
|
|
end
|