]> sjero.net Git - wget/blob - tests/Test-proxied-https-auth.px
Updated config.guess, config.sub, install.sh.
[wget] / tests / Test-proxied-https-auth.px
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use WgetTest;  # For $WGETPATH.
7
8 my $cert_path;
9 my $key_path;
10
11 if (@ARGV) {
12     my $top_srcdir = shift @ARGV;
13     $key_path = "$top_srcdir/tests/certs/server-key.pem";
14     $cert_path = "$top_srcdir/tests/certs/server-cert.pem";
15 }
16
17 # Have we even built an HTTPS-supporting Wget?
18 {
19     my @version_lines = `${WgetTest::WGETPATH} --version`;
20     unless (grep /\+(openssl|gnutls)/, @version_lines) {
21         print "Not running test: Wget under test doesn't support HTTPS.\n";
22         exit 0;
23     }
24 }
25
26 use HTTP::Daemon;
27 use HTTP::Request;
28 use IO::Socket::SSL;
29
30 my $SOCKET = HTTP::Daemon->new (LocalAddr => 'localhost',
31     ReuseAddr => 1) or die "Cannot create server!!!";
32
33 sub get_request {
34     my $conn = shift;
35     my $content = '';
36     my $line;
37
38     while (defined ($line = <$conn>)) {
39         $content .= $line;
40         last if $line eq "\r\n";
41     }
42
43     my $rqst = HTTP::Request->parse($content)
44         or die "Couldn't parse request:\n$content\n";
45
46     return $rqst;
47 }
48
49 sub do_server {
50     my $alrm = alarm 10;
51
52     my $s = $SOCKET;
53     my $conn;
54     my $rqst;
55     my $rspn;
56     for my $expect_inner_auth (0, 1) {
57         $conn = $s->accept;
58         $rqst = $conn->get_request;
59
60         # TODO: expect no auth the first time, request it, expect it the second
61         #   time.
62
63         die "Method not CONNECT\n" if ($rqst->method ne 'CONNECT');
64         $rspn = HTTP::Response->new(200, 'OK');
65         $conn->send_response($rspn);
66
67         my %options = (
68             SSL_server => 1,
69             SSL_passwd_cb => sub { return "Hello"; });
70
71         $options{SSL_cert_file} = $cert_path if ($cert_path);
72         $options{SSL_key_file} = $key_path if ($key_path);
73
74         my @options = %options;
75
76         $conn = IO::Socket::SSL->new_from_fd($conn->fileno, @options)
77             or die "Couldn't initiate SSL";
78
79         $rqst = &get_request($conn)
80             or die "Didn't get proxied request\n";
81
82         unless ($expect_inner_auth) {
83             die "Early proxied auth\n" if $rqst->header('Authorization');
84
85             # TODO: handle non-persistent connection here.
86             $rspn = HTTP::Response->new(401, 'Unauthorized', [
87                 'WWW-Authenticate' => 'Basic realm="gondor"',
88                 Connection => 'close'
89                 ]);
90             $rspn->protocol('HTTP/1.0');
91             print $rspn->as_string;
92             print $conn $rspn->as_string;
93         } else {
94             die "No proxied auth\n" unless $rqst->header('Authorization');
95
96             $rspn = HTTP::Response->new(200, 'OK', [
97                 'Content-Type' => 'text/plain',
98                 'Connection' => 'close',
99                 ], "foobarbaz\n");
100             $rspn->protocol('HTTP/1.0');
101             print "=====\n";
102             print $rspn->as_string;
103             print "\n=====\n";
104             print $conn $rspn->as_string;
105         }
106         $conn->close;
107     }
108     undef $conn;
109     undef $s;
110     alarm $alrm;
111 }
112
113 sub fork_server {
114     my $pid = fork;
115     die "Couldn't fork" if ($pid < 0);
116     return $pid if $pid;
117
118     &do_server;
119     exit;
120 }
121
122 system ('rm -f needs-auth.txt');
123 my $pid = &fork_server;
124
125 sleep 1;
126 my $cmdline = $WgetTest::WGETPATH . " --user=fiddle-dee-dee"
127     . " --password=Dodgson -e https_proxy=localhost:{{port}}"
128     . " --no-check-certificate"
129     . " https://no.such.domain/needs-auth.txt";
130 $cmdline =~ s/{{port}}/$SOCKET->sockport()/e;
131
132 my $code = system($cmdline);
133 system ('rm -f needs-auth.txt');
134
135 warn "Got code: $code\n" if $code;
136 kill ('TERM', $pid);
137 exit ($code >> 8);