summaryrefslogtreecommitdiffstats
path: root/t/02-read-write.t
blob: b7a26acab113948755c06d66e6a66b727a6d35e9 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Requires 'Test::TCP';
use IO::Pty::Easy;

use App::Termcast;

pipe(my $cread, my $swrite);
pipe(my $sread, my $cwrite);

test_tcp(
    client => sub {
        my $port = shift;
        close $swrite;
        close $sread;
        { sysread($cread, my $buf, 1) }
        my $inc = join ':', grep { !ref } @INC;
        my $client_script = <<EOF;
        BEGIN { \@INC = split /:/, '$inc' }
        use App::Termcast;
        my \$tc = App::Termcast->new(
            host => '127.0.0.1', port => $port,
            user => 'test', password => 'tset');
        \$tc->run('$^X', "-e", "while (<>) { last if /\\\\./; print; print qq{---\\n}; }");
EOF
        my $pty = IO::Pty::Easy->new;
        $pty->spawn("$^X", "-e", $client_script);
        syswrite($cwrite, 'a');
        { sysread($cread, my $buf, 1) }
        $pty->write("foo\n");
        syswrite($cwrite, 'a');
        { sysread($cread, my $buf, 1) }
        {
            local $SIG{ALRM} = sub { fail("got the right thing on stdout") };
            alarm 10;
            my $read = '';
            $read .= $pty->read until $read =~ /---/;
            alarm 0;
            is($read, "foo\r\nfoo\r\n---\r\n", 'got the right thing on stdout');
        }
        $pty->write("bar\n");
        syswrite($cwrite, 'a');
        { sysread($cread, my $buf, 1) }
        {
            local $SIG{ALRM} = sub { fail("got the right thing on stdout") };
            alarm 10;
            my $read = '';
            $read .= $pty->read until $read =~ /---/;
            alarm 0;
            is($read, "bar\r\nbar\r\n---\r\n", 'got the right thing on stdout');
        }
        $pty->write(".\n");
        syswrite($cwrite, 'a');
        { sysread($cread, my $buf, 1) }
        is($pty->read, ".\r\n", "didn't get too much data");
    },
    server => sub {
        my $port = shift;
        close $cwrite;
        close $cread;
        my $sock = IO::Socket::INET->new(LocalAddr => '127.0.0.1',
                                         LocalPort => $port,
                                         Listen    => 1);
        $sock->accept; # signal to the client that the port is available
        syswrite($swrite, 'a');
        my $client = $sock->accept;
        { sysread($sread, my $buf, 1) }
        my $login;
        $client->recv($login, 4096);
        my $auth_regexp = qr/^hello test tset\n(?:\e\[H\x00.+?\xff\e\[H\e\[2J)?/;
        like($login, $auth_regexp, 'got the correct login info');
        $client->send("hello, test\n");

        syswrite($swrite, 'a');
        my $output;
        my $total_out = '';
        while (1) {
            { sysread($sread, my $buf, 1) }
            $client->recv($output, 4096);
            last unless defined($output) && length($output);
            $total_out .= $output;
            syswrite($swrite, 'a');
        }
        is($total_out, "foo\r\nfoo\r\n---\r\nbar\r\nbar\r\n---\r\n.\r\n",
           'sent the right data to the server');
        syswrite($swrite, 'a');
    },
);

done_testing;