1#!perl
2
3use 5.008001;
4
5use strict;
6use warnings;
7
8use Test::More;
9
10BEGIN {
11    if (!eval { require Socket }) {
12        plan skip_all => "no Socket";
13    }
14    elsif (ord('A') == 193 && !eval { require Convert::EBCDIC }) {
15        plan skip_all => "EBCDIC but no Convert::EBCDIC";
16    }
17}
18
19use Config;
20use File::Temp 'tempfile';
21use Net::POP3;
22
23my $debug = 0; # Net::POP3->new( Debug => .. )
24
25my $inet6class = Net::POP3->can_inet6;
26plan skip_all => "no IPv6 support found in Net::POP3" if ! $inet6class;
27
28plan skip_all => "fork not supported on this platform"
29  unless $Config::Config{d_fork} || $Config::Config{d_pseudofork} ||
30    (($^O eq 'MSWin32' || $^O eq 'NetWare') and
31     $Config::Config{useithreads} and
32     $Config::Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/);
33
34my $srv = $inet6class->new(
35  LocalAddr => '::1',
36  Listen => 10
37);
38plan skip_all => "cannot create listener on ::1: $!" if ! $srv;
39my $saddr = "[".$srv->sockhost."]".':'.$srv->sockport;
40note("server on $saddr");
41
42plan tests => 1;
43
44defined( my $pid = fork()) or die "fork failed: $!";
45exit(pop3_server()) if ! $pid;
46
47my $cl = Net::POP3->new($saddr, Debug => $debug);
48note("created Net::POP3 object");
49if (!$cl) {
50  fail("IPv6 POP3 connect failed");
51} else {
52  $cl->quit;
53  pass("IPv6 success");
54}
55wait;
56
57sub pop3_server {
58  my $cl = $srv->accept or die "accept failed: $!";
59  print $cl "+OK localhost ready\r\n";
60  while (<$cl>) {
61    my ($cmd,$arg) = m{^(\S+)(?: +(.*))?\r\n} or die $_;
62    $cmd = uc($cmd);
63    if ($cmd eq 'QUIT' ) {
64      print $cl "+OK bye\r\n";
65      last;
66    } elsif ( $cmd eq 'CAPA' ) {
67      print $cl "+OK\r\n".
68        ".\r\n";
69    } else {
70      diag("received unknown command: $cmd");
71      print "-ERR unknown cmd\r\n";
72    }
73  }
74
75  note("POP3 dialog done");
76  return 0;
77}
78