|
19 | 19 | from mbedtls.tls import _enable_debug_output |
20 | 20 | from mbedtls.tls import _PSKSToreProxy as PSKStoreProxy |
21 | 21 | from mbedtls.tls import _set_debug_level |
22 | | -from mbedtls.tls import _TLSSession as TLSSession |
| 22 | +from mbedtls.tls import TLSSession |
23 | 23 | from mbedtls.x509 import CRT, CSR, BasicConstraints |
24 | 24 |
|
25 | 25 | try: |
@@ -55,6 +55,12 @@ def __exit__(self, *exc_info): |
55 | 55 | def __del__(self): |
56 | 56 | self.stop() |
57 | 57 |
|
| 58 | + @property |
| 59 | + def context(self): |
| 60 | + if self._sock is None: |
| 61 | + return None |
| 62 | + return self._sock.context |
| 63 | + |
58 | 64 | def do_handshake(self): |
59 | 65 | if not self._sock: |
60 | 66 | return |
@@ -110,6 +116,12 @@ def __exit__(self, *exc_info): |
110 | 116 | def __del__(self): |
111 | 117 | self.stop() |
112 | 118 |
|
| 119 | + @property |
| 120 | + def context(self): |
| 121 | + if self._sock is None: |
| 122 | + return None |
| 123 | + return self._sock.context |
| 124 | + |
113 | 125 | def start(self): |
114 | 126 | if self._sock: |
115 | 127 | self.stop() |
@@ -356,6 +368,15 @@ def test_serialization(self, header): |
356 | 368 | assert TLSRecordHeader.from_bytes(serialized) == header |
357 | 369 |
|
358 | 370 |
|
| 371 | +class TestTLSSession: |
| 372 | + @pytest.fixture |
| 373 | + def session(self): |
| 374 | + return TLSSession() |
| 375 | + |
| 376 | + def test_repr(self, session): |
| 377 | + assert isinstance(repr(session), str) |
| 378 | + |
| 379 | + |
359 | 380 | class Chain: |
360 | 381 | @pytest.fixture(scope="class") |
361 | 382 | def now(self): |
@@ -862,3 +883,22 @@ def test_client_server(self, client, buffer, chunksize): |
862 | 883 | break |
863 | 884 |
|
864 | 885 | assert client.echo(buffer, chunksize) == buffer |
| 886 | + |
| 887 | + @pytest.mark.timeout(10) |
| 888 | + @pytest.mark.usefixtures("server") |
| 889 | + @pytest.mark.parametrize("ciphers", (ciphers_available(),), indirect=True) |
| 890 | + def test_session_caching(self, client, cli_conf): |
| 891 | + while True: |
| 892 | + try: |
| 893 | + client.do_handshake() |
| 894 | + except (WantReadError, WantWriteError): |
| 895 | + pass |
| 896 | + else: |
| 897 | + break |
| 898 | + |
| 899 | + session = TLSSession() |
| 900 | + session.save(client.context) |
| 901 | + |
| 902 | + new_context = session.resume(cli_conf) |
| 903 | + assert isinstance(new_context, ClientContext) |
| 904 | + assert new_context._verified |
0 commit comments