Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::AccessGrantMsg;
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 accessgrantmsg_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 accessgrantmsg_var_u_ok => {
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 accessgrantmsg_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 $access_grant_msg = '"Speak friend, and enter"';
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 AccessGrantMsg => $access_grant_msg,
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 my ($resp_code, $resp_msg) = $client->login($user, $passwd);
112
113 my $expected;
114
115 $expected = 230;
116 $self->assert($expected == $resp_code,
117 test_msg("Expected $expected, got $resp_code"));
118
119 $expected = "Speak friend, and enter";
120 $self->assert($expected eq $resp_msg,
121 test_msg("Expected '$expected', got '$resp_msg'"));
122 };
123
124 if ($@) {
125 $ex = $@;
126 }
127
128 $wfh->print("done\n");
129 $wfh->flush();
130
131 } else {
132 eval { server_wait($config_file, $rfh) };
133 if ($@) {
134 warn($@);
135 exit 1;
136 }
137
138 exit 0;
139 }
140
141 # Stop server
142 server_stop($pid_file);
143
144 $self->assert_child_ok($pid);
145
146 if ($ex) {
147 die($ex);
148 }
149
150 unlink($log_file);
151 }
152
153 sub accessgrantmsg_var_u_ok {
154 my $self = shift;
155 my $tmpdir = $self->{tmpdir};
156
157 my $config_file = "$tmpdir/config.conf";
158 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
159 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
160
161 my $log_file = File::Spec->rel2abs('tests.log');
162
163 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
164 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
165
166 my $user = 'proftpd';
167 my $passwd = 'test';
168 my $home_dir = File::Spec->rel2abs($tmpdir);
169 my $uid = 500;
170 my $gid = 500;
171
172 # Make sure that, if we're running as root, that the home directory has
173 # permissions/privs set for the account we create
174 if ($< == 0) {
175 unless (chmod(0755, $home_dir)) {
176 die("Can't set perms on $home_dir to 0755: $!");
177 }
178
179 unless (chown($uid, $gid, $home_dir)) {
180 die("Can't set owner of $home_dir to $uid/$gid: $!");
181 }
182 }
183
184 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
185 '/bin/bash');
186 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
187
188 my $access_grant_msg = '"Welcome %u to my humble abode of files"';
189
190 my $config = {
191 PidFile => $pid_file,
192 ScoreboardFile => $scoreboard_file,
193 SystemLog => $log_file,
194
195 AuthUserFile => $auth_user_file,
196 AuthGroupFile => $auth_group_file,
197 AuthOrder => 'mod_auth_file.c',
198
199 AccessGrantMsg => $access_grant_msg,
200
201 IfModules => {
202 'mod_delay.c' => {
203 DelayEngine => 'off',
204 },
205 },
206 };
207
208 my ($port, $config_user, $config_group) = config_write($config_file, $config);
209
210 # Open pipes, for use between the parent and child processes. Specifically,
211 # the child will indicate when it's done with its test by writing a message
212 # to the parent.
213 my ($rfh, $wfh);
214 unless (pipe($rfh, $wfh)) {
215 die("Can't open pipe: $!");
216 }
217
218 my $ex;
219
220 # Fork child
221 $self->handle_sigchld();
222 defined(my $pid = fork()) or die("Can't fork: $!");
223 if ($pid) {
224 eval {
225 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
226 my ($resp_code, $resp_msg) = $client->login($user, $passwd);
227
228 my $expected;
229
230 $expected = 230;
231 $self->assert($expected == $resp_code,
232 test_msg("Expected $expected, got $resp_code"));
233
234 $expected = "Welcome $user to my humble abode of files";
235 $self->assert($expected eq $resp_msg,
236 test_msg("Expected '$expected', got '$resp_msg'"));
237 };
238
239 if ($@) {
240 $ex = $@;
241 }
242
243 $wfh->print("done\n");
244 $wfh->flush();
245
246 } else {
247 eval { server_wait($config_file, $rfh) };
248 if ($@) {
249 warn($@);
250 exit 1;
251 }
252
253 exit 0;
254 }
255
256 # Stop server
257 server_stop($pid_file);
258
259 $self->assert_child_ok($pid);
260
261 if ($ex) {
262 die($ex);
263 }
264
265 unlink($log_file);
266 }
267
268 1;