Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::MaxRetrieveFileSize;
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 maxretrievefilesize_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 maxretrievefilesize_exceeded => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 };
29
30 sub new {
31 return shift()->SUPER::new(@_);
32 }
33
34 sub list_tests {
35 return testsuite_get_runnable_tests($TESTS);
36 }
37
38 sub maxretrievefilesize_ok {
39 my $self = shift;
40 my $tmpdir = $self->{tmpdir};
41
42 my $config_file = "$tmpdir/config.conf";
43 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
44 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
45
46 my $log_file = File::Spec->rel2abs('tests.log');
47
48 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
49 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
50
51 my $user = 'proftpd';
52 my $passwd = 'test';
53 my $home_dir = File::Spec->rel2abs($tmpdir);
54 my $uid = 500;
55 my $gid = 500;
56
57 # Make sure that, if we're running as root, that the home directory has
58 # permissions/privs set for the account we create
59 if ($< == 0) {
60 unless (chmod(0755, $home_dir)) {
61 die("Can't set perms on $home_dir to 0755: $!");
62 }
63
64 unless (chown($uid, $gid, $home_dir)) {
65 die("Can't set owner of $home_dir to $uid/$gid: $!");
66 }
67 }
68
69 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
70 '/bin/bash');
71 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
72
73 my $src_file = File::Spec->rel2abs("$tmpdir/foo");
74 if (open(my $fh, "> $src_file")) {
75 print $fh "Hello, World!\n";
76
77 unless (close($fh)) {
78 die("Can't write $src_file: $!");
79 }
80
81 } else {
82 die("Can't open $src_file: $!");
83 }
84
85 my $config = {
86 PidFile => $pid_file,
87 ScoreboardFile => $scoreboard_file,
88 SystemLog => $log_file,
89
90 AuthUserFile => $auth_user_file,
91 AuthGroupFile => $auth_group_file,
92 AuthOrder => 'mod_auth_file.c',
93
94 MaxRetrieveFileSize => '100 B',
95
96 IfModules => {
97 'mod_delay.c' => {
98 DelayEngine => 'off',
99 },
100 },
101 };
102
103 my ($port, $config_user, $config_group) = config_write($config_file, $config);
104
105 # Open pipes, for use between the parent and child processes. Specifically,
106 # the child will indicate when it's done with its test by writing a message
107 # to the parent.
108 my ($rfh, $wfh);
109 unless (pipe($rfh, $wfh)) {
110 die("Can't open pipe: $!");
111 }
112
113 my $ex;
114
115 # Fork child
116 $self->handle_sigchld();
117 defined(my $pid = fork()) or die("Can't fork: $!");
118 if ($pid) {
119 eval {
120 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
121
122 $client->login($user, $passwd);
123
124 my $conn = $client->retr_raw('foo');
125 unless ($conn) {
126 die("RETR failed: " . $client->response_code() . " " .
127 $client->response_msg());
128 }
129
130 my $buf;
131 $conn->read($buf, 8192, 30);
132 eval { $conn->close() };
133
134 my $expected;
135
136 $expected = 14;
137 my $buflen = length($buf);
138 $self->assert($expected == $buflen,
139 test_msg("Expected $expected, got $buflen"));
140 };
141
142 if ($@) {
143 $ex = $@;
144 }
145
146 $wfh->print("done\n");
147 $wfh->flush();
148
149 } else {
150 eval { server_wait($config_file, $rfh) };
151 if ($@) {
152 warn($@);
153 exit 1;
154 }
155
156 exit 0;
157 }
158
159 # Stop server
160 server_stop($pid_file);
161
162 $self->assert_child_ok($pid);
163
164 if ($ex) {
165 die($ex);
166 }
167
168 unlink($log_file);
169 }
170
171 sub maxretrievefilesize_exceeded {
172 my $self = shift;
173 my $tmpdir = $self->{tmpdir};
174
175 my $config_file = "$tmpdir/config.conf";
176 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
177 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
178
179 my $log_file = File::Spec->rel2abs('tests.log');
180
181 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
182 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
183
184 my $user = 'proftpd';
185 my $passwd = 'test';
186 my $home_dir = File::Spec->rel2abs($tmpdir);
187 my $uid = 500;
188 my $gid = 500;
189
190 # Make sure that, if we're running as root, that the home directory has
191 # permissions/privs set for the account we create
192 if ($< == 0) {
193 unless (chmod(0755, $home_dir)) {
194 die("Can't set perms on $home_dir to 0755: $!");
195 }
196
197 unless (chown($uid, $gid, $home_dir)) {
198 die("Can't set owner of $home_dir to $uid/$gid: $!");
199 }
200 }
201
202 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
203 '/bin/bash');
204 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
205
206 my $src_file = File::Spec->rel2abs("$tmpdir/foo");
207 if (open(my $fh, "> $src_file")) {
208 print $fh "Hello, World!\n";
209
210 unless (close($fh)) {
211 die("Can't write $src_file: $!");
212 }
213
214 } else {
215 die("Can't open $src_file: $!");
216 }
217
218 my $config = {
219 PidFile => $pid_file,
220 ScoreboardFile => $scoreboard_file,
221 SystemLog => $log_file,
222
223 AuthUserFile => $auth_user_file,
224 AuthGroupFile => $auth_group_file,
225 AuthOrder => 'mod_auth_file.c',
226
227 MaxRetrieveFileSize => '4 B',
228
229 IfModules => {
230 'mod_delay.c' => {
231 DelayEngine => 'off',
232 },
233 },
234 };
235
236 my ($port, $config_user, $config_group) = config_write($config_file, $config);
237
238 # Open pipes, for use between the parent and child processes. Specifically,
239 # the child will indicate when it's done with its test by writing a message
240 # to the parent.
241 my ($rfh, $wfh);
242 unless (pipe($rfh, $wfh)) {
243 die("Can't open pipe: $!");
244 }
245
246 my $ex;
247
248 # Fork child
249 $self->handle_sigchld();
250 defined(my $pid = fork()) or die("Can't fork: $!");
251 if ($pid) {
252 eval {
253 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
254
255 $client->login($user, $passwd);
256
257 my ($resp_code, $resp_msg);
258
259 my $conn = $client->retr_raw('foo');
260 unless ($conn) {
261 die("RETR failed: " . $client->response_code() . " " .
262 $client->response_msg());
263 }
264
265 my $buf;
266 $conn->read($buf, 8192, 30);
267 eval { $conn->close() };
268
269 $resp_code = $client->response_code();
270 $resp_msg = $client->response_msg();
271
272 my $expected;
273
274 $expected = 426;
275 $self->assert($expected == $resp_code,
276 test_msg("Expected $expected, got $resp_code"));
277
278 $expected = "Transfer aborted. Operation not permitted";
279 $self->assert($expected eq $resp_msg,
280 test_msg("Expected '$expected', got '$resp_msg'"));
281 };
282
283 if ($@) {
284 $ex = $@;
285 }
286
287 $wfh->print("done\n");
288 $wfh->flush();
289
290 } else {
291 eval { server_wait($config_file, $rfh) };
292 if ($@) {
293 warn($@);
294 exit 1;
295 }
296
297 exit 0;
298 }
299
300 # Stop server
301 server_stop($pid_file);
302
303 $self->assert_child_ok($pid);
304
305 if ($ex) {
306 die($ex);
307 }
308
309 unlink($log_file);
310 }
311
312 1;