Debian Codegraph @

crt/s2n/tests/integrationv2/test_buffered_send.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3 import pytest
4
5 from configuration import (
6 available_ports,
7 PROTOCOLS,
8 MINIMAL_TEST_CIPHERS,
9 MINIMAL_TEST_CERTS,
10 )
11 from common import ProviderOptions, data_bytes
12 from fixtures import managed_process # noqa: F401
13 from providers import Provider, S2N, OpenSSL, GnuTLS
14 from utils import invalid_test_parameters, get_parameter_name, to_bytes, to_string
15
16 SEND_DATA_SIZE = 2**14
17
18 # CLOSE_MARKER must a substring of SEND_DATA exactly once, and must be its suffix
19 CLOSE_MARKER = "unique-suffix-close-marker"
20
21 SEND_DATA = data_bytes(SEND_DATA_SIZE - len(CLOSE_MARKER)) + to_bytes(CLOSE_MARKER)
22 SEND_DATA_STRING = to_string(SEND_DATA)
23
24 K_BYTES = 1024
25 SEND_BUFFER_SIZE_MIN = 1034
26 SEND_BUFFER_SIZE_MIN_RECOMMENDED = 2 * K_BYTES
27 SEND_BUFFER_SIZE_MULTI_RECORD = 17 * K_BYTES
28 SEND_BUFFER_SIZE_PREFER_THROUGHPUT = 35 * K_BYTES
29 SEND_BUFFER_SIZE_HUGE = 512 * K_BYTES
30
31 SEND_BUFFER_SIZES = [
32 SEND_BUFFER_SIZE_MIN,
33 SEND_BUFFER_SIZE_MIN_RECOMMENDED,
34 SEND_BUFFER_SIZE_MULTI_RECORD,
35 SEND_BUFFER_SIZE_PREFER_THROUGHPUT,
36 SEND_BUFFER_SIZE_HUGE,
37 ]
38
39 FRAGMENT_PREFERENCE = [None, "--prefer-low-latency", "--prefer-throughput"]
40
41
42 def test_SEND_BUFFER_SIZE_MIN_is_s2ns_min_buffer_size(managed_process): # noqa: F811
43 port = next(available_ports)
44
45 s2n_options = ProviderOptions(
46 mode=Provider.ServerMode,
47 port=port,
48 data_to_send="test",
49 extra_flags=["--buffered-send", SEND_BUFFER_SIZE_MIN],
50 )
51
52 s2nd = managed_process(S2N, s2n_options)
53
54 s2n_options.mode = Provider.ClientMode
55 s2n_options.extra_flags = ["--buffered-send", SEND_BUFFER_SIZE_MIN - 1]
56 s2nc = managed_process(S2N, s2n_options)
57
58 for results in s2nc.get_results():
59 assert "Error setting send buffer size" in str(results.stderr)
60 assert results.exit_code != 0
61
62 for results in s2nd.get_results():
63 assert results.exit_code != 0
64
65
66 @pytest.mark.uncollect_if(func=invalid_test_parameters)
67 @pytest.mark.parametrize("cipher", MINIMAL_TEST_CIPHERS, ids=get_parameter_name)
68 @pytest.mark.parametrize("provider", [GnuTLS, OpenSSL, S2N], ids=get_parameter_name)
69 @pytest.mark.parametrize("protocol", PROTOCOLS, ids=get_parameter_name)
70 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
71 @pytest.mark.parametrize("buffer_size", SEND_BUFFER_SIZES, ids=get_parameter_name)
72 @pytest.mark.parametrize(
73 "fragment_preference", FRAGMENT_PREFERENCE, ids=get_parameter_name
74 )
75 def test_s2n_server_buffered_send(
76 managed_process, # noqa: F811
77 cipher,
78 provider,
79 protocol,
80 certificate,
81 buffer_size,
82 fragment_preference,
83 ):
84 # Communication Timeline
85 # Client [S2N|OpenSSL|GnuTLS] | Server [S2N]
86 # Handshake | Handshake
87 # | Send SEND_DATA (with CLOSE_MARKER)
88 # Closes with CLOSE_MARKER | Close
89 port = next(available_ports)
90
91 provider_client_options = ProviderOptions(
92 mode=Provider.ClientMode,
93 port=port,
94 cipher=cipher,
95 data_to_send=None,
96 insecure=True,
97 protocol=protocol,
98 verbose=False,
99 )
100
101 extra_flags = ["--buffered-send", buffer_size]
102 if fragment_preference is not None:
103 extra_flags.append(fragment_preference)
104
105 s2n_server_options = ProviderOptions(
106 mode=Provider.ServerMode,
107 port=port,
108 cipher=cipher,
109 data_to_send=SEND_DATA,
110 insecure=True,
111 protocol=protocol,
112 key=certificate.key,
113 cert=certificate.cert,
114 extra_flags=extra_flags,
115 )
116
117 server = managed_process(
118 S2N, s2n_server_options, send_marker=[S2N.get_send_marker()]
119 )
120 client = managed_process(
121 provider, provider_client_options, close_marker=CLOSE_MARKER
122 )
123
124 for results in client.get_results():
125 assert SEND_DATA_STRING in str(results.stdout)
126 results.assert_success()
127
128 for results in server.get_results():
129 # the server should close without error
130 # but there is otherwise nothing of interest on stdout
131 results.assert_success()
132
133
134 @pytest.mark.uncollect_if(func=invalid_test_parameters)
135 @pytest.mark.parametrize("cipher", MINIMAL_TEST_CIPHERS, ids=get_parameter_name)
136 @pytest.mark.parametrize("provider", [S2N, OpenSSL], ids=get_parameter_name)
137 @pytest.mark.parametrize("protocol", PROTOCOLS, ids=get_parameter_name)
138 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
139 @pytest.mark.parametrize("buffer_size", SEND_BUFFER_SIZES, ids=get_parameter_name)
140 @pytest.mark.parametrize(
141 "fragment_preference", FRAGMENT_PREFERENCE, ids=get_parameter_name
142 )
143 def test_s2n_client_buffered_send(
144 managed_process, # noqa: F811
145 cipher,
146 provider,
147 protocol,
148 certificate,
149 buffer_size,
150 fragment_preference,
151 ):
152 # Communication Timeline
153 # Client [S2N] | Server [S2N|OpenSSL]
154 # Handshake | Handshake
155 # Send SEND_DATA (with CLOSE_MARKER) | Receive the Data Bytes
156 # Close | Close on CLOSE_MARKER
157 port = next(available_ports)
158
159 extra_flags = ["--buffered-send", buffer_size]
160 if fragment_preference is not None:
161 extra_flags.append(fragment_preference)
162
163 s2n_client_options = ProviderOptions(
164 mode=Provider.ClientMode,
165 port=port,
166 cipher=cipher,
167 data_to_send=SEND_DATA,
168 insecure=True,
169 protocol=protocol,
170 extra_flags=extra_flags,
171 )
172
173 provider_server_options = ProviderOptions(
174 mode=Provider.ServerMode,
175 port=port,
176 cipher=cipher,
177 insecure=True,
178 protocol=protocol,
179 key=certificate.key,
180 cert=certificate.cert,
181 verbose=False,
182 )
183
184 server = managed_process(
185 provider, provider_server_options, close_marker=CLOSE_MARKER
186 )
187 client = managed_process(S2N, s2n_client_options)
188
189 for results in client.get_results():
190 results.assert_success()
191
192 for results in server.get_results():
193 assert SEND_DATA_STRING in str(results.stdout)
194 results.assert_success()