]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-externals.t
xt/mem-imapd-tls: update aliases to DSdeflate subs
[public-inbox.git] / t / lei-externals.t
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict; use v5.10.1; use PublicInbox::TestCommon;
5 use Fcntl qw(SEEK_SET);
6 require_git 2.6;
7 require_mods(qw(json DBD::SQLite Search::Xapian));
8 use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE);
9
10 my @onions = map { "http://$_.onion/meta/" } qw(
11         4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd
12         ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd
13         7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd);
14
15 my $test_external_remote = sub {
16         my ($url, $k) = @_;
17 SKIP: {
18         skip "$k unset", 1 if !$url;
19         require_cmd 'curl', 1 or skip 'curl missing', 1;
20         if ($url =~ m!\.onion/!) {
21                 require_cmd 'torsocks', 1 or skip 'no torsocks', 1;
22         }
23         my $mid = '20140421094015.GA8962@dcvr.yhbt.net';
24         my @cmd = ('q', '--only', $url, '-q', "m:$mid");
25         lei_ok(@cmd, \"query $url");
26         is($lei_err, '', "no errors on $url");
27         my $res = json_utf8->decode($lei_out);
28         is($res->[0]->{'m'}, $mid, "got expected mid from $url") or
29                 skip 'further remote tests', 1;
30         lei_ok(@cmd, 'd:..20101002', \'no results, no error');
31         is($lei_err, '', 'no output on 404, matching local FS behavior');
32         is($lei_out, "[null]\n", 'got null results');
33         my ($pid_before, $pid_after);
34         if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
35                 lei_ok 'daemon-pid';
36                 chomp($pid_before = $lei_out);
37                 ok($pid_before, 'daemon is live');
38         }
39         for my $out ([], [qw(-f mboxcl2)]) {
40                 pipe(my ($r, $w)) or BAIL_OUT $!;
41                 open my $err, '+>', undef or BAIL_OUT $!;
42                 my $opt = { run_mode => 0, 1 => $w, 2 => $err };
43                 my $cmd = [qw(lei q -qt), @$out, 'z:1..'];
44                 my $tp = start_script($cmd, undef, $opt);
45                 close $w;
46                 sysread($r, my $buf, 1);
47                 close $r; # trigger SIGPIPE
48                 $tp->join;
49                 ok(WIFSIGNALED($?), "signaled @$out");
50                 is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
51                 seek($err, 0, 0);
52                 my @err = <$err>;
53                 is_deeply(\@err, [], "no errors @$out");
54         }
55         if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
56                 lei_ok 'daemon-pid';
57                 chomp(my $pid_after = $lei_out);
58                 is($pid_after, $pid_before, 'pid unchanged') or
59                         skip 'daemon died', 1;
60                 skip 'not killing persistent lei-daemon', 2 if
61                                 $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
62                 lei_ok 'daemon-kill';
63                 my $alive = 1;
64                 for (1..100) {
65                         $alive = kill(0, $pid_after) or last;
66                         tick();
67                 }
68                 ok(!$alive, 'daemon-kill worked');
69         }
70 } # /SKIP
71 }; # /sub
72
73 my ($ro_home, $cfg_path) = setup_public_inboxes;
74 test_lei(sub {
75         my $home = $ENV{HOME};
76         my $config_file = "$home/.config/lei/config";
77         my $store_dir = "$home/.local/share/lei";
78         lei_ok 'ls-external', \'ls-external on fresh install';
79         ignore_inline_c_missing($lei_err);
80         is($lei_out.$lei_err, '', 'ls-external no output, yet');
81         ok(!-e $config_file && !-e $store_dir,
82                 'nothing created by ls-external');
83
84         ok(!lei('add-external', "$home/nonexistent"),
85                 "fails on non-existent dir");
86         like($lei_err, qr/not a directory/, 'noted non-existence');
87         mkdir "$home/new\nline" or BAIL_OUT "mkdir: $!";
88         ok(!lei('add-external', "$home/new\nline"), "fails on newline");
89         like($lei_err, qr/`\\n' not allowed/, 'newline noted in error');
90         lei_ok('ls-external', \'ls-external works after add failure');
91         is($lei_out.$lei_err, '', 'ls-external still has no output');
92         my $cfg = PublicInbox::Config->new($cfg_path);
93         $cfg->each_inbox(sub {
94                 my ($ibx) = @_;
95                 lei_ok(qw(add-external -q), $ibx->{inboxdir},
96                                 \'added external');
97                 is($lei_out.$lei_err, '', 'no output');
98         });
99         ok(-s $config_file, 'add-external created config');
100         my $lcfg = PublicInbox::Config->new($config_file);
101         $cfg->each_inbox(sub {
102                 my ($ibx) = @_;
103                 is($lcfg->{"external.$ibx->{inboxdir}.boost"}, 0,
104                         "configured boost on $ibx->{name}");
105         });
106         lei_ok 'ls-external';
107         like($lei_out, qr/boost=0\n/s, 'ls-external has output');
108         lei_ok qw(add-external -q https://EXAMPLE.com/ibx), \'add remote';
109         is($lei_err, '', 'no warnings after add-external');
110
111         {
112                 lei_ok qw(ls-external --remote);
113                 my $r_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
114                 lei_ok qw(ls-external --local);
115                 my $l_only = +{ map { $_ => 1 } split(/^/m, $lei_out) };
116                 lei_ok 'ls-external';
117                 is_deeply([grep { $l_only->{$_} } keys %$r_only], [],
118                         'no locals in --remote');
119                 is_deeply([grep { $r_only->{$_} } keys %$l_only], [],
120                         'no remotes in --local');
121                 my $all = +{ map { $_ => 1 } split(/^/m, $lei_out) };
122                 is_deeply($all, { %$r_only, %$l_only },
123                                 'default output combines remote + local');
124                 lei_ok qw(ls-external --remote --local);
125                 my $both = +{ map { $_ => 1 } split(/^/m, $lei_out) };
126                 is_deeply($all, $both, '--remote --local == no args');
127         }
128
129         lei_ok qw(_complete lei forget-external), \'complete for externals';
130         my %comp = map { $_ => 1 } split(/\s+/, $lei_out);
131         ok($comp{'https://example.com/ibx/'}, 'forget external completion');
132         my @dirs;
133         $cfg->each_inbox(sub {
134                 my ($ibx) = @_;
135                 push @dirs, $ibx->{inboxdir};
136                 ok($comp{$ibx->{inboxdir}}, "local $ibx->{name} completion");
137         });
138         for my $u (qw(h http https https: https:/ https:// https://e
139                         https://example https://example. https://example.co
140                         https://example.com https://example.com/
141                         https://example.com/i https://example.com/ibx)) {
142                 lei_ok(qw(_complete lei forget-external), $u,
143                         \"partial completion for URL $u");
144                 is($lei_out, "https://example.com/ibx/\n",
145                         "completed partial URL $u");
146                 for my $qo (qw(-I --include --exclude --only)) {
147                         lei_ok(qw(_complete lei q), $qo, $u,
148                                 \"partial completion for URL q $qo $u");
149                         is($lei_out, "https://example.com/ibx/\n",
150                                 "completed partial URL $u on q $qo");
151                 }
152         }
153         lei_ok(qw(_complete lei add-external), 'https://',
154                 \'add-external hostname completion');
155         is($lei_out, "https://example.com/\n", 'completed up to hostname');
156
157         lei_ok('ls-external');
158         like($lei_out, qr!https://example\.com/ibx/!s, 'added canonical URL');
159         is($lei_err, '', 'no warnings on ls-external');
160         lei_ok(qw(forget-external -q https://EXAMPLE.com/ibx));
161         lei_ok('ls-external');
162         unlike($lei_out, qr!https://example\.com/ibx/!s,
163                 'removed canonical URL');
164
165         # do some queries
166         ok(!lei(qw(q s:prefix -o maildir:/dev/null)), 'bad maildir');
167         like($lei_err, qr!/dev/null exists and is not a directory!,
168                 'error shown');
169         is($? >> 8, 1, 'errored out with exit 1');
170
171         ok(!lei(qw(q s:prefix -o), "mboxcl2:$home"), 'bad mbox');
172         like($lei_err, qr!\Q$home\E exists and is not a writable file!,
173                 'error shown');
174         is($? >> 8, 1, 'errored out with exit 1');
175
176         ok(!lei(qw(q s:prefix -o Mbox2:/dev/stdout)), 'bad format');
177         like($lei_err, qr/bad mbox format: mbox2/, 'error shown');
178         is($? >> 8, 1, 'errored out with exit 1');
179
180         # note, on a Bourne shell users should be able to use either:
181         #       s:"use boolean prefix"
182         #       "s:use boolean prefix"
183         # or use single quotes, it should not matter.  Users only need
184         # to know shell quoting rules, not Xapian quoting rules.
185         # No double-quoting should be imposed on users on the CLI
186         lei_ok('q', 's:use boolean prefix');
187         like($lei_out, qr/search: use boolean prefix/,
188                 'phrase search got result');
189         my $res = json_utf8->decode($lei_out);
190         is(scalar(@$res), 2, 'only 2 element array (1 result)');
191         is($res->[1], undef, 'final element is undef'); # XXX should this be?
192         is(ref($res->[0]), 'HASH', 'first element is hashref');
193         lei_ok('q', '--pretty', 's:use boolean prefix');
194         my $pretty = json_utf8->decode($lei_out);
195         is_deeply($res, $pretty, '--pretty is identical after decode');
196
197         {
198                 open my $fh, '+>', undef or BAIL_OUT $!;
199                 $fh->autoflush(1);
200                 print $fh 's:use d:..5.days.from.now' or BAIL_OUT $!;
201                 seek($fh, 0, SEEK_SET) or BAIL_OUT $!;
202                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $fh },
203                                 \'--stdin on regular file works');
204                 like($lei_out, qr/use boolean/, '--stdin on regular file');
205         }
206         {
207                 pipe(my ($r, $w)) or BAIL_OUT $!;
208                 print $w 's:use' or BAIL_OUT $!;
209                 close $w or BAIL_OUT $!;
210                 lei_ok([qw(q -q --stdin)], undef, { %$lei_opt, 0 => $r },
211                                 \'--stdin on pipe file works');
212                 like($lei_out, qr/use boolean prefix/, '--stdin on pipe');
213         }
214         ok(!lei(qw(q -q --stdin s:use)), "--stdin and argv don't mix");
215         like($lei_err, qr/no query allowed.*--stdin/,
216                 '--stdin conflict error message');
217
218         for my $fmt (qw(ldjson ndjson jsonl)) {
219                 lei_ok('q', '-f', $fmt, 's:use boolean prefix');
220                 is($lei_out, json_utf8->encode($pretty->[0])."\n", "-f $fmt");
221         }
222
223         require IO::Uncompress::Gunzip;
224         for my $sfx ('', '.gz') {
225                 my $f = "$home/mbox$sfx";
226                 lei_ok('q', '-o', "mboxcl2:$f", 's:use boolean prefix');
227                 my $cat = $sfx eq '' ? sub {
228                         open my $mb, '<', $f or fail "no mbox: $!";
229                         <$mb>
230                 } : sub {
231                         my $z = IO::Uncompress::Gunzip->new($f, MultiStream=>1);
232                         <$z>;
233                 };
234                 my @s = grep(/^Subject:/, $cat->());
235                 is(scalar(@s), 1, "1 result in mbox$sfx");
236                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:see attachment');
237                 is(grep(!/^#/, $lei_err), 0, 'no errors from augment') or
238                         diag $lei_err;
239                 @s = grep(/^Subject:/, my @wtf = $cat->());
240                 is(scalar(@s), 2, "2 results in mbox$sfx");
241
242                 lei_ok('q', '-a', '-o', "mboxcl2:$f", 's:nonexistent');
243                 is(grep(!/^#/, $lei_err), 0, "no errors on no results ($sfx)")
244                         or diag $lei_err;
245
246                 my @s2 = grep(/^Subject:/, $cat->());
247                 is_deeply(\@s2, \@s,
248                         "same 2 old results w/ --augment and bad search $sfx");
249
250                 lei_ok('q', '-o', "mboxcl2:$f", 's:nonexistent');
251                 my @res = $cat->();
252                 is_deeply(\@res, [], "clobber w/o --augment $sfx");
253         }
254         ok(!lei('q', '-o', "$home/mbox", 's:nope'),
255                         'fails if mbox format unspecified');
256         like($lei_err, qr/unable to determine mbox/, 'mbox-related message');
257
258         ok(!lei(qw(q --no-local s:see)), '--no-local');
259         is($? >> 8, 1, 'proper exit code');
260         like($lei_err, qr/no local or remote.+? to search/, 'no inbox');
261
262         for my $no (['--no-local'], ['--no-external'],
263                         [qw(--no-local --no-external)]) {
264                 lei_ok(qw(q mid:testmessage@example.com), @$no,
265                         '-I', $dirs[0], \"-I and @$no combine");
266                 $res = json_utf8->decode($lei_out);
267                 is($res->[0]->{'m'}, 'testmessage@example.com',
268                         "-I \$DIR got results regardless of @$no");
269         }
270
271         {
272                 skip 'TEST_LEI_DAEMON_PERSIST_DIR in use', 1 if
273                                         $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
274                 my @q = qw(q -o mboxcl2:rel.mboxcl2 bye);
275                 lei_ok('-C', $home, @q);
276                 is(unlink("$home/rel.mboxcl2"), 1, '-C works before q');
277
278                 # we are more flexible than git, here:
279                 lei_ok(@q, '-C', $home);
280                 is(unlink("$home/rel.mboxcl2"), 1, '-C works after q');
281                 mkdir "$home/deep" or BAIL_OUT $!;
282                 lei_ok('-C', $home, @q, '-C', 'deep');
283                 is(unlink("$home/deep/rel.mboxcl2"), 1, 'multiple -C works');
284
285                 lei_ok('-C', '', '-C', $home, @q, '-C', 'deep', '-C', '');
286                 is(unlink("$home/deep/rel.mboxcl2"), 1, "-C '' accepted");
287                 ok(!-f "$home/rel.mboxcl2", 'wrong path not created');
288         }
289         my %e = (
290                 TEST_LEI_EXTERNAL_HTTPS => 'https://public-inbox.org/meta/',
291                 TEST_LEI_EXTERNAL_ONION => $onions[int(rand(scalar(@onions)))],
292         );
293         for my $k (keys %e) {
294                 my $url = $ENV{$k} // '';
295                 $url = $e{$k} if $url eq '1';
296                 $test_external_remote->($url, $k);
297         }
298 }); # test_lei
299 done_testing;