1 # Copyright (C) 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,
46 }, "lookup matches expected output");
48 is($cfg->lookup('blah@example.com'), undef,
49 "non-existent lookup returns undef");
51 my $test = $cfg->lookup('test@public-inbox.org');
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',
60 'url' => [ 'http://example.com/test' ],
61 -httpbackend_limiter => undef,
62 }, "lookup matches expected output for test");
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
75 my $ibx = $config->lookup_name('test');
76 is_deeply($ibx->{altid}, [ @altid ]);
78 $config = PublicInbox::Config->new(\<<EOF);
79 $cfgpfx.address=test\@example.com
80 $cfgpfx.mainrepo=/path/to/non/existent
82 $ibx = $config->lookup_name('test');
83 is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
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
90 $ibx = $config->lookup_name('test');
91 is($ibx->{inboxdir}, '/path/to/non/existent',
92 'inboxdir takes precedence');
96 my $pfx = "publicinbox.test";
98 $pfx.address=test\@example.com
99 $pfx.inboxdir=/path/to/non/existent
100 $pfx.newsgroup=inbox.test
101 publicinbox.nntpserver=news.example.com
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');
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 publicinbox.imapserver=imaps://mail.example.com
117 $cfg = PublicInbox::Config->new(\$str);
118 $ibx = $cfg->lookup_name('test');
119 is_deeply($ibx->nntp_url({ www => { pi_cfg => $cfg }}),
120 [ 'nntp://news.alt.example.com/inbox.test' ],
121 'nntp_url uses per-inbox NNTP server');
122 is_deeply($ibx->imap_url({ www => { pi_cfg => $cfg }}),
123 [ 'imaps://mail.example.com/inbox.test' ],
124 'nntp_url uses per-inbox NNTP server');
127 # no obfuscate domains
129 my $pfx = "publicinbox.test";
130 my $pfx2 = "publicinbox.foo";
132 $pfx.address=test\@example.com
133 $pfx.inboxdir=/path/to/non/existent
134 $pfx2.address=foo\@example.com
135 $pfx2.inboxdir=/path/to/foo
136 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
139 my $cfg = PublicInbox::Config->new(\$str);
140 my $ibx = $cfg->lookup_name('test');
141 my $re = $ibx->{-no_obfuscate_re};
142 like('meta@public-inbox.org', $re,
143 'public-inbox.org address not to be obfuscated');
144 like('t@example.com', $re, 'example.com address not to be obfuscated');
145 unlike('t@example.comM', $re, 'example.comM address does not match');
146 is_deeply($ibx->{-no_obfuscate}, {
147 'test@example.com' => 1,
148 'foo@example.com' => 1,
149 'z@example.com' => 1,
150 }, 'known addresses populated');
154 # git rejects this because it locks refnames, but we don't have
155 # this problem with inbox names:
159 '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
160 '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
161 'question?', 'colon:', '[square-brace]', "\fformfeed",
162 "\0zero", "\bbackspace",
166 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
167 my $xre = join('|', keys %X);
169 for my $s (@invalid) {
171 $d =~ s/($xre)/$X{$1}/g;
172 ok(!PublicInbox::Config::valid_foo_name($s), "`$d' name rejected");
175 # obviously-valid examples
176 my @valid = qw(a a@example a@example.com);
178 # Rejecting more was considered, but then it dawned on me that
179 # people may intentionally use inbox names which are not URL-friendly
180 # to prevent the PSGI interface from displaying them...
182 # '<', '>', '%', '#', '?', '&', '(', ')',
184 # maybe these aren't so bad, they're common in Message-IDs, even:
186 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
188 ok(PublicInbox::Config::valid_foo_name($s), "`$s' name accepted");
192 my $f = "$tmpdir/ordered";
193 open my $fh, '>', $f or die "open: $!";
195 foreach my $i (0..3) {
197 print $fh <<"" or die "print: $!";
199 inboxdir = /path/to/$i.git
200 address = $i\@example.com
203 close $fh or die "close: $!";
204 my $cfg = PublicInbox::Config->new($f);
206 $cfg->each_inbox(sub { push @result, $_[0]->{name} });
207 is_deeply(\@result, \@expect);
211 my $pfx1 = "publicinbox.test1";
212 my $pfx2 = "publicinbox.test2";
214 $pfx1.address=test\@example.com
215 $pfx1.inboxdir=/path/to/non/existent
216 $pfx2.address=foo\@example.com
217 $pfx2.inboxdir=/path/to/foo
218 $pfx1.coderepo=project
219 $pfx2.coderepo=project
220 coderepo.project.dir=/path/to/project.git
222 my $cfg = PublicInbox::Config->new(\$str);
223 my $t1 = $cfg->lookup_name('test1');
224 my $t2 = $cfg->lookup_name('test2');
225 is($cfg->repo_objs($t1)->[0], $cfg->repo_objs($t2)->[0],
226 'inboxes share ::Git object');
230 for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
231 is(PublicInbox::Config::git_bool($t), 1, "$t is true");
232 is(xqx([qw(git -c), "test.val=$t",
233 qw(config --bool test.val)]),
234 "true\n", "$t matches git-config behavior");
236 for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
237 is(PublicInbox::Config::git_bool($f), 0, "$f is false");
238 is(xqx([qw(git -c), "test.val=$f",
239 qw(config --bool test.val)]),
240 "false\n", "$f matches git-config behavior");
242 is(PublicInbox::Config::git_bool('bogus'), undef,
247 # XXX wildcard match requires git 2.26+
248 require_git('1.8.5', 2);
249 my $f = "$tmpdir/urlmatch";
250 open my $fh, '>', $f or BAIL_OUT $!;
251 print $fh <<EOF or BAIL_OUT $!;
252 [imap "imap://mail.example.com"]
255 close $fh or BAIL_OUT;
256 local $ENV{PI_CONFIG} = $f;
257 my $cfg = PublicInbox::Config->new;
258 my $url = 'imap://mail.example.com/INBOX';
259 is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit');
260 is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss');