Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Utils/ftpcount.pm

raw
1 package ProFTPD::Tests::Utils::ftpcount;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath rmtree);
8 use File::Spec;
9 use IO::Handle;
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 ftpcount_ok => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 };
25
26 sub new {
27 return shift()->SUPER::new(@_);
28 }
29
30 sub list_tests {
31 return testsuite_get_runnable_tests($TESTS);
32 }
33
34 sub ftpcount_wait_alarm {
35 # We merely need to interrupt the current process by throwing an exception.
36 # Messy, but it gets the job done (it's necessary, at least, on a
37 # Mac OSX 10.4 machine).
38 die();
39 }
40
41 sub ftpcount {
42 my $scoreboard_file = shift;
43 my $output_file = shift;
44 my $ftpcount_wait_timeout = shift;
45 $ftpcount_wait_timeout = 1 unless defined($ftpcount_wait_timeout);
46
47 my $ftpcount_bin;
48 if ($ENV{PROFTPD_TEST_PATH}) {
49 $ftpcount_bin = "$ENV{PROFTPD_TEST_PATH}/../ftpcount";
50
51 } else {
52 $ftpcount_bin = '../ftpcount';
53 }
54
55 my $cmd = "$ftpcount_bin -f $scoreboard_file > $output_file 2>/dev/null &";
56
57 if ($ENV{TEST_VERBOSE}) {
58 print STDERR "Executing ftpcount: $cmd\n";
59 }
60
61 eval {
62 $SIG{ALRM} = \&ftpcount_wait_alarm;
63 alarm($ftpcount_wait_timeout);
64 system($cmd);
65 alarm(0);
66 $SIG{ALRM} = 'DEFAULT';
67 };
68 }
69
70 sub ftpcount_ok {
71 my $self = shift;
72 my $tmpdir = $self->{tmpdir};
73
74 my $config_file = "$tmpdir/ftpcount.conf";
75 my $pid_file = File::Spec->rel2abs("$tmpdir/ftpcount.pid");
76 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/ftpcount.scoreboard");
77
78 my $log_file = File::Spec->rel2abs('tests.log');
79
80 my $auth_user_file = File::Spec->rel2abs("$tmpdir/exec.passwd");
81 my $auth_group_file = File::Spec->rel2abs("$tmpdir/exec.group");
82
83 my $user = 'proftpd';
84 my $passwd = 'test';
85 my $home_dir = File::Spec->rel2abs($tmpdir);
86 my $uid = 500;
87 my $gid = 500;
88
89 # Make sure that, if we're running as root, that the home directory has
90 # permissions/privs set for the account we create
91 if ($< == 0) {
92 unless (chmod(0755, $home_dir)) {
93 die("Can't set perms on $home_dir to 0755: $!");
94 }
95
96 unless (chown($uid, $gid, $home_dir)) {
97 die("Can't set owner of $home_dir to $uid/$gid: $!");
98 }
99 }
100
101 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
102 '/bin/bash');
103 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
104
105 my $ftpcount_file = File::Spec->rel2abs("$tmpdir/ftpcount.txt");
106
107 my $config = {
108 PidFile => $pid_file,
109 ScoreboardFile => $scoreboard_file,
110 SystemLog => $log_file,
111
112 AuthUserFile => $auth_user_file,
113 AuthGroupFile => $auth_group_file,
114 AuthOrder => 'mod_auth_file.c',
115
116 IfModules => {
117 'mod_delay.c' => {
118 DelayEngine => 'off',
119 },
120 },
121 };
122
123 my ($port, $config_user, $config_group) = config_write($config_file, $config);
124
125 # Open pipes, for use between the parent and child processes. Specifically,
126 # the child will indicate when it's done with its test by writing a message
127 # to the parent.
128 my ($rfh, $wfh);
129 unless (pipe($rfh, $wfh)) {
130 die("Can't open pipe: $!");
131 }
132
133 my $ex;
134
135 # Fork child
136 $self->handle_sigchld();
137 defined(my $pid = fork()) or die("Can't fork: $!");
138 if ($pid) {
139 eval {
140 sleep(1);
141
142 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
143 $client->login($user, $passwd);
144
145 # Before we stop the server, use ftpcount to read the scoreboard
146 ftpcount($scoreboard_file, $ftpcount_file);
147
148 $client->quit();
149 };
150
151 if ($@) {
152 $ex = $@;
153 }
154
155 $wfh->print("done\n");
156 $wfh->flush();
157
158 } else {
159 eval { server_wait($config_file, $rfh) };
160 if ($@) {
161 warn($@);
162 exit 1;
163 }
164
165 exit 0;
166 }
167
168 # Stop server
169 server_stop($pid_file);
170
171 $self->assert_child_ok($pid);
172
173 if ($ex) {
174 die($ex);
175 }
176
177 my $lines = [];
178 if (open(my $fh, "< $ftpcount_file")) {
179 while (my $line = <$fh>) {
180 chomp($line);
181 push(@$lines, $line);
182 }
183
184 close($fh);
185
186 } else {
187 die("Can't read $ftpcount_file: $!");
188 }
189
190 my $expected = 2;
191
192 my $count = scalar(@$lines);
193 $self->assert($expected == $count,
194 test_msg("Expected $expected, got $count"));
195
196 my $ok = 0;
197 foreach my $line (@$lines) {
198 chomp($line);
199
200 if ($line =~ /^Service class\s+\-\s+(\d+) user/) {
201 my $user_count = $1;
202
203 $expected = 1;
204 $self->assert($expected eq $user_count,
205 test_msg("Expected '$expected', got '$user_count'"));
206
207 $ok = 1;
208 }
209 }
210
211 $self->assert($ok,
212 test_msg("Unexpected ftpcount output (expected user count not found)"));
213
214 unlink($log_file);
215 }
216
217 1;