Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::TimeoutLogin;
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 timeoutlogin_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 timeoutlogin_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 timeoutlogin_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 $timeout_login = 4;
74
75 my $config = {
76 PidFile => $pid_file,
77 ScoreboardFile => $scoreboard_file,
78 SystemLog => $log_file,
79
80 AuthUserFile => $auth_user_file,
81 AuthGroupFile => $auth_group_file,
82 AuthOrder => 'mod_auth_file.c',
83
84 TimeoutLogin => $timeout_login,
85
86 IfModules => {
87 'mod_delay.c' => {
88 DelayEngine => 'off',
89 },
90 },
91 };
92
93 my ($port, $config_user, $config_group) = config_write($config_file, $config);
94
95 # Open pipes, for use between the parent and child processes. Specifically,
96 # the child will indicate when it's done with its test by writing a message
97 # to the parent.
98 my ($rfh, $wfh);
99 unless (pipe($rfh, $wfh)) {
100 die("Can't open pipe: $!");
101 }
102
103 my $ex;
104
105 # Fork child
106 $self->handle_sigchld();
107 defined(my $pid = fork()) or die("Can't fork: $!");
108 if ($pid) {
109 eval {
110 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
111
112 # Wait for one second
113 sleep(1);
114
115 $client->login($user, $passwd);
116 $client->quit();
117 };
118
119 if ($@) {
120 $ex = $@;
121 }
122
123 $wfh->print("done\n");
124 $wfh->flush();
125
126 } else {
127 eval { server_wait($config_file, $rfh) };
128 if ($@) {
129 warn($@);
130 exit 1;
131 }
132
133 exit 0;
134 }
135
136 # Stop server
137 server_stop($pid_file);
138
139 $self->assert_child_ok($pid);
140
141 if ($ex) {
142 die($ex);
143 }
144
145 unlink($log_file);
146 }
147
148 sub timeoutlogin_exceeded {
149 my $self = shift;
150 my $tmpdir = $self->{tmpdir};
151
152 my $config_file = "$tmpdir/config.conf";
153 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
154 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
155
156 my $log_file = File::Spec->rel2abs('tests.log');
157
158 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
159 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
160
161 my $user = 'proftpd';
162 my $passwd = 'test';
163 my $home_dir = File::Spec->rel2abs($tmpdir);
164 my $uid = 500;
165 my $gid = 500;
166
167 # Make sure that, if we're running as root, that the home directory has
168 # permissions/privs set for the account we create
169 if ($< == 0) {
170 unless (chmod(0755, $home_dir)) {
171 die("Can't set perms on $home_dir to 0755: $!");
172 }
173
174 unless (chown($uid, $gid, $home_dir)) {
175 die("Can't set owner of $home_dir to $uid/$gid: $!");
176 }
177 }
178
179 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
180 '/bin/bash');
181 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
182
183 my $timeout_login = 5;
184
185 my $config = {
186 PidFile => $pid_file,
187 ScoreboardFile => $scoreboard_file,
188 SystemLog => $log_file,
189
190 AuthUserFile => $auth_user_file,
191 AuthGroupFile => $auth_group_file,
192 AuthOrder => 'mod_auth_file.c',
193
194 TimeoutLogin => $timeout_login,
195
196 IfModules => {
197 'mod_delay.c' => {
198 DelayEngine => 'off',
199 },
200 },
201 };
202
203 my ($port, $config_user, $config_group) = config_write($config_file, $config);
204
205 # Open pipes, for use between the parent and child processes. Specifically,
206 # the child will indicate when it's done with its test by writing a message
207 # to the parent.
208 my ($rfh, $wfh);
209 unless (pipe($rfh, $wfh)) {
210 die("Can't open pipe: $!");
211 }
212
213 my $ex;
214
215 # Fork child
216 $self->handle_sigchld();
217 defined(my $pid = fork()) or die("Can't fork: $!");
218 if ($pid) {
219 eval {
220 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
221
222 # Wait for two seconds more than the login timeout
223 sleep($timeout_login + 2);
224
225 eval { $client->user($user) };
226 unless ($@) {
227 die("USER succeeded unexpectedly");
228 }
229
230 my $resp_code = $client->response_code();
231 my $resp_msg = $client->response_msg();
232
233 my $expected;
234
235 $expected = 421;
236 $self->assert($expected == $resp_code,
237 test_msg("Expected $expected, got $resp_code"));
238
239 $expected = "Login timeout ($timeout_login seconds): closing control connection";
240 $self->assert(lc($expected) eq lc($resp_msg),
241 test_msg("Expected '$expected', got '$resp_msg'"));
242 };
243
244 if ($@) {
245 $ex = $@;
246 }
247
248 $wfh->print("done\n");
249 $wfh->flush();
250
251 } else {
252 eval { server_wait($config_file, $rfh) };
253 if ($@) {
254 warn($@);
255 exit 1;
256 }
257
258 exit 0;
259 }
260
261 # Stop server
262 server_stop($pid_file);
263
264 $self->assert_child_ok($pid);
265
266 if ($ex) {
267 die($ex);
268 }
269
270 unlink($log_file);
271 }
272
273 1;