Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::MaxCommandRate;
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 use Time::HiRes qw(gettimeofday tv_interval);
10
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 maxcmdrate_ok => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 maxcmdrate_exceeded => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 };
30
31 sub new {
32 return shift()->SUPER::new(@_);
33 }
34
35 sub list_tests {
36 return testsuite_get_runnable_tests($TESTS);
37 }
38
39 sub maxcmdrate_ok {
40 my $self = shift;
41 my $tmpdir = $self->{tmpdir};
42
43 my $config_file = "$tmpdir/config.conf";
44 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
45 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
46
47 my $log_file = File::Spec->rel2abs('tests.log');
48
49 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
50 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
51
52 my $user = 'proftpd';
53 my $passwd = 'test';
54 my $group = 'ftpd';
55 my $home_dir = File::Spec->rel2abs($tmpdir);
56 my $uid = 500;
57 my $gid = 500;
58
59 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
60 '/bin/bash');
61 auth_group_write($auth_group_file, $group, $gid, $user);
62
63 my $max_cmd_rate = 10;
64
65 my $config = {
66 PidFile => $pid_file,
67 ScoreboardFile => $scoreboard_file,
68 SystemLog => $log_file,
69
70 AuthUserFile => $auth_user_file,
71 AuthGroupFile => $auth_group_file,
72 AuthOrder => 'mod_auth_file.c',
73
74 MaxCommandRate => $max_cmd_rate,
75
76 IfModules => {
77 'mod_delay.c' => {
78 DelayEngine => 'off',
79 },
80 },
81 };
82
83 my ($port, $config_user, $config_group) = config_write($config_file, $config);
84
85 # Open pipes, for use between the parent and child processes. Specifically,
86 # the child will indicate when it's done with its test by writing a message
87 # to the parent.
88 my ($rfh, $wfh);
89 unless (pipe($rfh, $wfh)) {
90 die("Can't open pipe: $!");
91 }
92
93 my $ex;
94
95 # Fork child
96 $self->handle_sigchld();
97 defined(my $pid = fork()) or die("Can't fork: $!");
98 if ($pid) {
99 eval {
100 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
101 $client->login($user, $passwd);
102 $client->syst();
103 $client->quit();
104 };
105
106 if ($@) {
107 $ex = $@;
108 }
109
110 $wfh->print("done\n");
111 $wfh->flush();
112
113 } else {
114 eval { server_wait($config_file, $rfh) };
115 if ($@) {
116 warn($@);
117 exit 1;
118 }
119
120 exit 0;
121 }
122
123 # Stop server
124 server_stop($pid_file);
125
126 $self->assert_child_ok($pid);
127
128 if ($ex) {
129 die($ex);
130 }
131
132 unlink($log_file);
133 }
134
135 sub maxcmdrate_exceeded {
136 my $self = shift;
137 my $tmpdir = $self->{tmpdir};
138
139 my $config_file = "$tmpdir/config.conf";
140 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
141 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
142
143 my $log_file = File::Spec->rel2abs('tests.log');
144
145 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
146 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
147
148 my $user = 'proftpd';
149 my $passwd = 'test';
150 my $group = 'ftpd';
151 my $home_dir = File::Spec->rel2abs($tmpdir);
152 my $uid = 500;
153 my $gid = 500;
154
155 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
156 '/bin/bash');
157 auth_group_write($auth_group_file, $group, $gid, $user);
158
159 my $max_cmd_rate = 10;
160
161 my $config = {
162 PidFile => $pid_file,
163 ScoreboardFile => $scoreboard_file,
164 SystemLog => $log_file,
165
166 AuthUserFile => $auth_user_file,
167 AuthGroupFile => $auth_group_file,
168 AuthOrder => 'mod_auth_file.c',
169
170 MaxCommandRate => $max_cmd_rate,
171
172 IfModules => {
173 'mod_delay.c' => {
174 DelayEngine => 'off',
175 },
176 },
177 };
178
179 my ($port, $config_user, $config_group) = config_write($config_file, $config);
180
181 # Open pipes, for use between the parent and child processes. Specifically,
182 # the child will indicate when it's done with its test by writing a message
183 # to the parent.
184 my ($rfh, $wfh);
185 unless (pipe($rfh, $wfh)) {
186 die("Can't open pipe: $!");
187 }
188
189 my $ex;
190
191 # Fork child
192 $self->handle_sigchld();
193 defined(my $pid = fork()) or die("Can't fork: $!");
194 if ($pid) {
195 eval {
196 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
197 $client->login($user, $passwd);
198
199 for (my $i = 0; $i < ($max_cmd_rate * 5); $i++) {
200 my $start = [gettimeofday()];
201 $client->syst();
202 my $elapsed = tv_interval($start);
203
204 if ($ENV{TEST_VERBOSE}) {
205 print STDERR "$i: elapsed = $elapsed\n";
206 }
207 }
208
209 $client->quit();
210 };
211
212 if ($@) {
213 $ex = $@;
214 }
215
216 $wfh->print("done\n");
217 $wfh->flush();
218
219 } else {
220 eval { server_wait($config_file, $rfh, 30) };
221 if ($@) {
222 warn($@);
223 exit 1;
224 }
225
226 exit 0;
227 }
228
229 # Stop server
230 server_stop($pid_file);
231
232 $self->assert_child_ok($pid);
233
234 if ($ex) {
235 die($ex);
236 }
237
238 unlink($log_file);
239 }
240
241 1;