81-test_cmp_cli.t: Do connections to 127.0.0.1 (e.g., Mock server) without proxy
[openssl.git] / test / recipes / 70-test_comp.t
1 #! /usr/bin/env perl
2 # Copyright 2017-2020 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 srctop_dir bldtop_dir/;
11 use OpenSSL::Test::Utils;
12 use File::Temp qw(tempfile);
13 use TLSProxy::Proxy;
14
15 my $test_name = "test_comp";
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 TLSv1.3 or TLSv1.2 enabled"
28     if disabled("tls1_3") && disabled("tls1_2");
29
30 $ENV{OPENSSL_ia32cap} = '~0x200000200000000';
31
32 use constant {
33     MULTIPLE_COMPRESSIONS => 0,
34     NON_NULL_COMPRESSION => 1
35 };
36 my $testtype;
37
38 my $proxy = TLSProxy::Proxy->new(
39     undef,
40     cmdstr(app(["openssl"]), display => 1),
41     srctop_file("apps", "server.pem"),
42     (!$ENV{HARNESS_ACTIVE} || $ENV{HARNESS_VERBOSE})
43 );
44
45 $proxy->start() or plan skip_all => "Unable to start up Proxy for tests";
46 plan tests => 4;
47
48 SKIP: {
49     skip "TLSv1.2 disabled", 2 if disabled("tls1_2");
50     #Test 1: Check that sending multiple compression methods in a TLSv1.2
51     #        ClientHello succeeds
52     $proxy->clear();
53     $proxy->filter(\&add_comp_filter);
54     $proxy->clientflags("-no_tls1_3");
55     $testtype = MULTIPLE_COMPRESSIONS;
56     $proxy->start();
57     ok(TLSProxy::Message->success(), "Non null compression");
58
59     #Test 2: NULL compression method must be present in TLSv1.2
60     $proxy->clear();
61     $proxy->clientflags("-no_tls1_3");
62     $testtype = NON_NULL_COMPRESSION;
63     $proxy->start();
64     ok(TLSProxy::Message->fail(), "NULL compression missing");
65 }
66
67 SKIP: {
68     skip "TLSv1.3 disabled", 2 if disabled("tls1_3");
69     #Test 3: Check that sending multiple compression methods in a TLSv1.3
70     #        ClientHello fails
71     $proxy->clear();
72     $proxy->filter(\&add_comp_filter);
73     $testtype = MULTIPLE_COMPRESSIONS;
74     $proxy->start();
75     ok(TLSProxy::Message->fail(), "Non null compression (TLSv1.3)");
76
77     #Test 4: NULL compression method must be present in TLSv1.3
78     $proxy->clear();
79     $testtype = NON_NULL_COMPRESSION;
80     $proxy->start();
81     ok(TLSProxy::Message->fail(), "NULL compression missing (TLSv1.3)");
82 }
83
84 sub add_comp_filter
85 {
86     my $proxy = shift;
87     my $flight;
88     my $message;
89     my @comp;
90
91     # Only look at the ClientHello
92     return if $proxy->flight != 0;
93
94     $message = ${$proxy->message_list}[0];
95
96     return if (!defined $message
97                || $message->mt != TLSProxy::Message::MT_CLIENT_HELLO);
98
99     if ($testtype == MULTIPLE_COMPRESSIONS) {
100         @comp = (
101             0x00, #Null compression method
102             0xff); #Unknown compression
103     } elsif ($testtype == NON_NULL_COMPRESSION) {
104         @comp = (0xff); #Unknown compression
105     }
106     $message->comp_meths(\@comp);
107     $message->comp_meth_len(scalar @comp);
108     $message->repack();
109 }