File: 37connectssl_tcp.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 (121 lines) | stat: -rw-r--r-- 3,923 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use Test::More;
use strict;
use warnings;
use IO::Socket::INET;
use File::Spec::Functions;
use t::Util;

use Net::Proxy;

my @lines = (
    "saturday wednesday tuesday sunday friday\n",
    "agnes_nitt granny_weatherwax greebo vorbis jason_ogg\n",
    "Moins Scrof Ferla_Quist Caine Grove_Of_The_Unicorn\n",
    "rook pawn queen king bishop\n",
    "Fedora QiLinux gnuLinEx ZoneCD ROSLIMS\n",
);
my $tests = @lines + 3;

init_rand(@ARGV);

plan tests => $tests;

SKIP: {
    # check required modules for this test case
    for my $module (qw( LWP::UserAgent HTTP::Daemon IO::Socket::SSL )) {
        eval "require $module;";
        skip "$module required to test connect_ssl", $tests if $@;
    }

    # lock 2 ports
    my @free = find_free_ports(2);
    skip "Not enough available ports", $tests if @free < 2;

    my ($proxy_port, $web_proxy_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
                    },
                    out => {
                        type       => 'connect_ssl',
                        host       => 'zlonk.crunch.com',
                        port       => 443,
                        proxy_host => 'localhost',
                        proxy_port => $web_proxy_port,
                    },
                }
            );

            $proxy->register();

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

            # wait for the proxy to set up
            sleep 1;

            # the parent process does the testing
            my $daemon = HTTP::Daemon->new(
               LocalAddr => 'localhost',
               LocalPort => $web_proxy_port,
               )
              or skip "Couldn't start the server: $!", $tests;
            my $client = connect_to_port($proxy_port)
              or skip_fail "Couldn't start the client: $!", $tests;
            my $server = $daemon->accept()
              or skip_fail "Proxy didn't connect: $!", $tests;

            # the server will first play the role of the web proxy,
            # and after a 200 OK will also act as a real server

            # check the request
            my $req = $server->get_request();
            is( $req->method(), 'CONNECT', 'Proxy did a CONNECT' );
            is( $req->uri()->host_port(), 'zlonk.crunch.com:443', 'host:port' );

            # first time, the web proxy says 200
            $server->send_response( HTTP::Response->new('200') );

            # now the client will use SSL
            IO::Socket::SSL->start_SSL(
                $server,
                SSL_server    => 1,
                SSL_cert_file => catfile( 't', 'test.cert' ),
                SSL_key_file  => catfile( 't', 'test.key' ),
            );

            # send some data through
            # FIXME this blocks when $server speaks first
            for my $line (@lines) {
                print $client $line;
                is( <$server>, $line, "Line received" );
                ( $client, $server ) = random_swap( $client, $server );
            }
            $client->close();
            $server->close();

            # second time, the web proxy says 403 (how to test this?)
            $client = connect_to_port($proxy_port)
              or skip_fail "Couldn't start the client: $!", 1;
            $server = $daemon->accept()
              or skip_fail "Proxy didn't connect: $!", 1;

            $server->get_request(); # ignore it
            $server->send_response( HTTP::Response->new('403') );
            is_closed($client);
        }
    }
}