]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntp.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / nntp.t
1 # Copyright (C) 2015-2021 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 use PublicInbox::Config;
12
13 {
14         sub quote_str {
15                 my (undef, $s) = split(/ = /, Data::Dumper::Dumper($_[0]), 2);
16                 $s =~ s/;\n//;
17                 $s;
18         }
19
20         sub wm_prepare {
21                 my ($wm) = @_;
22                 my $orig = qq{'$wm'};
23                 PublicInbox::NNTP::wildmat2re($_[0]);
24                 my $new = quote_str($_[0]);
25                 ($orig, $new);
26         }
27
28         sub wildmat_like {
29                 my ($str, $wm) = @_;
30                 my ($orig, $new) = wm_prepare($wm);
31                 like($str, $wm, "$orig matches '$str' using $new");
32         }
33
34         sub wildmat_unlike {
35                 my ($str, $wm, $check_ex) = @_;
36                 if ($check_ex) {
37                         use re 'eval';
38                         my $re = qr/$wm/;
39                         like($str, $re, "normal re with $wm matches, but ...");
40                 }
41                 my ($orig, $new) = wm_prepare($wm);
42                 unlike($str, $wm, "$orig does not match '$str' using $new");
43         }
44
45         wildmat_like('[foo]', '[\[foo\]]');
46         wildmat_like('any', '*');
47         wildmat_unlike('bar.foo.bar', 'foo.*');
48
49         # no code execution
50         wildmat_unlike('HI', '(?{"HI"})', 1);
51         wildmat_unlike('HI', '[(?{"HI"})]', 1);
52 }
53
54 {
55         sub ngpat_like {
56                 my ($str, $pat) = @_;
57                 my $orig = $pat;
58                 PublicInbox::NNTP::ngpat2re($pat);
59                 like($str, $pat, "'$orig' matches '$str' using $pat");
60         }
61
62         ngpat_like('any', '*');
63         ngpat_like('a.s.r', 'a.t,a.s.r');
64         ngpat_like('a.s.r', 'a.t,a.s.*');
65 }
66
67 {
68         use POSIX qw(strftime);
69         sub time_roundtrip {
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');
92
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         }
98 }
99
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');
128
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');
135 }
136
137 done_testing();