diff --git a/README.md b/README.md index 49649d69..4077d1c3 100644 --- a/README.md +++ b/README.md @@ -109,24 +109,16 @@ Mint is a low-level client. If you need higher-level features such as connection If you wish to contribute, check out the [issue list][issues] and let us know what you want to work on, so that we can discuss it and reduce duplicate work. -Tests are organized with tags. Integration tests that hit real websites over the internet are tagged with `:requires_internet_connection`. Proxy tests are tagged with `:proxy` (excluded by default) and require that you start local proxy instance through Docker Compose (or Podman Compose) from the Mint root directory in order to run. - -If you encounter `{:error, %Mint.TransportError{reason: :nxdomain}}` error during the integration test, this suggests your DNS-based adblocker (self-hosted or VPN) might be blocking social media sites used in the integration test cases. +Tests are organized with tags. Proxy tests are tagged with `:proxy` (excluded by default) and require that you start local proxy instance through Docker Compose (or Podman Compose) from the Mint root directory in order to run. Here are a few examples of running tests: -Run all default tests which including `:requires_internet_connection` except `:proxy`: +Run all default tests except `:proxy`: ```sh mix test ``` -Local tests, if you don't have an Internet connection available: - -```sh -mix test --exclude requires_internet_connection -``` - Run all tests: ```sh diff --git a/test/mint/cacerts_get_test.exs b/test/mint/cacerts_get_test.exs new file mode 100644 index 00000000..d6c29413 --- /dev/null +++ b/test/mint/cacerts_get_test.exs @@ -0,0 +1,64 @@ +defmodule Mint.CacertsGetTest do + # :public_key.cacerts_load/1 replaces the CA store that + # :public_key.cacerts_get/0 returns for the whole VM, so these tests can't + # run concurrently with other tests. + use ExUnit.Case, async: false + + alias Mint.{TransportError, HTTP, TestCertificates, TLSTestServer} + + if List.to_integer(:erlang.system_info(:otp_release)) < 25 do + @moduletag :skip + end + + setup do + cacertfile = + Path.join( + System.tmp_dir!(), + "mint_cacerts_get_test_#{System.unique_integer([:positive])}.pem" + ) + + on_exit(fn -> + apply(:public_key, :cacerts_load, []) + File.rm(cacertfile) + end) + + %{cacertfile: cacertfile} + end + + @tag :capture_log + test "using :public_key.cacerts_get/0", %{cacertfile: cacertfile} do + trusted = TestCertificates.pkix_test_chain() + untrusted = TestCertificates.pkix_test_chain() + + pem_entries = + for der <- trusted.client_config[:cacerts], do: {:Certificate, der, :not_encrypted} + + File.write!(cacertfile, :public_key.pem_encode(pem_entries)) + :ok = apply(:public_key, :cacerts_load, [cacertfile]) + + cacerts = apply(:public_key, :cacerts_get, []) + + {:ok, untrusted_port} = TLSTestServer.start(untrusted.server_config) + {:ok, trusted_port} = TLSTestServer.start(trusted.server_config) + + assert {:error, %TransportError{}} = + HTTP.connect( + :https, + "localhost", + untrusted_port, + transport_opts: [ + log_alert: false, + reuse_sessions: false, + cacerts: cacerts + ] + ) + + assert {:ok, _conn} = + HTTP.connect( + :https, + "localhost", + trusted_port, + transport_opts: [reuse_sessions: false, cacerts: cacerts] + ) + end +end diff --git a/test/mint/http1/integration_test.exs b/test/mint/http1/integration_test.exs index d78bfb77..1c289514 100644 --- a/test/mint/http1/integration_test.exs +++ b/test/mint/http1/integration_test.exs @@ -3,9 +3,7 @@ defmodule Mint.HTTP1.IntegrationTest do import Mint.HTTP1.TestHelpers - alias Mint.{TransportError, HTTP1, HttpBin} - - @moduletag :requires_internet_connection + alias Mint.{TransportError, HTTP1, HttpBin, TestCertificates, TLSTestServer} describe "local httpbin" do test "200 response" do @@ -102,15 +100,29 @@ defmodule Mint.HTTP1.IntegrationTest do end end - describe "twitter.com" do + describe "connect timeout" do + # A local connect can complete before a timeout of 0 fires. Linux drops the + # SYN for a listen socket whose accept queue is full, so the connect can't + # complete at all. Other systems reset the connection instead. + if :os.type() != {:unix, :linux} do + @tag skip: "needs Linux's handling of a full accept queue" + end + test "timeout with http" do + {:ok, listen_socket} = :gen_tcp.listen(0, backlog: 0) + {:ok, port} = :inet.port(listen_socket) + fill_accept_queue(port) + assert {:error, %TransportError{reason: :timeout}} = - HTTP1.connect(:http, "twitter.com", 80, transport_opts: [timeout: 0]) + HTTP1.connect(:http, "localhost", port, transport_opts: [timeout: 0]) end + # The server never answers the ClientHello, so the handshake can't finish. test "timeout with https" do + {:ok, port, _server_ref} = HTTP1.TestServer.start() + assert {:error, %TransportError{reason: :timeout}} = - HTTP1.connect(:https, "twitter.com", 443, transport_opts: [timeout: 0]) + HTTP1.connect(:https, "localhost", port, transport_opts: [timeout: 0]) end end @@ -197,13 +209,14 @@ defmodule Mint.HTTP1.IntegrationTest do end end - describe "badssl.com" do - @describetag :requires_internet_connection - + describe "certificate verification" do @tag :capture_log test "SSL with bad certificate" do + %{server_config: server_config} = TestCertificates.pkix_test_chain() + {:ok, port} = TLSTestServer.start(server_config) + assert {:error, %TransportError{reason: reason}} = - HTTP1.connect(:https, "untrusted-root.badssl.com", 443, + HTTP1.connect(:https, "localhost", port, transport_opts: [log_alert: false, reuse_sessions: false] ) @@ -212,16 +225,23 @@ defmodule Mint.HTTP1.IntegrationTest do match?({:tls_alert, {:unknown_ca, _}}, reason) assert {:ok, _conn} = - HTTP1.connect(:https, "untrusted-root.badssl.com", 443, - transport_opts: [verify: :verify_none] - ) + HTTP1.connect(:https, "localhost", port, transport_opts: [verify: :verify_none]) end @tag :capture_log test "SSL with bad hostname" do + %{server_config: server_config, client_config: client_config} = + TestCertificates.pkix_test_chain(hostname: "wrong.host.example") + + {:ok, port} = TLSTestServer.start(server_config) + assert {:error, %TransportError{reason: reason}} = - HTTP1.connect(:https, "wrong.host.badssl.com", 443, - transport_opts: [log_alert: false, reuse_sessions: false] + HTTP1.connect(:https, "localhost", port, + transport_opts: [ + cacerts: client_config[:cacerts], + log_alert: false, + reuse_sessions: false + ] ) # OTP 21.3 changes the format of SSL errors. OTP 29 reports a hostname @@ -232,9 +252,14 @@ defmodule Mint.HTTP1.IntegrationTest do match?({:tls_alert, {:bad_certificate, _}}, reason) assert {:ok, _conn} = - HTTP1.connect(:https, "wrong.host.badssl.com", 443, - transport_opts: [verify: :verify_none] - ) + HTTP1.connect(:https, "localhost", port, transport_opts: [verify: :verify_none]) + end + end + + defp fill_accept_queue(port) do + case :gen_tcp.connect(~c"localhost", port, [], 100) do + {:ok, _socket} -> fill_accept_queue(port) + {:error, :timeout} -> :ok end end end diff --git a/test/mint/http2/conn_test.exs b/test/mint/http2/conn_test.exs index 0cc4ae4c..c7a784f6 100644 --- a/test/mint/http2/conn_test.exs +++ b/test/mint/http2/conn_test.exs @@ -1477,6 +1477,20 @@ defmodule Mint.HTTP2Test do assert HTTP2.open?(conn) end + + # https://github.com/elixir-mint/mint/issues/171 + test "request header names are lowercased before HPACK encoding", %{conn: conn} do + headers = [{"If-Modified-Since", "Wed, 26 May 2019 07:43:40 GMT"}] + assert {:ok, conn, _ref} = HTTP2.request(conn, "GET", "/", headers, nil) + + assert_recv_frames [headers(hbf: hbf)] + + decoded_headers = server_decode_headers(hbf) + assert {"if-modified-since", "Wed, 26 May 2019 07:43:40 GMT"} in decoded_headers + refute List.keymember?(decoded_headers, "If-Modified-Since", 0) + + assert HTTP2.open?(conn) + end end describe "request header validation" do diff --git a/test/mint/http2/integration_test.exs b/test/mint/http2/integration_test.exs index a6692990..b8a59353 100644 --- a/test/mint/http2/integration_test.exs +++ b/test/mint/http2/integration_test.exs @@ -6,8 +6,6 @@ defmodule HTTP2.IntegrationTest do alias Mint.HTTP2 alias Mint.HttpBin - @moduletag :requires_internet_connection - setup context do transport_opts = if Mint.Core.Transport.SSL.ssl_version() >= [10, 2] do @@ -99,132 +97,4 @@ defmodule HTTP2.IntegrationTest do assert HTTP2.open?(conn) end end - - describe "twitter.com" do - @describetag connect: {"twitter.com", 443} - @browser_user_agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" - - test "ping", %{conn: conn} do - assert {:ok, %HTTP2{} = conn, ref} = HTTP2.ping(conn) - assert {:ok, %HTTP2{} = conn, [{:pong, ^ref}]} = receive_stream(conn) - assert conn.buffer == "" - assert HTTP2.open?(conn) - end - - test "GET /", %{conn: conn} do - assert {:ok, %HTTP2{} = conn, ref} = - HTTP2.request(conn, "GET", "/", [{"user-agent", @browser_user_agent}], nil) - - assert {:ok, %HTTP2{} = conn, responses} = receive_stream(conn) - - assert [{:status, ^ref, status}, {:headers, ^ref, headers} | rest] = responses - assert status in [200, 301, 302] - - assert {_, [{:done, ^ref}]} = Enum.split_while(rest, &match?({:data, ^ref, _}, &1)) - - assert is_list(headers) - - assert conn.buffer == "" - assert HTTP2.open?(conn) - end - end - - describe "facebook.com" do - @describetag connect: {"facebook.com", 443} - - test "ping", %{conn: conn} do - assert {:ok, %HTTP2{} = conn, ref} = HTTP2.ping(conn) - assert {:ok, %HTTP2{} = conn, [{:pong, ^ref}]} = receive_stream(conn) - assert conn.buffer == "" - assert HTTP2.open?(conn) - end - - test "GET /", %{conn: conn} do - assert {:ok, %HTTP2{} = conn, ref} = HTTP2.request(conn, "GET", "/", [], nil) - - assert {:ok, %HTTP2{} = conn, responses} = receive_stream(conn) - - assert [{:status, ^ref, status}, {:headers, ^ref, headers} | rest] = responses - assert {_, [{:done, ^ref}]} = Enum.split_while(rest, &match?({:data, ^ref, _}, &1)) - - assert status == 301 - assert is_list(headers) - - assert conn.buffer == "" - assert HTTP2.open?(conn) - end - end - - describe "robynthinks.wordpress.com" do - @describetag connect: {"robynthinks.wordpress.com", 443} - - test "GET /feed/ - regression for #171", %{conn: conn} do - # Using non-downcased header meant that HPACK wouldn't find it in the - # static built-in headers table and so it wouldn't encode it correctly. - headers = [{"If-Modified-Since", "Wed, 26 May 2019 07:43:40 GMT"}] - assert {:ok, %HTTP2{} = conn, ref} = HTTP2.request(conn, "GET", "/feed/", headers, nil) - - assert {:ok, %HTTP2{} = conn, responses} = receive_stream(conn) - - assert [{:status, ^ref, status}, {:headers, ^ref, _headers} | rest] = responses - assert {_, [{:done, ^ref}]} = Enum.split_while(rest, &match?({:data, ^ref, _}, &1)) - - assert status in [200, 304] - - assert conn.buffer == "" - assert HTTP2.open?(conn) - end - end - - describe "www.shopify.com" do - @describetag connect: {"www.shopify.com", 443} - - if List.to_integer(:erlang.system_info(:otp_release)) < 23 do - @tag :skip - end - - # Informational responses were the issue.s - # https://github.com/elixir-mint/mint/issues/349 - test "GET / with specific User-Agent header - regression for #349", %{conn: conn} do - assert %HTTP2{} = conn - - assert {:ok, %HTTP2{} = conn, ref} = - HTTP2.request(conn, "GET", "/", [{"user-agent", "curl/7.68.0"}], nil) - - assert {:ok, %HTTP2{} = conn, responses} = receive_stream(conn) - - case responses do - [ - {:status, ^ref, informational_status}, - {:headers, ^ref, informational_headers}, - {:status, ^ref, status}, - {:headers, ^ref, headers} - | rest - ] -> - assert informational_status == 103 - assert {"link", _} = List.keyfind(informational_headers, "link", 0) - assert status == 200 - assert is_list(headers) and length(headers) > 0 - - assert Enum.count(rest, &match?({:data, ^ref, _data}, &1)) >= 1 - assert List.last(rest) == {:done, ref} - - [{:status, ^ref, status}, {:headers, ^ref, headers} | rest] -> - assert status == 200 - assert is_list(headers) and length(headers) > 0 - assert Enum.count(rest, &match?({:data, ^ref, _data}, &1)) >= 1 - assert List.last(rest) == {:done, ref} - - _other -> - flunk( - "Unexpected responses. Expected status + headers + data, or informational " <> - "response + status + headers + data, got:\n#{inspect(responses, pretty: true)}" - ) - end - - assert HTTP2.open?(conn) - end - end - - # TODO: certificate verification; badssl.com does not seem to support HTTP2 end diff --git a/test/mint/integration_test.exs b/test/mint/integration_test.exs index af7b44db..56568a7f 100644 --- a/test/mint/integration_test.exs +++ b/test/mint/integration_test.exs @@ -3,11 +3,9 @@ defmodule Mint.IntegrationTest do import Mint.HTTP1.TestHelpers - alias Mint.{TransportError, HTTP, HttpBin} + alias Mint.{TransportError, HTTP, HttpBin, TestCertificates, TLSTestServer} - @moduletag :requires_internet_connection - - describe "nghttp2.org" do + describe "protocol selection" do test "SSL - select HTTP1" do assert {:ok, conn} = HTTP.connect(:https, HttpBin.host(), HttpBin.https_port(), @@ -48,12 +46,15 @@ defmodule Mint.IntegrationTest do describe "ssl certificate verification" do @tag :capture_log - test "bad certificate - badssl.com" do + test "bad certificate" do + %{server_config: server_config} = TestCertificates.pkix_test_chain() + {:ok, port} = TLSTestServer.start(server_config) + assert {:error, %TransportError{reason: reason}} = HTTP.connect( :https, - "untrusted-root.badssl.com", - 443, + "localhost", + port, transport_opts: [log_alert: false, reuse_sessions: false] ) @@ -64,20 +65,29 @@ defmodule Mint.IntegrationTest do assert {:ok, _conn} = HTTP.connect( :https, - "untrusted-root.badssl.com", - 443, + "localhost", + port, transport_opts: [verify: :verify_none] ) end @tag :capture_log - test "bad hostname - badssl.com" do + test "bad hostname" do + %{server_config: server_config, client_config: client_config} = + TestCertificates.pkix_test_chain(hostname: "wrong.host.example") + + {:ok, port} = TLSTestServer.start(server_config) + assert {:error, %TransportError{reason: reason}} = HTTP.connect( :https, - "wrong.host.badssl.com", - 443, - transport_opts: [log_alert: false, reuse_sessions: false] + "localhost", + port, + transport_opts: [ + cacerts: client_config[:cacerts], + log_alert: false, + reuse_sessions: false + ] ) # OTP 21.3 changes the format of SSL errors. OTP 29 reports a hostname @@ -90,66 +100,54 @@ defmodule Mint.IntegrationTest do assert {:ok, _conn} = HTTP.connect( :https, - "wrong.host.badssl.com", - 443, + "localhost", + port, transport_opts: [verify: :verify_none] ) end - - if List.to_integer(:erlang.system_info(:otp_release)) < 25 do - @tag :skip - end - - @tag :capture_log - test "using :public_key.cacerts_get/0" do - cacerts = apply(:public_key, :cacerts_get, []) - - assert {:error, %TransportError{}} = - HTTP.connect( - :https, - "untrusted-root.badssl.com", - 443, - transport_opts: [ - log_alert: false, - reuse_sessions: false, - cacerts: cacerts - ] - ) - - assert {:ok, _conn} = - HTTP.connect( - :https, - "nghttp2.org", - 443, - transport_opts: [reuse_sessions: false, cacerts: cacerts] - ) - end end describe "partial chain handling" do - @dst_and_isrg Path.expand("../support/mint/dst_and_isrg.pem", __DIR__) - - # OTP 18.3 fails to connect to letsencrypt.org, skip this test - if Mint.Core.Transport.SSL.ssl_version() < [8, 0] do - @tag skip: ":ssl version too old" - end + # The server sends an intermediate signed by an expired root. The client + # trusts both the expired root and a second root with the intermediate's + # key, the way Let's Encrypt's ISRG Root X1 was cross-signed by the + # expired DST Root CA X3. + test "intermediate cross-signed by an expired root" do + key = {:rsa, 2048, 17} + today = Date.utc_today() + expired = {Date.to_erl(Date.add(today, -30)), Date.to_erl(Date.add(today, -1))} + san_extension = {:Extension, {2, 5, 29, 17}, false, [dNSName: ~c"localhost"]} + + expired_root = + :public_key.pkix_test_root_cert(~c"Expired Root", + key: key, + validity: expired, + digest: :sha256 + ) + + cross_signed_key = :public_key.generate_key(key) + + trusted_root = + :public_key.pkix_test_root_cert(~c"Trusted Root", key: cross_signed_key, digest: :sha256) + + %{server_config: server_config} = + :public_key.pkix_test_data(%{ + server_chain: %{ + root: expired_root, + intermediates: [[key: cross_signed_key, digest: :sha256]], + peer: [key: key, digest: :sha256, extensions: [san_extension]] + }, + client_chain: %{root: [key: key], intermediates: [], peer: [key: key]} + }) + + {:ok, port} = TLSTestServer.start(server_config) - # This test assumes the letsencrypt.org server presents the 'long chain', - # consisting of the following certificates: - # - # 0 s:/CN=lencr.org - # i:/C=US/O=Let's Encrypt/CN=R3 - # 1 s:/C=US/O=Let's Encrypt/CN=R3 - # i:/C=US/O=Internet Security Research Group/CN=ISRG Root X1 - # 2 s:/C=US/O=Internet Security Research Group/CN=ISRG Root X1 - # i:/O=Digital Signature Trust Co./CN=DST Root CA X3 - # - # This is currently the case, but won't be the case after Sep 2024, or - # possibly earlier. - test "Let's Encrypt ISRG cross-signed by expired root" do assert {:ok, _conn} = - HTTP.connect(:https, "letsencrypt.org", 443, - transport_opts: [cacertfile: @dst_and_isrg, reuse_sessions: false] + HTTP.connect(:https, "localhost", port, + transport_opts: [ + cacerts: [expired_root.cert, trusted_root.cert], + reuse_sessions: false + ] ) end end @@ -231,13 +229,18 @@ defmodule Mint.IntegrationTest do end describe "information from connection's socket" do - test "TLSv1.2 - badssl.com" do + test "TLSv1.2" do + %{server_config: server_config, client_config: client_config} = + TestCertificates.pkix_test_chain() + + {:ok, port} = TLSTestServer.start([versions: [:"tlsv1.2"]] ++ server_config) + assert {:ok, conn} = HTTP.connect( :https, - "tls-v1-2.badssl.com", - 1012, - transport_opts: [keep_secrets: true] + "localhost", + port, + transport_opts: [keep_secrets: true, cacerts: client_config[:cacerts]] ) assert socket = Mint.HTTP.get_socket(conn) @@ -251,18 +254,23 @@ defmodule Mint.IntegrationTest do end describe "force TLS v1.3 only" do - test "rabbitmq.com" do + test "connects with only TLS 1.3 versions and ciphers" do if Mint.Core.Transport.SSL.ssl_version() >= [10, 2] do + %{server_config: server_config, client_config: client_config} = + TestCertificates.pkix_test_chain() + + {:ok, port} = TLSTestServer.start(server_config) ciphers = :ssl.filter_cipher_suites(:ssl.cipher_suites(:all, :"tlsv1.3"), []) opts = [ transport_opts: [ versions: [:"tlsv1.3"], - ciphers: ciphers + ciphers: ciphers, + cacerts: client_config[:cacerts] ] ] - assert {:ok, _conn} = HTTP.connect(:https, "rabbitmq.com", 443, opts) + assert {:ok, _conn} = HTTP.connect(:https, "localhost", port, opts) else :ok end diff --git a/test/mint/tunnel_proxy_test.exs b/test/mint/tunnel_proxy_test.exs index df24bfa0..6a5f34f2 100644 --- a/test/mint/tunnel_proxy_test.exs +++ b/test/mint/tunnel_proxy_test.exs @@ -7,7 +7,6 @@ defmodule Mint.TunnelProxyTest do alias Mint.HttpBin @moduletag :proxy - @moduletag :requires_internet_connection test "200 response - http://httpbin.org" do # Ensure we only match relevant messages diff --git a/test/mint/unsafe_proxy_test.exs b/test/mint/unsafe_proxy_test.exs index b6ef1294..a9a6b2d5 100644 --- a/test/mint/unsafe_proxy_test.exs +++ b/test/mint/unsafe_proxy_test.exs @@ -6,7 +6,6 @@ defmodule Mint.UnsafeProxyTest do alias Mint.HttpBin @moduletag :proxy - @moduletag :requires_internet_connection test "200 response - http://httpbin.org" do assert {:ok, conn} = diff --git a/test/support/mint/dst_and_isrg.pem b/test/support/mint/dst_and_isrg.pem deleted file mode 100644 index cd99c207..00000000 --- a/test/support/mint/dst_and_isrg.pem +++ /dev/null @@ -1,96 +0,0 @@ -### Digital Signature Trust Co. - -=== /O=Digital Signature Trust Co./CN=DST Root CA X3 -Certificate: - Data: - Version: 3 (0x2) - Serial Number: - 44:af:b0:80:d6:a3:27:ba:89:30:39:86:2e:f8:40:6b - Signature Algorithm: sha1WithRSAEncryption - Validity - Not Before: Sep 30 21:12:19 2000 GMT - Not After : Sep 30 14:01:15 2021 GMT - Subject: O=Digital Signature Trust Co., CN=DST Root CA X3 - X509v3 extensions: - X509v3 Basic Constraints: critical - CA:TRUE - X509v3 Key Usage: critical - Certificate Sign, CRL Sign - X509v3 Subject Key Identifier: - C4:A7:B1:A4:7B:2C:71:FA:DB:E1:4B:90:75:FF:C4:15:60:85:89:10 -SHA1 Fingerprint=DA:C9:02:4F:54:D8:F6:DF:94:93:5F:B1:73:26:38:CA:6A:D7:7C:13 -SHA256 Fingerprint=06:87:26:03:31:A7:24:03:D9:09:F1:05:E6:9B:CF:0D:32:E1:BD:24:93:FF:C6:D9:20:6D:11:BC:D6:77:07:39 ------BEGIN CERTIFICATE----- -MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/ -MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT -DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow -PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD -Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB -AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O -rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq -OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b -xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw -7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD -aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV -HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG -SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69 -ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr -AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz -R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5 -JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo -Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ ------END CERTIFICATE----- - -### Internet Security Research Group - -=== /C=US/O=Internet Security Research Group/CN=ISRG Root X1 -Certificate: - Data: - Version: 3 (0x2) - Serial Number: - 82:10:cf:b0:d2:40:e3:59:44:63:e0:bb:63:82:8b:00 - Signature Algorithm: sha256WithRSAEncryption - Validity - Not Before: Jun 4 11:04:38 2015 GMT - Not After : Jun 4 11:04:38 2035 GMT - Subject: C=US, O=Internet Security Research Group, CN=ISRG Root X1 - X509v3 extensions: - X509v3 Key Usage: critical - Certificate Sign, CRL Sign - X509v3 Basic Constraints: critical - CA:TRUE - X509v3 Subject Key Identifier: - 79:B4:59:E6:7B:B6:E5:E4:01:73:80:08:88:C8:1A:58:F6:E9:9B:6E -SHA1 Fingerprint=CA:BD:2A:79:A1:07:6A:31:F2:1D:25:36:35:CB:03:9D:43:29:A5:E8 -SHA256 Fingerprint=96:BC:EC:06:26:49:76:F3:74:60:77:9A:CF:28:C5:A7:CF:E8:A3:C0:AA:E1:1A:8F:FC:EE:05:C0:BD:DF:08:C6 ------BEGIN CERTIFICATE----- -MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw -TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh -cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4 -WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu -ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY -MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc -h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+ -0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U -A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW -T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH -B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC -B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv -KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWn -OlFuhjuefXKnEgV4We0+UXgVCwOPjdAvBbI+e0ocS3MFEvzG6uBQE3xDk3SzynTn -jh8BCNAw1FtxNrQHusEwMFxIt4I7mKZ9YIqioymCzLq9gwQbooMDQaHWBfEbwrbw -qHyGO0aoSCqI3Haadr8faqU9GY/rOPNk3sgrDQoo//fb4hVC1CLQJ13hef4Y53CI -rU7m2Ys6xt0nUW7/vGT1M0NPAgMBAAGjQjBAMA4GA1UdDwEB/wQEAwIBBjAPBgNV -HRMBAf8EBTADAQH/MB0GA1UdDgQWBBR5tFnme7bl5AFzgAiIyBpY9umbbjANBgkq -hkiG9w0BAQsFAAOCAgEAVR9YqbyyqFDQDLHYGmkgJykIrGF1XIpu+ILlaS/V9lZL -ubhzEFnTIZd+50xx+7LSYK05qAvqFyFWhfFQDlnrzuBZ6brJFe+GnY+EgPbk6ZGQ -3BebYhtF8GaV0nxvwuo77x/Py9auJ/GpsMiu/X1+mvoiBOv/2X/qkSsisRcOj/KK -NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5 -ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur -TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC -jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc -oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq -4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA -mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d -emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc= ------END CERTIFICATE----- diff --git a/test/support/mint/test_certificates.ex b/test/support/mint/test_certificates.ex index b8addb20..c770c08f 100644 --- a/test/support/mint/test_certificates.ex +++ b/test/support/mint/test_certificates.ex @@ -2,10 +2,12 @@ defmodule Mint.TestCertificates do @moduledoc false # Generates a fresh CA and server certificate chain at runtime, with a - # server certificate valid for "localhost" (SAN), so tests can exercise - # verify_peer against local servers without committed fixtures that expire. - def pkix_test_chain do - san_extension = {:Extension, {2, 5, 29, 17}, false, [dNSName: ~c"localhost"]} + # server certificate valid for the :hostname option ("localhost" by default, + # as SAN), so tests can exercise verify_peer against local servers without + # committed fixtures that expire. + def pkix_test_chain(opts \\ []) do + hostname = Keyword.get(opts, :hostname, "localhost") + san_extension = {:Extension, {2, 5, 29, 17}, false, [dNSName: String.to_charlist(hostname)]} cert_opts = [digest: :sha256, key: {:rsa, 2048, 17}] :public_key.pkix_test_data(%{ diff --git a/test/support/mint/tls_test_server.ex b/test/support/mint/tls_test_server.ex new file mode 100644 index 00000000..e81f5ec3 --- /dev/null +++ b/test/support/mint/tls_test_server.ex @@ -0,0 +1,27 @@ +defmodule Mint.TLSTestServer do + @moduledoc false + + # Listens on a random local port and completes the TLS handshake for every + # connection it accepts. Accepted connections stay open until the calling + # process exits. + def start(ssl_opts) do + socket_opts = [mode: :binary, packet: :raw, active: false, reuseaddr: true] + {:ok, listen_socket} = :ssl.listen(0, socket_opts ++ ssl_opts) + {:ok, {_address, port}} = :ssl.sockname(listen_socket) + + spawn_link(fn -> accept_loop(listen_socket) end) + + {:ok, port} + end + + defp accept_loop(listen_socket) do + case :ssl.transport_accept(listen_socket) do + {:ok, socket} -> + _ = :ssl.handshake(socket, 5_000) + accept_loop(listen_socket) + + {:error, _reason} -> + :ok + end + end +end