source: src/router/proftpd/tests/t/lib/ProFTPD/Tests/Config/MaxClientsPerHost.pm @ 17880

Last change on this file since 17880 was 17880, checked in by BrainSlayer, 19 months ago

update

File size: 5.6 KB
Line 
1package ProFTPD::Tests::Config::MaxClientsPerHost;
2
3use lib qw(t/lib);
4use base qw(ProFTPD::TestSuite::Child);
5use strict;
6
7use File::Spec;
8use IO::Handle;
9
10use ProFTPD::TestSuite::FTP;
11use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
12
13$| = 1;
14
15my $order = 0;
16
17my $TESTS = {
18  maxclientsperhost_one => {
19    order => ++$order,
20    test_class => [qw(forking)],
21  },
22
23  maxclientsperhost_one_multi_conns => {
24    order => ++$order,
25    test_class => [qw(forking)],
26  },
27
28};
29
30sub new {
31  return shift()->SUPER::new(@_);
32}
33
34sub list_tests {
35  return testsuite_get_runnable_tests($TESTS);
36}
37
38sub maxclientsperhost_one {
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 $group = 'ftpd';
54  my $home_dir = File::Spec->rel2abs($tmpdir);
55  my $uid = 500;
56  my $gid = 500;
57
58  auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
59    '/bin/bash');
60  auth_group_write($auth_group_file, $group, $gid, $user);
61
62  my $max_clients_per_host = 1;
63
64  my $config = {
65    PidFile => $pid_file,
66    ScoreboardFile => $scoreboard_file,
67    SystemLog => $log_file,
68
69    AuthUserFile => $auth_user_file,
70    AuthGroupFile => $auth_group_file,
71    MaxClientsPerHost => $max_clients_per_host,
72
73    IfModules => {
74      'mod_delay.c' => {
75        DelayEngine => 'off',
76      },
77    },
78  };
79
80  my ($port, $config_user, $config_group) = config_write($config_file, $config);
81
82  # Open pipes, for use between the parent and child processes.  Specifically,
83  # the child will indicate when it's done with its test by writing a message
84  # to the parent.
85  my ($rfh, $wfh);
86  unless (pipe($rfh, $wfh)) {
87    die("Can't open pipe: $!");
88  }
89
90  my $ex;
91
92  # Fork child
93  $self->handle_sigchld();
94  defined(my $pid = fork()) or die("Can't fork: $!");
95  if ($pid) {
96    eval {
97      # First client should be able to connect and log in...
98      my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
99      $client1->login($user, $passwd);
100
101      # ...but the second client should be able to connect, but not login.
102      my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
103
104      eval { $client2->login($user, $passwd) };
105      unless ($@) {
106        die("Login succeeded unexpectedly");
107      }
108
109      $client1->quit();
110    };
111
112    if ($@) {
113      $ex = $@;
114    }
115
116    $wfh->print("done\n");
117    $wfh->flush();
118
119  } else {
120    eval { server_wait($config_file, $rfh) };
121    if ($@) {
122      warn($@);
123      exit 1;
124    }
125
126    exit 0;
127  }
128
129  # Stop server
130  server_stop($pid_file);
131
132  $self->assert_child_ok($pid);
133
134  if ($ex) {
135    die($ex);
136  }
137
138  unlink($log_file);
139}
140
141sub maxclientsperhost_one_multi_conns {
142  my $self = shift;
143  my $tmpdir = $self->{tmpdir};
144
145  my $config_file = "$tmpdir/config.conf";
146  my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
147  my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
148
149  my $log_file = File::Spec->rel2abs('tests.log');
150
151  my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
152  my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
153
154  my $user = 'proftpd';
155  my $passwd = 'test';
156  my $group = 'ftpd';
157  my $home_dir = File::Spec->rel2abs($tmpdir);
158  my $uid = 500;
159  my $gid = 500;
160
161  auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
162    '/bin/bash');
163  auth_group_write($auth_group_file, $group, $gid, $user);
164
165  my $max_clients_per_host = 1;
166
167  my $config = {
168    PidFile => $pid_file,
169    ScoreboardFile => $scoreboard_file,
170    SystemLog => $log_file,
171
172    AuthUserFile => $auth_user_file,
173    AuthGroupFile => $auth_group_file,
174
175    MaxClientsPerHost => $max_clients_per_host,
176
177    IfModules => {
178      'mod_delay.c' => {
179        DelayEngine => 'off',
180      },
181    },
182  };
183
184  my ($port, $config_user, $config_group) = config_write($config_file, $config);
185
186  # Open pipes, for use between the parent and child processes.  Specifically,
187  # the child will indicate when it's done with its test by writing a message
188  # to the parent.
189  my ($rfh, $wfh);
190  unless (pipe($rfh, $wfh)) {
191    die("Can't open pipe: $!");
192  }
193
194  my $ex;
195
196  # Fork child
197  $self->handle_sigchld();
198  defined(my $pid = fork()) or die("Can't fork: $!");
199  if ($pid) {
200    eval {
201      # First client should be able to connect and log in...
202      my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
203      $client1->login($user, $passwd);
204
205      # ...but the second client should be able to connect, but not login.
206      my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
207
208      eval { $client2->login($user, $passwd) };
209      unless ($@) {
210        die("Login succeeded unexpectedly");
211      }
212
213      # Even though we can't log in, we should be able to connect quite
214      # a few more times
215
216      my $clients = [];
217      for (my $i = 0; $i < 10; $i++) {
218        my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
219        push(@$clients, $client);
220      }
221
222      $client1->quit();
223    };
224
225    if ($@) {
226      $ex = $@;
227    }
228
229    $wfh->print("done\n");
230    $wfh->flush();
231
232  } else {
233    eval { server_wait($config_file, $rfh) };
234    if ($@) {
235      warn($@);
236      exit 1;
237    }
238
239    exit 0;
240  }
241
242  # Stop server
243  server_stop($pid_file);
244
245  $self->assert_child_ok($pid);
246
247  if ($ex) {
248    die($ex);
249  }
250
251  unlink($log_file);
252}
253
2541;
Note: See TracBrowser for help on using the repository browser.