Home | History | Annotate | Line # | Download | only in recipes
70-test_npn.t revision 1.1.1.1
      1 #! /usr/bin/env perl
      2 # Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
      3 #
      4 # Licensed under the Apache License 2.0 (the "License").  You may not use
      5 # this file except in compliance with the License.  You can obtain a copy
      6 # in the file LICENSE in the source distribution or at
      7 # https://www.openssl.org/source/license.html
      8 
      9 use strict;
     10 use OpenSSL::Test qw/:DEFAULT cmdstr srctop_file/;
     11 use OpenSSL::Test::Utils;
     12 
     13 use TLSProxy::Proxy;
     14 
     15 my $test_name = "test_npn";
     16 setup($test_name);
     17 
     18 plan skip_all => "TLSProxy isn't usable on $^O"
     19     if $^O =~ /^(VMS)$/;
     20 
     21 plan skip_all => "$test_name needs the dynamic engine feature enabled"
     22     if disabled("engine") || disabled("dynamic-engine");
     23 
     24 plan skip_all => "$test_name needs the sock feature enabled"
     25     if disabled("sock");
     26 
     27 plan skip_all => "$test_name needs NPN enabled"
     28     if disabled("nextprotoneg");
     29 
     30 plan skip_all => "$test_name needs TLSv1.2 enabled"
     31     if disabled("tls1_2");
     32 
     33 my $proxy = TLSProxy::Proxy->new(
     34     undef,
     35     cmdstr(app(["openssl"]), display => 1),
     36     srctop_file("apps", "server.pem"),
     37     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
     38 );
     39 
     40 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
     41 plan tests => 1;
     42 
     43 my $npnseen = 0;
     44 
     45 # Test 1: Check sending an empty NextProto message from the client works. This is
     46 #         valid as per the spec, but OpenSSL does not allow you to send it.
     47 #         Therefore we must be prepared to receive such a message but we cannot
     48 #         generate it except via TLSProxy
     49 $proxy->clear();
     50 $proxy->filter(\&npn_filter);
     51 $proxy->clientflags("-nextprotoneg foo -no_tls1_3");
     52 $proxy->serverflags("-nextprotoneg foo");
     53 $proxy->start();
     54 ok($npnseen && TLSProxy::Message->success(), "Empty NPN message");
     55 
     56 sub npn_filter
     57 {
     58     my $proxy = shift;
     59     my $message;
     60 
     61     # The NextProto message always appears in flight 2
     62     return if $proxy->flight != 2;
     63 
     64     foreach my $message (@{$proxy->message_list}) {
     65         if ($message->mt == TLSProxy::Message::MT_NEXT_PROTO) {
     66             # Our TLSproxy NextProto message support doesn't support parsing of
     67             # the message. If we repack it just creates an empty NextProto
     68             # message - which is exactly the scenario we want to test here.
     69             $message->repack();
     70             $npnseen = 1;
     71         }
     72     }
     73 }
     74