Debian Codegraph @

tests/Test-proxied-https-auth-keepalive.px

raw
1 #!/usr/bin/env perl
2 # Simulate a tunneling proxy to a HTTPS URL that needs authentication.
3 # Use a persistent connection (Connection: keep-alive)
4
5 use strict;
6 use warnings;
7
8 use WgetFeature qw(https);
9 use WgetTests; # For $WGETPATH.
10
11 my $cert_path;
12 my $key_path;
13 my $srcdir;
14
15 our $VALGRIND_SUPP_FILE;
16
17 if (@ARGV) {
18 $srcdir = shift @ARGV;
19 } elsif (defined $ENV{srcdir}) {
20 $srcdir = $ENV{srcdir};
21 }
22
23 if (defined $srcdir) {
24 $key_path = "$srcdir/certs/server-key.pem";
25 $cert_path = "$srcdir/certs/server-cert.pem";
26 } else {
27 $key_path = "certs/server-key.pem";
28 $cert_path = "certs/server-cert.pem";
29 }
30
31
32 use HTTP::Daemon;
33 use HTTP::Request;
34 # Skip this test rather than fail it when the module isn't installed
35 if (!eval {require IO::Socket::SSL;1;}) {
36 print STDERR "This test needs the perl module \"IO::Socket::SSL\".\n";
37 print STDERR "Install e.g. on Debian with 'apt-get install libio-socket-ssl-perl'\n";
38 print STDERR " or if using cpanminus 'cpanm IO::Socket::SSL' could be used to install it.\n";
39 exit 77; # skip
40 }
41 IO::Socket::SSL->import();
42
43 my $SOCKET = HTTP::Daemon->new (LocalAddr => 'localhost',
44 ReuseAddr => 1) or die "Cannot create server!!!";
45
46 sub get_request {
47 my $conn = shift;
48 my $content = '';
49 my $line;
50
51 while (defined ($line = <$conn>)) {
52 $content .= $line;
53 last if $line eq "\r\n";
54 }
55
56 my $rqst = HTTP::Request->parse($content)
57 or die "Couldn't parse request:\n$content\n";
58
59 return $rqst;
60 }
61
62 sub do_server {
63 my ($synch_callback) = @_;
64 my $s = $SOCKET;
65 my $conn;
66 my $rqst;
67 my $rspn;
68
69 # sync with the parent
70 $synch_callback->();
71
72 # Simulate a HTTPS proxy server with tunneling.
73
74 $conn = $s->accept;
75 $rqst = $conn->get_request;
76 die "Method not CONNECT\n" if ($rqst->method ne 'CONNECT');
77 $rspn = HTTP::Response->new(200, 'OK');
78 $conn->send_response($rspn);
79
80 # Now switch from plain to SSL (for simulating a transparent tunnel
81 # to an HTTPS server).
82
83 my %options = (
84 SSL_server => 1,
85 SSL_passwd_cb => sub { return "Hello"; });
86 $options{SSL_cert_file} = $cert_path if ($cert_path);
87 $options{SSL_key_file} = $key_path if ($key_path);
88 my @options = %options;
89 $conn = IO::Socket::SSL->new_from_fd($conn->fileno, @options)
90 or die "Couldn't initiate SSL";
91
92 for my $expect_inner_auth (0, 1) {
93 # TODO: expect no auth the first time, request it, expect it the second
94 # time.
95
96 $rqst = &get_request($conn)
97 or die "Didn't get proxied request\n";
98
99 unless ($expect_inner_auth) {
100 die "Early proxied auth\n" if $rqst->header('Authorization');
101
102 $rspn = HTTP::Response->new(401, 'Unauthorized', [
103 'WWW-Authenticate' => 'Basic realm="gondor"',
104 Connection => 'keep-alive'
105 ]);
106 } else {
107 die "No proxied auth\n" unless $rqst->header('Authorization');
108
109 $rspn = HTTP::Response->new(200, 'OK', [
110 'Content-Type' => 'text/plain',
111 'Connection' => 'close',
112 ], "foobarbaz\n");
113 }
114
115 $rspn->protocol('HTTP/1.0');
116 print STDERR "=====\n";
117 print STDERR $rspn->as_string;
118 print STDERR "\n=====\n";
119 print $conn $rspn->as_string;
120 }
121
122 $conn->close;
123 undef $conn;
124 undef $s;
125 }
126
127 sub fork_server {
128 pipe(FROM_CHILD, TO_PARENT) or die "Cannot create pipe!";
129 select((select(TO_PARENT), $| = 1)[0]);
130
131 my $pid = fork();
132 if ($pid < 0) {
133 die "Cannot fork";
134 } elsif ($pid == 0) {
135 # child
136 close FROM_CHILD;
137 do_server(sub { print TO_PARENT "SYNC\n"; close TO_PARENT });
138 exit 0;
139 } else {
140 # parent
141 close TO_PARENT;
142 chomp(my $line = <FROM_CHILD>);
143 close FROM_CHILD;
144 }
145
146 return $pid;
147 }
148
149 unlink "needs-auth.txt";
150 my $pid = &fork_server;
151
152 my $cmdline = $WgetTest::WGETPATH . " --user=fiddle-dee-dee"
153 . " --password=Dodgson -e https_proxy=localhost:{{port}}"
154 . " --no-check-certificate"
155 . " https://no.such.domain/needs-auth.txt";
156 $cmdline =~ s/\Q{{port}}/$SOCKET->sockport()/e;
157
158 if (defined $srcdir) {
159 $VALGRIND_SUPP_FILE = $srcdir . '/valgrind-suppressions-ssl';
160 } else {
161 $VALGRIND_SUPP_FILE = './valgrind-suppressions-ssl';
162 }
163
164 my $valgrind = $ENV{VALGRIND_TESTS};
165 if (!defined $valgrind || $valgrind eq "" || $valgrind == 0) {
166 # Valgrind not requested - leave $cmdline as it is
167 } elsif ($valgrind == 1) {
168 $cmdline =
169 'valgrind --suppressions=' . $VALGRIND_SUPP_FILE
170 . ' --error-exitcode=301 --leak-check=yes --track-origins=yes '
171 . $cmdline;
172 } else {
173 $cmdline = $valgrind . " " . $cmdline;
174 }
175
176 my $code = system($cmdline . " 2>&1") >> 8;
177 unlink "needs-auth.txt";
178
179 warn "Got code: $code\n" if $code;
180 kill ('TERM', $pid);
181 exit ($code != 0);