xref: /openbsd/gnu/usr.bin/perl/cpan/libnet/t/datasend.t (revision 09467b48)
1#!perl
2
3use 5.008001;
4
5use strict;
6use warnings;
7
8BEGIN {
9    if (!eval { require Socket }) {
10        print "1..0 # no Socket\n"; exit 0;
11    }
12    if (ord('A') == 193 && !eval { require Convert::EBCDIC }) {
13        print "1..0 # EBCDIC but no Convert::EBCDIC\n"; exit 0;
14    }
15}
16
17BEGIN {
18  package Foo;
19
20  use IO::File;
21  use Net::Cmd;
22  our @ISA = qw(Net::Cmd IO::File);
23
24  sub timeout { 0 }
25
26  sub new {
27    my $fh = shift->new_tmpfile;
28    binmode($fh);
29    $fh;
30  }
31
32  sub output {
33    my $self = shift;
34    seek($self,0,0);
35    local $/ = undef;
36    scalar(<$self>);
37  }
38
39  sub response {
40    return Net::Cmd::CMD_OK;
41  }
42}
43
44(my $libnet_t = __FILE__) =~ s/datasend.t/libnet_t.pl/;
45require $libnet_t or die;
46
47print "1..54\n";
48
49sub check {
50  my $expect = pop;
51  my $cmd = Foo->new;
52  ok($cmd->datasend, 'datasend') unless @_;
53  foreach my $line (@_) {
54    ok($cmd->datasend($line), 'datasend');
55  }
56  ok($cmd->dataend, 'dataend');
57  is(
58    unpack("H*",$cmd->output),
59    unpack("H*",$expect)
60  );
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
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);
168