Debian Codegraph @

tests/Test-https-selfsigned.px

raw
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use Socket;
6 use WgetFeature qw(https);
7 use SSLTest;
8
9 ###############################################################################
10
11 # code, msg, headers, content
12 my %urls = (
13 '/somefile.txt' => {
14 code => "200",
15 msg => "Dontcare",
16 headers => {
17 "Content-type" => "text/plain",
18 },
19 content => "blabla",
20 },
21 );
22
23 my $srcdir;
24 if (@ARGV) {
25 $srcdir = shift @ARGV;
26 } elsif (defined $ENV{srcdir}) {
27 $srcdir = $ENV{srcdir};
28 }
29 $srcdir = Cwd::abs_path("$srcdir");
30
31 # HOSTALIASES env variable allows us to create hosts file alias.
32 my $testhostname = "WgetTestingServer";
33 $ENV{'HOSTALIASES'} = "$srcdir/certs/wgethosts";
34
35 my $addr = gethostbyname($testhostname);
36 unless ($addr)
37 {
38 warn "Failed to resolve $testhostname, using $srcdir/certs/wgethosts\n";
39 exit 77;
40 }
41 unless (inet_ntoa($addr) =~ "127.0.0.1")
42 {
43 warn "Unexpected IP for localhost: ".inet_ntoa($addr)."\n";
44 exit 77;
45 }
46
47 # Prepare self-signed certificates
48 my $certfile="$srcdir/certs/selfsigned.crt";
49 my $keyfile="$srcdir/certs/selfsigned.key";
50
51 # Try Wget using SSL first without --no-check-certificate. expect error
52 my $port = 26443;
53 my $cmdline = $WgetTest::WGETPATH . " --ca-certificate=$srcdir/certs/test-ca-cert.pem".
54 " https://$testhostname:$port/somefile.txt";
55 my $expected_error_code = 5;
56 my %existing_files = (
57 );
58
59 my %expected_downloaded_files = (
60 'somefile.txt' => {
61 content => "blabla",
62 },
63 );
64
65 my $sslsock = SSLTest->new(cmdline => $cmdline,
66 input => \%urls,
67 errcode => $expected_error_code,
68 existing => \%existing_files,
69 output => \%expected_downloaded_files,
70 certfile => $certfile,
71 keyfile => $keyfile,
72 lhostname => $testhostname,
73 sslport => $port);
74 if ($sslsock->run() == 0)
75 {
76 exit 0;
77 }
78
79 # Retry the test with --no-check-certificate. expect success
80 $port = 27443;
81 $cmdline = $WgetTest::WGETPATH . " --no-check-certificate ".
82 " --ca-certificate=$srcdir/certs/test-ca-cert.pem".
83 " https://$testhostname:$port/somefile.txt";
84
85 $expected_error_code = 0;
86
87 my $retryssl = SSLTest->new(cmdline => $cmdline,
88 input => \%urls,
89 errcode => $expected_error_code,
90 existing => \%existing_files,
91 output => \%expected_downloaded_files,
92 certfile => $certfile,
93 keyfile => $keyfile,
94 lhostname => $testhostname,
95 sslport => $port);
96 exit $retryssl->run();
97 # vim: et ts=4 sw=4