Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_geoip.pm

raw
1 package ProFTPD::Tests::Modules::mod_geoip;
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 geoip_explicitly_allowed => {
20 order => ++$order,
21 test_class => [qw(bug forking)],
22 },
23
24 geoip_multi_allow_bug4188 => {
25 order => ++$order,
26 test_class => [qw(bug forking)],
27 },
28
29 };
30
31 sub new {
32 return shift()->SUPER::new(@_);
33 }
34
35 sub list_tests {
36 return testsuite_get_runnable_tests($TESTS);
37 }
38
39 sub geoip_explicitly_allowed {
40 my $self = shift;
41 my $tmpdir = $self->{tmpdir};
42 my $setup = test_setup($tmpdir, 'geoip');
43
44 my $test_file = File::Spec->rel2abs($setup->{config_file});
45
46 my $geoip_ip_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoIP.dat');
47 my $geoip_ipv6_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoIPv6.dat');
48 my $geoip_city_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoLiteCity.dat');
49
50 my $config = {
51 PidFile => $setup->{pid_file},
52 ScoreboardFile => $setup->{scoreboard_file},
53 SystemLog => $setup->{log_file},
54 TraceLog => $setup->{log_file},
55 Trace => 'geoip:20',
56
57 AuthUserFile => $setup->{auth_user_file},
58 AuthGroupFile => $setup->{auth_group_file},
59 AuthOrder => 'mod_auth_file.c',
60
61 IfModules => {
62 'mod_delay.c' => {
63 DelayEngine => 'off',
64 },
65
66 'mod_geoip.c' => [
67 'GeoIPEngine on',
68 "GeoIPLog $setup->{log_file}",
69 "GeoIPTable $geoip_ip_table",
70 "GeoIPTable $geoip_ipv6_table",
71 "GeoIPTable $geoip_city_table",
72
73 'GeoIPPolicy allow,deny',
74 'GeoIPAllowFilter RegionCode TX',
75 'GeoIPDenyFilter CountryCode US',
76 ],
77 },
78 };
79
80 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
81 $config);
82
83 # Open pipes, for use between the parent and child processes. Specifically,
84 # the child will indicate when it's done with its test by writing a message
85 # to the parent.
86 my ($rfh, $wfh);
87 unless (pipe($rfh, $wfh)) {
88 die("Can't open pipe: $!");
89 }
90
91 my $ex;
92
93 # Fork child
94 $self->handle_sigchld();
95 defined(my $pid = fork()) or die("Can't fork: $!");
96 if ($pid) {
97 eval {
98 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 1);
99 $client->quit();
100 };
101
102 if ($@) {
103 $ex = $@;
104 }
105
106 $wfh->print("done\n");
107 $wfh->flush();
108
109 } else {
110 eval { server_wait($setup->{config_file}, $rfh) };
111 if ($@) {
112 warn($@);
113 exit 1;
114 }
115
116 exit 0;
117 }
118
119 # Stop server
120 server_stop($setup->{pid_file});
121
122 $self->assert_child_ok($pid);
123
124 test_cleanup($setup->{log_file}, $ex);
125 }
126
127 sub geoip_multi_allow_bug4188 {
128 my $self = shift;
129 my $tmpdir = $self->{tmpdir};
130 my $setup = test_setup($tmpdir, 'geoip');
131
132 my $test_file = File::Spec->rel2abs($setup->{config_file});
133
134 my $geoip_ip_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoIP.dat');
135 my $geoip_ipv6_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoIPv6.dat');
136 my $geoip_city_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoLiteCity.dat');
137
138 my $config = {
139 PidFile => $setup->{pid_file},
140 ScoreboardFile => $setup->{scoreboard_file},
141 SystemLog => $setup->{log_file},
142 TraceLog => $setup->{log_file},
143 Trace => 'geoip:20',
144
145 AuthUserFile => $setup->{auth_user_file},
146 AuthGroupFile => $setup->{auth_group_file},
147 AuthOrder => 'mod_auth_file.c',
148
149 IfModules => {
150 'mod_delay.c' => {
151 DelayEngine => 'off',
152 },
153
154 'mod_geoip.c' => [
155 'GeoIPEngine on',
156 "GeoIPLog $setup->{log_file}",
157 "GeoIPTable $geoip_ip_table",
158 "GeoIPTable $geoip_ipv6_table",
159 "GeoIPTable $geoip_city_table",
160
161 'GeoIPPolicy deny,allow',
162 'GeoIPAllowFilter RegionCode TX CountryCode US',
163 ],
164 },
165 };
166
167 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
168 $config);
169
170 # Open pipes, for use between the parent and child processes. Specifically,
171 # the child will indicate when it's done with its test by writing a message
172 # to the parent.
173 my ($rfh, $wfh);
174 unless (pipe($rfh, $wfh)) {
175 die("Can't open pipe: $!");
176 }
177
178 my $ex;
179
180 # Fork child
181 $self->handle_sigchld();
182 defined(my $pid = fork()) or die("Can't fork: $!");
183 if ($pid) {
184 eval {
185 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
186 $client->quit();
187 };
188
189 if ($@) {
190 $ex = $@;
191 }
192
193 $wfh->print("done\n");
194 $wfh->flush();
195
196 } else {
197 eval { server_wait($setup->{config_file}, $rfh) };
198 if ($@) {
199 warn($@);
200 exit 1;
201 }
202
203 exit 0;
204 }
205
206 # Stop server
207 server_stop($setup->{pid_file});
208
209 $self->assert_child_ok($pid);
210
211 test_cleanup($setup->{log_file}, $ex);
212 }
213
214 1;