|
| 1 | +%%% Pooled TLS upgrade must not hang on a stalled handshake. |
| 2 | +%%% |
| 3 | +%%% ssl:connect/2 has no handshake deadline, so a peer that accepts TCP but |
| 4 | +%%% never completes the TLS handshake would pin the connection process (and its |
| 5 | +%%% pool slot) forever. The upgrade must be bounded by connect_timeout. |
| 6 | +-module(hackney_conn_upgrade_timeout_tests). |
| 7 | + |
| 8 | +-include_lib("eunit/include/eunit.hrl"). |
| 9 | + |
| 10 | +pooled_tls_upgrade_times_out_test_() -> |
| 11 | + %% Without the bound the upgrade never returns and this test times out. |
| 12 | + {timeout, 10, fun pooled_tls_upgrade_times_out/0}. |
| 13 | + |
| 14 | +pooled_tls_upgrade_times_out() -> |
| 15 | + {ok, _} = application:ensure_all_started(hackney), |
| 16 | + %% Listener that accepts the TCP connection but never speaks TLS. |
| 17 | + {ok, LSock} = gen_tcp:listen(0, [binary, {active, false}, {ip, {127, 0, 0, 1}}]), |
| 18 | + {ok, Port} = inet:port(LSock), |
| 19 | + %% Let the conn open (and thus own) its own TCP socket to the stalled peer. |
| 20 | + Opts = #{host => "127.0.0.1", port => Port, transport => hackney_tcp, |
| 21 | + connect_timeout => 500}, |
| 22 | + {ok, Pid} = hackney_conn:start_link(Opts), |
| 23 | + ok = hackney_conn:connect(Pid, 1000), |
| 24 | + {ok, _ServerSock} = gen_tcp:accept(LSock, 1000), |
| 25 | + ?assertEqual({ok, connected}, hackney_conn:get_state(Pid)), |
| 26 | + %% verify_none so the handshake proceeds and then stalls waiting for the |
| 27 | + %% ServerHello that never arrives; only the timeout can end it. |
| 28 | + T0 = erlang:monotonic_time(millisecond), |
| 29 | + Result = hackney_conn:upgrade_to_ssl(Pid, [{verify, verify_none}], #{final => true}), |
| 30 | + Elapsed = erlang:monotonic_time(millisecond) - T0, |
| 31 | + ?assertMatch({error, _}, Result), |
| 32 | + %% 500ms bound with generous slack; a regression hangs instead. |
| 33 | + ?assert(Elapsed < 4000), |
| 34 | + catch hackney_conn:stop(Pid), |
| 35 | + gen_tcp:close(LSock). |
0 commit comments