1#!perl -w 2# Before `make install' is performed this script should be runnable with 3# `make test'. After `make install' it should work as `perl t/dhe.t' 4 5use Net::SSLeay; 6use Socket; 7use IO::Socket::SSL; 8use strict; 9 10 11if ( grep { $^O =~m{$_} } qw( MacOS VOS vmesa riscos amigaos ) ) { 12 print "1..0 # Skipped: fork not implemented on this platform\n"; 13 exit 14} 15 16# check first if we have loaded IO::Socket::IP, as if so we won't need or use 17# IO::Socket::INET6 18if( IO::Socket::SSL->CAN_IPV6 eq "IO::Socket::IP" ) { 19 print "1..0 # Skipped: using IO::Socket::IP instead\n"; 20 exit; 21} 22 23# check if we have loaded INET6, IO::Socket::SSL should do it by itself 24# if it is available 25unless( IO::Socket::SSL->CAN_IPV6 eq "IO::Socket::INET6" ) { 26 # not available or IO::Socket::SSL forgot to load it 27 if ( ! eval { require IO::Socket::INET6 } ) { 28 print "1..0 # Skipped: no IO::Socket::INET6 available\n"; 29 } else { 30 print "1..1\nnot ok # automatic use of INET6\n"; 31 } 32 exit 33} 34 35my $addr = '::1'; 36# check if we can use ::1, e.g if the computer has IPv6 enabled 37if ( ! IO::Socket::INET6->new( 38 Listen => 10, 39 LocalAddr => $addr, 40)) { 41 print "1..0 # no IPv6 enabled on this computer\n"; 42 exit 43} 44 45$|=1; 46print "1..3\n"; 47 48# first create simple ssl-server 49my $ID = 'server'; 50my $server = IO::Socket::SSL->new( 51 LocalAddr => $addr, 52 Listen => 2, 53 SSL_cert_file => "certs/server-cert.pem", 54 SSL_key_file => "certs/server-key.pem", 55) || do { 56 notok($!); 57 exit 58}; 59ok("Server Initialization at $addr"); 60 61# add server port to addr 62$addr = "[$addr]:".$server->sockport; 63print "# server at $addr\n"; 64 65my $pid = fork(); 66if ( !defined $pid ) { 67 die $!; # fork failed 68 69} elsif ( !$pid ) { ###### Client 70 71 $ID = 'client'; 72 close($server); 73 my $to_server = IO::Socket::SSL->new( $addr ) || do { 74 notok( "connect failed: ".IO::Socket::SSL->errstr() ); 75 exit 76 }; 77 ok( "client connected" ); 78 79} else { ###### Server 80 81 my $to_client = $server->accept || do { 82 notok( "accept failed: ".$server->errstr() ); 83 kill(9,$pid); 84 exit; 85 }; 86 ok( "Server accepted" ); 87 wait; 88} 89 90sub ok { print "ok # [$ID] @_\n"; } 91sub notok { print "not ok # [$ID] @_\n"; } 92