2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use Socket qw(IPPROTO_TCP SOL_SOCKET);
6 use PublicInbox::TestCommon;
7 # IO::Poll and Net::NNTP are part of the standard library, but
8 # distros may split them off...
9 require_mods(qw(-imapd IO::Socket::SSL Mail::IMAPClient IO::Poll Net::NNTP));
10 my $imap_client = 'Mail::IMAPClient';
11 $imap_client->can('starttls') or
12 plan skip_all => 'Mail::IMAPClient does not support TLS';
13 Net::NNTP->can('starttls') or
14 plan skip_all => 'Net::NNTP does not support TLS';
15 my $cert = 'certs/server-cert.pem';
16 my $key = 'certs/server-key.pem';
17 unless (-r $key && -r $cert) {
19 "certs/ missing for $0, run $^X ./create-certs.perl in certs/";
21 use_ok 'PublicInbox::TLS';
22 use_ok 'IO::Socket::SSL';
25 my ($tmpdir, $for_destroy) = tmpdir();
26 my $err = "$tmpdir/stderr.log";
27 my $out = "$tmpdir/stdout.log";
29 my $group = 'test-netd';
30 my $addr = $group . '@example.com';
32 # ensure we have free, low-numbered contiguous FDs from 3.. FD inheritance
35 pipe(my ($r, $w)) or xbail "pipe: $!";
36 push @pad_pipes, $r, $w;
38 my %srv = map { $_ => tcp_server() } qw(imap nntp imaps nntps);
39 my $ibx = create_inbox 'netd', version => 2,
40 -primary_address => $addr, indexlevel => 'basic', sub {
42 $im->add(eml_load('t/data/0001.patch')) or BAIL_OUT '->add';
43 $pi_config = "$ibx->{inboxdir}/pi_config";
44 open my $fh, '>', $pi_config or BAIL_OUT "open: $!";
45 print $fh <<EOF or BAIL_OUT "print: $!";
47 inboxdir = $ibx->{inboxdir}
52 close $fh or BAIL_OUT "close: $!\n";
54 $pi_config //= "$ibx->{inboxdir}/pi_config";
55 my @args = ("--cert=$cert", "--key=$key");
58 while (my ($k, $v) = each %srv) {
59 push @args, "-l$k://".tcp_host_port($v);
62 my $cmd = [ '-netd', '-W0', @args, "--stdout=$out", "--stderr=$err" ];
63 my $env = { PI_CONFIG => $pi_config };
64 my $td = start_script($cmd, $env, $rdr);
68 SSL_hostname => 'server.local',
69 SSL_verifycn_name => 'server.local',
70 SSL_verify_mode => SSL_VERIFY_PEER(),
71 SSL_ca_file => 'certs/test-ca.pem',
74 my $c = tcp_connect($srv{imap});
76 like($msg, qr/IMAP4rev1/, 'connected to IMAP');
79 my $c = tcp_connect($srv{nntp});
81 like($msg, qr/^201 .*? ready - post via email/, 'connected to NNTP');