1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use PublicInbox::TestCommon;
6 require_mods(qw(DBD::SQLite));
7 use_ok 'PublicInbox::NNTP';
8 use PublicInbox::Config;
9 use POSIX qw(strftime);
15 my (undef, $s) = split(/ = /, Dumper($orig), 2);
16 $s // diag explain(['$s undefined, $orig = ', $orig]);
21 my $wm_prepare = sub {
24 PublicInbox::NNTP::wildmat2re($_[0]);
25 my $new = $quote_str->($_[0]);
29 my $wildmat_like = sub {
31 my ($orig, $new) = $wm_prepare->($wm);
32 like($str, $wm, "$orig matches '$str' using $new");
35 my $wildmat_unlike = sub {
36 my ($str, $wm, $check_ex) = @_;
40 like($str, $re, "normal re with $wm matches, but ...");
42 my ($orig, $new) = $wm_prepare->($wm);
43 unlike($str, $wm, "$orig does not match '$str' using $new");
46 $wildmat_like->('[foo]', '[\[foo\]]');
47 $wildmat_like->('any', '*');
48 $wildmat_unlike->('bar.foo.bar', 'foo.*');
51 $wildmat_unlike->('HI', '(?{"HI"})', 1);
52 $wildmat_unlike->('HI', '[(?{"HI"})]', 1);
56 my $ngpat_like = sub {
59 PublicInbox::NNTP::ngpat2re($pat);
60 like($str, $pat, "'$orig' matches '$str' using $pat");
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 :<
78 is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
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));
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');
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',
107 domain => 'example.com',
108 url => [ '//example.com/a' ]});
109 is($ibx->base_url, $u, 'URL expanded');
111 my $mime = PublicInbox::Eml->new("Message-ID: <$mid>\r\n\r\n");
112 my $hdr = $mime->header_obj;
115 servername => 'example.com',
116 pi_cfg => bless {}, 'PublicInbox::Config',
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' ],
126 is_deeply([ $mime->header('Xref') ], [ 'example.com test:1' ],
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');