]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntp.t
nntp: fix cross-newsgroup Message-ID lookups
[public-inbox.git] / t / nntp.t
1 # Copyright (C) 2015-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::TestCommon;
7 use PublicInbox::Eml;
8 require_mods(qw(DBD::SQLite Data::Dumper));
9 use_ok 'PublicInbox::NNTP';
10 use_ok 'PublicInbox::Inbox';
11
12 {
13         sub quote_str {
14                 my (undef, $s) = split(/ = /, Data::Dumper::Dumper($_[0]), 2);
15                 $s =~ s/;\n//;
16                 $s;
17         }
18
19         sub wm_prepare {
20                 my ($wm) = @_;
21                 my $orig = qq{'$wm'};
22                 PublicInbox::NNTP::wildmat2re($_[0]);
23                 my $new = quote_str($_[0]);
24                 ($orig, $new);
25         }
26
27         sub wildmat_like {
28                 my ($str, $wm) = @_;
29                 my ($orig, $new) = wm_prepare($wm);
30                 like($str, $wm, "$orig matches '$str' using $new");
31         }
32
33         sub wildmat_unlike {
34                 my ($str, $wm, $check_ex) = @_;
35                 if ($check_ex) {
36                         use re 'eval';
37                         my $re = qr/$wm/;
38                         like($str, $re, "normal re with $wm matches, but ...");
39                 }
40                 my ($orig, $new) = wm_prepare($wm);
41                 unlike($str, $wm, "$orig does not match '$str' using $new");
42         }
43
44         wildmat_like('[foo]', '[\[foo\]]');
45         wildmat_like('any', '*');
46         wildmat_unlike('bar.foo.bar', 'foo.*');
47
48         # no code execution
49         wildmat_unlike('HI', '(?{"HI"})', 1);
50         wildmat_unlike('HI', '[(?{"HI"})]', 1);
51 }
52
53 {
54         sub ngpat_like {
55                 my ($str, $pat) = @_;
56                 my $orig = $pat;
57                 PublicInbox::NNTP::ngpat2re($pat);
58                 like($str, $pat, "'$orig' matches '$str' using $pat");
59         }
60
61         ngpat_like('any', '*');
62         ngpat_like('a.s.r', 'a.t,a.s.r');
63         ngpat_like('a.s.r', 'a.t,a.s.*');
64 }
65
66 {
67         use POSIX qw(strftime);
68         sub time_roundtrip {
69                 my ($date, $time, $gmt) = @_;
70                 my $m = join(' ', @_);
71                 my $ts = PublicInbox::NNTP::parse_time(@_);
72                 my @t = $gmt ? gmtime($ts) : localtime($ts);
73                 my ($d, $t) = split(' ', strftime('%Y%m%d %H%M%S', @t));
74                 if (length($date) != 8) { # Net::NNTP uses YYMMDD :<
75                         $d =~ s/^[0-9]{2}//;
76                 }
77                 is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
78                 $ts;
79         }
80         my $x1 = time_roundtrip(qw(20141109 060606 GMT));
81         my $x2 = time_roundtrip(qw(141109 060606 GMT));
82         my $x3 = time_roundtrip(qw(930724 060606 GMT));
83         my $x5 = time_roundtrip(qw(710101 000000));
84         my $x6 = time_roundtrip(qw(720101 000000));
85         SKIP: {
86                 skip('YYMMDD test needs updating', 6) if (time > 0x7fffffff);
87                 # our world probably ends in 2038, but if not we'll try to
88                 # remember to update the test then
89                 is($x1, $x2, 'YYYYMMDD and YYMMDD parse identically');
90                 is(strftime('%Y', gmtime($x3)), '1993', '930724 was in 1993');
91
92                 my $epoch = time_roundtrip(qw(700101 000000 GMT));
93                 is($epoch, 0, 'epoch parsed correctly');
94                 ok($x6 > $x5, '1972 > 1971');
95                 ok($x5 > $epoch, '1971 > Unix epoch');
96         }
97 }
98
99 { # test setting NNTP headers in HEAD and ARTICLE requests
100         my $u = 'https://example.com/a/';
101         my $ng = PublicInbox::Inbox->new({ name => 'test',
102                                         inboxdir => 'test.git',
103                                         address => 'a@example.com',
104                                         -primary_address => 'a@example.com',
105                                         newsgroup => 'test',
106                                         domain => 'example.com',
107                                         url => [ '//example.com/a' ]});
108         is($ng->base_url, $u, 'URL expanded');
109         my $mid = 'a@b';
110         my $mime = PublicInbox::Eml->new("Message-ID: <$mid>\r\n\r\n");
111         my $hdr = $mime->header_obj;
112         my $mock_self = {
113                 nntpd => { grouplist => [], servername => 'example.com' },
114                 ng => $ng,
115         };
116         my $smsg = { num => 1, mid => $mid, nntp => $mock_self, -ibx => $ng };
117         PublicInbox::NNTP::set_nntp_headers($hdr, $smsg);
118         is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
119                 'Message-ID unchanged');
120         is_deeply([ $mime->header('Archived-At') ], [ "<${u}a\@b/>" ],
121                 'Archived-At: set');
122         is_deeply([ $mime->header('List-Archive') ], [ "<$u>" ],
123                 'List-Archive: set');
124         is_deeply([ $mime->header('List-Post') ], [ '<mailto:a@example.com>' ],
125                 'List-Post: set');
126         is_deeply([ $mime->header('Newsgroups') ], [ 'test' ],
127                 'Newsgroups: set');
128         is_deeply([ $mime->header('Xref') ], [ 'example.com test:1' ],
129                 'Xref: set');
130
131         $ng->{-base_url} = 'http://mirror.example.com/m/';
132         $smsg->{num} = 2;
133         PublicInbox::NNTP::set_nntp_headers($hdr, $smsg);
134         is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
135                 'Message-ID unchanged');
136         is_deeply([ $mime->header('Archived-At') ],
137                 [ "<${u}a\@b/>", '<http://mirror.example.com/m/a@b/>' ],
138                 'Archived-At: appended');
139         is_deeply([ $mime->header('Xref') ], [ 'example.com test:2' ],
140                 'Old Xref: clobbered');
141 }
142
143 done_testing();