aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssh/test/ssh_chan_behaviours_SUITE.erl
blob: 16ed152bcdc7fa2978e3db872048b803179d5eff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2018-2018. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

%%

-module(ssh_chan_behaviours_SUITE).

-include_lib("common_test/include/ct.hrl").
-include_lib("ssh/src/ssh.hrl").
-include("ssh_test_lib.hrl").

%% Note: This directive should only be used in test suites.
-compile(export_all).

%%--------------------------------------------------------------------
%% Common Test interface functions -----------------------------------
%%--------------------------------------------------------------------

suite() ->
    [{ct_hooks,[ts_install_cth]},
     {timetrap,{seconds,60}}].

all() -> 
    [
     noexist_subsystem,
     undefined_subsystem,
     defined_subsystem,
     subsystem_client
    ].

%%--------------------------------------------------------------------
init_per_suite(Config) ->
    ?CHECK_CRYPTO(
       begin
           ssh:start(),
           Config
       end).

end_per_suite(_Config) ->
    {Time,R} = timer:tc(ssh, stop, []),
    ct:log("Stop ssh: ~p ms",[(100*(Time div 1000)) / 100]),
    R.

init_per_testcase(_TC, Config) ->
    SubSystems = [
                  {"bad_cb", {ssh_chan_behaviours_undefined, []}}, % A non-existing file
                  {"ch1",    {ssh_chan_behaviours_server,    [self(),true]}}
                 ],
    {Pid, Host, Port} = ssh_test_lib:std_daemon(Config, [{subsystems,SubSystems}]),
    C = ssh_test_lib:std_connect(Config, Host, Port, []),
    [{connref,C}, {daemon_pid,Pid}| Config].

end_per_testcase(_TC, Config) ->
    {Time,_} = timer:tc(ssh, stop_daemon, [proplists:get_value(daemon_pid,Config)]),
    ct:log("Stop daemon: ~p ms",[(100*(Time div 1000)) / 100]),
    case flush() of
        [] -> ok;
        Msgs -> ct:pal("Unhandled messages:~n~p", [Msgs])
    end.
    

-define(EXPECT(What, Bind),
        Bind =
            (fun() ->
                     receive What ->
                             ct:log("~p:~p ~p got ~p",[?MODULE,?LINE,self(),What]),
                             Bind
                     after 5000 ->
                             ct:log("~p:~p ~p Flushed:~n~p",[?MODULE,?LINE,self(),flush()]),
                             ct:fail("Timeout!",[])
                     end
             end)()
        ).

%%--------------------------------------------------------------------
%% Test Cases --------------------------------------------------------
%%--------------------------------------------------------------------
%% Try start a subsystem whos name is not known by the server
noexist_subsystem(Config) ->
    C = proplists:get_value(connref, Config),
    {ok, Ch} = ssh_connection:session_channel(C, infinity),
    failure = ssh_connection:subsystem(C, Ch, "noexist", infinity),
    ok = ssh_connection:close(C, Ch),
    ?EXPECT({ssh_cm,C,{closed,Ch}},[]),
    ok.

%% Try to start a subsystem with a known name, but without any callback file
undefined_subsystem(Config) ->
    C = proplists:get_value(connref, Config),
    {ok, Ch} = ssh_connection:session_channel(C, infinity),
    failure = ssh_connection:subsystem(C, Ch, "bad_cb", infinity),
    ok = ssh_connection:close(C, Ch),
    ?EXPECT({ssh_cm,C,{closed,Ch}},[]), % self() is instead of a proper channel handler
    ok.

%% Try to start and stop a subsystem with known name and defined callback file
defined_subsystem(Config) ->
    C = proplists:get_value(connref, Config),
    {ok, Ch1} = ssh_connection:session_channel(C, infinity),

    success = ssh_connection:subsystem(C, Ch1, "ch1", infinity),
    IDsrv = ?EXPECT({{_Csrv,_Ch1srv}, {ssh_channel_up,_Ch1srv,_Csrv}}, {_Csrv,_Ch1srv}),

    ok = ssh_connection:close(C, Ch1),
    ?EXPECT({IDsrv, {terminate,normal}}, []),
    ?EXPECT({ssh_cm, C, {closed,Ch1}}, []), % self() is instead of a proper channel handler
    ok.

%% Try to start and stop a subsystem from a ssh_client_channel behviour
subsystem_client(Config) ->
    C = proplists:get_value(connref, Config),

    {ok,ChRef} = ssh_chan_behaviours_client:start_link(C),
    IDclt = ?EXPECT({{C,Ch1clt},     {ssh_channel_up,Ch1clt,C}},     {C,Ch1clt}),
    IDsrv = ?EXPECT({{_Csrv,Ch1srv}, {ssh_channel_up,Ch1srv,_Csrv}}, {_Csrv,Ch1srv}),

    ok = ssh_chan_behaviours_client:stop(ChRef),
    ?EXPECT({IDclt, {terminate,normal}}, []), % From the proper channel handler
    ?EXPECT({IDsrv, {terminate,normal}}, []),
    ok.

%%%================================================================
%%%
%%%

flush() -> lists:reverse(flush([])).

flush(Acc) ->
    receive
        M ->
            flush([M|Acc])
    after 0 ->
            Acc
    end.