]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/nntpd-tls.t
nntp: add support for CAPABILITIES command
[public-inbox.git] / t / nntpd-tls.t
index e3ecdd4fa0ecd4d83c4122b17ab31ff17a5e0afe..4cf53daad8cd0325fdb7e9555ab56bc7a19906d4 100644 (file)
@@ -37,15 +37,8 @@ my $pi_config = "$tmpdir/pi_config";
 my $group = 'test-nntpd-tls';
 my $addr = $group . '@example.com';
 my $nntpd = 'blib/script/public-inbox-nntpd';
-my %opts = (
-       LocalAddr => '127.0.0.1',
-       ReuseAddr => 1,
-       Proto => 'tcp',
-       Type => SOCK_STREAM,
-       Listen => 1024,
-);
-my $starttls = IO::Socket::INET->new(%opts);
-my $nntps = IO::Socket::INET->new(%opts);
+my $starttls = tcp_server();
+my $nntps = tcp_server();
 my ($pid, $tail_pid);
 END {
        foreach ($pid, $tail_pid) {
@@ -135,6 +128,8 @@ for my $args (
        my $c = Net::NNTP->new($nntps_addr, %o, SSL => 1);
        my $list = $c->list;
        is_deeply($list, $expect, 'NNTPS LIST works');
+       unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
+               'STARTTLS not advertised for NNTPS');
        is($c->command('QUIT')->response(), Net::Cmd::CMD_OK(), 'QUIT works');
        is(0, sysread($c, my $buf, 1), 'got EOF after QUIT');
 
@@ -146,6 +141,8 @@ for my $args (
        is($c->code, 382, 'got 382 for STARTTLS');
        $list = $c->list;
        is_deeply($list, $expect, 'LIST works after STARTTLS');
+       unlike(get_capa($c), qr/\bSTARTTLS\r\n/,
+               'STARTTLS not advertised after STARTTLS');
 
        # Net::NNTP won't let us do dumb things, but we need to test
        # dumb things, so use Net::Cmd directly:
@@ -156,6 +153,7 @@ for my $args (
        # STARTTLS with bad hostname
        $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.invalid';
        $c = Net::NNTP->new($starttls_addr, %o);
+       like(get_capa($c), qr/\bSTARTTLS\r\n/, 'STARTTLS advertised');
        $list = $c->list;
        is_deeply($list, $expect, 'plain LIST works again');
        ok(!$c->starttls, 'STARTTLS fails with bad hostname');
@@ -224,4 +222,17 @@ for my $args (
        }
 }
 done_testing();
+
+sub get_capa {
+       my ($sock) = @_;
+       syswrite($sock, "CAPABILITIES\r\n");
+       my $capa = '';
+       do {
+               my $r = sysread($sock, $capa, 8192, length($capa));
+               die "unexpected: $!" unless defined($r);
+               die 'unexpected EOF' if $r == 0;
+       } until $capa =~ /\.\r\n\z/;
+       $capa;
+}
+
 1;