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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2008-2015. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
-module(ssh_protocol_SUITE).
-include_lib("common_test/include/ct.hrl").
-include_lib("kernel/include/inet.hrl").
-include_lib("ssh/src/ssh.hrl"). % ?UINT32, ?BYTE, #ssh{} ...
-include_lib("ssh/src/ssh_transport.hrl").
-include_lib("ssh/src/ssh_auth.hrl").
%% Note: This directive should only be used in test suites.
-compile(export_all).
-define(NEWLINE, <<"\r\n">>).
-define(REKEY_DATA_TMO, 65000).
-define(v(Key, Config), proplists:get_value(Key, Config)).
-define(v(Key, Config, Default), proplists:get_value(Key, Config, Default)).
%%--------------------------------------------------------------------
%% Common Test interface functions -----------------------------------
%%--------------------------------------------------------------------
suite() ->
[{ct_hooks,[ts_install_cth]}].
all() ->
[{group,tool_tests}
].
groups() ->
[{tool_tests, [], [lib_works_as_client,
lib_works_as_server,
lib_match,
lib_no_match
]}
].
init_per_suite(Config) ->
start_std_daemon( setup_dirs( start_apps(Config))).
end_per_suite(Config) ->
stop_apps(Config).
init_per_testcase(_TestCase, Config) ->
check_std_daemon_works(Config, ?LINE).
end_per_testcase(_TestCase, Config) ->
check_std_daemon_works(Config, ?LINE).
%%%--------------------------------------------------------------------
%%% Test Cases --------------------------------------------------------
%%%--------------------------------------------------------------------
%%%--------------------------------------------------------------------
%%% Connect to an erlang server and check that the testlib acts as a client.
lib_works_as_client(Config) ->
%% Connect and negotiate keys
{ok,InitialState} =
ssh_trpt_test_lib:exec(
[{set_options, [print_ops, print_seqnums, print_messages]},
{connect,
server_host(Config),server_port(Config),
[{silently_accept_hosts, true},
{user_dir, user_dir(Config)},
{user_interaction, false}]},
receive_hello,
{send, hello},
{send, ssh_msg_kexinit},
{match, #ssh_msg_kexinit{_='_'}, receive_msg},
{send, ssh_msg_kexdh_init},
{match,# ssh_msg_kexdh_reply{_='_'}, receive_msg},
{send, #ssh_msg_newkeys{}},
{match, #ssh_msg_newkeys{_='_'}, receive_msg}
]
),
%% Do the authentcation
{User,Pwd} = server_user_password(Config),
{ok,EndState} =
ssh_trpt_test_lib:exec(
[{send, #ssh_msg_service_request{name = "ssh-userauth"}},
{match, #ssh_msg_service_accept{name = "ssh-userauth"}, receive_msg},
{send, #ssh_msg_userauth_request{user = User,
service = "ssh-connection",
method = "password",
data = <<?BOOLEAN(?FALSE),
?STRING(unicode:characters_to_binary(Pwd))>>
}},
{match, #ssh_msg_userauth_success{_='_'}, receive_msg}
], InitialState),
%% Disconnect
{ok,_} =
ssh_trpt_test_lib:exec(
[{send, #ssh_msg_disconnect{code = ?SSH_DISCONNECT_BY_APPLICATION,
description = "End of the fun",
language = ""
}},
close_socket
], EndState).
%%--------------------------------------------------------------------
%%% Connect an erlang client and check that the testlib can act as a server.
lib_works_as_server(Config) ->
{User,_Pwd} = server_user_password(Config),
%% Create a listening socket as server socket:
{ok,InitialState} = ssh_trpt_test_lib:exec(listen),
HostPort = ssh_trpt_test_lib:server_host_port(InitialState),
%% Start a process handling one connection on the server side:
spawn_link(
fun() ->
{ok,_} =
ssh_trpt_test_lib:exec(
[{set_options, [print_ops, print_messages]},
{accept, [{system_dir, system_dir(Config)},
{user_dir, user_dir(Config)}]},
receive_hello,
{send, hello},
{send, ssh_msg_kexinit},
{match, #ssh_msg_kexinit{_='_'}, receive_msg},
{match, #ssh_msg_kexdh_init{_='_'}, receive_msg},
{send, ssh_msg_kexdh_reply},
{send, #ssh_msg_newkeys{}},
{match, #ssh_msg_newkeys{_='_'}, receive_msg},
{match, #ssh_msg_service_request{name="ssh-userauth"}, receive_msg},
{send, #ssh_msg_service_accept{name="ssh-userauth"}},
{match, #ssh_msg_userauth_request{service="ssh-connection",
method="none",
user=User,
_='_'}, receive_msg},
{send, #ssh_msg_userauth_failure{authentications = "password",
partial_success = false}},
{match, #ssh_msg_userauth_request{service="ssh-connection",
method="password",
user=User,
_='_'}, receive_msg},
{send, #ssh_msg_userauth_success{}},
close_socket,
print_state
],
InitialState)
end),
%% and finally connect to it with a regular Erlang SSH client:
{ok,_} = std_connect(HostPort, Config).
%%--------------------------------------------------------------------
%%% Matching
lib_match(_Config) ->
{ok,_} =
ssh_trpt_test_lib:exec([{set_options, [print_ops]},
{match, abc, abc},
{match, '$a', {cde,fgh}},
{match, {cde,fgh}, '$a'},
{match, '_', {cde,fgh}},
{match, [a,'$a',b], [a,{cde,fgh},b]},
{match, [a,'$a'|'$b'], [a,{cde,fgh},b,c]},
{match, '$b', [b,c]}
]).
%%--------------------------------------------------------------------
%%% Not matching
lib_no_match(_Config) ->
case ssh_trpt_test_lib:exec([{set_options, [print_ops]},
{match, '$x', b},
{match, a, '$x'}])
of
{ok,_} -> {fail,"Unexpected match"};
{error, {_Op,{expected,a,b},_State}} -> ok
end.
%%%================================================================
%%%==== Internal functions ========================================
%%%================================================================
%%%---- init_suite and end_suite ---------------------------------------
start_apps(Config) ->
catch crypto:stop(),
case catch crypto:start() of
ok ->
catch ssh:stop(),
ok = ssh:start(),
[{stop_apps,
fun() ->
ssh:stop(),
crypto:stop()
end} | Config];
_Else ->
{skip, "Crypto could not be started!"}
end.
stop_apps(Config) ->
(?v(stop_apps, Config, fun()-> ok end))(),
ssh:stop().
setup_dirs(Config) ->
DataDir = ?config(data_dir, Config),
PrivDir = ?config(priv_dir, Config),
ssh_test_lib:setup_rsa(DataDir, PrivDir),
Config.
system_dir(Config) -> filename:join(?config(priv_dir, Config), system).
user_dir(Config) -> ?config(priv_dir, Config).
%%%----------------------------------------------------------------
start_std_daemon(Config) ->
start_std_daemon(Config, []).
start_std_daemon(Config, ExtraOpts) ->
PrivDir = ?config(priv_dir, Config),
UserDir = filename:join(PrivDir, nopubkey), % to make sure we don't use public-key-auth
file:make_dir(UserDir),
UserPasswords = [{"user1","pwd1"}],
Options = [{system_dir, system_dir(Config)},
{user_dir, user_dir(Config)},
{user_passwords, UserPasswords},
{failfun, fun ssh_test_lib:failfun/2}
| ExtraOpts],
Ref = {Server, Host, Port} = ssh_test_lib:daemon(Options),
ct:log("Std server ~p started at ~p:~p~nOptions=~p",[Server, Host, Port, Options]),
[{server,Ref}, {user_passwords, UserPasswords} | Config].
stop_std_daemon(Config) ->
ssh:stop_daemon(server_pid(Config)),
ct:log("Std server ~p at ~p:~p stopped", [server_pid(Config), server_host(Config), server_port(Config)]),
lists:keydelete(server, 1, Config).
check_std_daemon_works(Config, Line) ->
case std_connect(Config) of
{ok,C} ->
ct:log("Server ~p:~p ~p is ok at line ~p",
[server_host(Config), server_port(Config),
server_pid(Config), Line]),
ok = ssh:close(C),
Config;
Error = {error,_} ->
{fail,
lists:flatten(
io_lib:format("Standard server ~p:~p ~p is ill at line ~p: ~p",
[server_host(Config), server_port(Config),
server_pid(Config), Line, Error])
)
}
end.
server_pid(Config) -> element(1,?v(server,Config)).
server_host(Config) -> element(2,?v(server,Config)).
server_port(Config) -> element(3,?v(server,Config)).
server_user_password(Config) -> server_user_password(1, Config).
server_user_password(N, Config) -> lists:nth(N, ?v(user_passwords,Config)).
std_connect(Config) ->
{User,Pwd} = server_user_password(Config),
std_connect(server_host(Config), server_port(Config),
Config,
[{user,User},{password,Pwd}]).
std_connect({Host,Port}, Config) ->
{User,Pwd} = server_user_password(Config),
std_connect(Host, Port, Config, [{user,User},{password,Pwd}]).
std_connect({Host,Port}, Config, Opts) ->
std_connect(Host, Port, Config, Opts).
std_connect(Host, Port, Config, Opts) ->
ssh:connect(Host, Port,
[{silently_accept_hosts, true},
{user_dir, user_dir(Config)},
{user_interaction, false} | Opts],
30000).
%%%----------------------------------------------------------------
|