]> Sergey Matveev's repositories - public-inbox.git/blob - t/config.t
7fb44acceea1b90cdd2505fc2bbb3c75e538be7a
[public-inbox.git] / t / config.t
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>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Config;
7 use PublicInbox::TestCommon;
8 use PublicInbox::Import;
9 my ($tmpdir, $for_destroy) = tmpdir();
10
11 {
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");
17
18         my $tmp = PublicInbox::Config->new("$tmpdir/config");
19
20         is($tmp->{'publicinbox.foo.inboxdir'}, $inboxdir,
21                 'config read correctly');
22         is($tmp->{'core.bare'}, 'true', 'init used --bare repo');
23
24         my @warn;
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');
30 }
31
32 {
33         my $f = "examples/public-inbox-config";
34         ok(-r $f, "$f is readable");
35
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',
43                 'name' => 'meta',
44                 -httpbackend_limiter => undef,
45                 nntpserver => undef,
46         }, "lookup matches expected output");
47
48         is($cfg->lookup('blah@example.com'), undef,
49                 "non-existent lookup returns undef");
50
51         my $test = $cfg->lookup('test@public-inbox.org');
52         is_deeply($test, {
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',
59                 'name' => 'test',
60                 'url' => [ 'http://example.com/test' ],
61                 -httpbackend_limiter => undef,
62                 nntpserver => undef,
63         }, "lookup matches expected output for test");
64 }
65
66
67 {
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
75 EOF
76         my $ibx = $config->lookup_name('test');
77         is_deeply($ibx->{altid}, [ @altid ]);
78
79         $config = PublicInbox::Config->new(\<<EOF);
80 $cfgpfx.address=test\@example.com
81 $cfgpfx.mainrepo=/path/to/non/existent
82 EOF
83         $ibx = $config->lookup_name('test');
84         is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
85
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
90 EOF
91         $ibx = $config->lookup_name('test');
92         is($ibx->{inboxdir}, '/path/to/non/existent',
93                 'inboxdir takes precedence');
94 }
95
96 {
97         my $pfx = "publicinbox.test";
98         my $str = <<EOF;
99 $pfx.address=test\@example.com
100 $pfx.inboxdir=/path/to/non/existent
101 publicinbox.nntpserver=news.example.com
102 EOF
103         my $cfg = PublicInbox::Config->new(\$str);
104         my $ibx = $cfg->lookup_name('test');
105         is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
106
107         $str = <<EOF;
108 $pfx.address=test\@example.com
109 $pfx.inboxdir=/path/to/non/existent
110 $pfx.nntpserver=news.alt.example.com
111 EOF
112         $cfg = PublicInbox::Config->new(\$str);
113         $ibx = $cfg->lookup_name('test');
114         is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
115 }
116
117 # no obfuscate domains
118 {
119         my $pfx = "publicinbox.test";
120         my $pfx2 = "publicinbox.foo";
121         my $str = <<EOF;
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
127 $pfx.obfuscate=true
128 EOF
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');
141 }
142
143 my @invalid = (
144         # git rejects this because it locks refnames, but we don't have
145         # this problem with inbox names:
146         # 'inbox.lock',
147
148         # git rejects these:
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",
153
154 );
155
156 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
157 my $xre = join('|', keys %X);
158
159 for my $s (@invalid) {
160         my $d = $s;
161         $d =~ s/($xre)/$X{$1}/g;
162         ok(!PublicInbox::Config::valid_inbox_name($s), "`$d' name rejected");
163 }
164
165 # obviously-valid examples
166 my @valid = qw(a a@example a@example.com);
167
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...
171 # URL-unfriendly
172 # '<', '>', '%', '#', '?', '&', '(', ')',
173
174 # maybe these aren't so bad, they're common in Message-IDs, even:
175 # '!', '$', '=', '+'
176 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
177 for my $s (@valid) {
178         ok(PublicInbox::Config::valid_inbox_name($s), "`$s' name accepted");
179 }
180
181 {
182         my $f = "$tmpdir/ordered";
183         open my $fh, '>', $f or die "open: $!";
184         my @expect;
185         foreach my $i (0..3) {
186                 push @expect, "$i";
187                 print $fh <<"" or die "print: $!";
188 [publicinbox "$i"]
189         inboxdir = /path/to/$i.git
190         address = $i\@example.com
191
192         }
193         close $fh or die "close: $!";
194         my $cfg = PublicInbox::Config->new($f);
195         my @result;
196         $cfg->each_inbox(sub { push @result, $_[0]->{name} });
197         is_deeply(\@result, \@expect);
198 }
199
200 {
201         my $pfx1 = "publicinbox.test1";
202         my $pfx2 = "publicinbox.test2";
203         my $str = <<EOF;
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
211 EOF
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');
217 }
218
219 {
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");
225         }
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");
231         }
232         is(PublicInbox::Config::git_bool('bogus'), undef,
233                 'bogus is undef');
234 }
235
236 SKIP: {
237         # XXX wildcard match requires git 2.26+
238         require_git('1.8.5', 2) or
239                 skip 'git 1.8.5+ required for --url-match', 2;
240         my $f = "$tmpdir/urlmatch";
241         open my $fh, '>', $f or BAIL_OUT $!;
242         print $fh <<EOF or BAIL_OUT $!;
243 [imap "imap://mail.example.com"]
244         pollInterval = 9
245 EOF
246         close $fh or BAIL_OUT;
247         local $ENV{PI_CONFIG} = $f;
248         my $cfg = PublicInbox::Config->new;
249         my $url = 'imap://mail.example.com/INBOX';
250         is($cfg->urlmatch('imap.pollInterval', $url), 9, 'urlmatch hit');
251         is($cfg->urlmatch('imap.idleInterval', $url), undef, 'urlmatch miss');
252 };
253
254
255 done_testing();