File: 34hook_out.t

package info (click to toggle)
libnet-proxy-perl 0.12-5
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 304 kB
  • ctags: 66
  • sloc: perl: 777; sh: 84; makefile: 44
file content (98 lines) | stat: -rw-r--r-- 2,864 bytes parent folder | download | duplicates (5)
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
93
94
95
96
97
98
use Test::More;
use strict;
use warnings;
use IO::Socket::INET;
use t::Util;

use Net::Proxy;

my @lines = (
    [ "swa_a_p zamm swish bap crunch\n", "swa_a_p zowie swish bap crunch\n" ],
    [   "zlonk zok zapeth crunch_eth crraack\n",
        "zowie zowie zowie crunch_eth crraack\n"
    ],
    [   "glipp zwapp urkkk cr_r_a_a_ck glurpp\n",
        "glipp zowie urkkk cr_r_a_a_ck glurpp\n"
    ],
    [   "zzzzzwap thwapp zgruppp awk eee_yow\n",
        "zowie thwapp zowie awk eee_yow\n"
    ],
);
my $tests = @lines + 1;

plan tests => $tests;

init_rand(@ARGV);

# lock 2 ports
my @free = find_free_ports(2);

SKIP: {
    skip "Not enough available ports", $tests if @free < 2;

    my ( $proxy_port, $server_port ) = @free;
    my $pid = fork;

SKIP: {
        skip "fork failed", $tests if !defined $pid;
        if ( $pid == 0 ) {

            # the child process runs the proxy
            my $proxy = Net::Proxy->new(
                {   in => {
                        type => 'tcp',
                        host => 'localhost',
                        port => $proxy_port,
                        timeout => 1,
                    },
                    out => {
                        type => 'tcp',
                        host => 'localhost',
                        port => $server_port,
                        hook => sub {
                            my ($dataref, $sock,  $connector) = @_;
                            $$dataref =~ s/\bz\w+/zowie/g;
                        },
                    },
                }
            );

            $proxy->register();

            Net::Proxy->set_verbosity( $ENV{NET_PROXY_VERBOSITY} || 0 );
            Net::Proxy->mainloop(1);
            exit;
        }
        else {

            # wait for the proxy to set up
            sleep 1;

            # the parent process does the testing
            my $listener = listen_on_port($server_port)
                or skip "Couldn't start the server: $!", $tests;
            my $client = connect_to_port($proxy_port)
                or skip "Couldn't start the client: $!", $tests;
            my $server = $listener->accept()
                or skip "Proxy didn't connect: $!", $tests;

            # data from the client is transformed by the hook
            my $orig_server = $server;
            for my $line (@lines) {

                # anyone speaks first
                ( $client, $server ) = random_swap( $server, $client );

                # send some data through
                print $client $line->[0];
                
                my $trans = $server ne $orig_server;
                is( <$server>, $line->[$trans],
                    "Line received " . ( "intact", "transformed" )[$trans] );
            }
            $client->close();
            is_closed( $server, 'peer' );
            $server->close();
        }
    }
}