1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::ContentId qw(content_digest);
8 use File::Temp qw/tempdir/;
9 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
11 plan skip_all => "$mod missing for nntpd.t" if $@;
13 use_ok 'PublicInbox::V2Writable';
14 my $mainrepo = tempdir('pi-v2writable-XXXXXX', TMPDIR => 1, CLEANUP => 1);
16 mainrepo => $mainrepo,
17 name => 'test-v2writable',
19 -primary_address => 'test@example.com',
21 $ibx = PublicInbox::Inbox->new($ibx);
22 my $mime = PublicInbox::MIME->create(
24 From => 'a@example.com',
25 To => 'test@example.com',
26 Subject => 'this is a subject',
27 'Message-ID' => '<a-mid@b>',
28 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
30 body => "hello world\n",
33 my $im = PublicInbox::V2Writable->new($ibx, 1);
34 ok($im->add($mime), 'ordinary message added');
36 if ('ensure git configs are correct') {
37 my @cmd = (qw(git config), "--file=$mainrepo/all.git/config",
38 qw(core.sharedRepository 0644));
39 is(system(@cmd), 0, "set sharedRepository in all.git");
40 my $git0 = PublicInbox::Git->new("$mainrepo/git/0.git");
41 my $fh = $git0->popen(qw(config core.sharedRepository));
42 my $v = eval { local $/; <$fh> };
44 is($v, '0644', 'child repo inherited core.sharedRepository');
45 $fh = $git0->popen(qw(config --bool repack.writeBitmaps));
46 $v = eval { local $/; <$fh> };
48 is($v, 'true', 'child repo inherited repack.writeBitmaps');
53 local $SIG{__WARN__} = sub { push @warn, @_ };
54 is(undef, $im->add($mime), 'obvious duplicate rejected');
55 like(join(' ', @warn), qr/resent/, 'warned about resent message');
58 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
59 ok($im->add($mime), 'secondary MID used');
60 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
61 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
62 is_deeply([ '<a-mid@b>', '<c@d>' ],
63 [ $mime->header_obj->header_raw('Message-Id') ],
64 'no new Message-Id added');
67 $mime->header_set('Message-Id', '<a-mid@b>');
68 $mime->body_set('different');
69 ok($im->add($mime), 'reused mid ok');
70 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
71 my @mids = $mime->header_obj->header_raw('Message-Id');
72 is($mids[1], '<a-mid@b>', 'original mid not changed');
73 like($mids[0], qr/\A<\w+\@localhost>\z/, 'new MID added');
74 is(scalar(@mids), 2, 'only one new MID added');
77 $mime->header_set('Message-Id', '<a-mid@b>');
78 $mime->body_set('this one needs a random mid');
79 my $gen = content_digest($mime)->hexdigest . '@localhost';
80 my $fake = PublicInbox::MIME->new($mime->as_string);
81 $fake->header_set('Message-Id', $gen);
82 ok($im->add($fake), 'fake added easily');
83 is_deeply(\@warn, [], 'no warnings from a faker');
84 ok($im->add($mime), 'random MID made');
85 like(join(' ', @warn), qr/using random/, 'warned about using random');
86 @mids = $mime->header_obj->header_raw('Message-Id');
87 is($mids[1], '<a-mid@b>', 'original mid not changed');
88 like($mids[0], qr/\A<\w+\@localhost>\z/, 'new MID added');
89 is(scalar(@mids), 2, 'only one new MID added');
92 $mime->header_set('Message-Id');
93 ok($im->add($mime), 'random MID made for MID free message');
94 @mids = $mime->header_obj->header_raw('Message-Id');
95 like($mids[0], qr/\A<\w+\@localhost>\z/, 'mid was generated');
96 is(scalar(@mids), 1, 'new generated');
100 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
101 ok($im->add($mime), 'message with multiple Message-ID');
104 $ibx->search->each_smsg_by_mid('abcde@1', sub { push @found, @_; 1 });
105 is(scalar(@found), 1, 'message found by first MID');
106 $ibx->search->each_smsg_by_mid('abcde@2', sub { push @found, @_; 1 });
107 is(scalar(@found), 2, 'message found by second MID');
108 is($found[0]->{doc_id}, $found[1]->{doc_id}, 'same document');
109 ok($found[1]->{doc_id} > 0, 'doc_id is positive');
113 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
116 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
117 eval { require Danga::Socket };
118 skip "Danga::Socket missing $@", 2 if $@;
119 my $err = "$mainrepo/stderr.log";
120 my $out = "$mainrepo/stdout.log";
122 LocalAddr => '127.0.0.1',
128 my $group = 'inbox.comp.test.v2writable';
129 my $pi_config = "$mainrepo/pi_config";
130 open my $fh, '>', $pi_config or die "open: $!\n";
132 [publicinbox "test-v2writable"]
135 address = test\@example.com
139 close $fh or die "close: $!\n";
140 my $sock = IO::Socket::INET->new(%opts);
141 ok($sock, 'sock created');
144 END { kill 'TERM', $pid if defined $pid };
146 my $fl = fcntl($sock, F_GETFD, 0);
147 ok(! $!, 'no error from fcntl(F_GETFD)');
148 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
152 $ENV{PI_CONFIG} = $pi_config;
153 # pretend to be systemd
154 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
155 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
156 $ENV{LISTEN_PID} = $$;
157 $ENV{LISTEN_FDS} = 1;
158 my $nntpd = 'blib/script/public-inbox-nntpd';
159 exec $nntpd, "--stdout=$out", "--stderr=$err";
162 ok(defined $pid, 'forked nntpd process successfully');
164 fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
165 ok(! $!, 'no error from fcntl(F_SETFD)');
166 my $host_port = $sock->sockhost . ':' . $sock->sockport;
167 my $n = Net::NNTP->new($host_port);
169 my $x = $n->xover('1-');
171 foreach my $num (sort { $a <=> $b } keys %$x) {
172 my $mid = $x->{$num}->[3];
173 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
174 is_deeply($n->xhdr('Message-ID', $num),
175 { $num => $mid }, "XHDR lookup OK on num $num");
176 is_deeply($n->xhdr('Message-ID', $mid),
177 { $mid => $mid }, "XHDR lookup OK on MID $num");
180 foreach my $mid (@{$n->newnews(0, $group)}) {
181 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
183 is_deeply([sort keys %nn], [sort keys %uniq]);