1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
7 use File::Temp qw/tempdir/;
11 use constant CGI => "blib/script/public-inbox.cgi";
12 my $mda = "blib/script/public-inbox-mda";
13 my $index = "blib/script/public-inbox-index";
14 my $tmpdir = tempdir('pi-cgi-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $home = "$tmpdir/pi-home";
16 my $pi_home = "$home/.public-inbox";
17 my $pi_config = "$pi_home/config";
18 my $maindir = "$tmpdir/main.git";
19 my $main_bin = getcwd()."/t/main-bin";
20 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
21 my $addr = 'test-public@example.com';
22 my $cfgpfx = "publicinbox.test";
25 ok(-x "$main_bin/spamc",
26 "spamc ham mock found (run in top of source tree");
27 ok(-x $mda, "$mda is executable");
28 is(1, mkdir($home, 0755), "setup ~/ for testing");
29 is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
30 is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
32 open my $fh, '>', "$maindir/description" or die "open: $!\n";
33 print $fh "test for public-inbox\n";
34 close $fh or die "close: $!\n";
36 "$cfgpfx.address" => $addr,
37 "$cfgpfx.mainrepo" => $maindir,
39 while (my ($k,$v) = each %cfg) {
40 is(0, system(qw(git config --file), $pi_config, $k, $v),
45 my $failbox = "$home/fail.mbox";
46 local $ENV{PI_EMERGENCY} = $failbox;
48 local $ENV{HOME} = $home;
49 local $ENV{ORIGINAL_RECIPIENT} = $addr;
51 # ensure successful message delivery
53 my $simple = Email::Simple->new(<<EOF);
54 From: Me <me\@example.com>
55 To: You <you\@example.com>
57 Message-Id: <blah\@example.com>
59 Date: Thu, 01 Jan 1970 00:00:00 +0000
63 my $in = $simple->as_string;
64 run_with_env({PATH => $main_path}, [$mda], \$in);
65 my $rev = `git --git-dir=$maindir rev-list HEAD`;
66 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
69 # deliver a reply, too
71 my $reply = Email::Simple->new(<<EOF);
72 From: You <you\@example.com>
73 To: Me <me\@example.com>
75 In-Reply-To: <blah\@example.com>
76 Message-Id: <blahblah\@example.com>
78 Date: Thu, 01 Jan 1970 00:00:01 +0000
85 my $in = $reply->as_string;
86 run_with_env({PATH => $main_path}, [$mda], \$in);
87 my $rev = `git --git-dir=$maindir rev-list HEAD`;
88 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
93 # obvious failures, first
95 local $ENV{HOME} = $home;
96 my $res = cgi_run("/", "", "PUT");
97 like($res->{head}, qr/Status:\s*405/i, "PUT not allowed");
100 like($res->{head}, qr/Status:\s*404/i, "index returns 404");
105 local $ENV{HOME} = $home;
106 my $path = "/test/info/refs";
107 my $res = cgi_run($path);
108 like($res->{head}, qr/Status:\s*200/i, "info/refs readable");
109 my $orig = $res->{body};
111 local $ENV{HTTP_RANGE} = 'bytes=5-10';
112 $res = cgi_run($path);
113 like($res->{head}, qr/Status:\s*206/i, "info/refs partial OK");
114 is($res->{body}, substr($orig, 5, 6), 'partial body OK');
116 local $ENV{HTTP_RANGE} = 'bytes=5-';
117 $res = cgi_run($path);
118 like($res->{head}, qr/Status:\s*206/i, "info/refs partial past end OK");
119 is($res->{body}, substr($orig, 5), 'partial body OK past end');
124 local $ENV{HOME} = $home;
125 my $res = cgi_run("/test/atom.xml");
126 like($res->{body}, qr/<title>test for public-inbox/,
127 "set title in XML feed");
129 qr!http://test\.example\.com/test/blah%40example\.com/!,
131 like($res->{body}, qr/what\?/, "reply included");
136 local $ENV{HOME} = $home;
137 my $res = cgi_run("/test/");
138 like($res->{head}, qr/Status: 200 OK/, "index returns 200");
140 my $idx = cgi_run("/test/index.html");
141 $idx->{body} =~ s!/index.html(\?r=)!/$1!g; # dirty...
142 $idx->{body} = [ split(/\n/, $idx->{body}) ];
143 $res->{body} = [ split(/\n/, $res->{body}) ];
144 is_deeply($res, $idx,
145 '/$LISTNAME/ and /$LISTNAME/index.html are nearly identical');
146 # more checks in t/feed.t
151 local $ENV{HOME} = $home;
152 my $slashy_mid = 'slashy/asdf@example.com';
153 my $reply = Email::Simple->new(<<EOF);
154 From: You <you\@example.com>
155 To: Me <me\@example.com>
157 Message-Id: <$slashy_mid>
159 Date: Thu, 01 Jan 1970 00:00:01 +0000
163 my $in = $reply->as_string;
166 local $ENV{HOME} = $home;
167 local $ENV{ORIGINAL_RECIPIENT} = $addr;
168 run_with_env({PATH => $main_path}, [$mda], \$in);
171 my $res = cgi_run("/test/slashy%2fasdf%40example.com/raw");
172 like($res->{body}, qr/Message-Id: <\Q$slashy_mid\E>/,
173 "slashy mid raw hit");
175 $res = cgi_run("/test/blahblah\@example.com/raw");
176 like($res->{body}, qr/Message-Id: <blahblah\@example\.com>/,
178 $res = cgi_run("/test/blahblah\@example.con/raw");
179 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid raw miss");
181 $res = cgi_run("/test/blahblah\@example.com/");
182 like($res->{body}, qr/\A<html>/, "mid html hit");
183 like($res->{head}, qr/Status: 200 OK/, "200 response");
184 $res = cgi_run("/test/blahblah\@example.con/");
185 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
187 $res = cgi_run("/test/blahblah\@example.com/f/");
188 like($res->{head}, qr/Status: 301 Moved/, "301 response");
190 qr!^Location: http://[^/]+/test/blahblah%40example\.com/\r\n!ms,
191 '301 redirect location');
192 $res = cgi_run("/test/blahblah\@example.con/");
193 like($res->{head}, qr/Status: 300 Multiple Choices/, "mid html miss");
195 $res = cgi_run("/test/");
196 like($res->{body}, qr/slashy%2Fasdf%40example\.com/,
197 "slashy URL generated correctly");
200 # retrieve thread as an mbox
202 local $ENV{HOME} = $home;
203 local $ENV{PATH} = $main_path;
204 my $path = "/test/blahblah%40example.com/t.mbox.gz";
205 my $res = cgi_run($path);
206 like($res->{head}, qr/^Status: 501 /, "search not-yet-enabled");
207 my $indexed = system($index, $maindir) == 0;
209 $res = cgi_run($path);
210 like($res->{head}, qr/^Status: 200 /, "search returned mbox");
212 require IO::Uncompress::Gunzip;
213 my $in = $res->{body};
215 IO::Uncompress::Gunzip::gunzip(\$in => \$out);
216 like($out, qr/^From /m, "From lines in mbox");
219 like($res->{head}, qr/^Status: 501 /, "search not available");
222 my $have_xml_feed = eval { require XML::Feed; 1 } if $indexed;
223 if ($have_xml_feed) {
224 $path = "/test/blahblah%40example.com/t.atom";
225 $res = cgi_run($path);
226 like($res->{head}, qr/^Status: 200 /, "atom returned 200");
227 like($res->{head}, qr!^Content-Type: application/atom\+xml!m,
228 "search returned atom");
229 my $p = XML::Feed->parse(\($res->{body}));
230 is($p->format, "Atom", "parsed atom feed");
231 is(scalar $p->entries, 3, "parsed three entries");
235 # redirect list-name-only URLs
237 local $ENV{HOME} = $home;
238 my $res = cgi_run("/test");
239 like($res->{head}, qr/Status: 301 Moved/, "redirected status");
240 like($res->{head}, qr!/test/!, "redirected with slash");
246 my ($env, @args) = @_;
247 my $init = sub { foreach my $k (keys %$env) { $ENV{$k} = $env->{$k} } };
248 run(@args, init => $init);
254 QUERY_STRING => $_[1] || "",
255 REQUEST_METHOD => $_[2] || "GET",
256 GATEWAY_INTERFACE => 'CGI/1.1',
257 HTTP_ACCEPT => '*/*',
258 HTTP_HOST => 'test.example.com',
260 my ($in, $out, $err) = ("", "", "");
261 my $rc = run_with_env(\%env, [CGI], \$in, \$out, \$err);
262 my ($head, $body) = split(/\r\n\r\n/, $out, 2);
263 { head => $head, body => $body, rc => $rc, err => $err }