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>
6 use PublicInbox::TestCommon;
8 require_mods(qw(DBD::SQLite Data::Dumper));
9 use_ok 'PublicInbox::NNTP';
10 use_ok 'PublicInbox::Inbox';
14 my (undef, $s) = split(/ = /, Data::Dumper::Dumper($_[0]), 2);
22 PublicInbox::NNTP::wildmat2re($_[0]);
23 my $new = quote_str($_[0]);
29 my ($orig, $new) = wm_prepare($wm);
30 like($str, $wm, "$orig matches '$str' using $new");
34 my ($str, $wm, $check_ex) = @_;
38 like($str, $re, "normal re with $wm matches, but ...");
40 my ($orig, $new) = wm_prepare($wm);
41 unlike($str, $wm, "$orig does not match '$str' using $new");
44 wildmat_like('[foo]', '[\[foo\]]');
45 wildmat_like('any', '*');
46 wildmat_unlike('bar.foo.bar', 'foo.*');
49 wildmat_unlike('HI', '(?{"HI"})', 1);
50 wildmat_unlike('HI', '[(?{"HI"})]', 1);
57 PublicInbox::NNTP::ngpat2re($pat);
58 like($str, $pat, "'$orig' matches '$str' using $pat");
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.*');
67 use POSIX qw(strftime);
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 :<
77 is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
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));
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');
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');
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',
106 domain => 'example.com',
107 url => [ '//example.com/a' ]});
108 is($ng->base_url, $u, 'URL expanded');
110 my $mime = PublicInbox::Eml->new("Message-ID: <$mid>\r\n\r\n");
111 my $hdr = $mime->header_obj;
113 nntpd => { grouplist => [], servername => 'example.com' },
116 my $smsg = { num => 1, mid => $mid };
117 PublicInbox::NNTP::set_nntp_headers($mock_self, $hdr, $smsg);
118 is_deeply([ $mime->header('Message-ID') ], [ "<$mid>" ],
119 'Message-ID unchanged');
120 is_deeply([ $mime->header('Archived-At') ], [ "<${u}a\@b/>" ],
122 is_deeply([ $mime->header('List-Archive') ], [ "<$u>" ],
123 'List-Archive: set');
124 is_deeply([ $mime->header('List-Post') ], [ '<mailto:a@example.com>' ],
126 is_deeply([ $mime->header('Newsgroups') ], [ 'test' ],
128 is_deeply([ $mime->header('Xref') ], [ 'example.com test:1' ],
131 $ng->{-base_url} = 'http://mirror.example.com/m/';
133 PublicInbox::NNTP::set_nntp_headers($mock_self, $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');