Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/PWD.pm

raw
1 package ProFTPD::Tests::Commands::PWD;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Spec;
8 use IO::Handle;
9
10 use ProFTPD::TestSuite::FTP;
11 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
12
13 $| = 1;
14
15 my $order = 0;
16
17 my $TESTS = {
18 pwd_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 xpwd_ok => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 pwd_upload_only => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 };
34
35 sub new {
36 return shift()->SUPER::new(@_);
37 }
38
39 sub list_tests {
40 return testsuite_get_runnable_tests($TESTS);
41 }
42
43 sub pwd_ok {
44 my $self = shift;
45 my $tmpdir = $self->{tmpdir};
46
47 my $config_file = "$tmpdir/cmds.conf";
48 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
49 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
50
51 my $log_file = test_get_logfile();
52
53 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
54 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
55
56 my $user = 'proftpd';
57 my $passwd = 'test';
58 my $group = 'ftpd';
59 my $home_dir = File::Spec->rel2abs($tmpdir);
60 my $uid = 500;
61 my $gid = 500;
62
63 # Make sure that, if we're running as root, that the home directory has
64 # permissions/privs set for the account we create
65 if ($< == 0) {
66 unless (chmod(0755, $home_dir)) {
67 die("Can't set perms on $home_dir to 0755: $!");
68 }
69
70 unless (chown($uid, $gid, $home_dir)) {
71 die("Can't set owner of $home_dir to $uid/$gid: $!");
72 }
73 }
74
75 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
76 '/bin/bash');
77 auth_group_write($auth_group_file, $group, $gid, $user);
78
79 my $config = {
80 PidFile => $pid_file,
81 ScoreboardFile => $scoreboard_file,
82 SystemLog => $log_file,
83
84 AuthUserFile => $auth_user_file,
85 AuthGroupFile => $auth_group_file,
86 AuthOrder => 'mod_auth_file.c',
87
88 IfModules => {
89 'mod_delay.c' => {
90 DelayEngine => 'off',
91 },
92 },
93 };
94
95 my ($port, $config_user, $config_group) = config_write($config_file, $config);
96
97 # Open pipes, for use between the parent and child processes. Specifically,
98 # the child will indicate when it's done with its test by writing a message
99 # to the parent.
100 my ($rfh, $wfh);
101 unless (pipe($rfh, $wfh)) {
102 die("Can't open pipe: $!");
103 }
104
105 my $ex;
106
107 # Fork child
108 $self->handle_sigchld();
109 defined(my $pid = fork()) or die("Can't fork: $!");
110 if ($pid) {
111 eval {
112 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
113 $client->login($user, $passwd);
114
115 my ($resp_code, $resp_msg);
116 ($resp_code, $resp_msg) = $client->pwd();
117
118 my $expected;
119
120 $expected = 257;
121 $self->assert($expected == $resp_code,
122 test_msg("Expected $expected, got $resp_code"));
123
124 $expected = "\"$home_dir\" is the current directory";
125
126 if ($^O eq 'darwin') {
127 # MacOSX-specific hack, due to how it handles tmp files
128 $expected = '"/private' . "$home_dir\" is the current directory";
129 }
130
131 $self->assert($expected eq $resp_msg,
132 test_msg("Expected '$expected', got '$resp_msg'"));
133 };
134
135 if ($@) {
136 $ex = $@;
137 }
138
139 $wfh->print("done\n");
140 $wfh->flush();
141
142 } else {
143 eval { server_wait($config_file, $rfh) };
144 if ($@) {
145 warn($@);
146 exit 1;
147 }
148
149 exit 0;
150 }
151
152 # Stop server
153 server_stop($pid_file);
154
155 $self->assert_child_ok($pid);
156
157 if ($ex) {
158 test_append_logfile($log_file, $ex);
159 unlink($log_file);
160
161 die($ex);
162 }
163
164 unlink($log_file);
165 }
166
167 sub xpwd_ok {
168 my $self = shift;
169 my $tmpdir = $self->{tmpdir};
170
171 my $config_file = "$tmpdir/cmds.conf";
172 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
173 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
174
175 my $log_file = test_get_logfile();
176
177 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
178 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
179
180 my $user = 'proftpd';
181 my $passwd = 'test';
182 my $group = 'ftpd';
183 my $home_dir = File::Spec->rel2abs($tmpdir);
184 my $uid = 500;
185 my $gid = 500;
186
187 # Make sure that, if we're running as root, that the home directory has
188 # permissions/privs set for the account we create
189 if ($< == 0) {
190 unless (chmod(0755, $home_dir)) {
191 die("Can't set perms on $home_dir to 0755: $!");
192 }
193
194 unless (chown($uid, $gid, $home_dir)) {
195 die("Can't set owner of $home_dir to $uid/$gid: $!");
196 }
197 }
198
199 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
200 '/bin/bash');
201 auth_group_write($auth_group_file, $group, $gid, $user);
202
203 my $config = {
204 PidFile => $pid_file,
205 ScoreboardFile => $scoreboard_file,
206 SystemLog => $log_file,
207
208 AuthUserFile => $auth_user_file,
209 AuthGroupFile => $auth_group_file,
210 AuthOrder => 'mod_auth_file.c',
211
212 IfModules => {
213 'mod_delay.c' => {
214 DelayEngine => 'off',
215 },
216 },
217 };
218
219 my ($port, $config_user, $config_group) = config_write($config_file, $config);
220
221 # Open pipes, for use between the parent and child processes. Specifically,
222 # the child will indicate when it's done with its test by writing a message
223 # to the parent.
224 my ($rfh, $wfh);
225 unless (pipe($rfh, $wfh)) {
226 die("Can't open pipe: $!");
227 }
228
229 my $ex;
230
231 # Fork child
232 $self->handle_sigchld();
233 defined(my $pid = fork()) or die("Can't fork: $!");
234 if ($pid) {
235 eval {
236 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
237
238 $client->login($user, $passwd);
239
240 my ($resp_code, $resp_msg);
241 ($resp_code, $resp_msg) = $client->xpwd();
242
243 my $expected;
244
245 $expected = 257;
246 $self->assert($expected == $resp_code,
247 test_msg("Expected $expected, got $resp_code"));
248
249 $expected = "\"$home_dir\" is the current directory";
250
251 if ($^O eq 'darwin') {
252 # MacOSX-specific hack, due to how it handles tmp files
253 $expected = '"/private' . "$home_dir\" is the current directory";
254 }
255
256 $self->assert($expected eq $resp_msg,
257 test_msg("Expected '$expected', got '$resp_msg'"));
258 };
259
260 if ($@) {
261 $ex = $@;
262 }
263
264 $wfh->print("done\n");
265 $wfh->flush();
266
267 } else {
268 eval { server_wait($config_file, $rfh) };
269 if ($@) {
270 warn($@);
271 exit 1;
272 }
273
274 exit 0;
275 }
276
277 # Stop server
278 server_stop($pid_file);
279
280 $self->assert_child_ok($pid);
281
282 if ($ex) {
283 test_append_logfile($log_file, $ex);
284 unlink($log_file);
285
286 die($ex);
287 }
288
289 unlink($log_file);
290 }
291
292 sub pwd_upload_only {
293 my $self = shift;
294 my $tmpdir = $self->{tmpdir};
295
296 my $config_file = "$tmpdir/cmds.conf";
297 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
298 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
299
300 my $log_file = test_get_logfile();
301
302 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
303 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
304
305 my $user = 'proftpd';
306 my $passwd = 'test';
307 my $group = 'ftpd';
308 my $home_dir = File::Spec->rel2abs($tmpdir);
309 my $uid = 500;
310 my $gid = 500;
311
312 # Make sure that, if we're running as root, that the home directory has
313 # permissions/privs set for the account we create
314 if ($< == 0) {
315 unless (chmod(0755, $home_dir)) {
316 die("Can't set perms on $home_dir to 0755: $!");
317 }
318
319 unless (chown($uid, $gid, $home_dir)) {
320 die("Can't set owner of $home_dir to $uid/$gid: $!");
321 }
322 }
323
324 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
325 '/bin/bash');
326 auth_group_write($auth_group_file, $group, $gid, $user);
327
328 my $config = {
329 PidFile => $pid_file,
330 ScoreboardFile => $scoreboard_file,
331 SystemLog => $log_file,
332
333 AuthUserFile => $auth_user_file,
334 AuthGroupFile => $auth_group_file,
335 AuthOrder => 'mod_auth_file.c',
336
337 DefaultChdir => '~',
338
339 IfModules => {
340 'mod_delay.c' => {
341 DelayEngine => 'off',
342 },
343 },
344 };
345
346 my ($port, $config_user, $config_group) = config_write($config_file, $config);
347
348 if (open(my $fh, ">> $config_file")) {
349 print $fh <<EOC;
350 <Directory ~>
351 <Limit ALL>
352 DenyAll
353 </Limit>
354
355 <Limit CDUP CWD PWD XCWD XCUP>
356 AllowAll
357 </Limit>
358
359 <Limit STOR STOU>
360 AllowAll
361 </Limit>
362 </Directory>
363 EOC
364 unless (close($fh)) {
365 die("Can't write $config_file: $!");
366 }
367
368 } else {
369 die("Can't open $config_file: $!");
370 }
371
372 # Open pipes, for use between the parent and child processes. Specifically,
373 # the child will indicate when it's done with its test by writing a message
374 # to the parent.
375 my ($rfh, $wfh);
376 unless (pipe($rfh, $wfh)) {
377 die("Can't open pipe: $!");
378 }
379
380 my $ex;
381
382 # Fork child
383 $self->handle_sigchld();
384 defined(my $pid = fork()) or die("Can't fork: $!");
385 if ($pid) {
386 eval {
387 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
388 $client->login($user, $passwd);
389
390 my ($resp_code, $resp_msg);
391 ($resp_code, $resp_msg) = $client->pwd();
392
393 my $expected;
394
395 $expected = 257;
396 $self->assert($expected == $resp_code,
397 test_msg("Expected $expected, got $resp_code"));
398
399 $expected = "\"$home_dir\" is the current directory";
400
401 if ($^O eq 'darwin') {
402 # MacOSX-specific hack, due to how it handles tmp files
403 $expected = '"/private' . "$home_dir\" is the current directory";
404 }
405
406 $self->assert($expected eq $resp_msg,
407 test_msg("Expected '$expected', got '$resp_msg'"));
408 };
409
410 if ($@) {
411 $ex = $@;
412 }
413
414 $wfh->print("done\n");
415 $wfh->flush();
416
417 } else {
418 eval { server_wait($config_file, $rfh) };
419 if ($@) {
420 warn($@);
421 exit 1;
422 }
423
424 exit 0;
425 }
426
427 # Stop server
428 server_stop($pid_file);
429
430 $self->assert_child_ok($pid);
431
432 if ($ex) {
433 test_append_logfile($log_file, $ex);
434 unlink($log_file);
435
436 die($ex);
437 }
438
439 unlink($log_file);
440 }
441
442 1;