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
|
use strict;
use warnings;
use Test::More tests => 11;
use Test::Exception;
BEGIN {
use_ok 'Spline::Socketmap', qw(
netstring_read
netstring_write
) or BAIL_OUT;
}
sub output($) {
my $data = shift;
my $output;
open(my $fh, '>', \$output);
netstring_write($fh, $data);
return $output;
}
sub input($) {
my $data = shift;
open(my $fh, '<', \$data);
return netstring_read($fh);
}
# Netstring output
is(output('test'), '4:test,', 'Netstring output');
is(output(''), '0:,', 'Empty netstring output');
is(output('. '), '2:. ,', 'Preserve spaces');
# Netstring input
is(input('4:test,'), 'test', 'Basic netstring input');
is(input('0:,'), '', 'Empty netstring input');
throws_ok { input('4:test') } qr/, missing/, 'Missing final ,';
throws_ok { input('5:test') } qr/4 of 5 bytes/, 'Data too short';
throws_ok { input('4test') } qr/Invalid length/, 'Format error';
throws_ok { input('') } qr/Cannot read .* length/, 'Empty input';
throws_ok { netstring_read('') } qr/Filehandle required/, 'No filehandle';
# vim: set et ts=4:
|