Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Commands::CLNT;
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 clnt_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 clnt_logformat_note_clnt => {
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 clnt_ok {
39 my $self = shift;
40 my $tmpdir = $self->{tmpdir};
41 my $setup = test_setup($tmpdir, 'cmds');
42
43 my $config = {
44 PidFile => $setup->{pid_file},
45 ScoreboardFile => $setup->{scoreboard_file},
46 SystemLog => $setup->{log_file},
47
48 IfModules => {
49 'mod_delay.c' => {
50 DelayEngine => 'off',
51 },
52 },
53 };
54
55 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
56 $config);
57
58 # Open pipes, for use between the parent and child processes. Specifically,
59 # the child will indicate when it's done with its test by writing a message
60 # to the parent.
61 my ($rfh, $wfh);
62 unless (pipe($rfh, $wfh)) {
63 die("Can't open pipe: $!");
64 }
65
66 my $ex;
67
68 # Fork child
69 $self->handle_sigchld();
70 defined(my $pid = fork()) or die("Can't fork: $!");
71 if ($pid) {
72 eval {
73 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
74 my ($resp_code, $resp_msg) = $client->clnt('FOOBAR');
75
76 my $expected;
77
78 $expected = 200;
79 $self->assert($expected == $resp_code,
80 test_msg("Expected response code $expected, got $resp_code"));
81
82 $expected = "OK";
83 $self->assert($expected eq $resp_msg,
84 test_msg("Expected response message '$expected', got '$resp_msg'"));
85 };
86
87 if ($@) {
88 $ex = $@;
89 }
90
91 $wfh->print("done\n");
92 $wfh->flush();
93
94 } else {
95 eval { server_wait($setup->{config_file}, $rfh) };
96 if ($@) {
97 warn($@);
98 exit 1;
99 }
100
101 exit 0;
102 }
103
104 # Stop server
105 server_stop($setup->{pid_file});
106 $self->assert_child_ok($pid);
107
108 test_cleanup($setup->{log_file}, $ex);
109 }
110
111 sub clnt_logformat_note_clnt {
112 my $self = shift;
113 my $tmpdir = $self->{tmpdir};
114 my $setup = test_setup($tmpdir, 'cmds');
115
116 my $ext_log = File::Spec->rel2abs("$tmpdir/extended.log");
117
118 my $config = {
119 PidFile => $setup->{pid_file},
120 ScoreboardFile => $setup->{scoreboard_file},
121 SystemLog => $setup->{log_file},
122
123 LogFormat => 'custom "method=%m client=%{note:clnt}"',
124 ExtendedLog => "$ext_log INFO custom",
125
126 IfModules => {
127 'mod_delay.c' => {
128 DelayEngine => 'off',
129 },
130 },
131 };
132
133 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
134 $config);
135
136 # Open pipes, for use between the parent and child processes. Specifically,
137 # the child will indicate when it's done with its test by writing a message
138 # to the parent.
139 my ($rfh, $wfh);
140 unless (pipe($rfh, $wfh)) {
141 die("Can't open pipe: $!");
142 }
143
144 my $ex;
145
146 # Fork child
147 $self->handle_sigchld();
148 defined(my $pid = fork()) or die("Can't fork: $!");
149 if ($pid) {
150 eval {
151 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
152 my ($resp_code, $resp_msg) = $client->clnt('FOOBAR');
153
154 my $expected = 200;
155 $self->assert($expected == $resp_code,
156 test_msg("Expected response code $expected, got $resp_code"));
157
158 $expected = "OK";
159 $self->assert($expected eq $resp_msg,
160 test_msg("Expected response message '$expected', got '$resp_msg'"));
161
162 $client->quit();
163 };
164 if ($@) {
165 $ex = $@;
166 }
167
168 $wfh->print("done\n");
169 $wfh->flush();
170
171 } else {
172 eval { server_wait($setup->{config_file}, $rfh) };
173 if ($@) {
174 warn($@);
175 exit 1;
176 }
177
178 exit 0;
179 }
180
181 # Stop server
182 server_stop($setup->{pid_file});
183 $self->assert_child_ok($pid);
184
185 eval {
186 if (open(my $fh, "< $ext_log")) {
187 my $client_info;
188
189 while (my $line = <$fh>) {
190 chomp($line);
191
192 if ($line =~ /^method=CLNT client=(\S+)$/) {
193 $client_info = $1;
194 last;
195 }
196 }
197
198 close($fh);
199
200 my $expected = 'FOOBAR';
201 $self->assert($client_info eq $expected,
202 test_msg("Expected ExtendedLog '$expected', got '$client_info'"));
203
204 } else {
205 die("Can't read $ext_log: $!");
206 }
207 };
208 if ($@) {
209 $ex = $@;
210 }
211
212 test_cleanup($setup->{log_file}, $ex);
213 }
214
215 1;