aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssh/test/ssh_test_cli.erl
diff options
context:
space:
mode:
authorIngela Anderton Andin <[email protected]>2013-11-14 10:33:11 +0100
committerIngela Anderton Andin <[email protected]>2013-11-14 10:33:11 +0100
commit19aedb5c0cb956e51b24fbba4923520efe7bf54b (patch)
tree5f38b856389ccfb43c83b7be5e33529b699e6574 /lib/ssh/test/ssh_test_cli.erl
parentc46240c78fbce1b8b026f975d80b397209ac4968 (diff)
parent7efe1b6dd3215261777b5f335b4f92dfca7cea42 (diff)
downloadotp-19aedb5c0cb956e51b24fbba4923520efe7bf54b.tar.gz
otp-19aedb5c0cb956e51b24fbba4923520efe7bf54b.tar.bz2
otp-19aedb5c0cb956e51b24fbba4923520efe7bf54b.zip
Merge branch 'ia/ssh/one-connection-process/OTP-11363' into maint
* ia/ssh/one-connection-process/OTP-11363: ssh: Logging fun and document enhancement ssh: Add CLI test case ssh: Quicker shutdown of an ssh dameon ssh: Add option to disallow CLI ssh: Simplify handling of connection attributes (e.i. user and sockname) ssh: Make inet option configurable and remove ipv6_disabled option ssh: Eliminate test case failure due to timing issues in test case code ssh: Enhance error handling ssh: Merge connection_manager and connection_handler processes ssh: Remove use of process dictionary
Diffstat (limited to 'lib/ssh/test/ssh_test_cli.erl')
-rw-r--r--lib/ssh/test/ssh_test_cli.erl81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/ssh/test/ssh_test_cli.erl b/lib/ssh/test/ssh_test_cli.erl
new file mode 100644
index 0000000000..cd9ad5f2ff
--- /dev/null
+++ b/lib/ssh/test/ssh_test_cli.erl
@@ -0,0 +1,81 @@
+-module(ssh_test_cli).
+
+-export([init/1, terminate/2, handle_ssh_msg/2, handle_msg/2]).
+
+-record(state, {
+ type,
+ id,
+ ref,
+ port
+ }).
+
+init([Type]) ->
+ {ok, #state{type = Type}}.
+
+handle_msg({ssh_channel_up, Id, Ref}, S) ->
+ User = get_ssh_user(Ref),
+ ok = ssh_connection:send(Ref,
+ Id,
+ << "\r\nYou are accessing a dummy, type \"q\" to exit\r\n\n" >>),
+ Port = run_portprog(User, S#state.type),
+ {ok, S#state{port = Port, id = Id, ref = Ref}};
+
+handle_msg({Port, {data, Data}}, S = #state{port = Port}) ->
+ ok = ssh_connection:send(S#state.ref, S#state.id, Data),
+ {ok, S};
+handle_msg({Port, {exit_status, Exit}}, S = #state{port = Port}) ->
+ if
+ S#state.type =:= cli ->
+ ok = ssh_connection:send(S#state.ref, S#state.id, << "\r\n" >>);
+ true ->
+ ok
+ end,
+ ok = ssh_connection:exit_status(S#state.ref, S#state.id, Exit),
+ {stop, S#state.id, S#state{port = undefined}};
+handle_msg({'EXIT', Port, _}, S = #state{port = Port}) ->
+ ok = ssh_connection:exit_status(S#state.ref, S#state.id, 0),
+ {stop, S#state.id, S#state{port = undefined}};
+handle_msg(_Msg, S) ->
+ {ok, S}.
+
+handle_ssh_msg({ssh_cm, Ref, {data, Id, _Type, <<"q">>}}, S) ->
+ ssh_connection:send_eof(Ref, Id),
+ {stop, Id, S};
+handle_ssh_msg({ssh_cm, _Ref, {data, _Id, _Type, Data}}, S) ->
+ true = port_command(S#state.port, Data),
+ {ok, S};
+handle_ssh_msg({ssh_cm, _, {eof, _}}, S) ->
+ {ok, S};
+handle_ssh_msg({ssh_cm, Ref, {env, Id, WantReply, _Var, _Value}}, S) ->
+ ok = ssh_connection:reply_request(Ref, WantReply, success, Id),
+ {ok, S};
+handle_ssh_msg({ssh_cm, Ref, {pty, Id, WantReply, _Terminal_jox}}, S) ->
+ ok = ssh_connection:reply_request(Ref, WantReply, success, Id),
+ {ok, S};
+handle_ssh_msg({ssh_cm, Ref, {shell, Id, WantReply}}, S) ->
+ ok = ssh_connection:reply_request(Ref, WantReply, success, Id),
+ {ok, S};
+handle_ssh_msg({ssh_cm, _, {signal, _, _}}, S) ->
+ %% Ignore signals according to RFC 4254 section 6.9.
+ {ok, S};
+handle_ssh_msg({ssh_cm, _,
+ {window_change, _Id, _Width, _Height, _Pixw, _PixH}}, S) ->
+ {ok, S};
+handle_ssh_msg({ssh_cm, _, {exit_signal, Id, _, _, _}},
+ S) ->
+ {stop, Id, S}.
+
+terminate(_Why, _S) ->
+ nop.
+
+run_portprog(User, cli) ->
+ Pty_bin = os:find_executable("cat"),
+ open_port({spawn_executable, Pty_bin},
+ [stream, {cd, "/tmp"}, {env, [{"USER", User}]},
+ {args, []}, binary,
+ exit_status, use_stdio, stderr_to_stdout]).
+
+get_ssh_user(Ref) ->
+ [{user, User}] = ssh:connection_info(Ref, [user]),
+ User.
+