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