test: fix how ocsp client is run in tests

For some yet unknown reason the old test version using `open_port`
does not work in OTP 25, but works fine in OTP 24.  There are no
messages at all received from The openssl client port program in OTP
25.
This commit is contained in:
Thales Macedo Garitezi 2023-03-07 10:17:33 -03:00
parent 63ef2f9b79
commit f6707d1dd0
1 changed files with 49 additions and 66 deletions

View File

@ -263,7 +263,7 @@ assert_http_get(N, Timeout) when N > 0 ->
end, end,
assert_http_get(N - 1, Timeout). assert_http_get(N - 1, Timeout).
spawn_openssl_client(TLSVsn, RequestStatus, Config) -> openssl_client_command(TLSVsn, RequestStatus, Config) ->
DataDir = ?config(data_dir, Config), DataDir = ?config(data_dir, Config),
ClientCert = filename:join([DataDir, "client.pem"]), ClientCert = filename:join([DataDir, "client.pem"]),
ClientKey = filename:join([DataDir, "client.key"]), ClientKey = filename:join([DataDir, "client.key"]),
@ -274,25 +274,38 @@ spawn_openssl_client(TLSVsn, RequestStatus, Config) ->
true -> ["-status"]; true -> ["-status"];
false -> [] false -> []
end, end,
[
Openssl,
"s_client",
"-connect",
"localhost:8883",
%% needed to trigger `sni_fun'
"-servername",
"localhost",
TLSVsn,
"-CAfile",
Cacert,
"-cert",
ClientCert,
"-key",
ClientKey
] ++ StatusOpt.
run_openssl_client(TLSVsn, RequestStatus, Config) ->
Command0 = openssl_client_command(TLSVsn, RequestStatus, Config),
Command = lists:flatten(lists:join(" ", Command0)),
os:cmd(Command).
%% fixme: for some reason, the port program doesn't return any output
%% when running in OTP 25 using `open_port`, but the `os:cmd` version
%% works fine.
%% the `open_port' version works fine in OTP 24 for some reason.
spawn_openssl_client(TLSVsn, RequestStatus, Config) ->
[Openssl | Args] = openssl_client_command(TLSVsn, RequestStatus, Config),
open_port( open_port(
{spawn_executable, Openssl}, {spawn_executable, Openssl},
[ [
{args, {args, Args},
[
"s_client",
"-connect",
"localhost:8883",
%% needed to trigger `sni_fun'
"-servername",
"localhost",
TLSVsn,
"-CAfile",
Cacert,
"-cert",
ClientCert,
"-key",
ClientKey
] ++ StatusOpt},
binary, binary,
stderr_to_stdout stderr_to_stdout
] ]
@ -331,56 +344,26 @@ kill_pid(OSPid) ->
os:cmd("kill -9 " ++ integer_to_list(OSPid)). os:cmd("kill -9 " ++ integer_to_list(OSPid)).
test_ocsp_connection(TLSVsn, WithRequestStatus = true, Config) -> test_ocsp_connection(TLSVsn, WithRequestStatus = true, Config) ->
ClientPort = spawn_openssl_client(TLSVsn, WithRequestStatus, Config), OCSPOutput = run_openssl_client(TLSVsn, WithRequestStatus, Config),
{os_pid, ClientOSPid} = erlang:port_info(ClientPort, os_pid), ?assertMatch(
try {match, _},
timer:sleep(timer:seconds(1)), re:run(OCSPOutput, "OCSP Response Status: successful"),
{messages, Messages} = process_info(self(), messages), #{mailbox => process_info(self(), messages)}
OCSPOutput0 = [ ),
Output ?assertMatch(
|| {_Port, {data, Output}} <- Messages, {match, _},
re:run(Output, "OCSP response:") =/= nomatch re:run(OCSPOutput, "Cert Status: good"),
], #{mailbox => process_info(self(), messages)}
?assertMatch( ),
[_], ok;
OCSPOutput0,
#{all_messages => Messages}
),
[OCSPOutput] = OCSPOutput0,
?assertMatch(
{match, _},
re:run(OCSPOutput, "OCSP Response Status: successful"),
#{all_messages => Messages}
),
?assertMatch(
{match, _},
re:run(OCSPOutput, "Cert Status: good"),
#{all_messages => Messages}
),
ok
after
catch kill_pid(ClientOSPid)
end;
test_ocsp_connection(TLSVsn, WithRequestStatus = false, Config) -> test_ocsp_connection(TLSVsn, WithRequestStatus = false, Config) ->
ClientPort = spawn_openssl_client(TLSVsn, WithRequestStatus, Config), OCSPOutput = run_openssl_client(TLSVsn, WithRequestStatus, Config),
{os_pid, ClientOSPid} = erlang:port_info(ClientPort, os_pid), ?assertMatch(
try nomatch,
timer:sleep(timer:seconds(1)), re:run(OCSPOutput, "Cert Status: good", [{capture, none}]),
{messages, Messages} = process_info(self(), messages), #{mailbox => process_info(self(), messages)}
OCSPOutput = [ ),
Output ok.
|| {_Port, {data, Output}} <- Messages,
re:run(Output, "OCSP response:") =/= nomatch
],
?assertEqual(
[],
OCSPOutput,
#{all_messages => Messages}
),
ok
after
catch kill_pid(ClientOSPid)
end.
ensure_port_open(Port) -> ensure_port_open(Port) ->
do_ensure_port_open(Port, 10). do_ensure_port_open(Port, 10).