summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/t/40session-ticket.t
blob: 2e5d5e4ac76542a90197487d0c2d6be98ba45098 (plain)
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
use strict;
use warnings;
use File::Temp qw(tempdir);
use Net::EmptyPort qw(check_port empty_port);
use Test::More;
use t::Util;

plan skip_all => "could not find openssl"
    unless prog_exists("openssl");
#plan skip_all => "openssl 1.0.2 or above is required"
#    unless `openssl version` =~ /^OpenSSL 1\.(?:0\.[2-9][^0-9]|[1-9])/s;

my $tempdir = tempdir(CLEANUP => 1);

subtest "internal" => sub {
    spawn_with(<< "EOT",
  mode: ticket
EOT
    sub {
        is test(), "New";
        test(); # openssl 0.9.8 seems to return "New" (maybe because in the first run we did not specify -sess_in)
        is test(), "Reused";
        is test(), "Reused";
    });
    spawn_with(<< "EOT",
  mode: ticket
EOT
    sub {
        is test(), "New";
    });
};

subtest "file" => sub {
    my $tickets_file = "t/40session-ticket/forever_ticket.yaml";
    spawn_with(<< "EOT",
  mode: ticket
  ticket-store: file
  ticket-file: $tickets_file
EOT
    sub {
        is test(), "New";
        is test(), "Reused";
        is test(), "Reused";
    });
    spawn_with(<< "EOT",
  mode: ticket
  ticket-store: file
  ticket-file: $tickets_file
EOT
    sub {
        sleep 1;
        is test(), "Reused";
    });
};

subtest "no-tickets-in-file" => sub {
    my $tickets_file = "t/40session-ticket/nonexistent";
    spawn_with(<< "EOT",
  mode: ticket
  ticket-store: file
  ticket-file: $tickets_file
EOT
    sub {
        is test(), "New";
        is test(), "New";
        is test(), "New";
    });
};

subtest "memcached" => sub {
    plan skip_all => "memcached not found"
        unless prog_exists("memcached");
    my $memc_port = empty_port();
    my $doit = sub {
        my $memc_proto = shift;
        my $memc_guard = spawn_server(
            argv     => [ qw(memcached -l 127.0.0.1 -p), $memc_port, "-B", $memc_proto ],
            is_ready => sub {
                check_port($memc_port);
            },
        );
        my $conf =<< "EOT";
  mode: ticket
  ticket-store: memcached
  memcached:
    host: 127.0.0.1
    port: $memc_port
    protocol: $memc_proto
EOT
        spawn_with($conf, sub {
            is test(), "New";
            is test(), "Reused";
            is test(), "Reused";
        });
        spawn_with($conf, sub {
            sleep 1;
            is test(), "Reused";
        });
    };
    $doit->("binary");
    $doit->("ascii");
};

done_testing;

my $server;

sub spawn_with {
    my ($opts, $cb) = @_;
    $server = spawn_h2o(<< "EOT");
ssl-session-resumption:
$opts
hosts:
  default:
    paths:
      /:
        file.dir: @{[ DOC_ROOT ]}
EOT
    $cb->();
}

sub test {
    my $lines = do {
        my $cmd_opts = (-e "$tempdir/session" ? "-sess_in $tempdir/session" : "") . " -sess_out $tempdir/session";
        open my $fh, "-|", "openssl s_client $cmd_opts -connect 127.0.0.1:$server->{tls_port} 2>&1 < /dev/null"
            or die "failed to open pipe:$!";
        local $/;
        <$fh>;
    };
    $lines =~ m{---\n(New|Reused),}s
        or die "failed to parse the output of s_client:{{{$lines}}}";
    $1;
}