Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::IfDefine;
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 ifdefine_exists => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 ifdefine_not_exist => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 ifdefine_exists_via_define => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 ifdefine_exists_via_env => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 };
39
40 sub new {
41 return shift()->SUPER::new(@_);
42 }
43
44 sub list_tests {
45 return testsuite_get_runnable_tests($TESTS);
46 }
47
48 sub ifdefine_exists {
49 my $self = shift;
50 my $tmpdir = $self->{tmpdir};
51
52 my $config_file = "$tmpdir/config.conf";
53 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
54 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
55
56 my $log_file = test_get_logfile();
57
58 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
59 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
60
61 my $user = 'proftpd';
62 my $passwd = 'test';
63 my $group = 'ftpd';
64 my $home_dir = File::Spec->rel2abs($tmpdir);
65 my $uid = 500;
66 my $gid = 500;
67
68 # Make sure that, if we're running as root, that the home directory has
69 # permissions/privs set for the account we create
70 if ($< == 0) {
71 unless (chmod(0755, $home_dir)) {
72 die("Can't set perms on $home_dir to 0755: $!");
73 }
74
75 unless (chown($uid, $gid, $home_dir)) {
76 die("Can't set owner of $home_dir to $uid/$gid: $!");
77 }
78 }
79
80 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
81 '/bin/bash');
82 auth_group_write($auth_group_file, $group, $gid, $user);
83
84 my $config = {
85 PidFile => $pid_file,
86 ScoreboardFile => $scoreboard_file,
87 SystemLog => $log_file,
88
89 AuthUserFile => $auth_user_file,
90 AuthGroupFile => $auth_group_file,
91 AuthOrder => 'mod_auth_file.c',
92
93 IfModules => {
94 'mod_delay.c' => {
95 DelayEngine => 'off',
96 },
97 },
98 };
99
100 my ($port, $config_user, $config_group) = config_write($config_file, $config);
101
102 my $define = 'FOO';
103
104 if (open(my $fh, ">> $config_file")) {
105 print $fh <<EOC;
106 <IfDefine $define>
107 ServerIdent on
108 </IfDefine>
109 EOC
110 unless (close($fh)) {
111 die("Can't write $config_file: $!");
112 }
113
114 } else {
115 die("Can't open $config_file: $!");
116 }
117
118 eval { server_start($config_file, undef, { define => [$define] }) };
119 if ($@) {
120 my $ex = $@;
121
122 test_append_logfile($log_file, $ex);
123 unlink($log_file);
124
125 die($ex);
126 }
127
128 # Give the daemon a chance to start up
129 sleep(1);
130 server_stop($pid_file);
131
132 unlink($log_file);
133 }
134
135 sub ifdefine_not_exist {
136 my $self = shift;
137 my $tmpdir = $self->{tmpdir};
138
139 my $config_file = "$tmpdir/config.conf";
140 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
141 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
142
143 my $log_file = test_get_logfile();
144
145 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
146 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
147
148 my $user = 'proftpd';
149 my $passwd = 'test';
150 my $group = 'ftpd';
151 my $home_dir = File::Spec->rel2abs($tmpdir);
152 my $uid = 500;
153 my $gid = 500;
154
155 # Make sure that, if we're running as root, that the home directory has
156 # permissions/privs set for the account we create
157 if ($< == 0) {
158 unless (chmod(0755, $home_dir)) {
159 die("Can't set perms on $home_dir to 0755: $!");
160 }
161
162 unless (chown($uid, $gid, $home_dir)) {
163 die("Can't set owner of $home_dir to $uid/$gid: $!");
164 }
165 }
166
167 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
168 '/bin/bash');
169 auth_group_write($auth_group_file, $group, $gid, $user);
170
171 my $config = {
172 PidFile => $pid_file,
173 ScoreboardFile => $scoreboard_file,
174 SystemLog => $log_file,
175
176 AuthUserFile => $auth_user_file,
177 AuthGroupFile => $auth_group_file,
178 AuthOrder => 'mod_auth_file.c',
179
180 IfModules => {
181 'mod_delay.c' => {
182 DelayEngine => 'off',
183 },
184 },
185 };
186
187 my ($port, $config_user, $config_group) = config_write($config_file, $config);
188
189 my $define = 'FOO';
190
191 if (open(my $fh, ">> $config_file")) {
192 print $fh <<EOC;
193 <IfDefine $define>
194 # Use a nonsense directive here; if the server incorrectly see the Define,
195 # it will fail to start.
196 FooBar baz
197 </IfDefine>
198 EOC
199 unless (close($fh)) {
200 die("Can't write $config_file: $!");
201 }
202
203 } else {
204 die("Can't open $config_file: $!");
205 }
206
207 eval { server_start($config_file) };
208 if ($@) {
209 my $ex = $@;
210
211 test_append_logfile($log_file, $ex);
212 unlink($log_file);
213
214 die($ex);
215 }
216
217 # Give the daemon a chance to start up
218 sleep(1);
219 server_stop($pid_file);
220
221 unlink($log_file);
222 }
223
224 sub ifdefine_exists_via_define {
225 my $self = shift;
226 my $tmpdir = $self->{tmpdir};
227
228 my $config_file = "$tmpdir/config.conf";
229 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
230 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
231
232 my $log_file = test_get_logfile();
233
234 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
235 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
236
237 my $user = 'proftpd';
238 my $passwd = 'test';
239 my $group = 'ftpd';
240 my $home_dir = File::Spec->rel2abs($tmpdir);
241 my $uid = 500;
242 my $gid = 500;
243
244 # Make sure that, if we're running as root, that the home directory has
245 # permissions/privs set for the account we create
246 if ($< == 0) {
247 unless (chmod(0755, $home_dir)) {
248 die("Can't set perms on $home_dir to 0755: $!");
249 }
250
251 unless (chown($uid, $gid, $home_dir)) {
252 die("Can't set owner of $home_dir to $uid/$gid: $!");
253 }
254 }
255
256 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
257 '/bin/bash');
258 auth_group_write($auth_group_file, $group, $gid, $user);
259
260 my $config = {
261 PidFile => $pid_file,
262 ScoreboardFile => $scoreboard_file,
263 SystemLog => $log_file,
264
265 AuthUserFile => $auth_user_file,
266 AuthGroupFile => $auth_group_file,
267 AuthOrder => 'mod_auth_file.c',
268
269 IfModules => {
270 'mod_delay.c' => {
271 DelayEngine => 'off',
272 },
273 },
274 };
275
276 my ($port, $config_user, $config_group) = config_write($config_file, $config);
277
278 my $define = 'FOO';
279
280 if (open(my $fh, ">> $config_file")) {
281 print $fh <<EOC;
282 Define $define
283
284 <IfDefine $define>
285 ServerIdent on
286 </IfDefine>
287 EOC
288 unless (close($fh)) {
289 die("Can't write $config_file: $!");
290 }
291
292 } else {
293 die("Can't open $config_file: $!");
294 }
295
296 eval { server_start($config_file) };
297 if ($@) {
298 my $ex = $@;
299
300 test_append_logfile($log_file, $ex);
301 unlink($log_file);
302
303 die($ex);
304 }
305
306 # Give the daemon a chance to start up
307 sleep(1);
308 server_stop($pid_file);
309
310 unlink($log_file);
311 }
312
313 sub ifdefine_exists_via_env {
314 my $self = shift;
315 my $tmpdir = $self->{tmpdir};
316
317 my $config_file = "$tmpdir/config.conf";
318 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
319 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
320
321 my $log_file = test_get_logfile();
322
323 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
324 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
325
326 my $user = 'proftpd';
327 my $passwd = 'test';
328 my $group = 'ftpd';
329 my $home_dir = File::Spec->rel2abs($tmpdir);
330 my $uid = 500;
331 my $gid = 500;
332
333 # Make sure that, if we're running as root, that the home directory has
334 # permissions/privs set for the account we create
335 if ($< == 0) {
336 unless (chmod(0755, $home_dir)) {
337 die("Can't set perms on $home_dir to 0755: $!");
338 }
339
340 unless (chown($uid, $gid, $home_dir)) {
341 die("Can't set owner of $home_dir to $uid/$gid: $!");
342 }
343 }
344
345 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
346 '/bin/bash');
347 auth_group_write($auth_group_file, $group, $gid, $user);
348
349 my $config = {
350 PidFile => $pid_file,
351 ScoreboardFile => $scoreboard_file,
352 SystemLog => $log_file,
353
354 AuthUserFile => $auth_user_file,
355 AuthGroupFile => $auth_group_file,
356 AuthOrder => 'mod_auth_file.c',
357
358 IfModules => {
359 'mod_delay.c' => {
360 DelayEngine => 'off',
361 },
362 },
363 };
364
365 my ($port, $config_user, $config_group) = config_write($config_file, $config);
366
367 my $key = 'FOO';
368 my $value = 'BAR';
369
370 $ENV{$key} = $value;
371
372 if (open(my $fh, ">> $config_file")) {
373 print $fh <<EOC;
374 <IfDefine $key=%{env:$key}>
375 ServerIdent on
376 </IfDefine>
377 EOC
378 unless (close($fh)) {
379 die("Can't write $config_file: $!");
380 }
381
382 } else {
383 die("Can't open $config_file: $!");
384 }
385
386 eval { server_start($config_file, undef,
387 { define => [$key . '=$' . $key] }) };
388 if ($@) {
389 my $ex = $@;
390
391 test_append_logfile($log_file, $ex);
392 unlink($log_file);
393
394 die($ex);
395 }
396
397 # Give the daemon a chance to start up
398 sleep(1);
399 server_stop($pid_file);
400
401 unlink($log_file);
402 }
403
404 1;