summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2009-06-29 04:09:27 -0500
committerJesse Luehrs <doy@tozt.net>2009-06-29 04:09:27 -0500
commit8d1d2cb4748adb7f18ffdadfe1f86395c2835746 (patch)
treefb66c1ca775e2f9526cbfb2603003cdd9a6bc78a
parent2b4f01cfcce1f76b090306e96a2a9a71fd6fa442 (diff)
downloadio-socket-telnet-halfduplex-8d1d2cb4748adb7f18ffdadfe1f86395c2835746.tar.gz
io-socket-telnet-halfduplex-8d1d2cb4748adb7f18ffdadfe1f86395c2835746.zip
add a simple (heh) test
-rw-r--r--t/001-ping.t66
1 files changed, 66 insertions, 0 deletions
diff --git a/t/001-ping.t b/t/001-ping.t
new file mode 100644
index 0000000..b9e2db6
--- /dev/null
+++ b/t/001-ping.t
@@ -0,0 +1,66 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 4;
+
+use IO::Socket::Telnet::HalfDuplex;
+
+pipe my $read, my $write;
+
+my $IAC = chr(255);
+my $DO = chr(253);
+my $WONT = chr(252);
+my $PONG = chr(99);
+my $localport = 23358;
+
+my $pid;
+unless ($pid = fork) {
+ my $ping = 0;
+ my $server = IO::Socket::INET->new(
+ LocalAddr => '127.0.0.1',
+ LocalPort => $localport,
+ Listen => 1,
+ );
+ die "can't create server: $!" if !$server;
+ my $connection = $server->accept;
+ my $buf;
+ my $tested = 0;
+ while (defined $connection->recv($buf, 4096)) {
+ last unless defined $buf && length $buf;
+ my $gotpong = ($buf =~ s/$IAC$DO$PONG//);
+ if (!$tested) {
+ print { $write } "$buf\n";
+ $tested = 1;
+ $connection->send('test');
+ }
+ if ($gotpong) {
+ $ping++;
+ $connection->send("$IAC$WONT$PONG");
+ }
+ }
+ print { $write } "$ping\n";
+ close $write;
+ exit;
+}
+sleep 1;
+my $pong = 0;
+my $client = IO::Socket::Telnet::HalfDuplex->new(
+ PeerAddr => '127.0.0.1',
+ PeerPort => $localport,
+);
+$client->telnet_simple_callback(sub {
+ my $self = shift;
+ my $msg = shift;
+ $pong++ if $msg =~ /99$/;
+ return '';
+});
+$client->send('blah');
+my $str = $client->read;
+$client->close;
+is($pong, 1, "client got a pong from the server");
+is($str, 'test', "client got the right string");
+my $buf;
+read $read, $buf, 7;
+my @results = split /\n/, $buf;
+is($results[0], 'blah', 'server got the right string');
+is($results[1], 1, 'server got a ping from the client');