Debian Codegraph @

tests/Test-https-badcerts.px

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