File: client-auth.t

package info (click to toggle)
libtwiggy-tls-perl 0.0020-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: perl: 1,516; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,790 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::TCP;
use Plack::Loader;
use LWP::UserAgent;
use FindBin '$Bin';
use Socket;

my $host       = "localhost";
my $ca_cert    = "$Bin/ca.pem";
my $server_pem = "$Bin/server.pem";
my $client_pem = "$Bin/client.pem";

subtest 'tls connection' => sub {
    my ($success, $content);

    test_tcp(
        client => sub {
            my $port = shift;

            alarm 2;
            local $SIG{ALRM} = sub {die};

            my $ua = LWP::UserAgent->new(
                ssl_opts => {
                    verify_hostname => 1,
                    SSL_ca_file     => $ca_cert,
                    SSL_key_file  => $client_pem,
                    SSL_cert_file => $client_pem,
                }
            );

            my $res = $ua->get("https://$host:$port");
            $success = $res->is_success or die $res->status_line;

            $content = $res->decoded_content;
        },
        server => sub {
            my $port   = shift;
            my $server = Plack::Loader->load(
                'Twiggy::TLS',
                host       => inet_ntoa(inet_aton($host)),
                port       => $port,
                tls_key    => $server_pem,
                tls_cert   => $server_pem,
                tls_ca     => $ca_cert,
                tls_verify => 'on',
            );

            $server->run(
                sub {
                    return [
                        200,
                        ['Content-Type' => 'text/plain'],
                        [shift->{'psgi.tls'}->client_certificate('cn')]
                    ];
                }
            );
        }
    );

    ok $success, "https connection success";
    is $content, 'user@localhost', "content is right";
};

done_testing;