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',
44 -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,
63 }, "lookup matches expected output for test");
68 my $cfgpfx = "publicinbox.test";
69 my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
70 my $config = PublicInbox::Config->new(\<<EOF);
71 $cfgpfx.address=test\@example.com
72 $cfgpfx.mainrepo=/path/to/non/existent
73 $cfgpfx.altid=serial:gmane:file=a
74 $cfgpfx.altid=serial:enamg:file=b
76 my $ibx = $config->lookup_name('test');
77 is_deeply($ibx->{altid}, [ @altid ]);
79 $config = PublicInbox::Config->new(\<<EOF);
80 $cfgpfx.address=test\@example.com
81 $cfgpfx.mainrepo=/path/to/non/existent
83 $ibx = $config->lookup_name('test');
84 is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
86 $config = PublicInbox::Config->new(\<<EOF);
87 $cfgpfx.address=test\@example.com
88 $cfgpfx.inboxdir=/path/to/non/existent
89 $cfgpfx.mainrepo=/path/to/deprecated
91 $ibx = $config->lookup_name('test');
92 is($ibx->{inboxdir}, '/path/to/non/existent',
93 'inboxdir takes precedence');
97 my $pfx = "publicinbox.test";
99 $pfx.address=test\@example.com
100 $pfx.inboxdir=/path/to/non/existent
101 publicinbox.nntpserver=news.example.com
103 my $cfg = PublicInbox::Config->new(\$str);
104 my $ibx = $cfg->lookup_name('test');
105 is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
108 $pfx.address=test\@example.com
109 $pfx.inboxdir=/path/to/non/existent
110 $pfx.nntpserver=news.alt.example.com
112 $cfg = PublicInbox::Config->new(\$str);
113 $ibx = $cfg->lookup_name('test');
114 is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
117 # no obfuscate domains
119 my $pfx = "publicinbox.test";
120 my $pfx2 = "publicinbox.foo";
122 $pfx.address=test\@example.com
123 $pfx.inboxdir=/path/to/non/existent
124 $pfx2.address=foo\@example.com
125 $pfx2.inboxdir=/path/to/foo
126 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
129 my $cfg = PublicInbox::Config->new(\$str);
130 my $ibx = $cfg->lookup_name('test');
131 my $re = $ibx->{-no_obfuscate_re};
132 like('meta@public-inbox.org', $re,
133 'public-inbox.org address not to be obfuscated');
134 like('t@example.com', $re, 'example.com address not to be obfuscated');
135 unlike('t@example.comM', $re, 'example.comM address does not match');
136 is_deeply($ibx->{-no_obfuscate}, {
137 'test@example.com' => 1,
138 'foo@example.com' => 1,
139 'z@example.com' => 1,
140 }, 'known addresses populated');
144 # git rejects this because it locks refnames, but we don't have
145 # this problem with inbox names:
149 '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
150 '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
151 'question?', 'colon:', '[square-brace]', "\fformfeed",
152 "\0zero", "\bbackspace",
156 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
157 my $xre = join('|', keys %X);
159 for my $s (@invalid) {
161 $d =~ s/($xre)/$X{$1}/g;
162 ok(!PublicInbox::Config::valid_inbox_name($s), "`$d' name rejected");
165 # obviously-valid examples
166 my @valid = qw(a a@example a@example.com);
168 # Rejecting more was considered, but then it dawned on me that
169 # people may intentionally use inbox names which are not URL-friendly
170 # to prevent the PSGI interface from displaying them...
172 # '<', '>', '%', '#', '?', '&', '(', ')',
174 # maybe these aren't so bad, they're common in Message-IDs, even:
176 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
178 ok(PublicInbox::Config::valid_inbox_name($s), "`$s' name accepted");
182 my $f = "$tmpdir/ordered";
183 open my $fh, '>', $f or die "open: $!";
185 foreach my $i (0..3) {
187 print $fh <<"" or die "print: $!";
189 inboxdir = /path/to/$i.git
190 address = $i\@example.com
193 close $fh or die "close: $!";
194 my $cfg = PublicInbox::Config->new($f);
196 $cfg->each_inbox(sub { push @result, $_[0]->{name} });
197 is_deeply(\@result, \@expect);
201 my $pfx1 = "publicinbox.test1";
202 my $pfx2 = "publicinbox.test2";
204 $pfx1.address=test\@example.com
205 $pfx1.inboxdir=/path/to/non/existent
206 $pfx2.address=foo\@example.com
207 $pfx2.inboxdir=/path/to/foo
208 $pfx1.coderepo=project
209 $pfx2.coderepo=project
210 coderepo.project.dir=/path/to/project.git
212 my $cfg = PublicInbox::Config->new(\$str);
213 my $t1 = $cfg->lookup_name('test1');
214 my $t2 = $cfg->lookup_name('test2');
215 is($t1->{-repo_objs}->[0], $t2->{-repo_objs}->[0],
216 'inboxes share ::Git object');
220 for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
221 is(PublicInbox::Config::git_bool($t), 1, "$t is true");
222 is(xqx([qw(git -c), "test.val=$t",
223 qw(config --bool test.val)]),
224 "true\n", "$t matches git-config behavior");
226 for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
227 is(PublicInbox::Config::git_bool($f), 0, "$f is false");
228 is(xqx([qw(git -c), "test.val=$f",
229 qw(config --bool test.val)]),
230 "false\n", "$f matches git-config behavior");
232 is(PublicInbox::Config::git_bool('bogus'), undef,
237 require_git('1.8.5', 2) or
238 skip 'git 1.8.5+ required for --url-match', 2;
239 my $f = "$tmpdir/urlmatch";
240 open my $fh, '>', $f or BAIL_OUT $!;
241 print $fh <<EOF or BAIL_OUT $!;
242 [imap "imap://*.example.com"]
245 close $fh or BAIL_OUT;
246 local $ENV{PI_CONFIG} = $f;
247 my $cfg = PublicInbox::Config->new;
248 my $url = 'imap://mail.example.com/INBOX';
249 is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit');
250 is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss');