xref: /freebsd/crypto/openssl/test/recipes/82-test_tfo_cli.t (revision e7be843b4a162e68651d3911f0357ed464915629)
1*e7be843bSPierre Pronchery#! /usr/bin/env perl
2*e7be843bSPierre Pronchery# Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery#
4*e7be843bSPierre Pronchery# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e7be843bSPierre Pronchery# this file except in compliance with the License.  You can obtain a copy
6*e7be843bSPierre Pronchery# in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery# https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery
9*e7be843bSPierre Proncheryuse strict;
10*e7be843bSPierre Proncheryuse warnings;
11*e7be843bSPierre Pronchery
12*e7be843bSPierre Proncheryuse IPC::Open2;
13*e7be843bSPierre Proncheryuse OpenSSL::Test qw/:DEFAULT srctop_file bldtop_file/;
14*e7be843bSPierre Proncheryuse OpenSSL::Test::Utils;
15*e7be843bSPierre Pronchery
16*e7be843bSPierre Proncherysetup("test_tfo");
17*e7be843bSPierre Pronchery
18*e7be843bSPierre Proncheryplan skip_all => "test_tfo_cli needs tfo enabled" if disabled("tfo");
19*e7be843bSPierre Proncheryplan skip_all => "test_tfo_cli needs sock enabled" if disabled("sock");
20*e7be843bSPierre Proncheryplan skip_all => "test_tfo_cli needs tls < 1.3 enabled"
21*e7be843bSPierre Pronchery    if disabled("tls1") && disabled("tls1_1") && disabled("tls1_2");
22*e7be843bSPierre Proncheryplan skip_all => "test_tfo_cli does not run on Windows nor VMS"
23*e7be843bSPierre Pronchery    if $^O =~ /^(VMS|MSWin32|msys)$/;
24*e7be843bSPierre Pronchery
25*e7be843bSPierre Proncheryplan tests => 8;
26*e7be843bSPierre Pronchery
27*e7be843bSPierre Proncherymy $shlib_wrap = bldtop_file("util", "shlib_wrap.sh");
28*e7be843bSPierre Proncherymy $apps_openssl = bldtop_file("apps", "openssl");
29*e7be843bSPierre Proncherymy $cert = srctop_file("apps", "server.pem");
30*e7be843bSPierre Pronchery
31*e7be843bSPierre Proncherysub run_test {
32*e7be843bSPierre Pronchery    my $tfo = shift;
33*e7be843bSPierre Pronchery
34*e7be843bSPierre Pronchery    my $client_good = ! $tfo;
35*e7be843bSPierre Pronchery    my $server_good = ! $tfo;
36*e7be843bSPierre Pronchery    my $connect_good = 0;
37*e7be843bSPierre Pronchery    my $port = "0";
38*e7be843bSPierre Pronchery
39*e7be843bSPierre Pronchery    # Not using TLSv1.3 allows the test to work with "no-ec"
40*e7be843bSPierre Pronchery    my @s_cmd = ("s_server", "-accept", ":0", "-cert", $cert, "-www", "-no_tls1_3", "-naccept", "1");
41*e7be843bSPierre Pronchery    push @s_cmd, "-tfo" if ($tfo);
42*e7be843bSPierre Pronchery
43*e7be843bSPierre Pronchery    my $spid = open2(my $sout, my $sin, $shlib_wrap, $apps_openssl, @s_cmd);
44*e7be843bSPierre Pronchery
45*e7be843bSPierre Pronchery    # Read until we get the port, TFO is output before the ACCEPT line
46*e7be843bSPierre Pronchery    while (<$sout>) {
47*e7be843bSPierre Pronchery        chomp;
48*e7be843bSPierre Pronchery        $server_good = $tfo if /^Listening for TFO$/;
49*e7be843bSPierre Pronchery        if (/^ACCEPT\s.*:(\d+)$/) {
50*e7be843bSPierre Pronchery            $port = $1;
51*e7be843bSPierre Pronchery            last;
52*e7be843bSPierre Pronchery        }
53*e7be843bSPierre Pronchery    }
54*e7be843bSPierre Pronchery    print STDERR "Port: $port\n";
55*e7be843bSPierre Pronchery    print STDERR "Invalid port\n" if ! ok($port);
56*e7be843bSPierre Pronchery
57*e7be843bSPierre Pronchery    # Start up the client
58*e7be843bSPierre Pronchery    my @c_cmd = ("s_client", "-connect", ":$port", "-no_tls1_3");
59*e7be843bSPierre Pronchery    push @c_cmd, "-tfo" if ($tfo);
60*e7be843bSPierre Pronchery
61*e7be843bSPierre Pronchery    my $cpid = open2(my $cout, my $cin, $shlib_wrap, $apps_openssl, @c_cmd);
62*e7be843bSPierre Pronchery
63*e7be843bSPierre Pronchery    # Do the "GET", which will cause the client to finish
64*e7be843bSPierre Pronchery    print $cin "GET /\r\n";
65*e7be843bSPierre Pronchery
66*e7be843bSPierre Pronchery    waitpid($cpid, 0);
67*e7be843bSPierre Pronchery    waitpid($spid, 0);
68*e7be843bSPierre Pronchery
69*e7be843bSPierre Pronchery    # Check the client output
70*e7be843bSPierre Pronchery    while (<$cout>) {
71*e7be843bSPierre Pronchery        chomp;
72*e7be843bSPierre Pronchery        $client_good = $tfo if /^Connecting via TFO$/;
73*e7be843bSPierre Pronchery        $connect_good = 1 if /^Content-type: text/;
74*e7be843bSPierre Pronchery    }
75*e7be843bSPierre Pronchery
76*e7be843bSPierre Pronchery    print STDERR "Client TFO check failed\n" if ! ok($client_good);
77*e7be843bSPierre Pronchery    print STDERR "Server TFO check failed\n" if ! ok($server_good);
78*e7be843bSPierre Pronchery    print STDERR "Connection failed\n" if ! ok($connect_good);
79*e7be843bSPierre Pronchery}
80*e7be843bSPierre Pronchery
81*e7be843bSPierre Proncheryfor my $tfo (0..1) {
82*e7be843bSPierre Pronchery    SKIP:
83*e7be843bSPierre Pronchery    {
84*e7be843bSPierre Pronchery        skip "TFO not enabled", 4 if disabled("tfo") && $tfo;
85*e7be843bSPierre Pronchery
86*e7be843bSPierre Pronchery        run_test($tfo);
87*e7be843bSPierre Pronchery    }
88*e7be843bSPierre Pronchery}
89