| 1 | package ProFTPD::Tests::Commands::QUIT; |
|---|
| 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 | quit_ok => { |
|---|
| 19 | order => ++$order, |
|---|
| 20 | test_class => [qw(forking)], |
|---|
| 21 | }, |
|---|
| 22 | |
|---|
| 23 | }; |
|---|
| 24 | |
|---|
| 25 | sub new { |
|---|
| 26 | return shift()->SUPER::new(@_); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | sub list_tests { |
|---|
| 30 | return testsuite_get_runnable_tests($TESTS); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | sub quit_ok { |
|---|
| 34 | my $self = shift; |
|---|
| 35 | my $tmpdir = $self->{tmpdir}; |
|---|
| 36 | |
|---|
| 37 | my $config_file = "$tmpdir/cmds.conf"; |
|---|
| 38 | my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid"); |
|---|
| 39 | my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard"); |
|---|
| 40 | |
|---|
| 41 | my $log_file = File::Spec->rel2abs('tests.log'); |
|---|
| 42 | |
|---|
| 43 | my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd"); |
|---|
| 44 | my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group"); |
|---|
| 45 | |
|---|
| 46 | my $user = 'proftpd'; |
|---|
| 47 | my $passwd = 'test'; |
|---|
| 48 | my $home_dir = File::Spec->rel2abs($tmpdir); |
|---|
| 49 | my $uid = 500; |
|---|
| 50 | my $gid = 500; |
|---|
| 51 | |
|---|
| 52 | # Make sure that, if we're running as root, that the home directory has |
|---|
| 53 | # permissions/privs set for the account we create |
|---|
| 54 | if ($< == 0) { |
|---|
| 55 | unless (chmod(0755, $home_dir)) { |
|---|
| 56 | die("Can't set perms on $home_dir to 0755: $!"); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | unless (chown($uid, $gid, $home_dir)) { |
|---|
| 60 | die("Can't set owner of $home_dir to $uid/$gid: $!"); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir, |
|---|
| 65 | '/bin/bash'); |
|---|
| 66 | auth_group_write($auth_group_file, 'ftpd', $gid, $user); |
|---|
| 67 | |
|---|
| 68 | my $config = { |
|---|
| 69 | PidFile => $pid_file, |
|---|
| 70 | ScoreboardFile => $scoreboard_file, |
|---|
| 71 | SystemLog => $log_file, |
|---|
| 72 | |
|---|
| 73 | AuthUserFile => $auth_user_file, |
|---|
| 74 | AuthGroupFile => $auth_group_file, |
|---|
| 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 | |
|---|
| 102 | $client->login($user, $passwd); |
|---|
| 103 | |
|---|
| 104 | my ($resp_code, $resp_msg); |
|---|
| 105 | ($resp_code, $resp_msg) = $client->quit(); |
|---|
| 106 | |
|---|
| 107 | my $expected; |
|---|
| 108 | |
|---|
| 109 | $expected = 221; |
|---|
| 110 | $self->assert($expected == $resp_code, |
|---|
| 111 | test_msg("Expected $expected, got $resp_code")); |
|---|
| 112 | |
|---|
| 113 | $expected = "Goodbye."; |
|---|
| 114 | $self->assert($expected eq $resp_msg, |
|---|
| 115 | test_msg("Expected '$expected', got '$resp_msg'")); |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | if ($@) { |
|---|
| 119 | $ex = $@; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | $wfh->print("done\n"); |
|---|
| 123 | $wfh->flush(); |
|---|
| 124 | |
|---|
| 125 | } else { |
|---|
| 126 | eval { server_wait($config_file, $rfh) }; |
|---|
| 127 | if ($@) { |
|---|
| 128 | warn($@); |
|---|
| 129 | exit 1; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | exit 0; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | # Stop server |
|---|
| 136 | server_stop($pid_file); |
|---|
| 137 | |
|---|
| 138 | $self->assert_child_ok($pid); |
|---|
| 139 | |
|---|
| 140 | if ($ex) { |
|---|
| 141 | die($ex); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | unlink($log_file); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | 1; |
|---|