| 1 | # Quick start |
| 2 | |
| 3 | You must have run through the standard codebuild setup as described in the root README. This will make sure you |
| 4 | have all the dependencies installed correctly. The integration test dependencies are: |
| 5 | |
| 6 | * s2nc and s2nd (should be in the bin/ directory) |
| 7 | * libs2n (should be in the lib/ directory) |
| 8 | * openssl (based on the S2N_LIBCRYPTO env var) |
| 9 | * tox |
| 10 | * Compiled Java SSLSocketClient for the Java provider |
| 11 | * Compiled an s2nc executable named s2nc_head in the bin directory for the cross compatibility test |
| 12 | |
| 13 | Alternately, you can use the "best effort" mode with `uv`. This will only run the integration tests with the currently available binaries. |
| 14 | ``` |
| 15 | # install uv |
| 16 | curl -LsSf https://astral.sh/uv/install.sh | sh |
| 17 | # run pytest |
| 18 | # -x: exit on the first failure |
| 19 | # -rpfs: print a (r)eport with (p)assing, (f)ailed, and (s)kipped tests. |
| 20 | uv run pytest --provider-version <LINKED_LIBCRYPTO> --best-effort-NOT-FOR-CI -x -rpfs -n auto |
| 21 | ``` |
| 22 | |
| 23 | ## Architecture |
| 24 | ``` |
| 25 | ┌────────────────────────────────────────────────────────────┐ |
| 26 | │ │ |
| 27 | │ Pytest Process │ |
| 28 | │ Managed Managed │ |
| 29 | │ Process (s2n) Process (Ossl) │ |
| 30 | │ │ ▲ │ ▲ │ |
| 31 | └─────┼───────┼─────────────────────────────┼────────┼───────┘ |
| 32 | │ │ │ │ |
| 33 | STDIN STDOUT STDIN STDOUT |
| 34 | │ │ │ │ |
| 35 | │ │ │ │ |
| 36 | ┌──▼───────┼───┐ ┌──▼────────┼────┐ |
| 37 | │ s2nc │◄────────TLS─────────►│ openssl server│ |
| 38 | └──────────────┘ ▲ └────────────────┘ |
| 39 | │ ▲ |
| 40 | │ │ |
| 41 | localhost/socket │ |
| 42 | process |
| 43 | ``` |
| 44 | The integration test harness relies on client and server executables. It coordinates |
| 45 | these through stdin/stdout. |
| 46 | |
| 47 | The above diagram shows an example setup with `s2nc` as the client and `openssl` |
| 48 | as the server. Note that these are just for the purpose of documentation, and the |
| 49 | actual integration tests run with a wide variety of executables. |
| 50 | |
| 51 | ## Run all tests |
| 52 | |
| 53 | The fastest way to run the integrationv2 tests is to run `make` from the S2N root directory. |
| 54 | |
| 55 | ``` |
| 56 | ubuntu@host:s2n_root/ $ make -C tests/integrationv2 all |
| 57 | ``` |
| 58 | |
| 59 | The Makefile automatically sets your PATH and LD_LIBRARY_PATH environment. It will execute `tox` to setup your |
| 60 | Python environment. Then all the integration tests will be collected and executed. |
| 61 | |
| 62 | ## Run one test |
| 63 | |
| 64 | If you only want to run a single test, you can set the `TOX_TEST_NAME` environment variable: |
| 65 | |
| 66 | ``` |
| 67 | ubuntu@host:s2n_root/ $ TOX_TEST_NAME=test_happy_path make -C tests/integrationv2 |
| 68 | ``` |
| 69 | |
| 70 | Multiple specific tests can also be run as follows: |
| 71 | ``` |
| 72 | ubuntu@host:s2n_root/ $ TOX_TEST_NAME="test_happy_path test_sslyze" make -C tests/integrationv2 |
| 73 | ``` |
| 74 | |
| 75 | # Writing tests |
| 76 | |
| 77 | The happy path test combines thousands of parameters, and has to validate that the |
| 78 | combinations match. Below is a simple test that demonstrates how lists of parameters |
| 79 | are combined to test all possible parameter combinations. |
| 80 | |
| 81 | ```python |
| 82 | import copy |
| 83 | import pytest |
| 84 | |
| 85 | from configuration import available_ports, ALL_CERTS |
| 86 | from common import ProviderOptions, Cert, Ciphers, Protocols |
| 87 | from fixtures import managed_process |
| 88 | from providers import Provider, S2N, OpenSSL |
| 89 | from utils import get_parameter_name, get_expected_s2n_version |
| 90 | |
| 91 | |
| 92 | """ |
| 93 | Pytest will generate 8 configuration based on the parameterize options below. The |
| 94 | test will be run with each of the possible configurations. |
| 95 | """ |
| 96 | |
| 97 | @pytest.mark.parametrize("cipher", |
| 98 | [Ciphers.AES128_GCM_SHA256, Ciphers.CHACHA20_POLY1305_SHA256], ids=get_parameter_name) |
| 99 | @pytest.mark.parametrize("provider", |
| 100 | [S2N, OpenSSL]) |
| 101 | @pytest.mark.parametrize("protocol", |
| 102 | [Protocols.TLS13], ids=get_parameter_name) |
| 103 | @pytest.mark.parametrize("certificate", |
| 104 | [Cert("ECDSA_256", "ecdsa_p256_pkcs1"), Cert("ECDSA_384", "ecdsa_p384_pkcs1")], ids=get_parameter_name) |
| 105 | def test_example(managed_process, cipher, provider, protocol, certificate): |
| 106 | host = "localhost" |
| 107 | port = next(available_ports) |
| 108 | |
| 109 | client_options = ProviderOptions( |
| 110 | mode=Provider.ClientMode, |
| 111 | host="localhost", |
| 112 | port=port, |
| 113 | cipher=cipher, |
| 114 | insecure=True, |
| 115 | protocol=protocol) |
| 116 | |
| 117 | server_options = copy.copy(client_options) |
| 118 | server_options.mode = Provider.ServerMode |
| 119 | server_options.key = certificate.key |
| 120 | server_options.cert = certificate.cert |
| 121 | |
| 122 | expected_version = get_expected_s2n_version(protocol, provider) |
| 123 | |
| 124 | server = managed_process(provider, server_options, timeout=5) |
| 125 | client = managed_process(S2N, client_options, timeout=5) |
| 126 | |
| 127 | for results in client.get_results(): |
| 128 | assert results.exception is None |
| 129 | assert results.exit_code == 0 |
| 130 | assert bytes("Actual protocol version: {}".format(expected_version).encode('utf-8')) in results.stdout |
| 131 | |
| 132 | for results in server.get_results(): |
| 133 | assert results.exception is None |
| 134 | assert results.exit_code == 0 |
| 135 | |
| 136 | ``` |
| 137 | |
| 138 | |
| 139 | # Testing a new feature |
| 140 | |
| 141 | If you are testing a new feature you need to determine how to use that feature with all supported |
| 142 | providers. |
| 143 | |
| 144 | ## s2nd / s2nc |
| 145 | |
| 146 | You may have to add a command line flag to s2nc.c or s2nd.c for your feature. Use long options if |
| 147 | possible, and use the same option name in both s2nc.c and s2nd.c. If you are able to use an option |
| 148 | name similar to the OpenSSL name, please do. This reduces complexity across TLS providers. |
| 149 | |
| 150 | An example of similar naming is '-reconnect' in OpenSSL and '-r' in S2N. Both have a hardcoded value |
| 151 | of 5 reconnects. The point is to remove logic from the test, and make the providers act as similar |
| 152 | as possible. |
| 153 | |
| 154 | ## Control the provider from the test |
| 155 | |
| 156 | If you are testing a feature which is similar across all TLS providers, add an option to the ProviderOptions |
| 157 | object in `common.py`. If this feature is specific to a single provider, you can use the `extra_flags` option. |
| 158 | |
| 159 | For example, to test the session resumption feature we need to tell various clients and servers to resume |
| 160 | a session multiple times. To do this we added the 'reconnect' and 'reconnects_before_exit' options to the |
| 161 | ProviderOptions object. But with the dynamic threshold feature we simply pass the '-D' argument as an |
| 162 | `extra_flag` to s2n. |
| 163 | |
| 164 | In each provider that supports your feature you need to check if the flag is set, and create a command |
| 165 | line option for that particular provider. You can also add logic checks, e.g with client authentication |
| 166 | the client must have a certificate to send. Otherwise the test will fail. |
| 167 | |
| 168 | ## Fine-tune how input data is sent in the tests |
| 169 | |
| 170 | There are several optional arguments in the managed_process function. Use these arguments to have a |
| 171 | greater level of control over how peers send test data. The argument send_marker can be used in parallel |
| 172 | with the data_to_send Provider options. If both of these arguments are lists, the process will only write a |
| 173 | data_to_send element when it reads a corresponding send_marker element from the process's stdout or stderr. |
| 174 | Additionally, the managed_process argument close_marker can be used to control when the process should shut down. |
| 175 | |
| 176 | An example of how to test that the server and the client can send and receive application data: |
| 177 | ``` |
| 178 | server_options.data_to_send = ["First server message", "Second server message"] |
| 179 | server_send_marker = ["Ready to send data", "First client message"] |
| 180 | server_close_marker = ["Second client message"] |
| 181 | |
| 182 | client_options.data_to_send = ["First client message", "Second client message"] |
| 183 | client_send_marker = ["Ready to send data", "First server message"] |
| 184 | client_close_marker = ["Second server message"] |
| 185 | |
| 186 | server = managed_process(provider, server_options, server_send_marker, server_close_marker, timeout=5) |
| 187 | client = managed_process(S2N, client_options, client_send_marker, client_close_marker, timeout=5) |
| 188 | ``` |
| 189 | |
| 190 | # Troubleshooting |
| 191 | |
| 192 | **INTERNALERROR> OSError: cannot send to <Channel id=1 closed>** |
| 193 | An error similar to this is caused by a runtime error in a test. In `tox.ini` change `-n8` to `-n0` to |
| 194 | see the actual error causing the OSError. |