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 if we have NPN available
17# if it is available
18if ( ! exists &Net::SSLeay::P_next_proto_negotiated ) {
19    print "1..0 # Skipped: NPN not available in Net::SSLeay\n";
20    exit
21}
22
23$|=1;
24print "1..5\n";
25
26# first create simple ssl-server
27my $ID = 'server';
28my $addr = '127.0.0.1';
29my $server = IO::Socket::SSL->new(
30    LocalAddr => $addr,
31    Listen => 2,
32    SSL_npn_protocols => [qw(one two)],
33) || do {
34    ok(0,$!);
35    exit
36};
37ok(1,"Server Initialization at $addr");
38
39# add server port to addr
40$addr = "$addr:".$server->sockport;
41print "# server at $addr\n";
42
43my $pid = fork();
44if ( !defined $pid ) {
45    die $!; # fork failed
46
47} elsif ( !$pid ) {    ###### Client
48
49    $ID = 'client';
50    close($server);
51    my $to_server = IO::Socket::SSL->new( 
52	PeerHost => $addr,
53	SSL_npn_protocols => [qw(two three)],
54    ) or do {
55    	ok(0, "connect failed: ".IO::Socket::SSL->errstr() );
56	exit
57    };
58    ok(1,"client connected" );
59    my $proto = $to_server->next_proto_negotiated;
60    ok($proto eq 'two',"negotiated $proto");
61
62
63} else {                ###### Server
64
65    my $to_client = $server->accept or do {
66    	ok(0,"accept failed: ".$server->errstr() );
67	kill(9,$pid);
68	exit;
69    };
70    ok(1,"Server accepted" );
71    my $proto = $to_client->next_proto_negotiated;
72    ok($proto eq 'two',"negotiated $proto");
73    wait;
74}
75
76sub ok { 
77    my $ok = shift;
78    print $ok ? '' : 'not ', "ok # [$ID] @_\n"; 
79}
80