File: readline.t

package info (click to toggle)
libio-socket-ssl-perl 2.002-2%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 1,348 kB
  • sloc: perl: 14,412; makefile: 4
file content (170 lines) | stat: -rw-r--r-- 3,760 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!perl
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl t/readline.t'

# This tests the behavior of readline with the variety of
# cases with $/:
# $/ undef - read all
# $/ ''    - read up to next nonempty line: .*?\n\n+
# $/ s     - read up to string s
# $/ \$num - read $num bytes
# scalar context - get first match
# array context  - get all matches

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

my @tests;
push @tests, [
    "multi\nple\n\n1234567890line\n\n\n\nbla\n\nblubb\n\nblip",
    sub {
	my $c = shift;
	local $/ = "\n\n";
	my $b;
	($b=<$c>) eq "multi\nple\n\n" || die "LFLF failed ($b)";
	$/ = \"10";
	($b=<$c>) eq "1234567890" || die "\\size failed ($b)";
	$/ = '';
	($b=<$c>) eq "line\n\n\n\n" || die "'' failed ($b)";
	my @c = <$c>;
	die "'' @ failed: @c" unless $c[0] eq "bla\n\n" &&
	    $c[1] eq "blubb\n\n" &&
	    $c[2] eq "blip" && @c == 3;
    },
];

push @tests, [
    "some\nstring\nwith\nsome\nlines\nwhatever",
    sub {
	my $c = shift;
	local $/ = "\n";
	my $b;
	($b=<$c>) eq "some\n" || die "LF failed ($b)";
	$/ = undef;
	($b=<$c>) eq "string\nwith\nsome\nlines\nwhatever" || die "undef failed ($b)";
    },
];

push @tests, [
    "some\nstring\nwith\nsome\nlines\nwhatever",
    sub {
	my $c = shift;
	local $/ = "\n";
	my @c = <$c>;
	die "LF @ failed: @c" unless $c[0] eq "some\n" &&
	    $c[1] eq "string\n" && $c[2] eq "with\n" && $c[3] eq "some\n" &&
	    $c[4] eq "lines\n" && $c[5] eq "whatever" && @c == 6;

    },
];

push @tests, [
    "some\nstring\nwith\nsome\nlines\nwhatever",
    sub {
	my $c = shift;
	local $/;
	my @c = <$c>;
	die "undef @ failed: @c" unless
	    $c[0] eq "some\nstring\nwith\nsome\nlines\nwhatever"
	    && @c == 1;

    },
];

push @tests, [
    "1234567890",
    sub {
	my $c = shift;
	local $/ = \2;
	my @c = <$c>;
	die "\\2 @ failed: @c" unless
	    $c[0] eq '12' && $c[1] eq '34' && $c[2] eq '56' &&
	    $c[3] eq '78' && $c[4] eq '90' && @c == 5;

    },
];

push @tests, [
    [ "bla\n","0","blubb\n","no newline" ],
    sub {
	my $c = shift;
	my $l = <$c>;
	$l eq "bla\n" or die "'bla\\n' failed";
	$l = <$c>;
	$l eq "0blubb\n" or die "'0blubb\\n' failed";
	$l = <$c>;
	$l eq "no newline" or die "'no newline' failed";
    },
];

$|=1;
print "1..".(1+3*@tests)."\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,
    ReuseAddr => 1,
    SSL_cert_file => "certs/server-cert.pem",
    SSL_key_file  => "certs/server-key.pem",
) || do {
    notok($!);
    exit
};
ok("Server Initialization");

# add server port to addr
$addr.= ':'.(sockaddr_in( getsockname( $server )))[0];

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

} elsif ( $pid ) {    ###### Server

    foreach my $test (@tests) {
	my $to_client = $server->accept || do {
	    notok( "accept failed: ".$server->errstr() );
	    kill(9,$pid);
	    exit;
	};
	ok( "Server accepted" );
	$to_client->autoflush;
	my $t = $test->[0];
	$t = [$t] if ! ref($t);
	for(@$t) {
	    $to_client->print($_);
	    select(undef,undef,undef,0.1);
	}
    }
    wait;
    exit;
}

$ID = 'client';
close($server);
my $testid = "Test00";
foreach my $test (@tests) {
    my $to_server = IO::Socket::SSL->new(
	PeerAddr => $addr,
	SSL_verify_mode => 0 ) || do {
	notok( "connect failed: ".IO::Socket::SSL->errstr() );
	exit
    };
    ok( "client connected" );
    eval { $test->[1]( $to_server ) };
    $@ ? notok( "$testid $@" ) : ok( $testid );
    $testid++
}



sub ok { print "ok # [$ID] @_\n"; }
sub notok { print "not ok # [$ID] @_\n"; }