|
| 1 | +%%% Test that cacertfile option is respected when making HTTPS requests. |
| 2 | +%%% |
| 3 | +%%% Currently fails because hackney_conn.erl bypasses hackney_ssl:ssl_opts/2 |
| 4 | +%%% and calls hackney_ssl:check_hostname_opts/1 directly, which always sets |
| 5 | +%%% {cacerts, certifi:cacerts()}. Erlang SSL then ignores cacertfile. |
| 6 | + |
| 7 | +-module(hackney_cacertfile_bug_test). |
| 8 | +-include_lib("eunit/include/eunit.hrl"). |
| 9 | + |
| 10 | +-define(HTTPS_PORT, 8126). |
| 11 | + |
| 12 | +cacertfile_test_() -> |
| 13 | + {setup, |
| 14 | + fun setup/0, |
| 15 | + fun teardown/1, |
| 16 | + [ |
| 17 | + {"cacertfile respected in ssl_opts/2", fun test_ssl_opts_handles_cacertfile/0}, |
| 18 | + {"cacertfile respected in request", fun test_request_with_cacertfile/0} |
| 19 | + ]}. |
| 20 | + |
| 21 | +setup() -> |
| 22 | + error_logger:tty(false), |
| 23 | + {ok, _} = application:ensure_all_started(cowboy), |
| 24 | + {ok, _} = application:ensure_all_started(hackney), |
| 25 | + |
| 26 | + CertDir = cert_dir(), |
| 27 | + CertFile = filename:join(CertDir, "server.pem"), |
| 28 | + KeyFile = filename:join(CertDir, "server.key"), |
| 29 | + |
| 30 | + Dispatch = cowboy_router:compile([{'_', [{"/[...]", test_http_resource, []}]}]), |
| 31 | + {ok, _} = cowboy:start_tls(cacertfile_test_server, |
| 32 | + [{certfile, CertFile}, |
| 33 | + {keyfile, KeyFile}, |
| 34 | + {port, ?HTTPS_PORT}], |
| 35 | + #{env => #{dispatch => Dispatch}}), |
| 36 | + ok. |
| 37 | + |
| 38 | +teardown(_) -> |
| 39 | + cowboy:stop_listener(cacertfile_test_server), |
| 40 | + application:stop(cowboy), |
| 41 | + application:stop(hackney), |
| 42 | + error_logger:tty(true), |
| 43 | + ok. |
| 44 | + |
| 45 | +test_ssl_opts_handles_cacertfile() -> |
| 46 | + CACertFile = filename:join(cert_dir(), "ca.pem"), |
| 47 | + Options = [{ssl_options, [{cacertfile, CACertFile}]}], |
| 48 | + SslOpts = hackney_ssl:ssl_opts("localhost", Options), |
| 49 | + ?assert(lists:keymember(cacertfile, 1, SslOpts)), |
| 50 | + ?assertNot(lists:keymember(cacerts, 1, SslOpts)). |
| 51 | + |
| 52 | +test_request_with_cacertfile() -> |
| 53 | + CACertFile = filename:join(cert_dir(), "ca.pem"), |
| 54 | + Url = "https://localhost:" ++ integer_to_list(?HTTPS_PORT) ++ "/get", |
| 55 | + Opts = [{ssl_options, [{cacertfile, CACertFile}]}, {pool, false}], |
| 56 | + {ok, 200, _, _} = hackney:request(get, Url, [], <<>>, Opts). |
| 57 | + |
| 58 | +cert_dir() -> |
| 59 | + filename:join([filename:dirname(code:which(?MODULE)), "..", "test", "certs"]). |
0 commit comments