diff options
author | Hans Nilsson <hans@erlang.org> | 2020-06-10 13:22:50 +0200 |
---|---|---|
committer | Hans Nilsson <hans@erlang.org> | 2020-06-16 11:44:25 +0200 |
commit | 79e7c12dfccbeb64727ee389c45f7061cfac018d (patch) | |
tree | c88bf67054b13cb81302ad5c2d1dc6fd2d20c7d7 | |
parent | f1c67d5a7496af78845f33b70da98f2b7ebe4e03 (diff) | |
download | erlang-79e7c12dfccbeb64727ee389c45f7061cfac018d.tar.gz |
ssh: Two new test cases for ssh:shell
-rw-r--r-- | lib/ssh/test/ssh_basic_SUITE.erl | 90 | ||||
-rw-r--r-- | lib/ssh/test/ssh_test_lib.erl | 39 |
2 files changed, 113 insertions, 16 deletions
diff --git a/lib/ssh/test/ssh_basic_SUITE.erl b/lib/ssh/test/ssh_basic_SUITE.erl index 1269ab393e..61d1538695 100644 --- a/lib/ssh/test/ssh_basic_SUITE.erl +++ b/lib/ssh/test/ssh_basic_SUITE.erl @@ -90,20 +90,12 @@ groups() -> exec_with_io_out, exec_with_io_in, cli, idle_time_client, idle_time_server, openssh_zlib_basic_test, - misc_ssh_options, inet_option, inet6_option - - ,shell, - shell_no_unicode, - shell_unicode_string, - close - + misc_ssh_options, inet_option, inet6_option, + shell, shell_socket, shell_ssh_conn, shell_no_unicode, shell_unicode_string, + close ]} ]. - - - - %%-------------------------------------------------------------------- init_per_suite(Config) -> ?CHECK_CRYPTO(begin @@ -430,7 +422,8 @@ shell(Config) when is_list(Config) -> IO = ssh_test_lib:start_io_server(), Shell = ssh_test_lib:start_shell(Port, IO, [{user_dir,UserDir}]), receive - {'EXIT', _, _} -> + {'EXIT', _, _} = Exit -> + ct:log("~p:~p ~p", [?MODULE,?LINE,Exit]), ct:fail(no_ssh_connection); ErlShellStart -> ct:log("Erlang shell start: ~p~n", [ErlShellStart]), @@ -440,6 +433,76 @@ shell(Config) when is_list(Config) -> end. %%-------------------------------------------------------------------- +%%% Test that ssh:shell/2 works when attaching to a open TCP-connection +shell_socket(Config) when is_list(Config) -> + process_flag(trap_exit, true), + SystemDir = filename:join(proplists:get_value(priv_dir, Config), system), + UserDir = proplists:get_value(priv_dir, Config), + + {_Pid, Host, Port} = ssh_test_lib:daemon([{system_dir, SystemDir},{user_dir, UserDir}, + {failfun, fun ssh_test_lib:failfun/2}]), + ct:sleep(500), + + %% First test with active mode: + {ok,ActiveSock} = gen_tcp:connect(ssh_test_lib:mangle_connect_address(Host), + Port, + [{active,true}]), + {error,not_passive_mode} = ssh:shell(ActiveSock), + ct:log("~p:~p active tcp socket failed ok", [?MODULE,?LINE]), + gen_tcp:close(ActiveSock), + + %% Secondly, test with an UDP socket: + {ok,BadSock} = gen_udp:open(0), + {error,not_tcp_socket} = ssh:shell(BadSock), + ct:log("~p:~p udp socket failed ok", [?MODULE,?LINE]), + gen_udp:close(BadSock), + + %% And finaly test with passive mode (which should work): + IO = ssh_test_lib:start_io_server(), + {ok,Sock} = gen_tcp:connect(Host, Port, [{active,false}]), + Shell = ssh_test_lib:start_shell(Sock, IO, [{user_dir,UserDir}]), + gen_tcp:controlling_process(Sock, Shell), + Shell ! start, + + receive + {'EXIT', _, _} = Exit -> + ct:log("~p:~p ~p", [?MODULE,?LINE,Exit]), + ct:fail(no_ssh_connection); + ErlShellStart -> + ct:log("Erlang shell start: ~p~n", [ErlShellStart]), + do_shell(IO, Shell) + after + 30000 -> ct:fail("timeout ~p:~p",[?MODULE,?LINE]) + end. + +%%-------------------------------------------------------------------- +%%% Test that ssh:shell/2 works when attaching to a open SSH-connection +shell_ssh_conn(Config) when is_list(Config) -> + process_flag(trap_exit, true), + SystemDir = filename:join(proplists:get_value(priv_dir, Config), system), + UserDir = proplists:get_value(priv_dir, Config), + + {_Pid, Host, Port} = ssh_test_lib:daemon([{system_dir, SystemDir},{user_dir, UserDir}, + {failfun, fun ssh_test_lib:failfun/2}]), + ct:sleep(500), + + IO = ssh_test_lib:start_io_server(), + {ok,C} = ssh:connect(Host, Port, [{silently_accept_hosts, true}, + {user_dir, UserDir}, + {user_interaction, false}]), + Shell = ssh_test_lib:start_shell(C, IO, undefined), + receive + {'EXIT', _, _} = Exit -> + ct:log("~p:~p ~p", [?MODULE,?LINE,Exit]), + ct:fail(no_ssh_connection); + ErlShellStart -> + ct:log("Erlang shell start: ~p~n", [ErlShellStart]), + do_shell(IO, Shell) + after + 30000 -> ct:fail("timeout ~p:~p",[?MODULE,?LINE]) + end. + +%%-------------------------------------------------------------------- cli(Config) when is_list(Config) -> process_flag(trap_exit, true), SystemDir = filename:join(proplists:get_value(priv_dir, Config), system), @@ -1357,7 +1420,8 @@ do_shell(IO, Shell) -> %%-------------------------------------------------------------------- wait_for_erlang_first_line(Config) -> receive - {'EXIT', _, _} -> + {'EXIT', _, _} = Exit -> + ct:log("~p:~p ~p", [?MODULE,?LINE,Exit]), {fail,no_ssh_connection}; <<"Eshell ",_/binary>> = _ErlShellStart -> ct:log("Erlang shell start: ~p~n", [_ErlShellStart]), diff --git a/lib/ssh/test/ssh_test_lib.erl b/lib/ssh/test/ssh_test_lib.erl index 2645c956cf..f8fe4392b9 100644 --- a/lib/ssh/test/ssh_test_lib.erl +++ b/lib/ssh/test/ssh_test_lib.erl @@ -184,11 +184,44 @@ start_shell(Port, IOServer) -> start_shell(Port, IOServer, ExtraOptions) -> spawn_link( fun() -> - Host = hostname(), + ct:log("~p:~p:~p ssh_test_lib:start_shell(~p, ~p, ~p)", + [?MODULE,?LINE,self(), Port, IOServer, ExtraOptions]), Options = [{user_interaction, false}, {silently_accept_hosts,true} | ExtraOptions], - group_leader(IOServer, self()), - ssh:shell(Host, Port, Options) + try + group_leader(IOServer, self()), + case Port of + 22 -> + Host = hostname(), + ct:log("Port==22 Call ssh:shell(~p, ~p)", + [Host, Options]), + ssh:shell(Host, Options); + _ when is_integer(Port) -> + Host = hostname(), + ct:log("is_integer(Port) Call ssh:shell(~p, ~p, ~p)", + [Host, Port, Options]), + ssh:shell(Host, Port, Options); + Socket when is_port(Socket) -> + receive + start -> ok + end, + ct:log("is_port(Socket) Call ssh:shell(~p, ~p)", + [Socket, Options]), + ssh:shell(Socket, Options); + ConnRef when is_pid(ConnRef) -> + ct:log("is_pid(ConnRef) Call ssh:shell(~p)", + [ConnRef]), + ssh:shell(ConnRef) % Options were given in ssh:connect + end + of + R -> + ct:log("~p:~p ssh_test_lib:start_shell(~p, ~p, ~p) -> ~p", + [?MODULE,?LINE,Port, IOServer, ExtraOptions, R]) + catch + C:E:S -> + ct:log("Exception ~p:~p~n~p", [C,E,S]), + ct:fail("Exception",[]) + end end). |