Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/USER.pm

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