| 1 |
import base64 |
| 2 |
import itertools |
| 3 |
import platform |
| 4 |
import socket |
| 5 |
import unittest |
| 6 |
|
| 7 |
from websockets.utils import ( |
| 8 |
accept_key, |
| 9 |
apply_mask as py_apply_mask, |
| 10 |
generate_key, |
| 11 |
get_socket_name, |
| 12 |
) |
| 13 |
|
| 14 |
from .utils import temp_unix_socket_path |
| 15 |
|
| 16 |
|
| 17 |
# Test vector from RFC 6455 |
| 18 |
KEY = "dGhlIHNhbXBsZSBub25jZQ==" |
| 19 |
ACCEPT = "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" |
| 20 |
|
| 21 |
|
| 22 |
class UtilsTests(unittest.TestCase): |
| 23 |
def test_generate_key(self): |
| 24 |
key = generate_key() |
| 25 |
self.assertEqual(len(base64.b64decode(key.encode())), 16) |
| 26 |
|
| 27 |
def test_accept_key(self): |
| 28 |
self.assertEqual(accept_key(KEY), ACCEPT) |
| 29 |
|
| 30 |
|
| 31 |
class ApplyMaskTests(unittest.TestCase): |
| 32 |
@staticmethod |
| 33 |
def apply_mask(*args, **kwargs): |
| 34 |
return py_apply_mask(*args, **kwargs) |
| 35 |
|
| 36 |
apply_mask_type_combos = list(itertools.product([bytes, bytearray], repeat=2)) |
| 37 |
|
| 38 |
apply_mask_test_values = [ |
| 39 |
(b"", b"1234", b""), |
| 40 |
(b"aBcDe", b"\x00\x00\x00\x00", b"aBcDe"), |
| 41 |
(b"abcdABCD", b"1234", b"PPPPpppp"), |
| 42 |
(b"abcdABCD" * 10, b"1234", b"PPPPpppp" * 10), |
| 43 |
] |
| 44 |
|
| 45 |
def test_apply_mask(self): |
| 46 |
for data_type, mask_type in self.apply_mask_type_combos: |
| 47 |
for data_in, mask, data_out in self.apply_mask_test_values: |
| 48 |
data_in, mask = data_type(data_in), mask_type(mask) |
| 49 |
|
| 50 |
with self.subTest(data_in=data_in, mask=mask): |
| 51 |
result = self.apply_mask(data_in, mask) |
| 52 |
self.assertEqual(result, data_out) |
| 53 |
|
| 54 |
def test_apply_mask_memoryview(self): |
| 55 |
for mask_type in [bytes, bytearray]: |
| 56 |
for data_in, mask, data_out in self.apply_mask_test_values: |
| 57 |
data_in, mask = memoryview(data_in), mask_type(mask) |
| 58 |
|
| 59 |
with self.subTest(data_in=data_in, mask=mask): |
| 60 |
result = self.apply_mask(data_in, mask) |
| 61 |
self.assertEqual(result, data_out) |
| 62 |
|
| 63 |
def test_apply_mask_non_contiguous_memoryview(self): |
| 64 |
for mask_type in [bytes, bytearray]: |
| 65 |
for data_in, mask, data_out in self.apply_mask_test_values: |
| 66 |
data_in, mask = memoryview(data_in)[::-1], mask_type(mask)[::-1] |
| 67 |
data_out = data_out[::-1] |
| 68 |
|
| 69 |
with self.subTest(data_in=data_in, mask=mask): |
| 70 |
result = self.apply_mask(data_in, mask) |
| 71 |
self.assertEqual(result, data_out) |
| 72 |
|
| 73 |
def test_apply_mask_check_input_types(self): |
| 74 |
for data_in, mask in [(None, None), (b"abcd", None), (None, b"abcd")]: |
| 75 |
with self.subTest(data_in=data_in, mask=mask): |
| 76 |
with self.assertRaises(TypeError): |
| 77 |
self.apply_mask(data_in, mask) |
| 78 |
|
| 79 |
def test_apply_mask_check_mask_length(self): |
| 80 |
for data_in, mask in [ |
| 81 |
(b"", b""), |
| 82 |
(b"abcd", b"123"), |
| 83 |
(b"", b"aBcDe"), |
| 84 |
(b"12345678", b"12345678"), |
| 85 |
]: |
| 86 |
with self.subTest(data_in=data_in, mask=mask): |
| 87 |
with self.assertRaises(ValueError): |
| 88 |
self.apply_mask(data_in, mask) |
| 89 |
|
| 90 |
|
| 91 |
try: |
| 92 |
from websockets.speedups import apply_mask as c_apply_mask |
| 93 |
except ImportError: |
| 94 |
pass |
| 95 |
else: |
| 96 |
|
| 97 |
class SpeedupsTests(ApplyMaskTests): |
| 98 |
@staticmethod |
| 99 |
def apply_mask(*args, **kwargs): |
| 100 |
try: |
| 101 |
return c_apply_mask(*args, **kwargs) |
| 102 |
except NotImplementedError as exc: # pragma: no cover |
| 103 |
# PyPy doesn't implement creating contiguous readonly buffer |
| 104 |
# from non-contiguous. We don't care about this edge case. |
| 105 |
if ( |
| 106 |
platform.python_implementation() == "PyPy" |
| 107 |
and "not implemented yet" in str(exc) |
| 108 |
): |
| 109 |
raise unittest.SkipTest(str(exc)) |
| 110 |
else: |
| 111 |
raise |
| 112 |
|
| 113 |
|
| 114 |
class GetSocketNameAsStrTests(unittest.TestCase): |
| 115 |
def test_af_inet(self): |
| 116 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: |
| 117 |
sock.bind(("127.0.0.1", 0)) |
| 118 |
port = sock.getsockname()[1] |
| 119 |
self.assertEqual(get_socket_name(sock), f"127.0.0.1:{port}") |
| 120 |
|
| 121 |
@unittest.skipUnless(socket.has_ipv6, "this test requires IPv6") |
| 122 |
def test_af_inet6(self): |
| 123 |
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock: |
| 124 |
try: |
| 125 |
sock.bind(("::1", 0)) |
| 126 |
except OSError: |
| 127 |
self.skipTest("IPv6 loopback isn't available") |
| 128 |
port = sock.getsockname()[1] |
| 129 |
self.assertEqual(get_socket_name(sock), f"[::1]:{port}") |
| 130 |
|
| 131 |
@unittest.skipUnless(hasattr(socket, "AF_UNIX"), "this test requires Unix sockets") |
| 132 |
def test_af_unix(self): |
| 133 |
with temp_unix_socket_path() as path: |
| 134 |
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: |
| 135 |
sock.bind(path) |
| 136 |
self.assertEqual(get_socket_name(sock), path) |