Debian Codegraph @

tests/legacy/test_handshake.py

raw
1 import contextlib
2 import unittest
3
4 from websockets.datastructures import Headers
5 from websockets.exceptions import (
6 InvalidHandshake,
7 InvalidHeader,
8 InvalidHeaderValue,
9 InvalidUpgrade,
10 )
11 from websockets.legacy.handshake import *
12 from websockets.utils import accept_key
13
14
15 class HandshakeTests(unittest.TestCase):
16 def test_round_trip(self):
17 request_headers = Headers()
18 request_key = build_request(request_headers)
19 response_key = check_request(request_headers)
20 self.assertEqual(request_key, response_key)
21 response_headers = Headers()
22 build_response(response_headers, response_key)
23 check_response(response_headers, request_key)
24
25 @contextlib.contextmanager
26 def assertValidRequestHeaders(self):
27 """
28 Provide request headers for modification.
29
30 Assert that the transformation kept them valid.
31
32 """
33 headers = Headers()
34 build_request(headers)
35 yield headers
36 check_request(headers)
37
38 @contextlib.contextmanager
39 def assertInvalidRequestHeaders(self, exc_type):
40 """
41 Provide request headers for modification.
42
43 Assert that the transformation made them invalid.
44
45 """
46 headers = Headers()
47 build_request(headers)
48 yield headers
49 assert issubclass(exc_type, InvalidHandshake)
50 with self.assertRaises(exc_type):
51 check_request(headers)
52
53 def test_request_invalid_connection(self):
54 with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
55 del headers["Connection"]
56 headers["Connection"] = "Downgrade"
57
58 def test_request_missing_connection(self):
59 with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
60 del headers["Connection"]
61
62 def test_request_additional_connection(self):
63 with self.assertValidRequestHeaders() as headers:
64 headers["Connection"] = "close"
65
66 def test_request_invalid_upgrade(self):
67 with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
68 del headers["Upgrade"]
69 headers["Upgrade"] = "socketweb"
70
71 def test_request_missing_upgrade(self):
72 with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
73 del headers["Upgrade"]
74
75 def test_request_additional_upgrade(self):
76 with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
77 headers["Upgrade"] = "socketweb"
78
79 def test_request_invalid_key_not_base64(self):
80 with self.assertInvalidRequestHeaders(InvalidHeaderValue) as headers:
81 del headers["Sec-WebSocket-Key"]
82 headers["Sec-WebSocket-Key"] = "!@#$%^&*()"
83
84 def test_request_invalid_key_not_well_padded(self):
85 with self.assertInvalidRequestHeaders(InvalidHeaderValue) as headers:
86 del headers["Sec-WebSocket-Key"]
87 headers["Sec-WebSocket-Key"] = "CSIRmL8dWYxeAdr/XpEHRw"
88
89 def test_request_invalid_key_not_16_bytes_long(self):
90 with self.assertInvalidRequestHeaders(InvalidHeaderValue) as headers:
91 del headers["Sec-WebSocket-Key"]
92 headers["Sec-WebSocket-Key"] = "ZLpprpvK4PE="
93
94 def test_request_missing_key(self):
95 with self.assertInvalidRequestHeaders(InvalidHeader) as headers:
96 del headers["Sec-WebSocket-Key"]
97
98 def test_request_additional_key(self):
99 with self.assertInvalidRequestHeaders(InvalidHeader) as headers:
100 # This duplicates the Sec-WebSocket-Key header.
101 headers["Sec-WebSocket-Key"] = headers["Sec-WebSocket-Key"]
102
103 def test_request_invalid_version(self):
104 with self.assertInvalidRequestHeaders(InvalidHeaderValue) as headers:
105 del headers["Sec-WebSocket-Version"]
106 headers["Sec-WebSocket-Version"] = "42"
107
108 def test_request_missing_version(self):
109 with self.assertInvalidRequestHeaders(InvalidHeader) as headers:
110 del headers["Sec-WebSocket-Version"]
111
112 def test_request_additional_version(self):
113 with self.assertInvalidRequestHeaders(InvalidHeader) as headers:
114 # This duplicates the Sec-WebSocket-Version header.
115 headers["Sec-WebSocket-Version"] = headers["Sec-WebSocket-Version"]
116
117 @contextlib.contextmanager
118 def assertValidResponseHeaders(self, key="CSIRmL8dWYxeAdr/XpEHRw=="):
119 """
120 Provide response headers for modification.
121
122 Assert that the transformation kept them valid.
123
124 """
125 headers = Headers()
126 build_response(headers, key)
127 yield headers
128 check_response(headers, key)
129
130 @contextlib.contextmanager
131 def assertInvalidResponseHeaders(self, exc_type, key="CSIRmL8dWYxeAdr/XpEHRw=="):
132 """
133 Provide response headers for modification.
134
135 Assert that the transformation made them invalid.
136
137 """
138 headers = Headers()
139 build_response(headers, key)
140 yield headers
141 assert issubclass(exc_type, InvalidHandshake)
142 with self.assertRaises(exc_type):
143 check_response(headers, key)
144
145 def test_response_invalid_connection(self):
146 with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
147 del headers["Connection"]
148 headers["Connection"] = "Downgrade"
149
150 def test_response_missing_connection(self):
151 with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
152 del headers["Connection"]
153
154 def test_response_additional_connection(self):
155 with self.assertValidResponseHeaders() as headers:
156 headers["Connection"] = "close"
157
158 def test_response_invalid_upgrade(self):
159 with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
160 del headers["Upgrade"]
161 headers["Upgrade"] = "socketweb"
162
163 def test_response_missing_upgrade(self):
164 with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
165 del headers["Upgrade"]
166
167 def test_response_additional_upgrade(self):
168 with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
169 headers["Upgrade"] = "socketweb"
170
171 def test_response_invalid_accept(self):
172 with self.assertInvalidResponseHeaders(InvalidHeaderValue) as headers:
173 del headers["Sec-WebSocket-Accept"]
174 other_key = "1Eq4UDEFQYg3YspNgqxv5g=="
175 headers["Sec-WebSocket-Accept"] = accept_key(other_key)
176
177 def test_response_missing_accept(self):
178 with self.assertInvalidResponseHeaders(InvalidHeader) as headers:
179 del headers["Sec-WebSocket-Accept"]
180
181 def test_response_additional_accept(self):
182 with self.assertInvalidResponseHeaders(InvalidHeader) as headers:
183 # This duplicates the Sec-WebSocket-Accept header.
184 headers["Sec-WebSocket-Accept"] = headers["Sec-WebSocket-Accept"]