Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/SITE/CHGRP.pm

raw
1 package ProFTPD::Tests::Commands::SITE::CHGRP;
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 chgrp_name_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 };
24
25 sub new {
26 return shift()->SUPER::new(@_);
27 }
28
29 sub list_tests {
30 return testsuite_get_runnable_tests($TESTS);
31 }
32
33 sub chgrp_name_ok {
34 my $self = shift;
35 my $tmpdir = $self->{tmpdir};
36
37 my $config_file = "$tmpdir/cmds.conf";
38 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
39 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
40
41 my $log_file = test_get_logfile();
42
43 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
44 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
45
46 my $user = 'proftpd';
47 my $passwd = 'test';
48 my $group = 'ftpd';
49 my $home_dir = File::Spec->rel2abs($tmpdir);
50 my $uid = 500;
51 my $gid = 500;
52
53 my $file_group = (config_get_identity())[1];
54 my $file_gid = (split(' ', $())[0];
55
56 # Make sure that, if we're running as root, that the home directory has
57 # permissions/privs set for the account we create
58 if ($< == 0) {
59 unless (chmod(0755, $home_dir)) {
60 die("Can't set perms on $home_dir to 0755: $!");
61 }
62
63 unless (chown($uid, $gid, $home_dir)) {
64 die("Can't set owner of $home_dir to $uid/$gid: $!");
65 }
66
67 $file_group = $group;
68 $file_gid = $gid;
69 }
70
71 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
72 '/bin/bash');
73 auth_group_write($auth_group_file, $group, $gid, $user);
74
75 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
76 if (open(my $fh, "> $test_file")) {
77 print $fh "Hello, World!\n";
78 unless (close($fh)) {
79 die("Can't write $test_file: $!");
80 }
81
82 } else {
83 die("Can't open $test_file: $!");
84 }
85
86 my $config = {
87 PidFile => $pid_file,
88 ScoreboardFile => $scoreboard_file,
89 SystemLog => $log_file,
90
91 AuthUserFile => $auth_user_file,
92 AuthGroupFile => $auth_group_file,
93
94 IfModules => {
95 'mod_delay.c' => {
96 DelayEngine => 'off',
97 },
98 },
99 };
100
101 my ($port, $config_user, $config_group) = config_write($config_file, $config);
102
103 # Open pipes, for use between the parent and child processes. Specifically,
104 # the child will indicate when it's done with its test by writing a message
105 # to the parent.
106 my ($rfh, $wfh);
107 unless (pipe($rfh, $wfh)) {
108 die("Can't open pipe: $!");
109 }
110
111 my $ex;
112
113 # Fork child
114 $self->handle_sigchld();
115 defined(my $pid = fork()) or die("Can't fork: $!");
116 if ($pid) {
117 eval {
118 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
119 $client->login($user, $passwd);
120
121 $client->site('CHGRP', $file_group, 'test.txt');
122
123 my $resp_code = $client->response_code();
124 my $resp_msg = $client->response_msg();
125
126 my $expected = 200;
127 $self->assert($expected == $resp_code,
128 test_msg("Expected response code $expected, got $resp_code"));
129
130 $expected = 'SITE CHGRP command successful';
131 $self->assert($expected eq $resp_msg,
132 test_msg("Expected response message '$expected', got '$resp_msg'"));
133
134 $client->quit();
135
136 my $test_gid = (stat($test_file))[5];
137 $expected = $file_gid;
138 $self->assert($expected == $test_gid,
139 test_msg("Expected GID $expected, got $test_gid"));
140 };
141
142 if ($@) {
143 $ex = $@;
144 }
145
146 $wfh->print("done\n");
147 $wfh->flush();
148
149 } else {
150 eval { server_wait($config_file, $rfh) };
151 if ($@) {
152 warn($@);
153 exit 1;
154 }
155
156 exit 0;
157 }
158
159 # Stop server
160 server_stop($pid_file);
161
162 $self->assert_child_ok($pid);
163
164 if ($ex) {
165 test_append_logfile($log_file, $ex);
166 unlink($log_file);
167
168 die($ex);
169 }
170
171 unlink($log_file);
172 }
173
174 1;