xref: /openbsd/gnu/usr.bin/perl/cpan/libnet/t/nntp_ipv6.t (revision 76d0caae)
1#!perl
2
3use 5.008001;
4
5use strict;
6use warnings;
7
8use Config;
9use File::Temp 'tempfile';
10use Net::NNTP;
11use Test::More;
12
13my $debug = 0; # Net::NNTP->new( Debug => .. )
14
15my $inet6class = Net::NNTP->can_inet6;
16plan skip_all => "no IPv6 support found in Net::NNTP" if ! $inet6class;
17
18plan skip_all => "fork not supported on this platform"
19  unless $Config::Config{d_fork} || $Config::Config{d_pseudofork} ||
20    (($^O eq 'MSWin32' || $^O eq 'NetWare') and
21     $Config::Config{useithreads} and
22     $Config::Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/);
23
24my $srv = $inet6class->new(
25  LocalAddr => '::1',
26  Listen => 10
27);
28plan skip_all => "cannot create listener on ::1: $!" if ! $srv;
29my $host = $srv->sockhost;
30my $port = $srv->sockport;
31note("server on $host port $port");
32
33plan tests => 1;
34
35defined( my $pid = fork()) or die "fork failed: $!";
36exit(nntp_server()) if ! $pid;
37
38my $cl = Net::NNTP->new(Host => $host, Port => $port,, Debug => $debug);
39note("created Net::NNTP object");
40if (!$cl) {
41  fail("IPv6 NNTP connect failed");
42} else {
43  $cl->quit;
44  pass("IPv6 success");
45}
46wait;
47
48sub nntp_server {
49  my $ssl = shift;
50  my $cl = $srv->accept or die "accept failed: $!";
51  print $cl "200 nntp.example.com\r\n";
52  while (<$cl>) {
53    my ($cmd,$arg) = m{^(\S+)(?: +(.*))?\r\n} or die $_;
54    $cmd = uc($cmd);
55    if ($cmd eq 'QUIT' ) {
56      print $cl "205 bye\r\n";
57      last;
58    } elsif ( $cmd eq 'MODE' ) {
59      print $cl "201 Posting denied\r\n";
60    } else {
61      diag("received unknown command: $cmd");
62      print "500 unknown cmd\r\n";
63    }
64  }
65  note("NNTP dialog done");
66  return 0;
67}
68