|
| 1 | +import pytest |
| 2 | +import ssl |
| 3 | +import socket |
| 4 | +import logging |
| 5 | + |
| 6 | +# This test is designed to verify that TLS connections is secured |
| 7 | +# |
| 8 | +# Requirements: |
| 9 | +# - An XCP-ng host |
| 10 | + |
| 11 | +@pytest.mark.parametrize("protocol_name", ["TLSv1", "TLSv1.1"]) |
| 12 | +def test_tls_disabled(host: str, protocol_name: str): |
| 13 | + """ |
| 14 | + Verifies that specified TLS protocols are disabled on the XCP-ng host. |
| 15 | + Uses the ssl library directly. Should raise SSLError. |
| 16 | + """ |
| 17 | + PORT = 443 |
| 18 | + |
| 19 | + protocol = { |
| 20 | + "TLSv1": ssl.PROTOCOL_TLSv1, |
| 21 | + "TLSv1.1": ssl.PROTOCOL_TLSv1_1 |
| 22 | + }[protocol_name] |
| 23 | + |
| 24 | + logging.info(f"Testing if protocol {protocol_name} is disabled on host {host}") |
| 25 | + |
| 26 | + with pytest.raises(ssl.SSLError): |
| 27 | + context = ssl.SSLContext(protocol) |
| 28 | + with socket.create_connection((str(host), PORT), timeout=10) as sock: |
| 29 | + with context.wrap_socket(sock, server_hostname=str(host)) as ssock: |
| 30 | + ssock.do_handshake() |
| 31 | + # If we reach this point, the protocol is enabled (test should fail) |
| 32 | + pytest.fail(f"Protocol {protocol} should be disabled but connection succeeded") |
| 33 | + |
| 34 | +@pytest.mark.parametrize("protocol_name", ["TLSv1.2"]) |
| 35 | +def test_enabled(host: str, protocol_name: str): |
| 36 | + """ |
| 37 | + Verifies that TLSv1.2 is enabled on the XCP-ng host. |
| 38 | + Uses the ssl library directly. |
| 39 | + """ |
| 40 | + PORT = 443 |
| 41 | + |
| 42 | + protocol = { |
| 43 | + "TLSv1.2": ssl.PROTOCOL_TLSv1_2 |
| 44 | + }[protocol_name] |
| 45 | + |
| 46 | + logging.info(f"Testing if protocol {protocol_name} is enabled on host {host}") |
| 47 | + |
| 48 | + try: |
| 49 | + context = ssl.SSLContext(protocol) |
| 50 | + with socket.create_connection((str(host), PORT), timeout=10) as sock: |
| 51 | + with context.wrap_socket(sock, server_hostname=str(host)) as ssock: |
| 52 | + ssock.do_handshake() |
| 53 | + assert ssock.version() |
| 54 | + except ssl.SSLError as e: |
| 55 | + pytest.fail(f"{protocol_name} should be enabled, but got SSLError: {e}") |
0 commit comments