]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - t/git-cgi.t
(no commit message)
[git.ikiwiki.info.git] / t / git-cgi.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
5 use Test::More;
7 BEGIN {
8         my $git = `which git`;
9         chomp $git;
10         plan(skip_all => 'git not available') unless -x $git;
12         plan(skip_all => "CGI not available")
13                 unless eval q{
14                         use CGI qw();
15                         1;
16                 };
18         plan(skip_all => "IPC::Run not available")
19                 unless eval q{
20                         use IPC::Run qw(run);
21                         1;
22                 };
24         use_ok('IkiWiki');
25         use_ok('YAML::XS');
26 }
28 # We check for English error messages
29 $ENV{LC_ALL} = 'C';
31 use Cwd qw(getcwd);
32 use Errno qw(ENOENT);
34 my $installed = $ENV{INSTALLED_TESTS};
36 my @command;
37 if ($installed) {
38         @command = qw(ikiwiki);
39 }
40 else {
41         ok(! system("make -s ikiwiki.out"));
42         @command = ("perl", "-I".getcwd."/blib/lib", './ikiwiki.out',
43                 '--underlaydir='.getcwd.'/underlays/basewiki',
44                 '--set', 'underlaydirbase='.getcwd.'/underlays',
45                 '--templatedir='.getcwd.'/templates');
46 }
48 sub write_old_file {
49         my $name = shift;
50         my $dir = shift;
51         my $content = shift;
52         writefile($name, $dir, $content);
53         ok(utime(333333333, 333333333, "$dir/$name"));
54 }
56 sub write_setup_file {
57         my %setup = (
58                 wikiname => 'this is the name of my wiki',
59                 srcdir => getcwd.'/t/tmp/in/doc',
60                 destdir => getcwd.'/t/tmp/out',
61                 url => 'http://example.com',
62                 cgiurl => 'http://example.com/cgi-bin/ikiwiki.cgi',
63                 cgi_wrapper => getcwd.'/t/tmp/ikiwiki.cgi',
64                 cgi_wrappermode => '0751',
65                 add_plugins => [qw(anonok attachment lockedit recentchanges)],
66                 disable_plugins => [qw(emailauth openid passwordauth)],
67                 anonok_pagespec => 'writable/*',
68                 locked_pages => '!writable/*',
69                 rcs => 'git',
70                 git_wrapper => getcwd.'/t/tmp/in/.git/hooks/post-commit',
71                 git_wrappermode => '0754',
72                 gitorigin_branch => '',
73         );
74         unless ($installed) {
75                 $setup{ENV} = { 'PERL5LIB' => getcwd.'/blib/lib' };
76         }
77         writefile("test.setup", "t/tmp",
78                 "# IkiWiki::Setup::Yaml - YAML formatted setup file\n" .
79                 Dump(\%setup));
80 }
82 sub thoroughly_rebuild {
83         ok(unlink("t/tmp/ikiwiki.cgi") || $!{ENOENT});
84         ok(unlink("t/tmp/in/.git/hooks/post-commit") || $!{ENOENT});
85         ok(! system(@command, qw(--setup t/tmp/test.setup --rebuild --wrappers)));
86 }
88 sub check_cgi_mode_bits {
89         my $mode;
91         (undef, undef, $mode, undef, undef,
92                 undef, undef, undef, undef, undef,
93                 undef, undef, undef) = stat('t/tmp/ikiwiki.cgi');
94         is ($mode & 07777, 0751);
95         (undef, undef, $mode, undef, undef,
96                 undef, undef, undef, undef, undef,
97                 undef, undef, undef) = stat('t/tmp/in/.git/hooks/post-commit');
98         is ($mode & 07777, 0754);
99 }
101 sub run_cgi {
102         my (%args) = @_;
103         my ($in, $out);
104         my $method = $args{method} || 'GET';
105         my $environ = $args{environ} || {};
106         my $params = $args{params} || { do => 'prefs' };
108         my %defaults = (
109                 SCRIPT_NAME     => '/cgi-bin/ikiwiki.cgi',
110                 HTTP_HOST       => 'example.com',
111         );
113         my $cgi = CGI->new($args{params});
114         my $query_string = $cgi->query_string();
116         if ($method eq 'POST') {
117                 $defaults{REQUEST_METHOD} = 'POST';
118                 $in = $query_string;
119                 $defaults{CONTENT_LENGTH} = length $in;
120         } else {
121                 $defaults{REQUEST_METHOD} = 'GET';
122                 $defaults{QUERY_STRING} = $query_string;
123         }
125         my %envvars = (
126                 %defaults,
127                 %$environ,
128         );
129         run(["./t/tmp/ikiwiki.cgi"], \$in, \$out, init => sub {
130                 map {
131                         $ENV{$_} = $envvars{$_}
132                 } keys(%envvars);
133         });
135         return $out;
138 sub run_git {
139         my (undef, $filename, $line) = caller;
140         my $args = shift;
141         my $desc = shift || join(' ', 'git', @$args);
142         my ($in, $out);
143         ok(run(['git', @$args], \$in, \$out, init => sub {
144                 chdir 't/tmp/in' or die $!;
145                 my $name = 'The IkiWiki Tests';
146                 my $email = 'nobody@ikiwiki-tests.invalid';
147                 if ($args->[0] eq 'commit') {
148                         $ENV{GIT_AUTHOR_NAME} = $ENV{GIT_COMMITTER_NAME} = $name;
149                         $ENV{GIT_AUTHOR_EMAIL} = $ENV{GIT_COMMITTER_EMAIL} = $email;
150                 }
151         }), "$desc at $filename:$line");
152         return $out;
155 sub test {
156         my $content;
157         my $status;
159         ok(! system(qw(rm -rf t/tmp)));
160         ok(! system(qw(mkdir t/tmp)));
162         write_old_file('.gitignore', 't/tmp/in', "/doc/.ikiwiki/\n");
163         write_old_file('doc/writable/one.mdwn', 't/tmp/in', 'This is the first test page');
164         write_old_file('doc/writable/two.mdwn', 't/tmp/in', 'This is the second test page');
165         write_old_file('doc/writable/three.mdwn', 't/tmp/in', 'This is the third test page');
166         write_old_file('doc/writable/three.bin', 't/tmp/in', 'An attachment');
168         unless ($installed) {
169                 ok(! system(qw(cp -pRL doc/wikiicons t/tmp/in/doc/)));
170                 ok(! system(qw(cp -pRL doc/recentchanges.mdwn t/tmp/in/doc/)));
171         }
173         run_git(['init']);
174         run_git(['add', '.']);
175         run_git(['commit', '-m', 'Initial commit']);
177         write_setup_file();
178         thoroughly_rebuild();
179         check_cgi_mode_bits();
181         ok(-e 't/tmp/out/writable/one/index.html');
182         $content = readfile('t/tmp/out/writable/one/index.html');
183         like($content, qr{This is the first test page});
184         my $orig_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
186         # We have to wait 1 second here so that new writes are guaranteed
187         # to have a strictly larger mtime.
188         sleep 1;
190         # Test the git hook, which accepts git commits
191         writefile('doc/writable/one.mdwn', 't/tmp/in',
192                 'This is new content for the first test page');
193         run_git(['add', '.']);
194         run_git(['commit', '-m', 'Git commit']);
195         my $first_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
196         isnt($orig_sha1, $first_revertable_sha1);
198         ok(-e 't/tmp/out/writable/one/index.html');
199         $content = readfile('t/tmp/out/writable/one/index.html');
200         like($content, qr{This is new content for the first test page});
202         # Test a web commit
203         $content = run_cgi(method => 'POST',
204                 params => {
205                         do => 'edit',
206                         page => 'writable/two',
207                         type => 'mdwn',
208                         editmessage => 'Web commit',
209                         editcontent => 'Here is new content for the second page',
210                         _submit => 'Save Page',
211                         _submitted => '1',
212                 },
213         );
214         like($content, qr{^Status:\s*302\s}m);
215         like($content, qr{^Location:\s*http://example\.com/writable/two/\?updated}m);
216         my $second_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
217         isnt($orig_sha1, $second_revertable_sha1);
218         isnt($first_revertable_sha1, $second_revertable_sha1);
220         ok(-e 't/tmp/out/writable/two/index.html');
221         $content = readfile('t/tmp/out/writable/two/index.html');
222         like($content, qr{Here is new content for the second page});
224         # Another edit
225         writefile('doc/writable/three.mdwn', 't/tmp/in',
226                 'Also new content for the third page');
227         unlink('t/tmp/in/doc/writable/three.bin');
228         writefile('doc/writable/three.bin', 't/tmp/in',
229                 'Changed attachment');
230         run_git(['add', '.']);
231         run_git(['commit', '-m', 'Git commit']);
232         ok(-e 't/tmp/out/writable/three/index.html');
233         $content = readfile('t/tmp/out/writable/three/index.html');
234         like($content, qr{Also new content for the third page});
235         $content = readfile('t/tmp/out/writable/three.bin');
236         like($content, qr{Changed attachment});
237         my $third_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
238         isnt($orig_sha1, $third_revertable_sha1);
239         isnt($second_revertable_sha1, $third_revertable_sha1);
241         run_git(['mv', 'doc/writable/one.mdwn', 'doc/one.mdwn']);
242         run_git(['mv', 'doc/writable/two.mdwn', 'two.mdwn']);
243         run_git(['commit', '-m', 'Rename files to test CVE-2016-10026']);
244         ok(! -e 't/tmp/out/writable/two/index.html');
245         ok(! -e 't/tmp/out/writable/one/index.html');
246         ok(-e 't/tmp/out/one/index.html');
247         my $sha1_before_revert = run_git(['rev-list', '--max-count=1', 'HEAD']);
248         isnt($sha1_before_revert, $third_revertable_sha1);
250         $content = run_cgi(method => 'post',
251                 params => {
252                         do => 'revert',
253                         revertmessage => 'CVE-2016-10026',
254                         rev => $first_revertable_sha1,
255                         _submit => 'Revert',
256                         _submitted_revert => '1',
257                 },
258         );
259         like($content, qr{is locked and cannot be edited});
260         # The tree is left clean
261         run_git(['diff', '--exit-code']);
262         run_git(['diff', '--cached', '--exit-code']);
263         my $sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
264         is($sha1, $sha1_before_revert);
266         ok(-e 't/tmp/out/one/index.html');
267         ok(! -e 't/tmp/in/doc/writable/one.mdwn');
268         ok(-e 't/tmp/in/doc/one.mdwn');
269         $content = readfile('t/tmp/out/one/index.html');
270         like($content, qr{This is new content for the first test page});
272         $content = run_cgi(method => 'post',
273                 params => {
274                         do => 'revert',
275                         revertmessage => 'CVE-2016-10026',
276                         rev => $second_revertable_sha1,
277                         _submit => 'Revert',
278                         _submitted_revert => '1',
279                 },
280         );
281         like($content, qr{you are not allowed to change two\.mdwn});
282         run_git(['diff', '--exit-code']);
283         run_git(['diff', '--cached', '--exit-code']);
284         $sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
285         is($sha1, $sha1_before_revert);
287         ok(! -e 't/tmp/out/writable/two/index.html');
288         ok(! -e 't/tmp/out/two/index.html');
289         ok(! -e 't/tmp/in/doc/writable/two.mdwn');
290         ok(-e 't/tmp/in/two.mdwn');
291         $content = readfile('t/tmp/in/two.mdwn');
292         like($content, qr{Here is new content for the second page});
294         # This one can legitimately be reverted
295         $content = run_cgi(method => 'post',
296                 params => {
297                         do => 'revert',
298                         revertmessage => 'not CVE-2016-10026',
299                         rev => $third_revertable_sha1,
300                         _submit => 'Revert',
301                         _submitted_revert => '1',
302                 },
303         );
304         like($content, qr{^Status:\s*302\s}m);
305         like($content, qr{^Location:\s*http://example\.com/recentchanges/}m);
306         run_git(['diff', '--exit-code']);
307         run_git(['diff', '--cached', '--exit-code']);
308         ok(-e 't/tmp/out/writable/three/index.html');
309         $content = readfile('t/tmp/out/writable/three/index.html');
310         like($content, qr{This is the third test page});
311         $content = readfile('t/tmp/out/writable/three.bin');
312         like($content, qr{An attachment});
315 test();
317 done_testing();