File: test_handshake.py

package info (click to toggle)
python-websockets 15.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,948 kB
  • sloc: python: 25,105; javascript: 350; ansic: 148; makefile: 43
file content (184 lines) | stat: -rw-r--r-- 6,887 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import contextlib
import unittest

from websockets.datastructures import Headers
from websockets.exceptions import (
    InvalidHandshake,
    InvalidHeader,
    InvalidHeaderValue,
    InvalidUpgrade,
)
from websockets.legacy.handshake import *
from websockets.utils import accept_key


class HandshakeTests(unittest.TestCase):
    def test_round_trip(self):
        request_headers = Headers()
        request_key = build_request(request_headers)
        response_key = check_request(request_headers)
        self.assertEqual(request_key, response_key)
        response_headers = Headers()
        build_response(response_headers, response_key)
        check_response(response_headers, request_key)

    @contextlib.contextmanager
    def assertValidRequestHeaders(self):
        """
        Provide request headers for modification.

        Assert that the transformation kept them valid.

        """
        headers = Headers()
        build_request(headers)
        yield headers
        check_request(headers)

    @contextlib.contextmanager
    def assertInvalidRequestHeaders(self, exc_type):
        """
        Provide request headers for modification.

        Assert that the transformation made them invalid.

        """
        headers = Headers()
        build_request(headers)
        yield headers
        assert issubclass(exc_type, InvalidHandshake)
        with self.assertRaises(exc_type):
            check_request(headers)

    def test_request_invalid_connection(self):
        with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
            del headers["Connection"]
            headers["Connection"] = "Downgrade"

    def test_request_missing_connection(self):
        with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
            del headers["Connection"]

    def test_request_additional_connection(self):
        with self.assertValidRequestHeaders() as headers:
            headers["Connection"] = "close"

    def test_request_invalid_upgrade(self):
        with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
            del headers["Upgrade"]
            headers["Upgrade"] = "socketweb"

    def test_request_missing_upgrade(self):
        with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
            del headers["Upgrade"]

    def test_request_additional_upgrade(self):
        with self.assertInvalidRequestHeaders(InvalidUpgrade) as headers:
            headers["Upgrade"] = "socketweb"

    def test_request_invalid_key_not_base64(self):
        with self.assertInvalidRequestHeaders(InvalidHeaderValue) as headers:
            del headers["Sec-WebSocket-Key"]
            headers["Sec-WebSocket-Key"] = "!@#$%^&*()"

    def test_request_invalid_key_not_well_padded(self):
        with self.assertInvalidRequestHeaders(InvalidHeaderValue) as headers:
            del headers["Sec-WebSocket-Key"]
            headers["Sec-WebSocket-Key"] = "CSIRmL8dWYxeAdr/XpEHRw"

    def test_request_invalid_key_not_16_bytes_long(self):
        with self.assertInvalidRequestHeaders(InvalidHeaderValue) as headers:
            del headers["Sec-WebSocket-Key"]
            headers["Sec-WebSocket-Key"] = "ZLpprpvK4PE="

    def test_request_missing_key(self):
        with self.assertInvalidRequestHeaders(InvalidHeader) as headers:
            del headers["Sec-WebSocket-Key"]

    def test_request_additional_key(self):
        with self.assertInvalidRequestHeaders(InvalidHeader) as headers:
            # This duplicates the Sec-WebSocket-Key header.
            headers["Sec-WebSocket-Key"] = headers["Sec-WebSocket-Key"]

    def test_request_invalid_version(self):
        with self.assertInvalidRequestHeaders(InvalidHeaderValue) as headers:
            del headers["Sec-WebSocket-Version"]
            headers["Sec-WebSocket-Version"] = "42"

    def test_request_missing_version(self):
        with self.assertInvalidRequestHeaders(InvalidHeader) as headers:
            del headers["Sec-WebSocket-Version"]

    def test_request_additional_version(self):
        with self.assertInvalidRequestHeaders(InvalidHeader) as headers:
            # This duplicates the Sec-WebSocket-Version header.
            headers["Sec-WebSocket-Version"] = headers["Sec-WebSocket-Version"]

    @contextlib.contextmanager
    def assertValidResponseHeaders(self, key="CSIRmL8dWYxeAdr/XpEHRw=="):
        """
        Provide response headers for modification.

        Assert that the transformation kept them valid.

        """
        headers = Headers()
        build_response(headers, key)
        yield headers
        check_response(headers, key)

    @contextlib.contextmanager
    def assertInvalidResponseHeaders(self, exc_type, key="CSIRmL8dWYxeAdr/XpEHRw=="):
        """
        Provide response headers for modification.

        Assert that the transformation made them invalid.

        """
        headers = Headers()
        build_response(headers, key)
        yield headers
        assert issubclass(exc_type, InvalidHandshake)
        with self.assertRaises(exc_type):
            check_response(headers, key)

    def test_response_invalid_connection(self):
        with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
            del headers["Connection"]
            headers["Connection"] = "Downgrade"

    def test_response_missing_connection(self):
        with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
            del headers["Connection"]

    def test_response_additional_connection(self):
        with self.assertValidResponseHeaders() as headers:
            headers["Connection"] = "close"

    def test_response_invalid_upgrade(self):
        with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
            del headers["Upgrade"]
            headers["Upgrade"] = "socketweb"

    def test_response_missing_upgrade(self):
        with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
            del headers["Upgrade"]

    def test_response_additional_upgrade(self):
        with self.assertInvalidResponseHeaders(InvalidUpgrade) as headers:
            headers["Upgrade"] = "socketweb"

    def test_response_invalid_accept(self):
        with self.assertInvalidResponseHeaders(InvalidHeaderValue) as headers:
            del headers["Sec-WebSocket-Accept"]
            other_key = "1Eq4UDEFQYg3YspNgqxv5g=="
            headers["Sec-WebSocket-Accept"] = accept_key(other_key)

    def test_response_missing_accept(self):
        with self.assertInvalidResponseHeaders(InvalidHeader) as headers:
            del headers["Sec-WebSocket-Accept"]

    def test_response_additional_accept(self):
        with self.assertInvalidResponseHeaders(InvalidHeader) as headers:
            # This duplicates the Sec-WebSocket-Accept header.
            headers["Sec-WebSocket-Accept"] = headers["Sec-WebSocket-Accept"]