Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_sftp/quotatab.pm

raw
1 package ProFTPD::Tests::Modules::mod_sftp::quotatab;
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 use POSIX qw(:fcntl_h);
11
12 use ProFTPD::TestSuite::FTP;
13 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
14
15 $| = 1;
16
17 my $order = 0;
18
19 my $TESTS = {
20 sftp_quotatab_upload_hard_limit_issue2098 => {
21 order => ++$order,
22 test_class => [qw(bug forking mod_quotatab_sql mod_sql_sqlite)],
23 },
24
25 };
26
27 sub new {
28 return shift()->SUPER::new(@_);
29 }
30
31 sub list_tests {
32 return testsuite_get_runnable_tests($TESTS);
33 }
34
35 sub set_up {
36 my $self = shift;
37 $self->SUPER::set_up(@_);
38
39 # Make sure that mod_sftp does not complain about permissions on the hostkey
40 # files.
41
42 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
43 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
44
45 unless (chmod(0400, $rsa_host_key, $dsa_host_key)) {
46 die("Can't set perms on $rsa_host_key, $dsa_host_key: $!");
47 }
48 }
49
50 sub sftp_quotatab_upload_hard_limit_issue2098 {
51 my $self = shift;
52 my $tmpdir = $self->{tmpdir};
53 my $setup = test_setup($tmpdir, 'quotatab');
54
55 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
56 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
57
58 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
59
60 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
61
62 if (open(my $fh, "> $db_script")) {
63 print $fh <<EOS;
64 CREATE TABLE quotalimits (
65 name TEXT NOT NULL PRIMARY KEY,
66 quota_type TEXT NOT NULL,
67 per_session TEXT NOT NULL,
68 limit_type TEXT NOT NULL,
69 bytes_in_avail REAL NOT NULL,
70 bytes_out_avail REAL NOT NULL,
71 bytes_xfer_avail REAL NOT NULL,
72 files_in_avail INTEGER NOT NULL,
73 files_out_avail INTEGER NOT NULL,
74 files_xfer_avail INTEGER NOT NULL
75 );
76 INSERT INTO quotalimits (name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail) VALUES ('$setup->{user}', 'user', 'false', 'hard', 32, 0, 0, 2, 0, 0);
77
78 CREATE TABLE quotatallies (
79 name TEXT NOT NULL PRIMARY KEY,
80 quota_type TEXT NOT NULL,
81 bytes_in_used REAL NOT NULL,
82 bytes_out_used REAL NOT NULL,
83 bytes_xfer_used REAL NOT NULL,
84 files_in_used INTEGER NOT NULL,
85 files_out_used INTEGER NOT NULL,
86 files_xfer_used INTEGER NOT NULL
87 );
88 EOS
89
90 unless (close($fh)) {
91 die("Can't write $db_script: $!");
92 }
93
94 } else {
95 die("Can't open $db_script: $!");
96 }
97
98 my $cmd = "sqlite3 $db_file < $db_script";
99
100 if ($ENV{TEST_VERBOSE}) {
101 print STDERR "Executing sqlite3: $cmd\n";
102 }
103
104 my @output = `$cmd`;
105 if (scalar(@output) &&
106 $ENV{TEST_VERBOSE}) {
107 print STDERR "Output: ", join('', @output), "\n";
108 }
109
110 # Make sure that, if we're running as root, the database file has
111 # the permissions/privs set for use by proftpd
112 if ($< == 0) {
113 unless (chmod(0666, $db_file)) {
114 die("Can't set perms on $db_file to 0666: $!");
115 }
116 }
117
118 my $config = {
119 PidFile => $setup->{pid_file},
120 ScoreboardFile => $setup->{scoreboard_file},
121 SystemLog => $setup->{log_file},
122 TraceLog => $setup->{log_file},
123 Trace => 'fsio:20 quotatab:20 sql:20 ssh2:20 sftp:30',
124
125 AuthUserFile => $setup->{auth_user_file},
126 AuthGroupFile => $setup->{auth_group_file},
127 AuthOrder => 'mod_auth_file.c',
128
129 IfModules => {
130 'mod_delay.c' => {
131 DelayEngine => 'off',
132 },
133
134 'mod_quotatab_sql.c' => [
135 'SQLNamedQuery get-quota-limit SELECT "name, quota_type, per_session, limit_type, bytes_in_avail, bytes_out_avail, bytes_xfer_avail, files_in_avail, files_out_avail, files_xfer_avail FROM quotalimits WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
136 'SQLNamedQuery get-quota-tally SELECT "name, quota_type, bytes_in_used, bytes_out_used, bytes_xfer_used, files_in_used, files_out_used, files_xfer_used FROM quotatallies WHERE name = \'%{0}\' AND quota_type = \'%{1}\'"',
137 'SQLNamedQuery update-quota-tally UPDATE "bytes_in_used = bytes_in_used + %{0}, bytes_out_used = bytes_out_used + %{1}, bytes_xfer_used = bytes_xfer_used + %{2}, files_in_used = files_in_used + %{3}, files_out_used = files_out_used + %{4}, files_xfer_used = files_xfer_used + %{5} WHERE name = \'%{6}\' AND quota_type = \'%{7}\'" quotatallies',
138 'SQLNamedQuery insert-quota-tally INSERT "%{0}, %{1}, %{2}, %{3}, %{4}, %{5}, %{6}, %{7}" quotatallies',
139
140 'QuotaEngine on',
141 "QuotaLog $setup->{log_file}",
142 'QuotaLimitTable sql:/get-quota-limit',
143 'QuotaTallyTable sql:/get-quota-tally/update-quota-tally/insert-quota-tally',
144 ],
145
146 'mod_sftp.c' => [
147 "SFTPEngine on",
148 "SFTPLog $setup->{log_file}",
149 "SFTPHostKey $rsa_host_key",
150 "SFTPHostKey $dsa_host_key",
151 ],
152
153 'mod_sql.c' => {
154 SQLEngine => 'log',
155 SQLBackend => 'sqlite3',
156 SQLConnectInfo => $db_file,
157 SQLLogFile => $setup->{log_file},
158 },
159 },
160 };
161
162 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
163 $config);
164
165 # Open pipes, for use between the parent and child processes. Specifically,
166 # the child will indicate when it's done with its test by writing a message
167 # to the parent.
168 my ($rfh, $wfh);
169 unless (pipe($rfh, $wfh)) {
170 die("Can't open pipe: $!");
171 }
172
173 require Net::SSH2;
174
175 my $ex;
176
177 # Fork child
178 $self->handle_sigchld();
179 defined(my $pid = fork()) or die("Can't fork: $!");
180 if ($pid) {
181 eval {
182 my $ssh2 = Net::SSH2->new();
183
184 sleep(1);
185
186 unless ($ssh2->connect('127.0.0.1', $port)) {
187 my ($err_code, $err_name, $err_str) = $ssh2->error();
188 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
189 }
190
191 unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) {
192 my ($err_code, $err_name, $err_str) = $ssh2->error();
193 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
194 }
195
196 my $sftp = $ssh2->sftp();
197 unless ($sftp) {
198 my ($err_code, $err_name, $err_str) = $ssh2->error();
199 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
200 }
201
202 my $fh = $sftp->open('test.dat', O_WRONLY|O_CREAT|O_TRUNC, 0644);
203 unless ($fh) {
204 my ($err_code, $err_name) = $sftp->error();
205 die("Can't open test.txt: [$err_name] ($err_code)");
206 }
207
208 my $count = 20;
209 for (my $i = 0; $i < $count; $i++) {
210 print $fh "ABCD" x 8192;
211 }
212
213 # To issue the FXP_CLOSE, we have to explicitly destroy the filehandle
214 $fh = undef;
215
216 $sftp = undef;
217 $ssh2->disconnect();
218 };
219 if ($@) {
220 $ex = $@;
221 }
222
223 $wfh->print("done\n");
224 $wfh->flush();
225
226 } else {
227 eval { server_wait($setup->{config_file}, $rfh) };
228 if ($@) {
229 warn($@);
230 exit 1;
231 }
232
233 exit 0;
234 }
235
236 # Stop server
237 server_stop($setup->{pid_file});
238 $self->assert_child_ok($pid);
239
240 eval {
241 if (open(my $fh, "< $setup->{log_file}")) {
242 my $ok = 0;
243
244 while (my $line = <$fh>) {
245 chomp($line);
246
247 if ($ENV{TEST_VERBOSE}) {
248 print STDERR "# $line\n";
249 }
250
251 if ($line =~ /limit exceeded/) {
252 $ok = 1;
253 last;
254 }
255 }
256
257 close($fh);
258 $self->assert($ok, test_msg("Did not see expected QuotaLog message"));
259
260 } else {
261 die("Can't read $setup->{log_file}: $!");
262 }
263 };
264 if ($@) {
265 $ex = $@;
266 }
267
268 test_cleanup($setup->{log_file}, $ex);
269 }
270
271 1;