Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/CommandBufferSize.pm

raw
1 package ProFTPD::Tests::Config::CommandBufferSize;
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 cmdbuffersz_small => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 cmdbuffersz_large => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 cmdbuffersz_default => {
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 cmdbuffersz_small {
44 my $self = shift;
45 my $tmpdir = $self->{tmpdir};
46
47 my $config_file = "$tmpdir/config.conf";
48 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
49 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
50
51 my $log_file = File::Spec->rel2abs('tests.log');
52
53 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
54 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.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 $cmdbufsz = 32;
80
81 # Create a file whose name is longer than the CommandBufferSize, even
82 # taking the LIST command, and the following space character, into account.
83 my $test_file = ("A" x ($cmdbufsz - 5)) . '.txt';
84
85 my $test_path = File::Spec->rel2abs("$tmpdir/$test_file");
86 if (open(my $fh, "> $test_path")) {
87 print $fh "Hello, World!\n";
88
89 unless (close($fh)) {
90 die("Can't write $test_path: $!");
91 }
92
93 } else {
94 die("Can't open $test_path: $!");
95 }
96
97 my $idle_timeout = 3;
98
99 my $config = {
100 PidFile => $pid_file,
101 ScoreboardFile => $scoreboard_file,
102 SystemLog => $log_file,
103
104 AuthUserFile => $auth_user_file,
105 AuthGroupFile => $auth_group_file,
106 AuthOrder => 'mod_auth_file.c',
107
108 CommandBufferSize => $cmdbufsz,
109 TimeoutIdle => $idle_timeout,
110
111 IfModules => {
112 'mod_delay.c' => {
113 DelayEngine => 'off',
114 },
115 },
116 };
117
118 my ($port, $config_user, $config_group) = config_write($config_file, $config);
119
120 # Open pipes, for use between the parent and child processes. Specifically,
121 # the child will indicate when it's done with its test by writing a message
122 # to the parent.
123 my ($rfh, $wfh);
124 unless (pipe($rfh, $wfh)) {
125 die("Can't open pipe: $!");
126 }
127
128 my $ex;
129
130 # Fork child
131 $self->handle_sigchld();
132 defined(my $pid = fork()) or die("Can't fork: $!");
133 if ($pid) {
134 eval {
135 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
136 $client->login($user, $passwd);
137
138 # Since our filename is longer than the CommandBufferSize, proftpd
139 # should simply ignore this. It will fail because of the idle timeout
140 # first.
141 eval { $client->stat($test_file) };
142 unless ($@) {
143 die("STAT command succeeded unexpectedly");
144 }
145 };
146
147 if ($@) {
148 $ex = $@;
149 }
150
151 $wfh->print("done\n");
152 $wfh->flush();
153
154 } else {
155 eval { server_wait($config_file, $rfh, 15) };
156 if ($@) {
157 warn($@);
158 exit 1;
159 }
160
161 exit 0;
162 }
163
164 # Stop server
165 server_stop($pid_file);
166
167 $self->assert_child_ok($pid);
168
169 if ($ex) {
170 die($ex);
171 }
172
173 unlink($log_file);
174 }
175
176 sub cmdbuffersz_large {
177 my $self = shift;
178 my $tmpdir = $self->{tmpdir};
179
180 my $config_file = "$tmpdir/config.conf";
181 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
182 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
183
184 my $log_file = File::Spec->rel2abs('tests.log');
185
186 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
187 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
188
189 my $user = 'proftpd';
190 my $passwd = 'test';
191 my $group = 'ftpd';
192 my $home_dir = File::Spec->rel2abs($tmpdir);
193 my $uid = 500;
194 my $gid = 500;
195
196 # Make sure that, if we're running as root, that the home directory has
197 # permissions/privs set for the account we create
198 if ($< == 0) {
199 unless (chmod(0755, $home_dir)) {
200 die("Can't set perms on $home_dir to 0755: $!");
201 }
202
203 unless (chown($uid, $gid, $home_dir)) {
204 die("Can't set owner of $home_dir to $uid/$gid: $!");
205 }
206 }
207
208 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
209 '/bin/bash');
210 auth_group_write($auth_group_file, $group, $gid, $user);
211
212 # As long as CommandBufferSize is larger than PR_TUNABLE_PATH_MAX plus
213 # some, it will be largely ignored; the "too long command" limit in
214 # pr_netio_telnet_gets() will be hit first.
215 my $cmdbufsz = 8192;
216
217 my $test_file = ("A" x 251) . '.txt';
218
219 my $test_path = File::Spec->rel2abs("$tmpdir/$test_file");
220 if (open(my $fh, "> $test_path")) {
221 print $fh "Hello, World!\n";
222
223 unless (close($fh)) {
224 die("Can't write $test_path: $!");
225 }
226
227 } else {
228 die("Can't open $test_path: $!");
229 }
230
231 my $config = {
232 PidFile => $pid_file,
233 ScoreboardFile => $scoreboard_file,
234 SystemLog => $log_file,
235
236 AuthUserFile => $auth_user_file,
237 AuthGroupFile => $auth_group_file,
238 AuthOrder => 'mod_auth_file.c',
239
240 CommandBufferSize => $cmdbufsz,
241
242 IfModules => {
243 'mod_delay.c' => {
244 DelayEngine => 'off',
245 },
246 },
247 };
248
249 my ($port, $config_user, $config_group) = config_write($config_file, $config);
250
251 # Open pipes, for use between the parent and child processes. Specifically,
252 # the child will indicate when it's done with its test by writing a message
253 # to the parent.
254 my ($rfh, $wfh);
255 unless (pipe($rfh, $wfh)) {
256 die("Can't open pipe: $!");
257 }
258
259 my $ex;
260
261 # Fork child
262 $self->handle_sigchld();
263 defined(my $pid = fork()) or die("Can't fork: $!");
264 if ($pid) {
265 eval {
266 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, undef, 3);
267 $client->login($user, $passwd);
268
269 my $conn = $client->list_raw($test_file);
270 unless ($conn) {
271 die("Failed to LIST $test_file: " . $client->response_code() . " " .
272 $client->response_msg());
273 }
274
275 my $buf;
276 $conn->read($buf, 8192, 30);
277 eval { $conn->close() };
278
279 my $resp_code = $client->response_code();
280 my $resp_msg = $client->response_msg();
281
282 my $expected;
283
284 $expected = 226;
285 $self->assert($expected == $resp_code,
286 test_msg("Expected $expected, got $resp_code"));
287
288 $expected = "Transfer complete";
289 $self->assert($expected eq $resp_msg,
290 test_msg("Expected '$expected', got '$resp_msg'"));
291
292 my $bad_file = ("A" x ($cmdbufsz - 5)) . '.txt';
293
294 eval { $client->stat($bad_file) };
295 unless ($@) {
296 die("STAT $bad_file succeeded unexpectedly");
297 }
298
299 $client->quit();
300 };
301
302 if ($@) {
303 $ex = $@;
304 }
305
306 $wfh->print("done\n");
307 $wfh->flush();
308
309 } else {
310 eval { server_wait($config_file, $rfh) };
311 if ($@) {
312 warn($@);
313 exit 1;
314 }
315
316 exit 0;
317 }
318
319 # Stop server
320 server_stop($pid_file);
321
322 $self->assert_child_ok($pid);
323
324 if ($ex) {
325 die($ex);
326 }
327
328 unlink($log_file);
329 }
330
331 sub cmdbuffersz_default {
332 my $self = shift;
333 my $tmpdir = $self->{tmpdir};
334
335 my $config_file = "$tmpdir/config.conf";
336 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
337 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
338
339 my $log_file = File::Spec->rel2abs('tests.log');
340
341 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
342 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
343
344 my $user = 'proftpd';
345 my $passwd = 'test';
346 my $group = 'ftpd';
347 my $home_dir = File::Spec->rel2abs($tmpdir);
348 my $uid = 500;
349 my $gid = 500;
350
351 # Make sure that, if we're running as root, that the home directory has
352 # permissions/privs set for the account we create
353 if ($< == 0) {
354 unless (chmod(0755, $home_dir)) {
355 die("Can't set perms on $home_dir to 0755: $!");
356 }
357
358 unless (chown($uid, $gid, $home_dir)) {
359 die("Can't set owner of $home_dir to $uid/$gid: $!");
360 }
361 }
362
363 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
364 '/bin/bash');
365 auth_group_write($auth_group_file, $group, $gid, $user);
366
367 my $test_file = ("A" x 251) . '.txt';
368
369 my $test_path = File::Spec->rel2abs("$tmpdir/$test_file");
370 if (open(my $fh, "> $test_path")) {
371 print $fh "Hello, World!\n";
372
373 unless (close($fh)) {
374 die("Can't write $test_path: $!");
375 }
376
377 } else {
378 die("Can't open $test_path: $!");
379 }
380
381 my $config = {
382 PidFile => $pid_file,
383 ScoreboardFile => $scoreboard_file,
384 SystemLog => $log_file,
385
386 AuthUserFile => $auth_user_file,
387 AuthGroupFile => $auth_group_file,
388 AuthOrder => 'mod_auth_file.c',
389
390 IfModules => {
391 'mod_delay.c' => {
392 DelayEngine => 'off',
393 },
394 },
395 };
396
397 my ($port, $config_user, $config_group) = config_write($config_file, $config);
398
399 # Open pipes, for use between the parent and child processes. Specifically,
400 # the child will indicate when it's done with its test by writing a message
401 # to the parent.
402 my ($rfh, $wfh);
403 unless (pipe($rfh, $wfh)) {
404 die("Can't open pipe: $!");
405 }
406
407 my $ex;
408
409 # Fork child
410 $self->handle_sigchld();
411 defined(my $pid = fork()) or die("Can't fork: $!");
412 if ($pid) {
413 eval {
414 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, undef, 3);
415 $client->login($user, $passwd);
416
417 my $conn = $client->list_raw($test_file);
418 unless ($conn) {
419 die("Failed to LIST $test_file: " . $client->response_code() . " " .
420 $client->response_msg());
421 }
422
423 my $buf;
424 $conn->read($buf, 8192, 30);
425 eval { $conn->close() };
426
427 my $resp_code = $client->response_code();
428 my $resp_msg = $client->response_msg();
429
430 my $expected;
431
432 $expected = 226;
433 $self->assert($expected == $resp_code,
434 test_msg("Expected $expected, got $resp_code"));
435
436 $expected = "Transfer complete";
437 $self->assert($expected eq $resp_msg,
438 test_msg("Expected '$expected', got '$resp_msg'"));
439
440 my $bad_file = ("A" x (8192 - 5)) . '.txt';
441
442 eval { $client->stat($bad_file) };
443 unless ($@) {
444 die("STAT $bad_file succeeded unexpectedly");
445 }
446
447 $client->quit();
448 };
449
450 if ($@) {
451 $ex = $@;
452 }
453
454 $wfh->print("done\n");
455 $wfh->flush();
456
457 } else {
458 eval { server_wait($config_file, $rfh) };
459 if ($@) {
460 warn($@);
461 exit 1;
462 }
463
464 exit 0;
465 }
466
467 # Stop server
468 server_stop($pid_file);
469
470 $self->assert_child_ok($pid);
471
472 if ($ex) {
473 die($ex);
474 }
475
476 unlink($log_file);
477 }
478
479 1;