lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / nntp.t
blob42a4ea97697970e4576c9bb36e236c855e7369fd
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use v5.12;
4 use PublicInbox::TestCommon;
5 use PublicInbox::Eml;
6 require_mods(qw(DBD::SQLite));
7 use_ok 'PublicInbox::NNTP';
8 use PublicInbox::Config;
9 use POSIX qw(strftime);
10 use Data::Dumper;
13         my $quote_str = sub {
14                 my ($orig) = @_;
15                 my (undef, $s) = split(/ = /, Dumper($orig), 2);
16                 $s // diag explain(['$s undefined, $orig = ', $orig]);
17                 $s =~ s/;\n//;
18                 $s;
19         };
21         my $wm_prepare = sub {
22                 my ($wm) = @_;
23                 my $orig = qq{'$wm'};
24                 PublicInbox::NNTP::wildmat2re($_[0]);
25                 my $new = $quote_str->($_[0]);
26                 ($orig, $new);
27         };
29         my $wildmat_like = sub {
30                 my ($str, $wm) = @_;
31                 my ($orig, $new) = $wm_prepare->($wm);
32                 like($str, $wm, "$orig matches '$str' using $new");
33         };
35         my $wildmat_unlike = sub {
36                 my ($str, $wm, $check_ex) = @_;
37                 if ($check_ex) {
38                         use re 'eval';
39                         my $re = qr/$wm/;
40                         like($str, $re, "normal re with $wm matches, but ...");
41                 }
42                 my ($orig, $new) = $wm_prepare->($wm);
43                 unlike($str, $wm, "$orig does not match '$str' using $new");
44         };
46         $wildmat_like->('[foo]', '[\[foo\]]');
47         $wildmat_like->('any', '*');
48         $wildmat_unlike->('bar.foo.bar', 'foo.*');
50         # no code execution
51         $wildmat_unlike->('HI', '(?{"HI"})', 1);
52         $wildmat_unlike->('HI', '[(?{"HI"})]', 1);
56         my $ngpat_like = sub {
57                 my ($str, $pat) = @_;
58                 my $orig = $pat;
59                 PublicInbox::NNTP::ngpat2re($pat);
60                 like($str, $pat, "'$orig' matches '$str' using $pat");
61         };
63         $ngpat_like->('any', '*');
64         $ngpat_like->('a.s.r', 'a.t,a.s.r');
65         $ngpat_like->('a.s.r', 'a.t,a.s.*');
69         my $time_roundtrip = sub {
70                 my ($date, $time, $gmt) = @_;
71                 my $m = join(' ', @_);
72                 my $ts = PublicInbox::NNTP::parse_time(@_);
73                 my @t = $gmt ? gmtime($ts) : localtime($ts);
74                 my ($d, $t) = split(' ', strftime('%Y%m%d %H%M%S', @t));
75                 if (length($date) != 8) { # Net::NNTP uses YYMMDD :<
76                         $d =~ s/^[0-9]{2}//;
77                 }
78                 is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
79                 $ts;
80         };
81         my $x1 = $time_roundtrip->(qw(20141109 060606 GMT));
82         my $x2 = $time_roundtrip->(qw(141109 060606 GMT));
83         my $x3 = $time_roundtrip->(qw(930724 060606 GMT));
84         my $x5 = $time_roundtrip->(qw(710101 000000));
85         my $x6 = $time_roundtrip->(qw(720101 000000));
86         SKIP: {
87                 skip('YYMMDD test needs updating', 6) if (time > 0x7fffffff);
88                 # our world probably ends in 2038, but if not we'll try to
89                 # remember to update the test then
90                 is($x1, $x2, 'YYYYMMDD and YYMMDD parse identically');
91                 is(strftime('%Y', gmtime($x3)), '1993', '930724 was in 1993');
93                 my $epoch = $time_roundtrip->(qw(700101 000000 GMT));
94                 is($epoch, 0, 'epoch parsed correctly');
95                 ok($x6 > $x5, '1972 > 1971');
96                 ok($x5 > $epoch, '1971 > Unix epoch');
97         }
100 { # test setting NNTP headers in HEAD and ARTICLE requests
101         my $u = 'https://example.com/a/';
102         my $ibx = PublicInbox::Inbox->new({ name => 'test',
103                                         inboxdir => 'test.git',
104                                         address => 'a@example.com',
105                                         -primary_address => 'a@example.com',
106                                         newsgroup => 'test',
107                                         domain => 'example.com',
108                                         url => [ '//example.com/a' ]});
109         is($ibx->base_url, $u, 'URL expanded');
110         my $mid = 'a@b';
111         my $mime = PublicInbox::Eml->new("Message-ID: <$mid>\r\n\r\n");
112         my $hdr = $mime->header_obj;
113         my $mock_self = {
114                 nntpd => {
115                         servername => 'example.com',
116                         pi_cfg => bless {}, 'PublicInbox::Config',
117                 },
118                 ibx => $ibx,
119         };
120         my $smsg = { num => 1, mid => $mid, nntp => $mock_self, -ibx => $ibx };
121         PublicInbox::NNTP::set_nntp_headers($hdr, $smsg);
122         is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
123                 'Message-ID unchanged');
124         is_deeply([ $mime->header('Newsgroups') ], [ 'test' ],
125                 'Newsgroups: set');
126         is_deeply([ $mime->header('Xref') ], [ 'example.com test:1' ],
127                 'Xref: set');
129         $smsg->{num} = 2;
130         PublicInbox::NNTP::set_nntp_headers($hdr, $smsg);
131         is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
132                 'Message-ID unchanged');
133         is_deeply([ $mime->header('Xref') ], [ 'example.com test:2' ],
134                 'Old Xref: clobbered');
137 done_testing();