summaryrefslogtreecommitdiffstats
path: root/t/001-ping.t
blob: b9e2db6ca787980f0bc2f93553de769e3bf424c0 (plain) (blame)
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
#!/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');