Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::SetEnv;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Path qw(mkpath);
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 setenv_serverroot => {
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 setenv_serverroot {
35 my $self = shift;
36 my $tmpdir = $self->{tmpdir};
37
38 my $config_file = "$tmpdir/config.conf";
39
40 my $server_root = File::Spec->rel2abs($tmpdir);
41
42 my $pid_path = File::Spec->rel2abs("$tmpdir/config.pid");
43 my $pid_file = '%{env:SERVER_ROOT}/config.pid';
44 my $scoreboard_file = '%{env:SERVER_ROOT}/config.scoreboard';
45
46 my $log_file = test_get_logfile();
47
48 my $auth_user_path = File::Spec->rel2abs("$tmpdir/config.passwd");
49 my $auth_user_file = '%{env:SERVER_ROOT}/config.passwd';
50
51 my $auth_group_path = File::Spec->rel2abs("$tmpdir/config.group");
52 my $auth_group_file = '%{env:SERVER_ROOT}/config.group';
53
54 my $user = 'proftpd';
55 my $passwd = 'test';
56 my $group = 'ftpd';
57 my $home_dir = File::Spec->rel2abs($tmpdir);
58 my $uid = 500;
59 my $gid = 500;
60
61 # Make sure that, if we're running as root, that the home directory has
62 # permissions/privs set for the account we create
63 if ($< == 0) {
64 unless (chmod(0755, $home_dir)) {
65 die("Can't set perms on $home_dir to 0755: $!");
66 }
67
68 unless (chown($uid, $gid, $home_dir)) {
69 die("Can't set owner of $home_dir to $uid/$gid: $!");
70 }
71 }
72
73 auth_user_write($auth_user_path, $user, $passwd, $uid, $gid, $home_dir,
74 '/bin/bash');
75 auth_group_write($auth_group_path, $group, $gid, $user);
76
77 my $config = [
78 "SetEnv SERVER_ROOT $server_root",
79 "PidFile $pid_file",
80 "ScoreboardFile $scoreboard_file",
81 "SystemLog $log_file",
82
83 'AllowOverwrite on',
84 'AllowStoreRestart on',
85 "AuthUserFile $auth_user_file",
86 "AuthGroupFile $auth_group_file",
87 "AuthOrder mod_auth_file.c",
88
89 'DefaultChdir ~',
90
91 '<IfModule mod_delay.c>',
92 ' DelayEngine off',
93 '</IfModule>',
94 ];
95
96 my ($port, $config_user, $config_group) = config_write($config_file, $config);
97
98 # Open pipes, for use between the parent and child processes. Specifically,
99 # the child will indicate when it's done with its test by writing a message
100 # to the parent.
101 my ($rfh, $wfh);
102 unless (pipe($rfh, $wfh)) {
103 die("Can't open pipe: $!");
104 }
105
106 my $ex;
107
108 # Fork child
109 $self->handle_sigchld();
110 defined(my $pid = fork()) or die("Can't fork: $!");
111 if ($pid) {
112 eval {
113 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
114 $client->login($user, $passwd);
115
116 my $conn = $client->stor_raw('test.txt');
117 unless ($conn) {
118 die("Failed to STOR test.txt: " . $client->response_code() . " " .
119 $client->response_msg());
120 }
121
122 my $buf = "Hello, World!\n";
123 $conn->write($buf, length($buf), 25);
124 eval { $conn->close() };
125
126 my $resp_code = $client->response_code();
127 my $resp_msg = $client->response_msg();
128 $self->assert_transfer_ok($resp_code, $resp_msg);
129
130 $client->quit();
131 };
132
133 if ($@) {
134 $ex = $@;
135 }
136
137 $wfh->print("done\n");
138 $wfh->flush();
139
140 } else {
141 eval { server_wait($config_file, $rfh) };
142 if ($@) {
143 warn($@);
144 exit 1;
145 }
146
147 exit 0;
148 }
149
150 # Stop server
151 server_stop($pid_path);
152
153 $self->assert_child_ok($pid);
154
155 if ($ex) {
156 test_append_logfile($log_file, $ex);
157 unlink($log_file);
158
159 die($ex);
160 }
161
162 unlink($log_file);
163 }
164
165 1;