source: src/router/proftpd/tests/t/lib/ProFTPD/Tests/Config/MaxRetrieveFileSize.pm @ 17876

Last change on this file since 17876 was 17876, checked in by BrainSlayer, 20 months ago

update proftp

File size: 6.8 KB
Line 
1package ProFTPD::Tests::Config::MaxRetrieveFileSize;
2
3use lib qw(t/lib);
4use base qw(ProFTPD::TestSuite::Child);
5use strict;
6
7use File::Spec;
8use IO::Handle;
9
10use ProFTPD::TestSuite::FTP;
11use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
12
13$| = 1;
14
15my $order = 0;
16
17my $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
30sub new {
31  return shift()->SUPER::new(@_);
32}
33
34sub list_tests {
35  return testsuite_get_runnable_tests($TESTS);
36}
37
38sub 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
93    MaxRetrieveFileSize => '100 B',
94
95    IfModules => {
96      'mod_delay.c' => {
97        DelayEngine => 'off',
98      },
99    },
100  };
101
102  my ($port, $config_user, $config_group) = config_write($config_file, $config);
103
104  # Open pipes, for use between the parent and child processes.  Specifically,
105  # the child will indicate when it's done with its test by writing a message
106  # to the parent.
107  my ($rfh, $wfh);
108  unless (pipe($rfh, $wfh)) {
109    die("Can't open pipe: $!");
110  }
111
112  my $ex;
113
114  # Fork child
115  $self->handle_sigchld();
116  defined(my $pid = fork()) or die("Can't fork: $!");
117  if ($pid) {
118    eval {
119      my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
120
121      $client->login($user, $passwd);
122
123      my $conn = $client->retr_raw('foo');
124      unless ($conn) {
125        die("RETR failed: " . $client->response_code() . " " .
126          $client->response_msg());
127      }
128
129      my $buf;
130      $conn->read($buf, 8192, 30);
131      eval { $conn->close() };
132
133      my $expected;
134
135      $expected = 14;
136      my $buflen = length($buf);
137      $self->assert($expected == $buflen,
138        test_msg("Expected $expected, got $buflen"));
139    };
140
141    if ($@) {
142      $ex = $@;
143    }
144
145    $wfh->print("done\n");
146    $wfh->flush();
147
148  } else {
149    eval { server_wait($config_file, $rfh) };
150    if ($@) {
151      warn($@);
152      exit 1;
153    }
154
155    exit 0;
156  }
157
158  # Stop server
159  server_stop($pid_file);
160
161  $self->assert_child_ok($pid);
162
163  if ($ex) {
164    die($ex);
165  }
166
167  unlink($log_file);
168}
169
170sub maxretrievefilesize_exceeded {
171  my $self = shift;
172  my $tmpdir = $self->{tmpdir};
173
174  my $config_file = "$tmpdir/config.conf";
175  my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
176  my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
177
178  my $log_file = File::Spec->rel2abs('tests.log');
179
180  my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
181  my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
182
183  my $user = 'proftpd';
184  my $passwd = 'test';
185  my $home_dir = File::Spec->rel2abs($tmpdir);
186  my $uid = 500;
187  my $gid = 500;
188
189  # Make sure that, if we're running as root, that the home directory has
190  # permissions/privs set for the account we create
191  if ($< == 0) {
192    unless (chmod(0755, $home_dir)) {
193      die("Can't set perms on $home_dir to 0755: $!");
194    }
195   
196    unless (chown($uid, $gid, $home_dir)) {
197      die("Can't set owner of $home_dir to $uid/$gid: $!");
198    }
199  }
200
201  auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
202    '/bin/bash');
203  auth_group_write($auth_group_file, 'ftpd', $gid, $user);
204
205  my $src_file = File::Spec->rel2abs("$tmpdir/foo");
206  if (open(my $fh, "> $src_file")) {
207    print $fh "Hello, World!\n";
208
209    unless (close($fh)) {
210      die("Can't write $src_file: $!");
211    }
212
213  } else {
214    die("Can't open $src_file: $!");
215  }
216
217  my $config = {
218    PidFile => $pid_file,
219    ScoreboardFile => $scoreboard_file,
220    SystemLog => $log_file,
221
222    AuthUserFile => $auth_user_file,
223    AuthGroupFile => $auth_group_file,
224
225    MaxRetrieveFileSize => '4 B',
226
227    IfModules => {
228      'mod_delay.c' => {
229        DelayEngine => 'off',
230      },
231    },
232  };
233
234  my ($port, $config_user, $config_group) = config_write($config_file, $config);
235
236  # Open pipes, for use between the parent and child processes.  Specifically,
237  # the child will indicate when it's done with its test by writing a message
238  # to the parent.
239  my ($rfh, $wfh);
240  unless (pipe($rfh, $wfh)) {
241    die("Can't open pipe: $!");
242  }
243
244  my $ex;
245
246  # Fork child
247  $self->handle_sigchld();
248  defined(my $pid = fork()) or die("Can't fork: $!");
249  if ($pid) {
250    eval {
251      my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
252
253      $client->login($user, $passwd);
254
255      my ($resp_code, $resp_msg);
256
257      my $conn = $client->retr_raw('foo');
258      unless ($conn) {
259        die("RETR failed: " . $client->response_code() . " " .
260          $client->response_msg());
261      }
262
263      my $buf;
264      $conn->read($buf, 8192, 30);
265      eval { $conn->close() };
266
267      $resp_code = $client->response_code();
268      $resp_msg = $client->response_msg();
269
270      my $expected;
271
272      $expected = 426;
273      $self->assert($expected == $resp_code,
274        test_msg("Expected $expected, got $resp_code"));
275
276      $expected = "Transfer aborted. Operation not permitted";
277      $self->assert($expected eq $resp_msg,
278        test_msg("Expected '$expected', got '$resp_msg'"));
279    };
280
281    if ($@) {
282      $ex = $@;
283    }
284
285    $wfh->print("done\n");
286    $wfh->flush();
287
288  } else {
289    eval { server_wait($config_file, $rfh) };
290    if ($@) {
291      warn($@);
292      exit 1;
293    }
294
295    exit 0;
296  }
297
298  # Stop server
299  server_stop($pid_file);
300
301  $self->assert_child_ok($pid);
302
303  if ($ex) {
304    die($ex);
305  }
306
307  unlink($log_file);
308}
309
3101;
Note: See TracBrowser for help on using the repository browser.