Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_ratio.pm

raw
1 package ProFTPD::Tests::Modules::mod_ratio;
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 ratio_bug3600 => {
19 order => ++$order,
20 test_class => [qw(bug forking)],
21 },
22
23 ratio_after_disconnect => {
24 order => ++$order,
25 test_class => [qw(bug forking)],
26 },
27
28 ratio_userratio_with_credit => {
29 order => ++$order,
30 test_class => [qw(bug 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 ratio_bug3600 {
44 my $self = shift;
45 my $tmpdir = $self->{tmpdir};
46
47 my $config_file = "$tmpdir/ratio.conf";
48 my $pid_file = File::Spec->rel2abs("$tmpdir/ratio.pid");
49 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ratio.scoreboard");
50
51 my $log_file = test_get_logfile();
52
53 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ratio.passwd");
54 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ratio.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 $ratio_file = File::Spec->rel2abs("$home_dir/.ratio");
80 if (open(my $fh, "> $ratio_file")) {
81 unless (close($fh)) {
82 die("Can't write $ratio_file: $!");
83 }
84
85 } else {
86 die("Can't open $ratio_file: $!");
87 }
88
89 my $config = {
90 PidFile => $pid_file,
91 ScoreboardFile => $scoreboard_file,
92 SystemLog => $log_file,
93 TraceLog => $log_file,
94 Trace => 'response:10',
95
96 AuthUserFile => $auth_user_file,
97 AuthGroupFile => $auth_group_file,
98 AuthOrder => 'mod_auth_file.c',
99
100 IfModules => {
101 'mod_delay.c' => {
102 DelayEngine => 'off',
103 },
104
105 'mod_ratio.c' => {
106 Ratios => 'on',
107 },
108 },
109 };
110
111 my ($port, $config_user, $config_group) = config_write($config_file, $config);
112
113 # Open pipes, for use between the parent and child processes. Specifically,
114 # the child will indicate when it's done with its test by writing a message
115 # to the parent.
116 my ($rfh, $wfh);
117 unless (pipe($rfh, $wfh)) {
118 die("Can't open pipe: $!");
119 }
120
121 my $ex;
122
123 # Fork child
124 $self->handle_sigchld();
125 defined(my $pid = fork()) or die("Can't fork: $!");
126 if ($pid) {
127 eval {
128 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
129 $client->login($user, $passwd);
130
131 my $resp_code = $client->response_code();
132 my $resp_msg = $client->response_msg(0);
133
134 my $expected;
135
136 $expected = 230;
137 $self->assert($expected == $resp_code,
138 test_msg("Expected response code $expected, got $resp_code"));
139
140 $expected = "Down: 0 Files (0mb) Up: 0 Files (0mb) 10,000,000:1 CR: LEECH";
141 $self->assert($expected eq $resp_msg,
142 test_msg("Expected response message '$expected', got '$resp_msg'"));
143
144 ($resp_code, $resp_msg) = $client->syst();
145
146 $expected = 215;
147 $self->assert($expected == $resp_code,
148 test_msg("Expected response code $expected, got $resp_code"));
149
150 $expected = "UNIX Type: L8";
151 $self->assert($expected eq $resp_msg,
152 test_msg("Expected response message '$expected', got '$resp_msg'"));
153
154 $client->quit();
155 };
156
157 if ($@) {
158 $ex = $@;
159 }
160
161 $wfh->print("done\n");
162 $wfh->flush();
163
164 } else {
165 eval { server_wait($config_file, $rfh) };
166 if ($@) {
167 warn($@);
168 exit 1;
169 }
170
171 exit 0;
172 }
173
174 # Stop server
175 server_stop($pid_file);
176
177 $self->assert_child_ok($pid);
178
179 if ($ex) {
180 test_append_logfile($log_file, $ex);
181 unlink($log_file);
182
183 die($ex);
184 }
185
186 unlink($log_file);
187 }
188
189 sub ratio_after_disconnect {
190 my $self = shift;
191 my $tmpdir = $self->{tmpdir};
192
193 my $config_file = "$tmpdir/ratio.conf";
194 my $pid_file = File::Spec->rel2abs("$tmpdir/ratio.pid");
195 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ratio.scoreboard");
196
197 my $log_file = test_get_logfile();
198
199 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ratio.passwd");
200 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ratio.group");
201
202 my $user = 'proftpd';
203 my $passwd = 'test';
204 my $group = 'ftpd';
205 my $home_dir = File::Spec->rel2abs($tmpdir);
206 my $uid = 500;
207 my $gid = 500;
208
209 # Make sure that, if we're running as root, that the home directory has
210 # permissions/privs set for the account we create
211 if ($< == 0) {
212 unless (chmod(0755, $home_dir)) {
213 die("Can't set perms on $home_dir to 0755: $!");
214 }
215
216 unless (chown($uid, $gid, $home_dir)) {
217 die("Can't set owner of $home_dir to $uid/$gid: $!");
218 }
219 }
220
221 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
222 '/bin/bash');
223 auth_group_write($auth_group_file, $group, $gid, $user);
224
225 my $ratio_file = File::Spec->rel2abs("$home_dir/ratios.dat");
226 if (open(my $fh, "> $ratio_file")) {
227 unless (close($fh)) {
228 die("Can't write $ratio_file: $!");
229 }
230
231 } else {
232 die("Can't open $ratio_file: $!");
233 }
234
235 my $ratio_tmp_file = File::Spec->rel2abs("$home_dir/ratios.tmp");
236
237 my $config = {
238 PidFile => $pid_file,
239 ScoreboardFile => $scoreboard_file,
240 SystemLog => $log_file,
241 TraceLog => $log_file,
242 Trace => 'response:10',
243
244 AuthUserFile => $auth_user_file,
245 AuthGroupFile => $auth_group_file,
246 AuthOrder => 'mod_auth_file.c',
247
248 IfModules => {
249 'mod_delay.c' => {
250 DelayEngine => 'off',
251 },
252
253 'mod_ratio.c' => {
254 Ratios => 'on',
255 SaveRatios => 'on',
256 RatioFile => $ratio_file,
257 RatioTempFile => $ratio_tmp_file,
258 CwdRatioMsg => "Please Upload first",
259 FileRatioErrMsg => "Come on, gimme a lil more",
260 ByteRatioErrMsg => "COME ON NOW",
261 },
262 },
263 };
264
265 my ($port, $config_user, $config_group) = config_write($config_file, $config);
266
267 # Open pipes, for use between the parent and child processes. Specifically,
268 # the child will indicate when it's done with its test by writing a message
269 # to the parent.
270 my ($rfh, $wfh);
271 unless (pipe($rfh, $wfh)) {
272 die("Can't open pipe: $!");
273 }
274
275 my $ex;
276
277 # Fork child
278 $self->handle_sigchld();
279 defined(my $pid = fork()) or die("Can't fork: $!");
280 if ($pid) {
281 eval {
282 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
283 $client->login($user, $passwd);
284
285 my $resp_code = $client->response_code();
286 my $resp_msg = $client->response_msg(0);
287
288 my $expected;
289
290 $expected = 230;
291 $self->assert($expected == $resp_code,
292 test_msg("Expected response code $expected, got $resp_code"));
293
294 $expected = "Down: 0 Files (0mb) Up: 0 Files (0mb) 10,000,000:1 CR: LEECH";
295 $self->assert($expected eq $resp_msg,
296 test_msg("Expected response message '$expected', got '$resp_msg'"));
297
298 my $conn = $client->stor_raw("test.txt");
299 unless ($conn) {
300 die("STOR test.txt failed: " . $client->response_code() . " " .
301 $client->response_msg());
302 }
303
304 my $buf = "Hello, World!";
305 $conn->write($buf, length($buf), 5);
306 eval { $conn->close() };
307
308 $resp_code = $client->response_code();
309 $resp_msg = $client->response_msg(1);
310 $self->assert_transfer_ok($resp_code, $resp_msg);
311
312 $conn = $client->retr_raw("test.txt");
313 unless ($conn) {
314 die("RETR test.txt failed: " . $client->response_code() . " " .
315 $client->response_msg());
316 }
317
318 $conn->read($buf, 8192, 5);
319 eval { $conn->close() };
320
321 $resp_code = $client->response_code();
322 $resp_msg = $client->response_msg(1);
323 $self->assert_transfer_ok($resp_code, $resp_msg);
324
325 $client->quit();
326 };
327
328 if ($@) {
329 $ex = $@;
330 }
331
332 $wfh->print("done\n");
333 $wfh->flush();
334
335 } else {
336 eval { server_wait($config_file, $rfh) };
337 if ($@) {
338 warn($@);
339 exit 1;
340 }
341
342 exit 0;
343 }
344
345 # Stop server
346 server_stop($pid_file);
347
348 $self->assert_child_ok($pid);
349
350 if (open(my $fh, "< $ratio_file")) {
351 my $line = <$fh>;
352 chomp($line);
353 close($fh);
354
355 my $expected = "$user|2|0|1|0";
356 $self->assert($line eq $expected,
357 test_msg("Expected RatioFile content '$expected', got '$line'"));
358
359 } else {
360 die("Can't read $ratio_file: $!");
361 }
362
363 if ($ex) {
364 test_append_logfile($log_file, $ex);
365 unlink($log_file);
366
367 die($ex);
368 }
369
370 unlink($log_file);
371 }
372
373 # See: https://bugs.launchpad.net/ubuntu/+source/proftpd-dfsg/+bug/479963
374 sub ratio_userratio_with_credit {
375 my $self = shift;
376 my $tmpdir = $self->{tmpdir};
377
378 my $config_file = "$tmpdir/ratio.conf";
379 my $pid_file = File::Spec->rel2abs("$tmpdir/ratio.pid");
380 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ratio.scoreboard");
381
382 my $log_file = test_get_logfile();
383
384 my $auth_user_file = File::Spec->rel2abs("$tmpdir/ratio.passwd");
385 my $auth_group_file = File::Spec->rel2abs("$tmpdir/ratio.group");
386
387 my $user = 'proftpd';
388 my $passwd = 'test';
389 my $group = 'ftpd';
390 my $home_dir = File::Spec->rel2abs($tmpdir);
391 my $uid = 500;
392 my $gid = 500;
393
394 # Make sure that, if we're running as root, that the home directory has
395 # permissions/privs set for the account we create
396 if ($< == 0) {
397 unless (chmod(0755, $home_dir)) {
398 die("Can't set perms on $home_dir to 0755: $!");
399 }
400
401 unless (chown($uid, $gid, $home_dir)) {
402 die("Can't set owner of $home_dir to $uid/$gid: $!");
403 }
404 }
405
406 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
407 '/bin/bash');
408 auth_group_write($auth_group_file, $group, $gid, $user);
409
410 my $ratio_file = File::Spec->rel2abs("$home_dir/ratios.dat");
411 if (open(my $fh, "> $ratio_file")) {
412 print $fh "$user|7|102404|1|51218\n";
413
414 unless (close($fh)) {
415 die("Can't write $ratio_file: $!");
416 }
417
418 } else {
419 die("Can't open $ratio_file: $!");
420 }
421
422 my $ratio_tmp_file = File::Spec->rel2abs("$home_dir/ratios.tmp");
423 if (open(my $fh, "> $ratio_tmp_file")) {
424 print $fh "$user|7|102404|1|51218\n";
425
426 unless (close($fh)) {
427 die("Can't write $ratio_tmp_file: $!");
428 }
429
430 } else {
431 die("Can't open $ratio_tmp_file: $!");
432 }
433
434 my $config = {
435 PidFile => $pid_file,
436 ScoreboardFile => $scoreboard_file,
437 SystemLog => $log_file,
438 TraceLog => $log_file,
439 Trace => 'response:10',
440
441 AuthUserFile => $auth_user_file,
442 AuthGroupFile => $auth_group_file,
443 AuthOrder => 'mod_auth_file.c',
444
445 IfModules => {
446 'mod_delay.c' => {
447 DelayEngine => 'off',
448 },
449
450 'mod_ratio.c' => {
451 Ratios => 'on',
452 SaveRatios => 'on',
453 RatioFile => $ratio_file,
454 RatioTempFile => $ratio_tmp_file,
455 CwdRatioMsg => "Please Upload first",
456 FileRatioErrMsg => "Come on, gimme a lil more",
457 ByteRatioErrMsg => "COME ON NOW",
458 UserRatio => '* 0 0 5 2355200',
459 },
460 },
461 };
462
463 my ($port, $config_user, $config_group) = config_write($config_file, $config);
464
465 # Open pipes, for use between the parent and child processes. Specifically,
466 # the child will indicate when it's done with its test by writing a message
467 # to the parent.
468 my ($rfh, $wfh);
469 unless (pipe($rfh, $wfh)) {
470 die("Can't open pipe: $!");
471 }
472
473 my $ex;
474
475 # Fork child
476 $self->handle_sigchld();
477 defined(my $pid = fork()) or die("Can't fork: $!");
478 if ($pid) {
479 eval {
480 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
481 $client->login($user, $passwd);
482
483 my $resp_code = $client->response_code();
484 my $resp_msg = $client->response_msg(0);
485
486 my $expected;
487
488 $expected = 230;
489 $self->assert($expected == $resp_code,
490 test_msg("Expected response code $expected, got $resp_code"));
491
492 $expected = "Down: 1 Files (0mb) Up: 7 Files (0mb) 1:5B CR: 2300";
493 $self->assert($expected eq $resp_msg,
494 test_msg("Expected response message '$expected', got '$resp_msg'"));
495
496 my $conn = $client->stor_raw("test.txt");
497 unless ($conn) {
498 die("STOR test.txt failed: " . $client->response_code() . " " .
499 $client->response_msg());
500 }
501
502 my $buf = "Hello, World!";
503 $conn->write($buf, length($buf), 5);
504 eval { $conn->close() };
505
506 $resp_code = $client->response_code();
507 $resp_msg = $client->response_msg(1);
508 $self->assert_transfer_ok($resp_code, $resp_msg);
509
510 $conn = $client->retr_raw("test.txt");
511 unless ($conn) {
512 die("RETR test.txt failed: " . $client->response_code() . " " .
513 $client->response_msg());
514 }
515
516 $conn->read($buf, 8192, 5);
517 eval { $conn->close() };
518
519 $resp_code = $client->response_code();
520 $resp_msg = $client->response_msg(1);
521 $self->assert_transfer_ok($resp_code, $resp_msg);
522
523 $client->quit();
524 };
525
526 if ($@) {
527 $ex = $@;
528 }
529
530 $wfh->print("done\n");
531 $wfh->flush();
532
533 } else {
534 eval { server_wait($config_file, $rfh) };
535 if ($@) {
536 warn($@);
537 exit 1;
538 }
539
540 exit 0;
541 }
542
543 # Stop server
544 server_stop($pid_file);
545
546 $self->assert_child_ok($pid);
547
548 if (open(my $fh, "< $ratio_file")) {
549 my $line = <$fh>;
550 chomp($line);
551 close($fh);
552
553 my $expected = "$user|7|102404|1|51218";
554 $self->assert($line eq $expected,
555 test_msg("Expected RatioFile content '$expected', got '$line'"));
556
557 } else {
558 die("Can't read $ratio_file: $!");
559 }
560
561 if ($ex) {
562 test_append_logfile($log_file, $ex);
563 unlink($log_file);
564
565 die($ex);
566 }
567
568 unlink($log_file);
569 }
570
571 1;