]> Sergey Matveev's repositories - public-inbox.git/blob - t/config.t
inbox: streamline ->nntp_url
[public-inbox.git] / t / config.t
1 # Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use v5.10.1;
5 use PublicInbox::TestCommon;
6 use PublicInbox::Import;
7 use_ok 'PublicInbox';
8 ok(defined(eval('$PublicInbox::VERSION')), 'VERSION defined');
9 use_ok 'PublicInbox::Config';
10 my ($tmpdir, $for_destroy) = tmpdir();
11
12 {
13         PublicInbox::Import::init_bare($tmpdir);
14         my $inboxdir = "$tmpdir/new\nline";
15         my @cmd = ('git', "--git-dir=$tmpdir",
16                 qw(config publicinbox.foo.inboxdir), $inboxdir);
17         is(xsys(@cmd), 0, "set config");
18
19         my $tmp = PublicInbox::Config->new("$tmpdir/config");
20
21         is($tmp->{'publicinbox.foo.inboxdir'}, $inboxdir,
22                 'config read correctly');
23         is($tmp->{'core.bare'}, 'true', 'init used --bare repo');
24
25         my @warn;
26         local $SIG{__WARN__} = sub { push @warn, @_ };
27         $tmp = PublicInbox::Config->new("$tmpdir/config");
28         is($tmp->lookup_name('foo'), undef, 'reject invalid inboxdir');
29         like("@warn", qr/^E:.*must not contain `\\n'/sm,
30                 'warned about newline');
31 }
32
33 {
34         my $f = "examples/public-inbox-config";
35         ok(-r $f, "$f is readable");
36
37         my $cfg = PublicInbox::Config->new($f);
38         is_deeply($cfg->lookup('meta@public-inbox.org'), {
39                 'inboxdir' => '/home/pi/meta-main.git',
40                 'address' => [ 'meta@public-inbox.org' ],
41                 'domain' => 'public-inbox.org',
42                 'url' => [ 'http://example.com/meta' ],
43                 -primary_address => 'meta@public-inbox.org',
44                 'name' => 'meta',
45                 -httpbackend_limiter => undef,
46         }, "lookup matches expected output");
47
48         is($cfg->lookup('blah@example.com'), undef,
49                 "non-existent lookup returns undef");
50
51         my $test = $cfg->lookup('test@public-inbox.org');
52         is_deeply($test, {
53                 'address' => ['try@public-inbox.org',
54                               'sandbox@public-inbox.org',
55                               'test@public-inbox.org'],
56                 -primary_address => 'try@public-inbox.org',
57                 'inboxdir' => '/home/pi/test-main.git',
58                 'domain' => 'public-inbox.org',
59                 'name' => 'test',
60                 'url' => [ 'http://example.com/test' ],
61                 -httpbackend_limiter => undef,
62         }, "lookup matches expected output for test");
63 }
64
65
66 {
67         my $cfgpfx = "publicinbox.test";
68         my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
69         my $config = PublicInbox::Config->new(\<<EOF);
70 $cfgpfx.address=test\@example.com
71 $cfgpfx.mainrepo=/path/to/non/existent
72 $cfgpfx.altid=serial:gmane:file=a
73 $cfgpfx.altid=serial:enamg:file=b
74 EOF
75         my $ibx = $config->lookup_name('test');
76         is_deeply($ibx->{altid}, [ @altid ]);
77
78         $config = PublicInbox::Config->new(\<<EOF);
79 $cfgpfx.address=test\@example.com
80 $cfgpfx.mainrepo=/path/to/non/existent
81 EOF
82         $ibx = $config->lookup_name('test');
83         is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
84
85         $config = PublicInbox::Config->new(\<<EOF);
86 $cfgpfx.address=test\@example.com
87 $cfgpfx.inboxdir=/path/to/non/existent
88 $cfgpfx.mainrepo=/path/to/deprecated
89 EOF
90         $ibx = $config->lookup_name('test');
91         is($ibx->{inboxdir}, '/path/to/non/existent',
92                 'inboxdir takes precedence');
93 }
94
95 {
96         my $pfx = "publicinbox.test";
97         my $str = <<EOF;
98 $pfx.address=test\@example.com
99 $pfx.inboxdir=/path/to/non/existent
100 $pfx.newsgroup=inbox.test
101 publicinbox.nntpserver=news.example.com
102 EOF
103         my $cfg = PublicInbox::Config->new(\$str);
104         my $ibx = $cfg->lookup_name('test');
105         is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
106                 [ 'nntp://news.example.com/inbox.test' ],
107                 'nntp_url uses global NNTP server');
108
109         $str = <<EOF;
110 $pfx.address=test\@example.com
111 $pfx.inboxdir=/path/to/non/existent
112 $pfx.newsgroup=inbox.test
113 $pfx.nntpserver=news.alt.example.com
114 publicinbox.nntpserver=news.example.com
115 EOF
116         $cfg = PublicInbox::Config->new(\$str);
117         $ibx = $cfg->lookup_name('test');
118         is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
119                 [ 'nntp://news.alt.example.com/inbox.test' ],
120                 'nntp_url uses per-inbox NNTP server');
121 }
122
123 # no obfuscate domains
124 {
125         my $pfx = "publicinbox.test";
126         my $pfx2 = "publicinbox.foo";
127         my $str = <<EOF;
128 $pfx.address=test\@example.com
129 $pfx.inboxdir=/path/to/non/existent
130 $pfx2.address=foo\@example.com
131 $pfx2.inboxdir=/path/to/foo
132 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
133 $pfx.obfuscate=true
134 EOF
135         my $cfg = PublicInbox::Config->new(\$str);
136         my $ibx = $cfg->lookup_name('test');
137         my $re = $ibx->{-no_obfuscate_re};
138         like('meta@public-inbox.org', $re,
139                 'public-inbox.org address not to be obfuscated');
140         like('t@example.com', $re, 'example.com address not to be obfuscated');
141         unlike('t@example.comM', $re, 'example.comM address does not match');
142         is_deeply($ibx->{-no_obfuscate}, {
143                         'test@example.com' => 1,
144                         'foo@example.com' => 1,
145                         'z@example.com' => 1,
146                 }, 'known addresses populated');
147 }
148
149 my @invalid = (
150         # git rejects this because it locks refnames, but we don't have
151         # this problem with inbox names:
152         # 'inbox.lock',
153
154         # git rejects these:
155         '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
156         '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
157         'question?', 'colon:', '[square-brace]', "\fformfeed",
158         "\0zero", "\bbackspace",
159
160 );
161
162 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
163 my $xre = join('|', keys %X);
164
165 for my $s (@invalid) {
166         my $d = $s;
167         $d =~ s/($xre)/$X{$1}/g;
168         ok(!PublicInbox::Config::valid_foo_name($s), "`$d' name rejected");
169 }
170
171 # obviously-valid examples
172 my @valid = qw(a a@example a@example.com);
173
174 # Rejecting more was considered, but then it dawned on me that
175 # people may intentionally use inbox names which are not URL-friendly
176 # to prevent the PSGI interface from displaying them...
177 # URL-unfriendly
178 # '<', '>', '%', '#', '?', '&', '(', ')',
179
180 # maybe these aren't so bad, they're common in Message-IDs, even:
181 # '!', '$', '=', '+'
182 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
183 for my $s (@valid) {
184         ok(PublicInbox::Config::valid_foo_name($s), "`$s' name accepted");
185 }
186
187 {
188         my $f = "$tmpdir/ordered";
189         open my $fh, '>', $f or die "open: $!";
190         my @expect;
191         foreach my $i (0..3) {
192                 push @expect, "$i";
193                 print $fh <<"" or die "print: $!";
194 [publicinbox "$i"]
195         inboxdir = /path/to/$i.git
196         address = $i\@example.com
197
198         }
199         close $fh or die "close: $!";
200         my $cfg = PublicInbox::Config->new($f);
201         my @result;
202         $cfg->each_inbox(sub { push @result, $_[0]->{name} });
203         is_deeply(\@result, \@expect);
204 }
205
206 {
207         my $pfx1 = "publicinbox.test1";
208         my $pfx2 = "publicinbox.test2";
209         my $str = <<EOF;
210 $pfx1.address=test\@example.com
211 $pfx1.inboxdir=/path/to/non/existent
212 $pfx2.address=foo\@example.com
213 $pfx2.inboxdir=/path/to/foo
214 $pfx1.coderepo=project
215 $pfx2.coderepo=project
216 coderepo.project.dir=/path/to/project.git
217 EOF
218         my $cfg = PublicInbox::Config->new(\$str);
219         my $t1 = $cfg->lookup_name('test1');
220         my $t2 = $cfg->lookup_name('test2');
221         is($cfg->repo_objs($t1)->[0], $cfg->repo_objs($t2)->[0],
222                 'inboxes share ::Git object');
223 }
224
225 {
226         for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
227                 is(PublicInbox::Config::git_bool($t), 1, "$t is true");
228                 is(xqx([qw(git -c), "test.val=$t",
229                         qw(config --bool test.val)]),
230                         "true\n", "$t matches git-config behavior");
231         }
232         for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
233                 is(PublicInbox::Config::git_bool($f), 0, "$f is false");
234                 is(xqx([qw(git -c), "test.val=$f",
235                         qw(config --bool test.val)]),
236                         "false\n", "$f matches git-config behavior");
237         }
238         is(PublicInbox::Config::git_bool('bogus'), undef,
239                 'bogus is undef');
240 }
241
242 SKIP: {
243         # XXX wildcard match requires git 2.26+
244         require_git('1.8.5', 2) or
245                 skip 'git 1.8.5+ required for --url-match', 2;
246         my $f = "$tmpdir/urlmatch";
247         open my $fh, '>', $f or BAIL_OUT $!;
248         print $fh <<EOF or BAIL_OUT $!;
249 [imap "imap://mail.example.com"]
250         pollInterval = 9
251 EOF
252         close $fh or BAIL_OUT;
253         local $ENV{PI_CONFIG} = $f;
254         my $cfg = PublicInbox::Config->new;
255         my $url = 'imap://mail.example.com/INBOX';
256         is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit');
257         is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss');
258 };
259
260
261 done_testing();