| 1 |
import contextlib |
| 2 |
import email.utils |
| 3 |
import os |
| 4 |
import pathlib |
| 5 |
import platform |
| 6 |
import ssl |
| 7 |
import tempfile |
| 8 |
import time |
| 9 |
import unittest |
| 10 |
import warnings |
| 11 |
|
| 12 |
from websockets.version import released |
| 13 |
|
| 14 |
|
| 15 |
# Generate TLS certificate with: |
| 16 |
# $ openssl req -x509 -config test_localhost.cnf -days 15340 -newkey rsa:2048 \ |
| 17 |
# -out test_localhost.crt -keyout test_localhost.key |
| 18 |
# $ cat test_localhost.key test_localhost.crt > test_localhost.pem |
| 19 |
# $ rm test_localhost.key test_localhost.crt |
| 20 |
|
| 21 |
CERTIFICATE = pathlib.Path(__file__).with_name("test_localhost.pem") |
| 22 |
|
| 23 |
CLIENT_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 24 |
CLIENT_CONTEXT.load_verify_locations(bytes(CERTIFICATE)) |
| 25 |
|
| 26 |
SERVER_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) |
| 27 |
SERVER_CONTEXT.load_cert_chain(bytes(CERTIFICATE)) |
| 28 |
|
| 29 |
# Work around https://github.com/openssl/openssl/issues/7967 |
| 30 |
|
| 31 |
# This bug causes connect() to hang in tests for the client. Including this |
| 32 |
# workaround acknowledges that the issue could happen outside of the test suite. |
| 33 |
|
| 34 |
# It shouldn't happen too often, or else OpenSSL 1.1.1 would be unusable. If it |
| 35 |
# happens, we can look for a library-level fix, but it won't be easy. |
| 36 |
|
| 37 |
SERVER_CONTEXT.num_tickets = 0 |
| 38 |
|
| 39 |
|
| 40 |
DATE = email.utils.formatdate(usegmt=True) |
| 41 |
|
| 42 |
|
| 43 |
# Unit for timeouts. May be increased in slow or noisy environments by setting |
| 44 |
# the WEBSOCKETS_TESTS_TIMEOUT_FACTOR environment variable. |
| 45 |
|
| 46 |
# Downstream distributors insist on running the test suite despites my plea to |
| 47 |
# the contrary. They do it on build farms with unstable performance, leading to |
| 48 |
# flakiness, and then they file bugs. Make tests 100x slower to avoid flakiness. |
| 49 |
|
| 50 |
MS = 0.001 * float( |
| 51 |
os.environ.get( |
| 52 |
"WEBSOCKETS_TESTS_TIMEOUT_FACTOR", |
| 53 |
"100" if released else "1", |
| 54 |
) |
| 55 |
) |
| 56 |
|
| 57 |
# PyPy, asyncio's debug mode, and coverage penalize performance of this |
| 58 |
# test suite. Increase timeouts to reduce the risk of spurious failures. |
| 59 |
if platform.python_implementation() == "PyPy": # pragma: no cover |
| 60 |
MS *= 2 |
| 61 |
if os.environ.get("PYTHONASYNCIODEBUG"): # pragma: no cover |
| 62 |
MS *= 2 |
| 63 |
if os.environ.get("COVERAGE_RUN"): # pragma: no branch |
| 64 |
MS *= 2 |
| 65 |
|
| 66 |
# Ensure that timeouts are larger than the clock's resolution (for Windows). |
| 67 |
MS = max(MS, 2.5 * time.get_clock_info("monotonic").resolution) |
| 68 |
|
| 69 |
|
| 70 |
class GeneratorTestCase(unittest.TestCase): |
| 71 |
""" |
| 72 |
Base class for testing generator-based coroutines. |
| 73 |
|
| 74 |
""" |
| 75 |
|
| 76 |
def assertGeneratorRunning(self, gen): |
| 77 |
""" |
| 78 |
Check that a generator-based coroutine hasn't completed yet. |
| 79 |
|
| 80 |
""" |
| 81 |
next(gen) |
| 82 |
|
| 83 |
def assertGeneratorReturns(self, gen): |
| 84 |
""" |
| 85 |
Check that a generator-based coroutine completes and return its value. |
| 86 |
|
| 87 |
""" |
| 88 |
with self.assertRaises(StopIteration) as raised: |
| 89 |
next(gen) |
| 90 |
return raised.exception.value |
| 91 |
|
| 92 |
|
| 93 |
class LoggingTestCase(unittest.TestCase): |
| 94 |
""" |
| 95 |
Base class for testing logging output. |
| 96 |
|
| 97 |
""" |
| 98 |
|
| 99 |
def assertExceptionLogged(self, logs, log_message, exc_message): |
| 100 |
""" |
| 101 |
Check that a log record with the given message carries the given exception. |
| 102 |
|
| 103 |
""" |
| 104 |
messages = [record.getMessage() for record in logs.records] |
| 105 |
self.assertIn(log_message, messages) |
| 106 |
record = logs.records[messages.index(log_message)] |
| 107 |
self.assertEqual(str(record.exc_info[1]), exc_message) |
| 108 |
|
| 109 |
|
| 110 |
class DeprecationTestCase(unittest.TestCase): |
| 111 |
""" |
| 112 |
Base class for testing deprecations. |
| 113 |
|
| 114 |
""" |
| 115 |
|
| 116 |
@contextlib.contextmanager |
| 117 |
def assertDeprecationWarning(self, message): |
| 118 |
""" |
| 119 |
Check that a deprecation warning was raised with the given message. |
| 120 |
|
| 121 |
""" |
| 122 |
with warnings.catch_warnings(record=True) as recorded_warnings: |
| 123 |
warnings.simplefilter("always") |
| 124 |
yield |
| 125 |
|
| 126 |
self.assertEqual(len(recorded_warnings), 1) |
| 127 |
warning = recorded_warnings[0] |
| 128 |
self.assertEqual(warning.category, DeprecationWarning) |
| 129 |
self.assertEqual(str(warning.message), message) |
| 130 |
|
| 131 |
|
| 132 |
async def alist(async_iterable): |
| 133 |
items = [] |
| 134 |
async for item in async_iterable: |
| 135 |
items.append(item) |
| 136 |
return items |
| 137 |
|
| 138 |
|
| 139 |
@contextlib.contextmanager |
| 140 |
def temp_unix_socket_path(): |
| 141 |
with tempfile.TemporaryDirectory() as temp_dir: |
| 142 |
yield str(pathlib.Path(temp_dir) / "websockets") |