Debian Codegraph @

crt/s2n/tests/integrationv2/test_happy_path.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3 import copy
4 import pytest
5
6 from configuration import (
7 available_ports,
8 ALL_TEST_CIPHERS,
9 ALL_TEST_CURVES,
10 ALL_TEST_CERTS,
11 PROTOCOLS,
12 )
13 from common import ProviderOptions, data_bytes
14 from fixtures import managed_process # noqa: F401
15 from providers import Provider, S2N, OpenSSL, JavaSSL, GnuTLS, SSLv3Provider
16 from utils import (
17 invalid_test_parameters,
18 get_parameter_name,
19 get_expected_s2n_version,
20 to_bytes,
21 )
22
23
24 @pytest.mark.flaky(reruns=5, reruns_delay=2)
25 @pytest.mark.uncollect_if(func=invalid_test_parameters)
26 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
27 @pytest.mark.parametrize("provider", [S2N, OpenSSL, GnuTLS, JavaSSL, SSLv3Provider])
28 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
29 @pytest.mark.parametrize("protocol", PROTOCOLS, ids=get_parameter_name)
30 @pytest.mark.parametrize("certificate", ALL_TEST_CERTS, ids=get_parameter_name)
31 def test_s2n_server_happy_path(
32 managed_process, # noqa: F811
33 cipher,
34 provider,
35 curve,
36 protocol,
37 certificate,
38 ):
39 port = next(available_ports)
40
41 # s2nd can receive large amounts of data because all the data is
42 # echo'd to stdout unmodified. This lets us compare received to
43 # expected easily.
44 # We purposefully send a non block aligned number to make sure
45 # nothing blocks waiting for more data.
46 random_bytes = data_bytes(65519)
47 client_options = ProviderOptions(
48 mode=Provider.ClientMode,
49 port=port,
50 cipher=cipher,
51 cert=certificate.cert,
52 curve=curve,
53 data_to_send=random_bytes,
54 insecure=True,
55 protocol=protocol,
56 )
57
58 server_options = copy.copy(client_options)
59 server_options.data_to_send = None
60 server_options.mode = Provider.ServerMode
61 server_options.key = certificate.key
62 server_options.cert = certificate.cert
63
64 # Passing the type of client and server as a parameter will
65 # allow us to use a fixture to enumerate all possibilities.
66 server = managed_process(S2N, server_options, timeout=5)
67 client = managed_process(provider, client_options, timeout=5)
68
69 # The client will be one of all supported providers. We
70 # just want to make sure there was no exception and that
71 # the client exited cleanly.
72 for client_results in client.get_results():
73 client_results.assert_success()
74
75 expected_version = get_expected_s2n_version(protocol, provider)
76
77 # The server is always S2N in this test, so we can examine
78 # the stdout reliably.
79 for server_results in server.get_results():
80 server_results.assert_success()
81 assert (
82 to_bytes("Actual protocol version: {}".format(expected_version))
83 in server_results.stdout
84 )
85 assert random_bytes in server_results.stdout
86
87 if provider is not S2N:
88 assert (
89 to_bytes("Cipher negotiated: {}".format(cipher.name))
90 in server_results.stdout
91 )
92
93
94 @pytest.mark.flaky(reruns=5, reruns_delay=2)
95 @pytest.mark.uncollect_if(func=invalid_test_parameters)
96 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
97 @pytest.mark.parametrize("provider", [S2N, OpenSSL, GnuTLS, SSLv3Provider])
98 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
99 @pytest.mark.parametrize("protocol", PROTOCOLS, ids=get_parameter_name)
100 @pytest.mark.parametrize("certificate", ALL_TEST_CERTS, ids=get_parameter_name)
101 def test_s2n_client_happy_path(
102 managed_process, # noqa: F811
103 cipher,
104 provider,
105 curve,
106 protocol,
107 certificate,
108 ):
109 port = next(available_ports)
110
111 # We can only send 4096 - 1 (\n at the end) bytes here because of the
112 # way some servers chunk output (when writing to stdout). If we send
113 # 8192 bytes, then openssl will print some debugging information in
114 # the middle of our chunk. We still want that debugging data in case
115 # of a failure, so we just send less data, rather than lose debug
116 # information.
117 random_bytes = data_bytes(4095)
118 client_options = ProviderOptions(
119 mode=Provider.ClientMode,
120 port=port,
121 cipher=cipher,
122 curve=curve,
123 data_to_send=random_bytes,
124 insecure=True,
125 protocol=protocol,
126 )
127
128 server_options = copy.copy(client_options)
129 server_options.data_to_send = None
130 server_options.mode = Provider.ServerMode
131 server_options.key = certificate.key
132 server_options.cert = certificate.cert
133
134 kill_marker = None
135 if provider == GnuTLS:
136 kill_marker = random_bytes
137
138 # Passing the type of client and server as a parameter will
139 # allow us to use a fixture to enumerate all possibilities.
140 server = managed_process(
141 provider, server_options, timeout=5, kill_marker=kill_marker
142 )
143 client = managed_process(S2N, client_options, timeout=5)
144
145 expected_version = get_expected_s2n_version(protocol, provider)
146
147 # The client is always S2N in this test, so we can examine
148 # the stdout reliably.
149 for client_results in client.get_results():
150 client_results.assert_success()
151 assert (
152 to_bytes("Actual protocol version: {}".format(expected_version))
153 in client_results.stdout
154 )
155
156 # The server will be one of all supported providers. We
157 # just want to make sure there was no exception and that
158 # the client exited cleanly.
159 for server_results in server.get_results():
160 server_results.assert_success()
161 # Avoid debugging information that sometimes gets inserted after the first character.
162 assert any(
163 [random_bytes[1:] in stream for stream in server_results.output_streams()]
164 )