1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::Config;
7 use PublicInbox::TestCommon;
8 use PublicInbox::Import;
9 my ($tmpdir, $for_destroy) = tmpdir();
12 PublicInbox::Import::init_bare($tmpdir);
13 my $inboxdir = "$tmpdir/new\nline";
14 my @cmd = ('git', "--git-dir=$tmpdir",
15 qw(config publicinbox.foo.inboxdir), $inboxdir);
16 is(xsys(@cmd), 0, "set config");
18 my $tmp = PublicInbox::Config->new("$tmpdir/config");
20 is($tmp->{'publicinbox.foo.inboxdir'}, $inboxdir,
21 'config read correctly');
22 is($tmp->{'core.bare'}, 'true', 'init used --bare repo');
25 local $SIG{__WARN__} = sub { push @warn, @_ };
26 $tmp = PublicInbox::Config->new("$tmpdir/config");
27 is($tmp->lookup_name('foo'), undef, 'reject invalid inboxdir');
28 like("@warn", qr/^E:.*must not contain `\\n'/sm,
29 'warned about newline');
33 my $f = "examples/public-inbox-config";
34 ok(-r $f, "$f is readable");
36 my $cfg = PublicInbox::Config->new($f);
37 is_deeply($cfg->lookup('meta@public-inbox.org'), {
38 'inboxdir' => '/home/pi/meta-main.git',
39 'address' => [ 'meta@public-inbox.org' ],
40 'domain' => 'public-inbox.org',
41 'url' => [ 'http://example.com/meta' ],
42 -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',
62 'url' => [ 'http://example.com/test' ],
63 -httpbackend_limiter => undef,
65 }, "lookup matches expected output for test");
70 my $cfgpfx = "publicinbox.test";
71 my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
72 my $config = PublicInbox::Config->new(\<<EOF);
73 $cfgpfx.address=test\@example.com
74 $cfgpfx.mainrepo=/path/to/non/existent
75 $cfgpfx.altid=serial:gmane:file=a
76 $cfgpfx.altid=serial:enamg:file=b
78 my $ibx = $config->lookup_name('test');
79 is_deeply($ibx->{altid}, [ @altid ]);
81 $config = PublicInbox::Config->new(\<<EOF);
82 $cfgpfx.address=test\@example.com
83 $cfgpfx.mainrepo=/path/to/non/existent
85 $ibx = $config->lookup_name('test');
86 is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
88 $config = PublicInbox::Config->new(\<<EOF);
89 $cfgpfx.address=test\@example.com
90 $cfgpfx.inboxdir=/path/to/non/existent
91 $cfgpfx.mainrepo=/path/to/deprecated
93 $ibx = $config->lookup_name('test');
94 is($ibx->{inboxdir}, '/path/to/non/existent',
95 'inboxdir takes precedence');
99 my $pfx = "publicinbox.test";
101 $pfx.address=test\@example.com
102 $pfx.inboxdir=/path/to/non/existent
103 publicinbox.nntpserver=news.example.com
105 my $cfg = PublicInbox::Config->new(\$str);
106 my $ibx = $cfg->lookup_name('test');
107 is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
110 $pfx.address=test\@example.com
111 $pfx.inboxdir=/path/to/non/existent
112 $pfx.nntpserver=news.alt.example.com
114 $cfg = PublicInbox::Config->new(\$str);
115 $ibx = $cfg->lookup_name('test');
116 is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
119 # no obfuscate domains
121 my $pfx = "publicinbox.test";
122 my $pfx2 = "publicinbox.foo";
124 $pfx.address=test\@example.com
125 $pfx.inboxdir=/path/to/non/existent
126 $pfx2.address=foo\@example.com
127 $pfx2.inboxdir=/path/to/foo
128 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
131 my $cfg = PublicInbox::Config->new(\$str);
132 my $ibx = $cfg->lookup_name('test');
133 my $re = $ibx->{-no_obfuscate_re};
134 like('meta@public-inbox.org', $re,
135 'public-inbox.org address not to be obfuscated');
136 like('t@example.com', $re, 'example.com address not to be obfuscated');
137 unlike('t@example.comM', $re, 'example.comM address does not match');
138 is_deeply($ibx->{-no_obfuscate}, {
139 'test@example.com' => 1,
140 'foo@example.com' => 1,
141 'z@example.com' => 1,
142 }, 'known addresses populated');
146 # git rejects this because it locks refnames, but we don't have
147 # this problem with inbox names:
151 '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
152 '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
153 'question?', 'colon:', '[square-brace]', "\fformfeed",
154 "\0zero", "\bbackspace",
158 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
159 my $xre = join('|', keys %X);
161 for my $s (@invalid) {
163 $d =~ s/($xre)/$X{$1}/g;
164 ok(!PublicInbox::Config::valid_inbox_name($s), "`$d' name rejected");
167 # obviously-valid examples
168 my @valid = qw(a a@example a@example.com);
170 # Rejecting more was considered, but then it dawned on me that
171 # people may intentionally use inbox names which are not URL-friendly
172 # to prevent the PSGI interface from displaying them...
174 # '<', '>', '%', '#', '?', '&', '(', ')',
176 # maybe these aren't so bad, they're common in Message-IDs, even:
178 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
180 ok(PublicInbox::Config::valid_inbox_name($s), "`$s' name accepted");
184 my $f = "$tmpdir/ordered";
185 open my $fh, '>', $f or die "open: $!";
187 foreach my $i (0..3) {
189 print $fh <<"" or die "print: $!";
191 inboxdir = /path/to/$i.git
192 address = $i\@example.com
195 close $fh or die "close: $!";
196 my $cfg = PublicInbox::Config->new($f);
198 $cfg->each_inbox(sub { push @result, $_[0]->{name} });
199 is_deeply(\@result, \@expect);
203 my $pfx1 = "publicinbox.test1";
204 my $pfx2 = "publicinbox.test2";
206 $pfx1.address=test\@example.com
207 $pfx1.inboxdir=/path/to/non/existent
208 $pfx2.address=foo\@example.com
209 $pfx2.inboxdir=/path/to/foo
210 $pfx1.coderepo=project
211 $pfx2.coderepo=project
212 coderepo.project.dir=/path/to/project.git
214 my $cfg = PublicInbox::Config->new(\$str);
215 my $t1 = $cfg->lookup_name('test1');
216 my $t2 = $cfg->lookup_name('test2');
217 is($t1->{-repo_objs}->[0], $t2->{-repo_objs}->[0],
218 'inboxes share ::Git object');
222 for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
223 is(PublicInbox::Config::git_bool($t), 1, "$t is true");
224 is(xqx([qw(git -c), "test.val=$t",
225 qw(config --bool test.val)]),
226 "true\n", "$t matches git-config behavior");
228 for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
229 is(PublicInbox::Config::git_bool($f), 0, "$f is false");
230 is(xqx([qw(git -c), "test.val=$f",
231 qw(config --bool test.val)]),
232 "false\n", "$f matches git-config behavior");
234 is(PublicInbox::Config::git_bool('bogus'), undef,
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://*.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');