This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / cpan / libnet / t / datasend.t
CommitLineData
2e173144
CBW
1#!perl
2
3use 5.008001;
4
5use strict;
6use warnings;
f92f3fcb 7
27b896ab
SH
8use Test::More;
9
f92f3fcb 10BEGIN {
162b417c 11 if (!eval { require Socket }) {
27b896ab
SH
12 plan skip_all => "no Socket";
13 }
14 elsif (ord('A') == 193 && !eval { require Convert::EBCDIC }) {
15 plan skip_all => "EBCDIC but no Convert::EBCDIC";
c465390a 16 }
27b896ab
SH
17 else {
18 plan tests => 54;
c465390a
RGS
19 }
20}
21
22BEGIN {
f92f3fcb
GB
23 package Foo;
24
25 use IO::File;
26 use Net::Cmd;
2e173144 27 our @ISA = qw(Net::Cmd IO::File);
f92f3fcb 28
a9282e3c
SH
29 sub timeout { 0 }
30
f92f3fcb
GB
31 sub new {
32 my $fh = shift->new_tmpfile;
33 binmode($fh);
34 $fh;
35 }
36
37 sub output {
38 my $self = shift;
39 seek($self,0,0);
40 local $/ = undef;
41 scalar(<$self>);
42 }
43
44 sub response {
45 return Net::Cmd::CMD_OK;
46 }
47}
48
f92f3fcb
GB
49sub check {
50 my $expect = pop;
51 my $cmd = Foo->new;
9714c667 52 ok($cmd->datasend, 'datasend') unless @_;
f92f3fcb 53 foreach my $line (@_) {
9714c667 54 ok($cmd->datasend($line), 'datasend');
f92f3fcb 55 }
9714c667
GB
56 ok($cmd->dataend, 'dataend');
57 is(
58 unpack("H*",$cmd->output),
59 unpack("H*",$expect)
60 );
f92f3fcb
GB
61}
62
63my $cmd;
64
65check(
66 # nothing
67
68 ".\015\012"
69);
70
71check(
72 "a",
73
74 "a\015\012.\015\012",
75);
76
77check(
78 "a\r",
79
80 "a\015\015\012.\015\012",
81);
82
83check(
84 "a\rb",
85
86 "a\015b\015\012.\015\012",
87);
88
89check(
90 "a\rb\n",
91
92 "a\015b\015\012.\015\012",
93);
94
95check(
96 "a\rb\n\n",
97
98 "a\015b\015\012\015\012.\015\012",
99);
100
101check(
102 "a\r",
103 "\nb",
104
105 "a\015\012b\015\012.\015\012",
106);
107
108check(
109 "a\r",
110 "\nb\n",
111
112 "a\015\012b\015\012.\015\012",
113);
114
115check(
116 "a\r",
117 "\nb\r\n",
118
119 "a\015\012b\015\012.\015\012",
120);
121
122check(
123 "a\r",
124 "\nb\r\n\n",
125
126 "a\015\012b\015\012\015\012.\015\012",
127);
128
129check(
130 "a\n.b\n",
131
132 "a\015\012..b\015\012.\015\012",
133);
134
135check(
136 ".a\n.b\n",
137
138 "..a\015\012..b\015\012.\015\012",
139);
140
141check(
142 ".a\n",
143 ".b\n",
144
145 "..a\015\012..b\015\012.\015\012",
146);
147
148check(
149 ".a",
150 ".b\n",
151
152 "..a.b\015\012.\015\012",
153);
154
155check(
156 "a\n.",
157
158 "a\015\012..\015\012.\015\012",
159);
160
db956464
CBW
161# Test that datasend() plays nicely with bytes in an upgraded string,
162# even though the input should really be encode()d already.
163check(
164 substr("\x{100}", 0, 0) . "\x{e9}",
165
166 "\x{e9}\015\012.\015\012"
167);