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 @cmd = ('git', "--git-dir=$tmpdir", qw(config foo.bar), "hi\nhi");
14 is(xsys(@cmd), 0, "set config");
16 my $tmp = PublicInbox::Config->new("$tmpdir/config");
18 is("hi\nhi", $tmp->{"foo.bar"}, "config read correctly");
19 is("true", $tmp->{"core.bare"}, "used --bare repo");
23 my $f = "examples/public-inbox-config";
24 ok(-r $f, "$f is readable");
26 my $cfg = PublicInbox::Config->new($f);
27 is_deeply($cfg->lookup('meta@public-inbox.org'), {
28 'inboxdir' => '/home/pi/meta-main.git',
29 'address' => [ 'meta@public-inbox.org' ],
30 'domain' => 'public-inbox.org',
31 'url' => [ 'http://example.com/meta' ],
32 -primary_address => 'meta@public-inbox.org',
35 -httpbackend_limiter => undef,
37 }, "lookup matches expected output");
39 is($cfg->lookup('blah@example.com'), undef,
40 "non-existent lookup returns undef");
42 my $test = $cfg->lookup('test@public-inbox.org');
44 'address' => ['try@public-inbox.org',
45 'sandbox@public-inbox.org',
46 'test@public-inbox.org'],
47 -primary_address => 'try@public-inbox.org',
48 'inboxdir' => '/home/pi/test-main.git',
49 'domain' => 'public-inbox.org',
52 'url' => [ 'http://example.com/test' ],
53 -httpbackend_limiter => undef,
55 }, "lookup matches expected output for test");
60 my $cfgpfx = "publicinbox.test";
61 my @altid = qw(serial:gmane:file=a serial:enamg:file=b);
62 my $config = PublicInbox::Config->new(\<<EOF);
63 $cfgpfx.address=test\@example.com
64 $cfgpfx.mainrepo=/path/to/non/existent
65 $cfgpfx.altid=serial:gmane:file=a
66 $cfgpfx.altid=serial:enamg:file=b
68 my $ibx = $config->lookup_name('test');
69 is_deeply($ibx->{altid}, [ @altid ]);
71 $config = PublicInbox::Config->new(\<<EOF);
72 $cfgpfx.address=test\@example.com
73 $cfgpfx.mainrepo=/path/to/non/existent
75 $ibx = $config->lookup_name('test');
76 is($ibx->{inboxdir}, '/path/to/non/existent', 'mainrepo still works');
78 $config = PublicInbox::Config->new(\<<EOF);
79 $cfgpfx.address=test\@example.com
80 $cfgpfx.inboxdir=/path/to/non/existent
81 $cfgpfx.mainrepo=/path/to/deprecated
83 $ibx = $config->lookup_name('test');
84 is($ibx->{inboxdir}, '/path/to/non/existent',
85 'inboxdir takes precedence');
89 my $pfx = "publicinbox.test";
91 $pfx.address=test\@example.com
92 $pfx.inboxdir=/path/to/non/existent
93 publicinbox.nntpserver=news.example.com
95 my $cfg = PublicInbox::Config->new(\$str);
96 my $ibx = $cfg->lookup_name('test');
97 is($ibx->{nntpserver}, 'news.example.com', 'global NNTP server');
100 $pfx.address=test\@example.com
101 $pfx.inboxdir=/path/to/non/existent
102 $pfx.nntpserver=news.alt.example.com
104 $cfg = PublicInbox::Config->new(\$str);
105 $ibx = $cfg->lookup_name('test');
106 is($ibx->{nntpserver}, 'news.alt.example.com','per-inbox NNTP server');
109 # no obfuscate domains
111 my $pfx = "publicinbox.test";
112 my $pfx2 = "publicinbox.foo";
114 $pfx.address=test\@example.com
115 $pfx.inboxdir=/path/to/non/existent
116 $pfx2.address=foo\@example.com
117 $pfx2.inboxdir=/path/to/foo
118 publicinbox.noobfuscate=public-inbox.org \@example.com z\@EXAMPLE.com
121 my $cfg = PublicInbox::Config->new(\$str);
122 my $ibx = $cfg->lookup_name('test');
123 my $re = $ibx->{-no_obfuscate_re};
124 like('meta@public-inbox.org', $re,
125 'public-inbox.org address not to be obfuscated');
126 like('t@example.com', $re, 'example.com address not to be obfuscated');
127 unlike('t@example.comM', $re, 'example.comM address does not match');
128 is_deeply($ibx->{-no_obfuscate}, {
129 'test@example.com' => 1,
130 'foo@example.com' => 1,
131 'z@example.com' => 1,
132 }, 'known addresses populated');
136 # git rejects this because it locks refnames, but we don't have
137 # this problem with inbox names:
141 '', '..', '.', 'stash@{9}', 'inbox.', '^caret', '~tilde',
142 '*asterisk', 's p a c e s', ' leading-space', 'trailing-space ',
143 'question?', 'colon:', '[square-brace]', "\fformfeed",
144 "\0zero", "\bbackspace",
148 my %X = ("\0" => '\\0', "\b" => '\\b', "\f" => '\\f', "'" => "\\'");
149 my $xre = join('|', keys %X);
151 for my $s (@invalid) {
153 $d =~ s/($xre)/$X{$1}/g;
154 ok(!PublicInbox::Config::valid_inbox_name($s), "`$d' name rejected");
157 # obviously-valid examples
158 my @valid = qw(a a@example a@example.com);
160 # Rejecting more was considered, but then it dawned on me that
161 # people may intentionally use inbox names which are not URL-friendly
162 # to prevent the PSGI interface from displaying them...
164 # '<', '>', '%', '#', '?', '&', '(', ')',
166 # maybe these aren't so bad, they're common in Message-IDs, even:
168 push @valid, qw[bang! ca$h less< more> 1% (parens) &more eql= +plus], '#hash';
170 ok(PublicInbox::Config::valid_inbox_name($s), "`$s' name accepted");
174 my $f = "$tmpdir/ordered";
175 open my $fh, '>', $f or die "open: $!";
177 foreach my $i (0..3) {
179 print $fh <<"" or die "print: $!";
181 inboxdir = /path/to/$i.git
182 address = $i\@example.com
185 close $fh or die "close: $!";
186 my $cfg = PublicInbox::Config->new($f);
188 $cfg->each_inbox(sub { push @result, $_[0]->{name} });
189 is_deeply(\@result, \@expect);
193 my $pfx1 = "publicinbox.test1";
194 my $pfx2 = "publicinbox.test2";
196 $pfx1.address=test\@example.com
197 $pfx1.inboxdir=/path/to/non/existent
198 $pfx2.address=foo\@example.com
199 $pfx2.inboxdir=/path/to/foo
200 $pfx1.coderepo=project
201 $pfx2.coderepo=project
202 coderepo.project.dir=/path/to/project.git
204 my $cfg = PublicInbox::Config->new(\$str);
205 my $t1 = $cfg->lookup_name('test1');
206 my $t2 = $cfg->lookup_name('test2');
207 is($t1->{-repo_objs}->[0], $t2->{-repo_objs}->[0],
208 'inboxes share ::Git object');
212 my $check_git = !!$ENV{CHECK_GIT_BOOL};
213 for my $t (qw(TRUE true yes on 1 +1 -1 13 0x1 0x12 0X5)) {
214 is(PublicInbox::Config::_git_config_bool($t), 1, "$t is true");
216 is(`git -c test.val=$t config --bool test.val`,
217 "true\n", "$t matches git-config behavior");
220 for my $f (qw(FALSE false no off 0 +0 +000 00 0x00 0X0)) {
221 is(PublicInbox::Config::_git_config_bool($f), 0, "$f is false");
223 is(`git -c test.val=$f config --bool test.val`,
224 "false\n", "$f matches git-config behavior");
227 is(PublicInbox::Config::_git_config_bool('bogus'), undef,