10 plan(skip_all => 'git not available') unless -x $git;
12 plan(skip_all => "CGI not available")
18 plan(skip_all => "IPC::Run not available")
28 # We check for English error messages
34 my $installed = $ENV{INSTALLED_TESTS};
38 @command = qw(ikiwiki);
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');
52 writefile($name, $dir, $content);
53 ok(utime(333333333, 333333333, "$dir/$name"));
56 sub write_setup_file {
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/*',
70 git_wrapper => getcwd.'/t/tmp/in/.git/hooks/post-commit',
71 git_wrappermode => '0754',
72 gitorigin_branch => '',
75 $setup{ENV} = { 'PERL5LIB' => getcwd.'/blib/lib' };
77 writefile("test.setup", "t/tmp",
78 "# IkiWiki::Setup::Yaml - YAML formatted setup file\n" .
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)));
88 sub check_cgi_mode_bits {
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);
104 my $method = $args{method} || 'GET';
105 my $environ = $args{environ} || {};
106 my $params = $args{params} || { do => 'prefs' };
109 SCRIPT_NAME => '/cgi-bin/ikiwiki.cgi',
110 HTTP_HOST => 'example.com',
113 my $cgi = CGI->new($args{params});
114 my $query_string = $cgi->query_string();
116 if ($method eq 'POST') {
117 $defaults{REQUEST_METHOD} = 'POST';
119 $defaults{CONTENT_LENGTH} = length $in;
121 $defaults{REQUEST_METHOD} = 'GET';
122 $defaults{QUERY_STRING} = $query_string;
129 run(["./t/tmp/ikiwiki.cgi"], \$in, \$out, init => sub {
131 $ENV{$_} = $envvars{$_}
139 my (undef, $filename, $line) = caller;
141 my $desc = shift || join(' ', 'git', @$args);
143 ok(run(['git', @$args], \$in, \$out, init => sub {
144 chdir 't/tmp/in' or die $!;
145 $ENV{EMAIL} = 'nobody@ikiwiki-tests.invalid';
146 }), "$desc at $filename:$line");
154 ok(! system(qw(rm -rf t/tmp)));
155 ok(! system(qw(mkdir t/tmp)));
157 write_old_file('.gitignore', 't/tmp/in', "/doc/.ikiwiki/\n");
158 write_old_file('doc/writable/one.mdwn', 't/tmp/in', 'This is the first test page');
159 write_old_file('doc/writable/two.mdwn', 't/tmp/in', 'This is the second test page');
160 write_old_file('doc/writable/three.mdwn', 't/tmp/in', 'This is the third test page');
161 write_old_file('doc/writable/three.bin', 't/tmp/in', 'An attachment');
163 unless ($installed) {
164 ok(! system(qw(cp -pRL doc/wikiicons t/tmp/in/doc/)));
165 ok(! system(qw(cp -pRL doc/recentchanges.mdwn t/tmp/in/doc/)));
169 run_git(['add', '.']);
170 run_git(['commit', '-m', 'Initial commit']);
173 thoroughly_rebuild();
174 check_cgi_mode_bits();
176 ok(-e 't/tmp/out/writable/one/index.html');
177 $content = readfile('t/tmp/out/writable/one/index.html');
178 like($content, qr{This is the first test page});
179 my $orig_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
181 # We have to wait 1 second here so that new writes are guaranteed
182 # to have a strictly larger mtime.
185 # Test the git hook, which accepts git commits
186 writefile('doc/writable/one.mdwn', 't/tmp/in',
187 'This is new content for the first test page');
188 run_git(['add', '.']);
189 run_git(['commit', '-m', 'Git commit']);
190 my $first_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
191 isnt($orig_sha1, $first_revertable_sha1);
193 ok(-e 't/tmp/out/writable/one/index.html');
194 $content = readfile('t/tmp/out/writable/one/index.html');
195 like($content, qr{This is new content for the first test page});
198 $content = run_cgi(method => 'POST',
201 page => 'writable/two',
203 editmessage => 'Web commit',
204 editcontent => 'Here is new content for the second page',
205 _submit => 'Save Page',
209 like($content, qr{^Status:\s*302\s}m);
210 like($content, qr{^Location:\s*http://example\.com/writable/two/\?updated}m);
211 my $second_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
212 isnt($orig_sha1, $second_revertable_sha1);
213 isnt($first_revertable_sha1, $second_revertable_sha1);
215 ok(-e 't/tmp/out/writable/two/index.html');
216 $content = readfile('t/tmp/out/writable/two/index.html');
217 like($content, qr{Here is new content for the second page});
220 writefile('doc/writable/three.mdwn', 't/tmp/in',
221 'Also new content for the third page');
222 unlink('t/tmp/in/doc/writable/three.bin');
223 writefile('doc/writable/three.bin', 't/tmp/in',
224 'Changed attachment');
225 run_git(['add', '.']);
226 run_git(['commit', '-m', 'Git commit']);
227 ok(-e 't/tmp/out/writable/three/index.html');
228 $content = readfile('t/tmp/out/writable/three/index.html');
229 like($content, qr{Also new content for the third page});
230 $content = readfile('t/tmp/out/writable/three.bin');
231 like($content, qr{Changed attachment});
232 my $third_revertable_sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
233 isnt($orig_sha1, $third_revertable_sha1);
234 isnt($second_revertable_sha1, $third_revertable_sha1);
236 run_git(['mv', 'doc/writable/one.mdwn', 'doc/one.mdwn']);
237 run_git(['mv', 'doc/writable/two.mdwn', 'two.mdwn']);
238 run_git(['commit', '-m', 'Rename files to test CVE-2016-10026']);
239 ok(! -e 't/tmp/out/writable/two/index.html');
240 ok(! -e 't/tmp/out/writable/one/index.html');
241 ok(-e 't/tmp/out/one/index.html');
242 my $sha1_before_revert = run_git(['rev-list', '--max-count=1', 'HEAD']);
243 isnt($sha1_before_revert, $third_revertable_sha1);
245 $content = run_cgi(method => 'post',
248 revertmessage => 'CVE-2016-10026',
249 rev => $first_revertable_sha1,
251 _submitted_revert => '1',
254 like($content, qr{is locked and cannot be edited});
255 # The tree is left clean
256 run_git(['diff', '--exit-code']);
257 run_git(['diff', '--cached', '--exit-code']);
258 my $sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
259 is($sha1, $sha1_before_revert);
261 ok(-e 't/tmp/out/one/index.html');
262 ok(! -e 't/tmp/in/doc/writable/one.mdwn');
263 ok(-e 't/tmp/in/doc/one.mdwn');
264 $content = readfile('t/tmp/out/one/index.html');
265 like($content, qr{This is new content for the first test page});
267 $content = run_cgi(method => 'post',
270 revertmessage => 'CVE-2016-10026',
271 rev => $second_revertable_sha1,
273 _submitted_revert => '1',
276 like($content, qr{you are not allowed to change two\.mdwn});
277 run_git(['diff', '--exit-code']);
278 run_git(['diff', '--cached', '--exit-code']);
279 $sha1 = run_git(['rev-list', '--max-count=1', 'HEAD']);
280 is($sha1, $sha1_before_revert);
282 ok(! -e 't/tmp/out/writable/two/index.html');
283 ok(! -e 't/tmp/out/two/index.html');
284 ok(! -e 't/tmp/in/doc/writable/two.mdwn');
285 ok(-e 't/tmp/in/two.mdwn');
286 $content = readfile('t/tmp/in/two.mdwn');
287 like($content, qr{Here is new content for the second page});
289 # This one can legitimately be reverted
290 $content = run_cgi(method => 'post',
293 revertmessage => 'not CVE-2016-10026',
294 rev => $third_revertable_sha1,
296 _submitted_revert => '1',
299 like($content, qr{^Status:\s*302\s}m);
300 like($content, qr{^Location:\s*http://example\.com/recentchanges/}m);
301 run_git(['diff', '--exit-code']);
302 run_git(['diff', '--cached', '--exit-code']);
303 ok(-e 't/tmp/out/writable/three/index.html');
304 $content = readfile('t/tmp/out/writable/three/index.html');
305 like($content, qr{This is the third test page});
306 $content = readfile('t/tmp/out/writable/three.bin');
307 like($content, qr{An attachment});