]> Sergey Matveev's repositories - public-inbox.git/blob - t/config.t
treewide: run update-copyrights from gnulib for 2019
[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 my ($tmpdir, $for_destroy) = tmpdir();
9
10 {
11         is(system(qw(git init -q --bare), $tmpdir), 0, "git init successful");
12         my @cmd = ('git', "--git-dir=$tmpdir", qw(config foo.bar), "hi\nhi");
13         is(system(@cmd), 0, "set config");
14
15         my $tmp = PublicInbox::Config->new("$tmpdir/config");
16
17         is("hi\nhi", $tmp->{"foo.bar"}, "config read correctly");
18         is("true", $tmp->{"core.bare"}, "used --bare repo");
19 }
20
21 {
22         my $f = "examples/public-inbox-config";
23         ok(-r $f, "$f is readable");
24
25         my $cfg = PublicInbox::Config->new($f);
26         is_deeply($cfg->lookup('meta@public-inbox.org'), {
27                 'inboxdir' => '/home/pi/meta-main.git',
28                 'address' => [ 'meta@public-inbox.org' ],
29                 'domain' => 'public-inbox.org',
30                 'url' => [ 'http://example.com/meta' ],
31                 -primary_address => 'meta@public-inbox.org',
32                 'name' => 'meta',
33                 feedmax => 25,
34                 -httpbackend_limiter => undef,
35                 nntpserver => undef,
36         }, "lookup matches expected output");
37
38         is($cfg->lookup('blah@example.com'), undef,
39                 "non-existent lookup returns undef");
40
41         my $test = $cfg->lookup('test@public-inbox.org');
42         is_deeply($test, {
43                 'address' => ['try@public-inbox.org',
44                               'sandbox@public-inbox.org',
45                               'test@public-inbox.org'],
46                 -primary_address => 'try@public-inbox.org',
47                 'inboxdir' => '/home/pi/test-main.git',
48                 'domain' => 'public-inbox.org',
49                 'name' => 'test',
50                 feedmax => 25,
51                 'url' => [ 'http://example.com/test' ],
52                 -httpbackend_limiter => undef,
53                 nntpserver => undef,
54         }, "lookup matches expected output for test");
55 }
56
57
58 {
59         my $cfgpfx = "publicinbox.test";
60         my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
61         my $config = PublicInbox::Config->new(\<<EOF);
62 $cfgpfx.address=test\@example.com
63 $cfgpfx.mainrepo=/path/to/non/existent
64 $cfgpfx.altid=serial:gmane:file=a
65 $cfgpfx.altid=serial:enamg:file=b
66 EOF
67         my $ibx = $config->lookup_name('test');
68         is_deeply($ibx->{altid}, [ @altid ]);
69
70         $config = PublicInbox::Config->new(\<<EOF);
71 $cfgpfx.address=test\@example.com
72 $cfgpfx.mainrepo=/path/to/non/existent
73 EOF
74         $ibx = $config->lookup_name('test');
75         is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
76
77         $config = PublicInbox::Config->new(\<<EOF);
78 $cfgpfx.address=test\@example.com
79 $cfgpfx.inboxdir=/path/to/non/existent
80 $cfgpfx.mainrepo=/path/to/deprecated
81 EOF
82         $ibx = $config->lookup_name('test');
83         is($ibx->{inboxdir}, '/path/to/non/existent',
84                 'inboxdir takes precedence');
85 }
86
87 {
88         my $pfx = "publicinbox.test";
89         my $str = <<EOF;
90 $pfx.address=test\@example.com
91 $pfx.inboxdir=/path/to/non/existent
92 publicinbox.nntpserver=news.example.com
93 EOF
94         my $cfg = PublicInbox::Config->new(\$str);
95         my $ibx = $cfg->lookup_name('test');
96         is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
97
98         $str = <<EOF;
99 $pfx.address=test\@example.com
100 $pfx.inboxdir=/path/to/non/existent
101 $pfx.nntpserver=news.alt.example.com
102 EOF
103         $cfg = PublicInbox::Config->new(\$str);
104         $ibx = $cfg->lookup_name('test');
105         is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
106 }
107
108 # no obfuscate domains
109 {
110         my $pfx = "publicinbox.test";
111         my $pfx2 = "publicinbox.foo";
112         my $str = <<EOF;
113 $pfx.address=test\@example.com
114 $pfx.inboxdir=/path/to/non/existent
115 $pfx2.address=foo\@example.com
116 $pfx2.inboxdir=/path/to/foo
117 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
118 $pfx.obfuscate=true
119 EOF
120         my $cfg = PublicInbox::Config->new(\$str);
121         my $ibx = $cfg->lookup_name('test');
122         my $re = $ibx->{-no_obfuscate_re};
123         like('meta@public-inbox.org', $re,
124                 'public-inbox.org address not to be obfuscated');
125         like('t@example.com', $re, 'example.com address not to be obfuscated');
126         unlike('t@example.comM', $re, 'example.comM address does not match');
127         is_deeply($ibx->{-no_obfuscate}, {
128                         'test@example.com' => 1,
129                         'foo@example.com' => 1,
130                         'z@example.com' => 1,
131                 }, 'known addresses populated');
132 }
133
134 my @invalid = (
135         # git rejects this because it locks refnames, but we don't have
136         # this problem with inbox names:
137         # 'inbox.lock',
138
139         # git rejects these:
140         '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
141         '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
142         'question?', 'colon:', '[square-brace]', "\fformfeed",
143         "\0zero", "\bbackspace",
144
145 );
146
147 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
148 my $xre = join('|', keys %X);
149
150 for my $s (@invalid) {
151         my $d = $s;
152         $d =~ s/($xre)/$X{$1}/g;
153         ok(!PublicInbox::Config::valid_inbox_name($s), "`$d' name rejected");
154 }
155
156 # obviously-valid examples
157 my @valid = qw(a a@example a@example.com);
158
159 # Rejecting more was considered, but then it dawned on me that
160 # people may intentionally use inbox names which are not URL-friendly
161 # to prevent the PSGI interface from displaying them...
162 # URL-unfriendly
163 # '<', '>', '%', '#', '?', '&', '(', ')',
164
165 # maybe these aren't so bad, they're common in Message-IDs, even:
166 # '!', '$', '=', '+'
167 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
168 for my $s (@valid) {
169         ok(PublicInbox::Config::valid_inbox_name($s), "`$s' name accepted");
170 }
171
172 {
173         my $f = "$tmpdir/ordered";
174         open my $fh, '>', $f or die "open: $!";
175         my @expect;
176         foreach my $i (0..3) {
177                 push @expect, "$i";
178                 print $fh <<"" or die "print: $!";
179 [publicinbox "$i"]
180         inboxdir = /path/to/$i.git
181         address = $i\@example.com
182
183         }
184         close $fh or die "close: $!";
185         my $cfg = PublicInbox::Config->new($f);
186         my @result;
187         $cfg->each_inbox(sub { push @result, $_[0]->{name} });
188         is_deeply(\@result, \@expect);
189 }
190
191 {
192         my $pfx1 = "publicinbox.test1";
193         my $pfx2 = "publicinbox.test2";
194         my $str = <<EOF;
195 $pfx1.address=test\@example.com
196 $pfx1.inboxdir=/path/to/non/existent
197 $pfx2.address=foo\@example.com
198 $pfx2.inboxdir=/path/to/foo
199 $pfx1.coderepo=project
200 $pfx2.coderepo=project
201 coderepo.project.dir=/path/to/project.git
202 EOF
203         my $cfg = PublicInbox::Config->new(\$str);
204         my $t1 = $cfg->lookup_name('test1');
205         my $t2 = $cfg->lookup_name('test2');
206         is($t1->{-repo_objs}->[0], $t2->{-repo_objs}->[0],
207                 'inboxes share ::Git object');
208 }
209
210 {
211         my $check_git = !!$ENV{CHECK_GIT_BOOL};
212         for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
213                 is(PublicInbox::Config::_git_config_bool($t), 1, "$t is true");
214                 if ($check_git) {
215                         is(`git -c test.val=$t config --bool test.val`,
216                                 "true\n", "$t matches git-config behavior");
217                 }
218         }
219         for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
220                 is(PublicInbox::Config::_git_config_bool($f), 0, "$f is false");
221                 if ($check_git) {
222                         is(`git -c test.val=$f config --bool test.val`,
223                                 "false\n", "$f matches git-config behavior");
224                 }
225         }
226         is(PublicInbox::Config::_git_config_bool('bogus'), undef,
227                 'bogus is undef');
228 }
229
230 done_testing();