File: 210_live_ssl.t

package info (click to toggle)
libhttp-tiny-perl 0.090-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: perl: 3,124; makefile: 6
file content (151 lines) | stat: -rw-r--r-- 4,830 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!perl

use strict;
use warnings;

use Test::More 0.96;
use IO::Socket::INET;

use HTTP::Tiny;

plan skip_all => 'Only run for $ENV{AUTOMATED_TESTING}'
  unless $ENV{AUTOMATED_TESTING};

plan skip_all => "Only run if HTTP::Tiny->can_ssl()"
  unless HTTP::Tiny->can_ssl();

delete $ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT};

use IPC::Cmd qw/can_run/;

if ( can_run('openssl') ) {
  diag "\nNote: running test with ", qx/openssl version/;
}

test_ssl('https://cpan.org/' => {
    host => 'cpan.org',
    pass => { verify_SSL => 1 },
    fail => { verify_SSL => 1, SSL_options => { SSL_ca_file => "corpus/snake-oil.crt" } },
    default_verify_should_return => !!1,
});

test_ssl('https://github.com/' => {
    host => 'github.com',
    pass => { verify_SSL => 1 },
    fail => { verify_SSL => 1, SSL_options => { SSL_ca_file => "corpus/snake-oil.crt" } },
    default_verify_should_return => !!1,
});

test_ssl('https://wrong.host.badssl.com/' => {
    host => 'wrong.host.badssl.com',
    pass => { SSL_options => { SSL_verifycn_scheme => 'none', SSL_verifycn_name => 'wrong.host.badssl.com', SSL_verify_mode => 0x00 } },
    fail => { SSL_options => { SSL_verifycn_scheme => 'http', SSL_verifycn_name => 'wrong.host.badssl.com', SSL_verify_mode => 0x01 } },
    default_verify_should_return => !!0,
});

test_ssl('https://untrusted-root.badssl.com/' => {
    host => 'untrusted-root.badssl.com',
    pass => { verify_SSL => 0 },
    fail => { verify_SSL => 1 },
    default_verify_should_return => !!0,
});

test_ssl('https://mozilla-modern.badssl.com/' => {
    host => 'mozilla-modern.badssl.com',
    pass => { verify_SSL => 1 },
    fail => { verify_SSL => 1, SSL_options => { SSL_ca_file => "corpus/snake-oil.crt" } },
    default_verify_should_return => !!1,
});

{
    local $ENV{PERL_HTTP_TINY_SSL_INSECURE_BY_DEFAULT} = 1;
    test_ssl('https://wrong.host.badssl.com/' => {
        host => 'wrong.host.badssl.com',
        pass => { verify_SSL => 0 },
        fail => { verify_SSL => 1 },
        default_verify_should_return => !!1,
    });
    test_ssl('https://expired.badssl.com/' => {
        host => 'expired.badssl.com',
        pass => { verify_SSL => 0 },
        fail => { verify_SSL => 1 },
        default_verify_should_return => !!1,
    });

}

test_ssl('https://wrong.host.badssl.com/' => {
    host => 'wrong.host.badssl.com',
    pass => { verify_SSL => 0 },
    fail => { verify_SSL => 1 },
    default_verify_should_return => !!0,
});

test_ssl('https://expired.badssl.com/' => {
    host => 'expired.badssl.com',
    pass => { verify_SSL => 0 },
    fail => { verify_SSL => 1 },
    default_verify_should_return => !!0,
});



subtest "can_ssl" => sub {
    ok( HTTP::Tiny->can_ssl, "class method" );
    ok( HTTP::Tiny->new->can_ssl, "object method, default params" );
    ok( HTTP::Tiny->new(verify_SSL => 1)->can_ssl, "object method, verify_SSL" );

    my $ht = HTTP::Tiny->new(
        verify_SSL => 1,
        SSL_options => { SSL_ca_file => 'adlfadkfadlfad' },
    );
    my ($ok, $why) = $ht->can_ssl;
    ok( ! $ok, "object methods, verify_SSL, bogus CA file (FAILS)" );
    like( $why, qr/not found or not readable/, "failure reason" );
};

done_testing();

sub test_ssl {
    my ($url, $data) = @_;
    subtest $url => sub {
        plan 'skip_all' => 'Internet connection timed out'
            unless IO::Socket::INET->new(
                PeerHost  => $data->{host},
                PeerPort  => 443,
                Proto     => 'tcp',
                Timeout   => 10,
        );

        # the default verification
        my $response = HTTP::Tiny->new()->get($url);
        is $response->{success}, $data->{default_verify_should_return}, "Request to $url passed/failed using default as expected"
            or do {
                # $response->{content} = substr $response->{content}, 0, 50;
                $response->{content} =~ s{\n.*}{}s;
                diag explain [IO::Socket::SSL::errstr(), $response]
            };

        # force validation to succeed
        if ($data->{pass}) {
            my $pass = HTTP::Tiny->new( %{$data->{pass}} )->get($url);
            isnt $pass->{status}, '599', "Request to $url completed (forced pass)"
              or do {
                  $pass->{content} =~ s{\n.*}{}s;
                  diag explain $pass
              };
            ok $pass->{content}, 'Got some content';
        }

        # force validation to fail
        if ($data->{fail}) {
            my $fail = HTTP::Tiny->new( %{$data->{fail}} )->get($url);
            is $fail->{status}, '599', "Request to $url failed (forced fail)"
              or do {
                  $fail->{content} =~ s{\n.*}{}s;
                  diag explain [IO::Socket::SSL::errstr(), $fail]
              };
            ok $fail->{content}, 'Got some content';
        }
    };
}