File: 08_pipe.t

package info (click to toggle)
libnet-ssleay-perl 1.36-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 940 kB
  • ctags: 798
  • sloc: perl: 3,668; ansic: 2,711; makefile: 7
file content (102 lines) | stat: -rw-r--r-- 2,410 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/perl

use strict;
use warnings;
use Test::More;
use Net::SSLeay;
use Symbol qw( gensym );
use IO::Handle;
use File::Spec;

if ($^O eq 'MSWin32')
{
    plan skip_all => 'pipes not properly supported on Windows' if 1;
    exit;
}
else
{
    plan tests => 11;
}

Net::SSLeay::randomize();
Net::SSLeay::load_error_strings();
Net::SSLeay::OpenSSL_add_ssl_algorithms();

my $cert = File::Spec->catfile(qw( t data cert.pem ));
my $key  = File::Spec->catfile(qw( t data key.pem  ));

my $how_much = 1024 ** 2;

my $rs = gensym();
my $ws = gensym();
my $rc = gensym();
my $wc = gensym();

pipe $rs, $wc or die "pipe 1 ($!)";
pipe $rc, $ws or die "pipe 2 ($!)";

for my $h ($rs, $ws, $rc, $wc) {
    my $old_select = select $h;
    $| = 1;
    select $old_select;
}

my $pid = fork();
die unless defined $pid;

if ($pid == 0) {
    my $ctx = Net::SSLeay::CTX_new();
    Net::SSLeay::set_server_cert_and_key($ctx, $cert, $key);

    my $ssl = Net::SSLeay::new($ctx);

    ok( Net::SSLeay::set_rfd($ssl, fileno($rs)), 'set_rfd using fileno' );
    ok( Net::SSLeay::set_wfd($ssl, fileno($ws)), 'set_wfd using fileno' );

    ok( Net::SSLeay::accept($ssl), 'accept' );

    ok( my $got = Net::SSLeay::ssl_read_all($ssl, $how_much), 'ssl_read_all' );

    is( Net::SSLeay::ssl_write_all($ssl, \$got), length $got, 'ssl_write_all' );

    Net::SSLeay::free($ssl);
    Net::SSLeay::CTX_free($ctx);

    close $ws;
    close $rs;
    exit;
}

my @results;
{
    my $ctx = Net::SSLeay::CTX_new();
    my $ssl = Net::SSLeay::new($ctx);

    my $rc_handle = IO::Handle->new_from_fd( fileno($rc), 'r' );
    my $wc_handle = IO::Handle->new_from_fd( fileno($wc), 'w' );
    push @results, [ Net::SSLeay::set_rfd($ssl, $rc_handle), 'set_rfd using an io handle' ];
    push @results, [ Net::SSLeay::set_wfd($ssl, $wc_handle), 'set_wfd using an io handle' ];

    push @results, [ Net::SSLeay::connect($ssl), 'connect' ];

    my $data = 'B' x $how_much;

    push @results, [ Net::SSLeay::ssl_write_all($ssl, \$data) == length $data, 'ssl_write_all' ];

    my $got = Net::SSLeay::ssl_read_all($ssl, $how_much);
    push @results, [ $got eq $data, 'ssl_read_all' ];

    Net::SSLeay::free($ssl);
    Net::SSLeay::CTX_free($ctx);

    close $wc;
    close $rc;
}

waitpid $pid, 0;
push @results, [ $? == 0, 'server exited with 0' ];

Test::More->builder->current_test(5);
for my $t (@results) {
    ok( $t->[0], $t->[1] );
}