1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use PublicInbox::TestCommon;
5 use PublicInbox::Import;
7 ok(defined(eval('$PublicInbox::VERSION')), 'VERSION defined');
8 use_ok 'PublicInbox::Config';
9 my ($tmpdir, $for_destroy) = tmpdir();
10 use autodie qw(open close);
11 my $validate_git_behavior = $ENV{TEST_VALIDATE_GIT_BEHAVIOR};
14 my $f = "$tmpdir/bool_config";
22 my $cfg = PublicInbox::Config->git_config_dump($f);
23 $validate_git_behavior and
24 is(xqx([qw(git config -f), $f, qw(--bool imap.debug)]),
25 "true\n", 'git handles key-only as truth');
26 ok($cfg->git_bool($cfg->{'imap.debug'}), 'key-only value handled');
27 is($cfg->{'imap.port'}, 2, 'normal k=v read after key-only');
31 PublicInbox::Import::init_bare($tmpdir);
32 my $inboxdir = "$tmpdir/new\nline";
33 my @cmd = ('git', "--git-dir=$tmpdir",
34 qw(config publicinbox.foo.inboxdir), $inboxdir);
35 is(xsys(@cmd), 0, "set config");
37 my $tmp = PublicInbox::Config->new("$tmpdir/config");
39 is($tmp->{'publicinbox.foo.inboxdir'}, $inboxdir,
40 'config read correctly');
41 is($tmp->{'core.bare'}, 'true', 'init used --bare repo');
44 local $SIG{__WARN__} = sub { push @warn, @_ };
45 $tmp = PublicInbox::Config->new("$tmpdir/config");
46 is($tmp->lookup_name('foo'), undef, 'reject invalid inboxdir');
47 like("@warn", qr/^E:.*must not contain `\\n'/sm,
48 'warned about newline');
52 my $f = "examples/public-inbox-config";
53 ok(-r $f, "$f is readable");
55 my $cfg = PublicInbox::Config->new($f);
56 is_deeply($cfg->lookup('meta@public-inbox.org'), {
57 'inboxdir' => '/home/pi/meta-main.git',
58 'address' => [ 'meta@public-inbox.org' ],
59 'domain' => 'public-inbox.org',
60 'url' => [ 'http://example.com/meta' ],
61 -primary_address => 'meta@public-inbox.org',
63 -httpbackend_limiter => undef,
64 }, "lookup matches expected output");
66 is($cfg->lookup('blah@example.com'), undef,
67 "non-existent lookup returns undef");
69 my $test = $cfg->lookup('test@public-inbox.org');
71 'address' => ['try@public-inbox.org',
72 'sandbox@public-inbox.org',
73 'test@public-inbox.org'],
74 -primary_address => 'try@public-inbox.org',
75 'inboxdir' => '/home/pi/test-main.git',
76 'domain' => 'public-inbox.org',
78 'url' => [ 'http://example.com/test' ],
79 -httpbackend_limiter => undef,
80 }, "lookup matches expected output for test");
85 my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
86 my $config = cfg_new $tmpdir, <<EOF;
88 address = test\@example.com
89 inboxdir = /path/to/non/existent
90 altid=serial:gmane:file=a
91 altid=serial:enamg:file=b
93 my $ibx = $config->lookup_name('test');
94 is_deeply($ibx->{altid}, [ @altid ]);
96 $config = cfg_new $tmpdir, <<EOF;
98 address = test\@example.com
99 inboxdir = /path/to/non/existent
101 $ibx = $config->lookup_name('test');
102 is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
104 $config = cfg_new $tmpdir, <<EOF;
106 address = test\@example.com
107 inboxdir = /path/to/non/existent
108 mainrepo = /path/to/deprecated
110 $ibx = $config->lookup_name('test');
111 is($ibx->{inboxdir}, '/path/to/non/existent',
112 'inboxdir takes precedence');
116 my $cfg = cfg_new $tmpdir, <<EOF;
118 address = test\@example.com
119 inboxdir = /path/to/non/existent
120 newsgroup = inbox.test
122 nntpserver = news.example.com
124 my $ibx = $cfg->lookup_name('test');
125 is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
126 [ 'nntp://news.example.com/inbox.test' ],
127 'nntp_url uses global NNTP server');
129 $cfg = cfg_new $tmpdir, <<EOF;
131 address = test\@example.com
132 inboxdir = /path/to/non/existent
133 newsgroup = inbox.test
134 nntpserver = news.alt.example.com
136 nntpserver = news.example.com
137 imapserver = imaps://mail.example.com
139 $ibx = $cfg->lookup_name('test');
140 is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
141 [ 'nntp://news.alt.example.com/inbox.test' ],
142 'nntp_url uses per-inbox NNTP server');
143 is_deeply($ibx->imap_url({ www => { pi_cfg => $cfg }}),
144 [ 'imaps://mail.example.com/inbox.test' ],
145 'nntp_url uses per-inbox NNTP server');
148 # no obfuscate domains
150 my $cfg = cfg_new $tmpdir, <<EOF;
152 address = test\@example.com
153 inboxdir = /path/to/non/existent
155 address = foo\@example.com
156 inboxdir = /path/to/foo
158 noobfuscate = public-inbox.org \@example.com z\@EXAMPLE.com
162 my $ibx = $cfg->lookup_name('test');
163 my $re = $ibx->{-no_obfuscate_re};
164 like('meta@public-inbox.org', $re,
165 'public-inbox.org address not to be obfuscated');
166 like('t@example.com', $re, 'example.com address not to be obfuscated');
167 unlike('t@example.comM', $re, 'example.comM address does not match');
168 is_deeply($ibx->{-no_obfuscate}, {
169 'test@example.com' => 1,
170 'foo@example.com' => 1,
171 'z@example.com' => 1,
172 }, 'known addresses populated');
176 # git rejects this because it locks refnames, but we don't have
177 # this problem with inbox names:
181 '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
182 '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
183 'question?', 'colon:', '[square-brace]', "\fformfeed",
184 "\0zero", "\bbackspace",
188 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
189 my $xre = join('|', keys %X);
191 for my $s (@invalid) {
193 $d =~ s/($xre)/$X{$1}/g;
194 ok(!PublicInbox::Config::valid_foo_name($s), "`$d' name rejected");
197 # obviously-valid examples
198 my @valid = qw(a a@example a@example.com);
200 # Rejecting more was considered, but then it dawned on me that
201 # people may intentionally use inbox names which are not URL-friendly
202 # to prevent the PSGI interface from displaying them...
204 # '<', '>', '%', '#', '?', '&', '(', ')',
206 # maybe these aren't so bad, they're common in Message-IDs, even:
208 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
210 ok(PublicInbox::Config::valid_foo_name($s), "`$s' name accepted");
214 my $f = "$tmpdir/ordered";
215 open my $fh, '>', $f or die "open: $!";
217 foreach my $i (0..3) {
219 print $fh <<"" or die "print: $!";
221 inboxdir = /path/to/$i.git
222 address = $i\@example.com
225 close $fh or die "close: $!";
226 my $cfg = PublicInbox::Config->new($f);
228 $cfg->each_inbox(sub { push @result, $_[0]->{name} });
229 is_deeply(\@result, \@expect);
233 my $cfg = cfg_new $tmpdir, <<EOF;
234 [publicinbox "test1"]
235 address = test\@example.com
236 inboxdir = /path/to/non/existent
238 [publicinbox "test2"]
239 address = foo\@example.com
240 inboxdir = /path/to/foo
243 dir = /path/to/project.git
245 my $t1 = $cfg->lookup_name('test1');
246 my $t2 = $cfg->lookup_name('test2');
247 ok $cfg->repo_objs($t1)->[0], 'coderepo parsed';
248 is($cfg->repo_objs($t1)->[0], $cfg->repo_objs($t2)->[0],
249 'inboxes share ::Git object');
253 for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
254 is(PublicInbox::Config::git_bool($t), 1, "$t is true");
255 is(xqx([qw(git -c), "test.val=$t",
256 qw(config --bool test.val)]),
257 "true\n", "$t matches git-config behavior");
259 for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
260 is(PublicInbox::Config::git_bool($f), 0, "$f is false");
261 is(xqx([qw(git -c), "test.val=$f",
262 qw(config --bool test.val)]),
263 "false\n", "$f matches git-config behavior");
265 is(PublicInbox::Config::git_bool('bogus'), undef,
270 # XXX wildcard match requires git 2.26+
271 require_git v1.8.5, 2;
272 my $cfg = cfg_new $tmpdir, <<EOF;
273 [imap "imap://mail.example.com"]
276 my $url = 'imap://mail.example.com/INBOX';
277 is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit');
278 is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss');
281 my $glob2re = PublicInbox::Config->can('glob2re');
282 is($glob2re->('http://[::1]:1234/foo/'), undef, 'IPv6 URL not globbed');
283 is($glob2re->('foo'), undef, 'plain string unchanged');
284 is_deeply($glob2re->('[f-o]'), '[f-o]' , 'range accepted');
285 is_deeply($glob2re->('*'), '[^/]*?' , 'wildcard accepted');
286 is_deeply($glob2re->('{a,b,c}'), '(a|b|c)' , 'braces');
287 is_deeply($glob2re->('{,b,c}'), '(|b|c)' , 'brace with empty @ start');
288 is_deeply($glob2re->('{a,b,}'), '(a|b|)' , 'brace with empty @ end');
289 is_deeply($glob2re->('{a}'), undef, 'ungrouped brace');
290 is_deeply($glob2re->('{a'), undef, 'open left brace');
291 is_deeply($glob2re->('a}'), undef, 'open right brace');
292 is_deeply($glob2re->('*.[ch]'), '[^/]*?\\.[ch]', 'suffix glob');
293 is_deeply($glob2re->('{[a-z],9,}'), '([a-z]|9|)' , 'brace with range');
294 is_deeply($glob2re->('\\{a,b\\}'), undef, 'escaped brace');
295 is_deeply($glob2re->('\\\\{a,b}'), '\\\\\\\\(a|b)', 'fake escape brace');
296 is_deeply($glob2re->('**/foo'), '.*/foo', 'double asterisk start');
297 is_deeply($glob2re->('foo/**'), 'foo/.*', 'double asterisk end');
298 my $re = $glob2re->('a/**/b');
299 is_deeply($re, 'a(?:/.*?/|/)b', 'double asterisk middle');
300 like($_, qr!$re!, "a/**/b matches $_") for ('a/b', 'a/c/b', 'a/c/a/b');
301 unlike($_, qr!$re!, "a/**/b doesn't match $_") for ('a/ab');
305 local $SIG{__WARN__} = sub { $w .= "@_"; };
306 my $cfg = cfg_new $tmpdir, <<EOF;
308 address = a\@example.com
309 inboxdir = $tmpdir/aa
311 address = b\@example.com
312 inboxdir = $tmpdir/aa
315 like $w, qr!`\Q$tmpdir/aa\E' used by both!, 'inboxdir conflict warned';
320 local $SIG{__WARN__} = sub { $w .= "@_"; };
321 my $cfg = cfg_new $tmpdir, <<EOF;
323 address = a\@example.com
325 newsgroup = inbox.test
327 address = b\@example.com
329 newsgroup = inbox.tesT
332 like $w, qr!`inbox\.test' used by both!, 'newsgroup conflict warned';
333 like $w, qr!`inbox\.tesT' lowercased!, 'upcase warned';