File: npn.t

package info (click to toggle)
libio-socket-ssl-perl 2.002-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 1,012 kB
  • sloc: perl: 14,405; makefile: 4
file content (78 lines) | stat: -rw-r--r-- 1,785 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!perl
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl t/dhe.t'

use strict;
use warnings;
use Net::SSLeay;
use Socket;
use IO::Socket::SSL;
do './testlib.pl' || do './t/testlib.pl' || die "no testlib";

# check if we have NPN available
# if it is available
if ( ! IO::Socket::SSL->can_npn ) {
    print "1..0 # Skipped: NPN not available in Net::SSLeay\n";
    exit
}

$|=1;
print "1..5\n";

# first create simple ssl-server
my $ID = 'server';
my $addr = '127.0.0.1';
my $server = IO::Socket::SSL->new(
    LocalAddr => $addr,
    Listen => 2,
    SSL_cert_file => 'certs/server-cert.pem',
    SSL_key_file => 'certs/server-key.pem',
    SSL_npn_protocols => [qw(one two)],
) || do {
    ok(0,$!);
    exit
};
ok(1,"Server Initialization at $addr");

# add server port to addr
$addr = "$addr:".$server->sockport;
print "# server at $addr\n";

my $pid = fork();
if ( !defined $pid ) {
    die $!; # fork failed

} elsif ( !$pid ) {    ###### Client

    $ID = 'client';
    close($server);
    my $to_server = IO::Socket::SSL->new(
	PeerHost => $addr,
	SSL_verify_mode => 0,
	SSL_npn_protocols => [qw(two three)],
    ) or do {
	ok(0, "connect failed: ".IO::Socket::SSL->errstr() );
	exit
    };
    ok(1,"client connected" );
    my $proto = $to_server->next_proto_negotiated;
    ok($proto eq 'two',"negotiated $proto");


} else {                ###### Server

    my $to_client = $server->accept or do {
	ok(0,"accept failed: ".$server->errstr() );
	kill(9,$pid);
	exit;
    };
    ok(1,"Server accepted" );
    my $proto = $to_client->next_proto_negotiated;
    ok($proto eq 'two',"negotiated $proto");
    wait;
}

sub ok {
    my $ok = shift;
    print $ok ? '' : 'not ', "ok # [$ID] @_\n";
}