aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssh/test/ssh_sftpd_erlclient_SUITE.erl
blob: 6ae5e73dcb602580ec6bfe65812c60a4652241ec (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
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2007-2010. 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_sftpd_erlclient_SUITE).

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

-include("test_server.hrl").
-include("test_server_line.hrl").

-include_lib("kernel/include/file.hrl").

-define(SSHD_PORT, 9999).
-define(USER, "Alladin").
-define(PASSWD, "Sesame").
-define(SSH_MAX_PACKET_SIZE, 32768).

%% Test server callback functions
%%--------------------------------------------------------------------
%% Function: init_per_suite(Config) -> Config
%% Config - [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Description: Initiation before the whole suite
%%
%% Note: This function is free to add any key/value pairs to the Config
%% variable, but should NOT alter/remove any existing entries.
%%--------------------------------------------------------------------
init_per_suite(Config) ->
    ssh:stop(),
    crypto:start(),
    DataDir = ?config(data_dir, Config),
    FileAlt = filename:join(DataDir, "ssh_sftpd_file_alt.erl"),
    c:c(FileAlt),
    FileName = filename:join(DataDir, "test.txt"),
    {ok, FileInfo} = file:read_file_info(FileName),
    ok = file:write_file_info(FileName,
			      FileInfo#file_info{mode = 8#400}),

    Config.

%%--------------------------------------------------------------------
%% Function: end_per_suite(Config) -> _
%% Config - [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Description: Cleanup after the whole suite
%%--------------------------------------------------------------------
end_per_suite(_Config) ->
    crypto:stop(),
    ok.

%%--------------------------------------------------------------------
%% Function: init_per_testcase(TestCase, Config) -> Config
%% Case - atom()
%%   Name of the test case that is about to be run.
%% Config - [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%%
%% Description: Initiation before each test case
%%
%% Note: This function is free to add any key/value pairs to the Config
%% variable, but should NOT alter/remove any existing entries.
%% Description: Initiation before each test case
%%--------------------------------------------------------------------
init_per_testcase(TestCase, Config) ->
    ssh:start(),
    DataDir = ?config(data_dir, Config),

    Options =
	case atom_to_list(TestCase) of
	    "file_cb" ++ _ ->
		Spec =
		    ssh_sftpd:subsystem_spec([{file_handler,
					       ssh_sftpd_file_alt}]),
		[{user_passwords,[{?USER, ?PASSWD}]},
		 {system_dir, DataDir},
		 {subsystems, [Spec]}];
	    "root_dir" ->
		Privdir = ?config(priv_dir, Config),
		Root = filename:join(Privdir, root),
		file:make_dir(Root),
		Spec = ssh_sftpd:subsystem_spec([{root,Root}]),
		[{user_passwords,[{?USER, ?PASSWD}]},
		 {system_dir, DataDir},
		 {subsystems, [Spec]}];
	    "list_dir_limited" ->
		Spec =
		    ssh_sftpd:subsystem_spec([{max_files,1}]),
		[{user_passwords,[{?USER, ?PASSWD}]},
		 {system_dir, DataDir},
		 {subsystems, [Spec]}];

	    _ ->
		[{user_passwords,[{?USER, ?PASSWD}]},
		 {system_dir, DataDir}]
	end,

    {ok, Sftpd} = ssh:daemon(any, ?SSHD_PORT, Options),

    Host = ssh_test_lib:hostname(),
    {ok, ChannelPid, Connection} =
	ssh_sftp:start_channel(Host, ?SSHD_PORT,
			       [{silently_accept_hosts, true},
				{user, ?USER}, {password, ?PASSWD}, {timeout, 30000}]),
    TmpConfig = lists:keydelete(sftp, 1, Config),
    NewConfig = lists:keydelete(sftpd, 1, TmpConfig),
    [{sftp, {ChannelPid, Connection}}, {sftpd, Sftpd} | NewConfig].

%%--------------------------------------------------------------------
%% Function: end_per_testcase(TestCase, Config) -> _
%% Case - atom()
%%   Name of the test case that is about to be run.
%% Config - [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Description: Cleanup after each test case
%%--------------------------------------------------------------------
end_per_testcase(_TestCase, Config) ->
    catch ssh_sftpd:stop(?config(sftpd, Config)),
    {Sftp, Connection} = ?config(sftp, Config),
    catch ssh_sftp:stop_channel(Sftp),
    catch ssh:close(Connection),
    ssh:stop(),
    ok.

%%--------------------------------------------------------------------
%% Function: all(Clause) -> TestCases
%% Clause - atom() - suite | doc
%% TestCases - [Case]
%% Case - atom()
%%   Name of a test case.
%% Description: Returns a list of all test cases in this test suite
%%--------------------------------------------------------------------
all(doc) ->
    ["Test ssh_sftpd"];

all(suite) ->
    [close_file_OTP_6350, quit_OTP_6349, file_cb_OTP_6356,
     root_dir, list_dir_limited].

%% Test cases starts here.
%%--------------------------------------------------------------------
close_file_OTP_6350(doc) ->
    ["Test that sftpd closes its fildescriptors after compleating the "
     "transfer"];

close_file_OTP_6350(suite) ->
    [];

close_file_OTP_6350(Config) when is_list(Config) ->
    DataDir = ?config(data_dir, Config),
    FileName = filename:join(DataDir, "test.txt"),

    {Sftp, _} = ?config(sftp, Config),

    NumOfPorts = length(erlang:ports()),

    test_server:format("Number of open ports:  ~p~n", [NumOfPorts]),

    {ok, <<_/binary>>} = ssh_sftp:read_file(Sftp, FileName),

    NumOfPorts = length(erlang:ports()),

    test_server:format("Number of open ports:  ~p~n",
		       [length(erlang:ports())]),

    ok.

%%--------------------------------------------------------------------

quit_OTP_6349(doc) ->
    [" When the sftp client ends the session the "
     "server will now behave correctly and not leave the "
     "client hanging."];

quit_OTP_6349(suite) ->
    [];

quit_OTP_6349(Config) when is_list(Config) ->
    DataDir = ?config(data_dir, Config),
    FileName = filename:join(DataDir, "test.txt"),

    {Sftp, _} = ?config(sftp, Config),

    {ok, <<_/binary>>} = ssh_sftp:read_file(Sftp, FileName),

    ok = ssh_sftp:stop_channel(Sftp),

    Host = ssh_test_lib:hostname(),

    timer:sleep(5000),
    {ok, NewSftp, _Conn} = ssh_sftp:start_channel(Host, ?SSHD_PORT,
						 [{silently_accept_hosts, true},
						  {user, ?USER}, {password, ?PASSWD}]),

    {ok, <<_/binary>>} = ssh_sftp:read_file(NewSftp, FileName),

    ok = ssh_sftp:stop_channel(NewSftp),
    ok.

%%--------------------------------------------------------------------

file_cb_OTP_6356(doc) ->
    ["Test that it is possible to change the callback module for"
    " the sftpds filehandling."];

file_cb_OTP_6356(suite) ->
    [];

file_cb_OTP_6356(Config) when is_list(Config) ->
    DataDir = ?config(data_dir, Config),
    PrivDir =  ?config(priv_dir, Config),
    FileName = filename:join(DataDir, "test.txt"),

    register(sftpd_file_alt_tester, self()),

    {Sftp, _} = ?config(sftp, Config),

    {ok, Bin} = ssh_sftp:read_file(Sftp, FileName),
    alt_file_handler_check(alt_open),
    alt_file_handler_check(alt_read_file_info),
    alt_file_handler_check(alt_position),
    alt_file_handler_check(alt_read),
    alt_file_handler_check(alt_position),
    alt_file_handler_check(alt_read),
    alt_file_handler_check(alt_close),


    NewFileName = filename:join(PrivDir, "test.txt"),
    ok = ssh_sftp:write_file(Sftp, NewFileName, Bin),
    alt_file_handler_check(alt_open),
    alt_file_handler_check(alt_read_file_info),
    alt_file_handler_check(alt_position),
    alt_file_handler_check(alt_write),
    alt_file_handler_check(alt_close),

    ReFileName = filename:join(PrivDir, "test1.txt"),
    ok = ssh_sftp:rename(Sftp, NewFileName, ReFileName),
    alt_file_handler_check(alt_rename),

    ok = ssh_sftp:delete(Sftp, ReFileName),
    alt_file_handler_check(alt_delete),

    NewDir = filename:join(PrivDir, "testdir"),
    ok =  ssh_sftp:make_dir(Sftp, NewDir),
    alt_file_handler_check(alt_make_dir),

    ok = ssh_sftp:del_dir(Sftp, NewDir),
    alt_file_handler_check(alt_read_link_info),
    alt_file_handler_check(alt_write_file_info),
    alt_file_handler_check(alt_del_dir),
    ok.

root_dir(doc) ->
    [""];
root_dir(suite) ->
    [];
root_dir(Config) when is_list(Config) ->
    {Sftp, _} = ?config(sftp, Config),
    FileName = "test.txt",
    Bin =  <<"Test file for root dir option">>,
    ok = ssh_sftp:write_file(Sftp, FileName, Bin),
    {ok, Bin} = ssh_sftp:read_file(Sftp, FileName),
    {ok, Listing} =
	ssh_sftp:list_dir(Sftp, "."),
    test_server:format("Listing: ~p~n", [Listing]),
    ok.

list_dir_limited(doc) ->
    [""];
list_dir_limited(suite) ->
    [];
list_dir_limited(Config) when is_list(Config) ->
    {Sftp, _} = ?config(sftp, Config),
    {ok, Listing} =
	ssh_sftp:list_dir(Sftp, "."),
    test_server:format("Listing: ~p~n", [Listing]),
    ok.

alt_file_handler_check(Msg) ->
    receive
	Msg ->
	    ok;
	Other ->
	    test_server:fail({Msg, Other})
    after 10000 ->
	    test_server:fail("Not alt file handler")
    end.