Debian lenny version packages
[pkg-perl] / deb-src / libnet-ssleay-perl / libnet-ssleay-perl-1.35 / t / local / 08_pipe.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Test::More;
6 use Net::SSLeay;
7 use Symbol qw( gensym );
8 use IO::Handle;
9 use File::Spec;
10
11 if ($^O eq 'MSWin32')
12 {
13     plan skip_all => 'pipes not properly supported on Windows' if 1;
14     exit;
15 }
16 else
17 {
18     plan tests => 11;
19 }
20
21 Net::SSLeay::randomize();
22 Net::SSLeay::load_error_strings();
23 Net::SSLeay::OpenSSL_add_ssl_algorithms();
24
25 my $cert = File::Spec->catfile(qw( t data cert.pem ));
26 my $key  = File::Spec->catfile(qw( t data key.pem  ));
27
28 my $how_much = 1024 ** 2;
29
30 my $rs = gensym();
31 my $ws = gensym();
32 my $rc = gensym();
33 my $wc = gensym();
34
35 pipe $rs, $wc or die "pipe 1 ($!)";
36 pipe $rc, $ws or die "pipe 2 ($!)";
37
38 for my $h ($rs, $ws, $rc, $wc) {
39     my $old_select = select $h;
40     $| = 1;
41     select $old_select;
42 }
43
44 my $pid = fork();
45 die unless defined $pid;
46
47 if ($pid == 0) {
48     my $ctx = Net::SSLeay::CTX_new();
49     Net::SSLeay::set_server_cert_and_key($ctx, $cert, $key);
50
51     my $ssl = Net::SSLeay::new($ctx);
52
53     ok( Net::SSLeay::set_rfd($ssl, fileno($rs)), 'set_rfd using fileno' );
54     ok( Net::SSLeay::set_wfd($ssl, fileno($ws)), 'set_wfd using fileno' );
55
56     ok( Net::SSLeay::accept($ssl), 'accept' );
57
58     ok( my $got = Net::SSLeay::ssl_read_all($ssl, $how_much), 'ssl_read_all' );
59
60     is( Net::SSLeay::ssl_write_all($ssl, \$got), length $got, 'ssl_write_all' );
61
62     Net::SSLeay::free($ssl);
63     Net::SSLeay::CTX_free($ctx);
64
65     close $ws;
66     close $rs;
67     exit;
68 }
69
70 my @results;
71 {
72     my $ctx = Net::SSLeay::CTX_new();
73     my $ssl = Net::SSLeay::new($ctx);
74
75     my $rc_handle = IO::Handle->new_from_fd( fileno($rc), 'r' );
76     my $wc_handle = IO::Handle->new_from_fd( fileno($wc), 'w' );
77     push @results, [ Net::SSLeay::set_rfd($ssl, $rc_handle), 'set_rfd using an io handle' ];
78     push @results, [ Net::SSLeay::set_wfd($ssl, $wc_handle), 'set_wfd using an io handle' ];
79
80     push @results, [ Net::SSLeay::connect($ssl), 'connect' ];
81
82     my $data = 'B' x $how_much;
83
84     push @results, [ Net::SSLeay::ssl_write_all($ssl, \$data) == length $data, 'ssl_write_all' ];
85
86     my $got = Net::SSLeay::ssl_read_all($ssl, $how_much);
87     push @results, [ $got eq $data, 'ssl_read_all' ];
88
89     Net::SSLeay::free($ssl);
90     Net::SSLeay::CTX_free($ctx);
91
92     close $wc;
93     close $rc;
94 }
95
96 waitpid $pid, 0;
97 push @results, [ $? == 0, 'server exited with 0' ];
98
99 Test::More->builder->current_test(5);
100 for my $t (@results) {
101     ok( $t->[0], $t->[1] );
102 }