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>
5 use PublicInbox::TestCommon;
6 use PublicInbox::Import;
8 ok(defined(eval('$PublicInbox::VERSION')), 'VERSION defined');
9 use_ok 'PublicInbox::Config';
10 my ($tmpdir, $for_destroy) = tmpdir();
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");
19 my $tmp = PublicInbox::Config->new("$tmpdir/config");
21 is($tmp->{'publicinbox.foo.inboxdir'}, $inboxdir,
22 'config read correctly');
23 is($tmp->{'core.bare'}, 'true', 'init used --bare repo');
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');
34 my $f = "examples/public-inbox-config";
35 ok(-r $f, "$f is readable");
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',
45 -httpbackend_limiter => undef,
47 }, "lookup matches expected output");
49 is($cfg->lookup('blah@example.com'), undef,
50 "non-existent lookup returns undef");
52 my $test = $cfg->lookup('test@public-inbox.org');
54 'address' => ['try@public-inbox.org',
55 'sandbox@public-inbox.org',
56 'test@public-inbox.org'],
57 -primary_address => 'try@public-inbox.org',
58 'inboxdir' => '/home/pi/test-main.git',
59 'domain' => 'public-inbox.org',
61 'url' => [ 'http://example.com/test' ],
62 -httpbackend_limiter => undef,
64 }, "lookup matches expected output for test");
69 my $cfgpfx = "publicinbox.test";
70 my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
71 my $config = PublicInbox::Config->new(\<<EOF);
72 $cfgpfx.address=test\@example.com
73 $cfgpfx.mainrepo=/path/to/non/existent
74 $cfgpfx.altid=serial:gmane:file=a
75 $cfgpfx.altid=serial:enamg:file=b
77 my $ibx = $config->lookup_name('test');
78 is_deeply($ibx->{altid}, [ @altid ]);
80 $config = PublicInbox::Config->new(\<<EOF);
81 $cfgpfx.address=test\@example.com
82 $cfgpfx.mainrepo=/path/to/non/existent
84 $ibx = $config->lookup_name('test');
85 is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
87 $config = PublicInbox::Config->new(\<<EOF);
88 $cfgpfx.address=test\@example.com
89 $cfgpfx.inboxdir=/path/to/non/existent
90 $cfgpfx.mainrepo=/path/to/deprecated
92 $ibx = $config->lookup_name('test');
93 is($ibx->{inboxdir}, '/path/to/non/existent',
94 'inboxdir takes precedence');
98 my $pfx = "publicinbox.test";
100 $pfx.address=test\@example.com
101 $pfx.inboxdir=/path/to/non/existent
102 publicinbox.nntpserver=news.example.com
104 my $cfg = PublicInbox::Config->new(\$str);
105 my $ibx = $cfg->lookup_name('test');
106 is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
109 $pfx.address=test\@example.com
110 $pfx.inboxdir=/path/to/non/existent
111 $pfx.nntpserver=news.alt.example.com
113 $cfg = PublicInbox::Config->new(\$str);
114 $ibx = $cfg->lookup_name('test');
115 is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
118 # no obfuscate domains
120 my $pfx = "publicinbox.test";
121 my $pfx2 = "publicinbox.foo";
123 $pfx.address=test\@example.com
124 $pfx.inboxdir=/path/to/non/existent
125 $pfx2.address=foo\@example.com
126 $pfx2.inboxdir=/path/to/foo
127 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
130 my $cfg = PublicInbox::Config->new(\$str);
131 my $ibx = $cfg->lookup_name('test');
132 my $re = $ibx->{-no_obfuscate_re};
133 like('meta@public-inbox.org', $re,
134 'public-inbox.org address not to be obfuscated');
135 like('t@example.com', $re, 'example.com address not to be obfuscated');
136 unlike('t@example.comM', $re, 'example.comM address does not match');
137 is_deeply($ibx->{-no_obfuscate}, {
138 'test@example.com' => 1,
139 'foo@example.com' => 1,
140 'z@example.com' => 1,
141 }, 'known addresses populated');
145 # git rejects this because it locks refnames, but we don't have
146 # this problem with inbox names:
150 '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
151 '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
152 'question?', 'colon:', '[square-brace]', "\fformfeed",
153 "\0zero", "\bbackspace",
157 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
158 my $xre = join('|', keys %X);
160 for my $s (@invalid) {
162 $d =~ s/($xre)/$X{$1}/g;
163 ok(!PublicInbox::Config::valid_foo_name($s), "`$d' name rejected");
166 # obviously-valid examples
167 my @valid = qw(a a@example a@example.com);
169 # Rejecting more was considered, but then it dawned on me that
170 # people may intentionally use inbox names which are not URL-friendly
171 # to prevent the PSGI interface from displaying them...
173 # '<', '>', '%', '#', '?', '&', '(', ')',
175 # maybe these aren't so bad, they're common in Message-IDs, even:
177 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
179 ok(PublicInbox::Config::valid_foo_name($s), "`$s' name accepted");
183 my $f = "$tmpdir/ordered";
184 open my $fh, '>', $f or die "open: $!";
186 foreach my $i (0..3) {
188 print $fh <<"" or die "print: $!";
190 inboxdir = /path/to/$i.git
191 address = $i\@example.com
194 close $fh or die "close: $!";
195 my $cfg = PublicInbox::Config->new($f);
197 $cfg->each_inbox(sub { push @result, $_[0]->{name} });
198 is_deeply(\@result, \@expect);
202 my $pfx1 = "publicinbox.test1";
203 my $pfx2 = "publicinbox.test2";
205 $pfx1.address=test\@example.com
206 $pfx1.inboxdir=/path/to/non/existent
207 $pfx2.address=foo\@example.com
208 $pfx2.inboxdir=/path/to/foo
209 $pfx1.coderepo=project
210 $pfx2.coderepo=project
211 coderepo.project.dir=/path/to/project.git
213 my $cfg = PublicInbox::Config->new(\$str);
214 my $t1 = $cfg->lookup_name('test1');
215 my $t2 = $cfg->lookup_name('test2');
216 is($cfg->repo_objs($t1)->[0], $cfg->repo_objs($t2)->[0],
217 'inboxes share ::Git object');
221 for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
222 is(PublicInbox::Config::git_bool($t), 1, "$t is true");
223 is(xqx([qw(git -c), "test.val=$t",
224 qw(config --bool test.val)]),
225 "true\n", "$t matches git-config behavior");
227 for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
228 is(PublicInbox::Config::git_bool($f), 0, "$f is false");
229 is(xqx([qw(git -c), "test.val=$f",
230 qw(config --bool test.val)]),
231 "false\n", "$f matches git-config behavior");
233 is(PublicInbox::Config::git_bool('bogus'), undef,
238 # XXX wildcard match requires git 2.26+
239 require_git('1.8.5', 2) or
240 skip 'git 1.8.5+ required for --url-match', 2;
241 my $f = "$tmpdir/urlmatch";
242 open my $fh, '>', $f or BAIL_OUT $!;
243 print $fh <<EOF or BAIL_OUT $!;
244 [imap "imap://mail.example.com"]
247 close $fh or BAIL_OUT;
248 local $ENV{PI_CONFIG} = $f;
249 my $cfg = PublicInbox::Config->new;
250 my $url = 'imap://mail.example.com/INBOX';
251 is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit');
252 is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss');