source: src/router/proftpd/tests/t/lib/ProFTPD/Tests/Commands/MFF.pm @ 14672

Last change on this file since 14672 was 14672, checked in by BrainSlayer, 3 years ago

proftp update

File size: 9.6 KB
Line 
1package ProFTPD::Tests::Commands::MFF;
2
3use lib qw(t/lib);
4use base qw(Test::Unit::TestCase ProFTPD::TestSuite::Child);
5use strict;
6
7use File::Path qw(mkpath rmtree);
8use File::Spec;
9use IO::Handle;
10
11use ProFTPD::TestSuite::FTP;
12use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14$| = 1;
15
16my $order = 0;
17
18my $TESTS = {
19  mff_ok => {
20    order => ++$order,
21    test_class => [qw(forking)],
22  },
23
24  mff_chrooted_ok => {
25    order => ++$order,
26    test_class => [qw(forking rootprivs)],
27  },
28
29  mff_bug3142 => {
30    order => ++$order,
31    test_class => [qw(bug forking)],
32  },
33
34};
35
36sub new {
37  return shift()->SUPER::new(@_);
38}
39
40sub list_tests {
41  return testsuite_get_runnable_tests($TESTS);
42}
43
44sub set_up {
45  my $self = shift;
46  $self->{tmpdir} = testsuite_get_tmp_dir();
47
48  # Create temporary scratch dir
49  eval { mkpath($self->{tmpdir}) };
50  if ($@) {
51    my $abs_path = File::Spec->rel2abs($self->{tmpdir});
52    die("Can't create dir $abs_path: $@");
53  }
54}
55
56sub tear_down {
57  my $self = shift;
58
59  # Remove temporary scratch dir
60  if ($self->{tmpdir}) {
61    eval { rmtree($self->{tmpdir}) };
62  }
63
64  undef $self;
65}
66
67sub mff_ok {
68  my $self = shift;
69  my $tmpdir = $self->{tmpdir};
70
71  my $config_file = "$tmpdir/cmds.conf";
72  my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
73  my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
74
75  my $log_file = File::Spec->rel2abs('tests.log');
76
77  my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
78  my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
79
80  my $user = 'proftpd';
81  my $passwd = 'test';
82  my $home_dir = File::Spec->rel2abs($tmpdir);
83  my $uid = 500;
84  my $gid = 500;
85
86  my $test_file = File::Spec->rel2abs("$tmpdir/foo");
87  if (open(my $fh, "> $test_file")) {
88    close($fh);
89
90  } else {
91    die("Can't open $test_file: $!");
92  }
93
94  # Make sure that, if we're running as root, that the home directory has
95  # permissions/privs set for the account we create
96  if ($< == 0) {
97    unless (chmod(0755, $home_dir)) {
98      die("Can't set perms on $home_dir to 0755: $!");
99    }
100
101    unless (chown($uid, $gid, $home_dir, $test_file)) {
102      die("Can't set owner of $home_dir, $test_file to $uid/$gid: $!");
103    }
104  }
105
106  auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
107    '/bin/bash');
108  auth_group_write($auth_group_file, 'ftpd', $gid, $user);
109
110  my $config = {
111    PidFile => $pid_file,
112    ScoreboardFile => $scoreboard_file,
113    SystemLog => $log_file,
114
115    AuthUserFile => $auth_user_file,
116    AuthGroupFile => $auth_group_file,
117
118    IfModules => {
119      'mod_delay.c' => {
120        DelayEngine => 'off',
121      },
122    },
123  };
124
125  my ($port, $config_user, $config_group) = config_write($config_file, $config);
126
127  # Open pipes, for use between the parent and child processes.  Specifically,
128  # the child will indicate when it's done with its test by writing a message
129  # to the parent.
130  my ($rfh, $wfh);
131  unless (pipe($rfh, $wfh)) {
132    die("Can't open pipe: $!");
133  }
134
135  my $ex;
136
137  # Fork child
138  $self->handle_sigchld();
139  defined(my $pid = fork()) or die("Can't fork: $!");
140  if ($pid) {
141    eval {
142      my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
143
144      $client->login($user, $passwd);
145
146      my ($resp_code, $resp_msg);
147      ($resp_code, $resp_msg) = $client->mff('modify=20020717210715;', 'foo');
148
149      my $expected;
150
151      $expected = 213;
152      $self->assert($expected == $resp_code,
153        test_msg("Expected $expected, got $resp_code"));
154
155      $expected = 'modify=20020717210715; foo';
156      $self->assert($expected eq $resp_msg,
157        test_msg("Expected '$expected', got '$resp_msg'"));
158    };
159
160    if ($@) {
161      $ex = $@;
162    }
163
164    $wfh->print("done\n");
165    $wfh->flush();
166
167  } else {
168    eval { server_wait($config_file, $rfh) };
169    if ($@) {
170      warn($@);
171      exit 1;
172    }
173
174    exit 0;
175  }
176
177  # Stop server
178  server_stop($pid_file);
179
180  $self->assert_child_ok($pid);
181
182  if ($ex) {
183    die($ex);
184  }
185
186  unlink($log_file);
187}
188
189sub mff_chrooted_ok {
190  my $self = shift;
191  my $tmpdir = $self->{tmpdir};
192
193  my $config_file = "$tmpdir/cmds.conf";
194  my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
195  my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
196
197  my $log_file = File::Spec->rel2abs('tests.log');
198
199  my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
200  my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
201
202  my $user = 'proftpd';
203  my $passwd = 'test';
204  my $home_dir = File::Spec->rel2abs($tmpdir);
205  my $uid = 500;
206  my $gid = 500;
207
208  my $test_file = File::Spec->rel2abs("$tmpdir/foo");
209  if (open(my $fh, "> $test_file")) {
210    close($fh);
211
212  } else {
213    die("Can't open $test_file: $!");
214  }
215
216  # Make sure that, if we're running as root, that the home directory has
217  # permissions/privs set for the account we create
218  if ($< == 0) {
219    unless (chmod(0755, $home_dir)) {
220      die("Can't set perms on $home_dir to 0755: $!");
221    }
222
223    unless (chown($uid, $gid, $home_dir, $test_file)) {
224      die("Can't set owner of $home_dir, $test_file to $uid/$gid: $!");
225    }
226  }
227
228  auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
229    '/bin/bash');
230  auth_group_write($auth_group_file, 'ftpd', $gid, $user);
231
232  my $config = {
233    PidFile => $pid_file,
234    ScoreboardFile => $scoreboard_file,
235    SystemLog => $log_file,
236
237    AuthUserFile => $auth_user_file,
238    AuthGroupFile => $auth_group_file,
239
240    DefaultRoot => '~',
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);
267
268      $client->login($user, $passwd);
269
270      my ($resp_code, $resp_msg);
271      ($resp_code, $resp_msg) = $client->mff('modify=20020717210715;', 'foo');
272
273      my $expected;
274
275      $expected = 213;
276      $self->assert($expected == $resp_code,
277        test_msg("Expected $expected, got $resp_code"));
278
279      $expected = 'modify=20020717210715; foo';
280      $self->assert($expected eq $resp_msg,
281        test_msg("Expected '$expected', got '$resp_msg'"));
282    };
283
284    if ($@) {
285      $ex = $@;
286    }
287
288    $wfh->print("done\n");
289    $wfh->flush();
290
291  } else {
292    eval { server_wait($config_file, $rfh) };
293    if ($@) {
294      warn($@);
295      exit 1;
296    }
297
298    exit 0;
299  }
300
301  # Stop server
302  server_stop($pid_file);
303
304  $self->assert_child_ok($pid);
305
306  if ($ex) {
307    die($ex);
308  }
309
310  unlink($log_file);
311}
312
313sub mff_bug3142 {
314  my $self = shift;
315  my $tmpdir = $self->{tmpdir};
316
317  my $config_file = "$tmpdir/cmds.conf";
318  my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
319  my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
320
321  my $log_file = File::Spec->rel2abs('tests.log');
322
323  my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
324  my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
325
326  my $user = 'proftpd';
327  my $passwd = 'test';
328  my $home_dir = File::Spec->rel2abs($tmpdir);
329  my $uid = 500;
330  my $gid = 500;
331
332  my $test_file = File::Spec->rel2abs("$tmpdir/foo bar");
333  if (open(my $fh, "> $test_file")) {
334    close($fh);
335
336  } else {
337    die("Can't open $test_file: $!");
338  }
339
340  # Make sure that, if we're running as root, that the home directory has
341  # permissions/privs set for the account we create
342  if ($< == 0) {
343    unless (chmod(0755, $home_dir)) {
344      die("Can't set perms on $home_dir to 0755: $!");
345    }
346
347    unless (chown($uid, $gid, $home_dir, $test_file)) {
348      die("Can't set owner of $home_dir, $test_file to $uid/$gid: $!");
349    }
350  }
351
352  auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
353    '/bin/bash');
354  auth_group_write($auth_group_file, 'ftpd', $gid, $user);
355
356  my $config = {
357    PidFile => $pid_file,
358    ScoreboardFile => $scoreboard_file,
359    SystemLog => $log_file,
360
361    AuthUserFile => $auth_user_file,
362    AuthGroupFile => $auth_group_file,
363
364    IfModules => {
365      'mod_delay.c' => {
366        DelayEngine => 'off',
367      },
368    },
369  };
370
371  my ($port, $config_user, $config_group) = config_write($config_file, $config);
372
373  # Open pipes, for use between the parent and child processes.  Specifically,
374  # the child will indicate when it's done with its test by writing a message
375  # to the parent.
376  my ($rfh, $wfh);
377  unless (pipe($rfh, $wfh)) {
378    die("Can't open pipe: $!");
379  }
380
381  my $ex;
382
383  # Fork child
384  $self->handle_sigchld();
385  defined(my $pid = fork()) or die("Can't fork: $!");
386  if ($pid) {
387    eval {
388      my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
389
390      $client->login($user, $passwd);
391
392      my ($resp_code, $resp_msg);
393      ($resp_code, $resp_msg) = $client->mff('modify=20020717210715;', 'foo bar');
394
395      my $expected;
396
397      $expected = 213;
398      $self->assert($expected == $resp_code,
399        test_msg("Expected $expected, got $resp_code"));
400
401      $expected = 'modify=20020717210715; foo bar';
402      $self->assert($expected eq $resp_msg,
403        test_msg("Expected '$expected', got '$resp_msg'"));
404    };
405
406    if ($@) {
407      $ex = $@;
408    }
409
410    $wfh->print("done\n");
411    $wfh->flush();
412
413  } else {
414    eval { server_wait($config_file, $rfh) };
415    if ($@) {
416      warn($@);
417      exit 1;
418    }
419
420    exit 0;
421  }
422
423  # Stop server
424  server_stop($pid_file);
425
426  $self->assert_child_ok($pid);
427
428  if ($ex) {
429    die($ex);
430  }
431
432  unlink($log_file);
433}
434
4351;
Note: See TracBrowser for help on using the repository browser.