Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_websocket.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. # -*- coding: utf-8 -*-
  2. #
  3. import os
  4. import os.path
  5. import socket
  6. import websocket as ws
  7. import unittest
  8. from websocket._handshake import _create_sec_websocket_key, \
  9. _validate as _validate_header
  10. from websocket._http import read_headers
  11. from websocket._utils import validate_utf8
  12. from base64 import decodebytes as base64decode
  13. """
  14. test_websocket.py
  15. websocket - WebSocket client library for Python
  16. Copyright 2023 engn33r
  17. Licensed under the Apache License, Version 2.0 (the "License");
  18. you may not use this file except in compliance with the License.
  19. You may obtain a copy of the License at
  20. http://www.apache.org/licenses/LICENSE-2.0
  21. Unless required by applicable law or agreed to in writing, software
  22. distributed under the License is distributed on an "AS IS" BASIS,
  23. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. See the License for the specific language governing permissions and
  25. limitations under the License.
  26. """
  27. try:
  28. import ssl
  29. from ssl import SSLError
  30. except ImportError:
  31. # dummy class of SSLError for ssl none-support environment.
  32. class SSLError(Exception):
  33. pass
  34. # Skip test to access the internet unless TEST_WITH_INTERNET == 1
  35. TEST_WITH_INTERNET = os.environ.get('TEST_WITH_INTERNET', '0') == '1'
  36. # Skip tests relying on local websockets server unless LOCAL_WS_SERVER_PORT != -1
  37. LOCAL_WS_SERVER_PORT = os.environ.get('LOCAL_WS_SERVER_PORT', '-1')
  38. TEST_WITH_LOCAL_SERVER = LOCAL_WS_SERVER_PORT != '-1'
  39. TRACEABLE = True
  40. def create_mask_key(_):
  41. return "abcd"
  42. class SockMock:
  43. def __init__(self):
  44. self.data = []
  45. self.sent = []
  46. def add_packet(self, data):
  47. self.data.append(data)
  48. def gettimeout(self):
  49. return None
  50. def recv(self, bufsize):
  51. if self.data:
  52. e = self.data.pop(0)
  53. if isinstance(e, Exception):
  54. raise e
  55. if len(e) > bufsize:
  56. self.data.insert(0, e[bufsize:])
  57. return e[:bufsize]
  58. def send(self, data):
  59. self.sent.append(data)
  60. return len(data)
  61. def close(self):
  62. pass
  63. class HeaderSockMock(SockMock):
  64. def __init__(self, fname):
  65. SockMock.__init__(self)
  66. path = os.path.join(os.path.dirname(__file__), fname)
  67. with open(path, "rb") as f:
  68. self.add_packet(f.read())
  69. class WebSocketTest(unittest.TestCase):
  70. def setUp(self):
  71. ws.enableTrace(TRACEABLE)
  72. def tearDown(self):
  73. pass
  74. def testDefaultTimeout(self):
  75. self.assertEqual(ws.getdefaulttimeout(), None)
  76. ws.setdefaulttimeout(10)
  77. self.assertEqual(ws.getdefaulttimeout(), 10)
  78. ws.setdefaulttimeout(None)
  79. def testWSKey(self):
  80. key = _create_sec_websocket_key()
  81. self.assertTrue(key != 24)
  82. self.assertTrue(str("¥n") not in key)
  83. def testNonce(self):
  84. """ WebSocket key should be a random 16-byte nonce.
  85. """
  86. key = _create_sec_websocket_key()
  87. nonce = base64decode(key.encode("utf-8"))
  88. self.assertEqual(16, len(nonce))
  89. def testWsUtils(self):
  90. key = "c6b8hTg4EeGb2gQMztV1/g=="
  91. required_header = {
  92. "upgrade": "websocket",
  93. "connection": "upgrade",
  94. "sec-websocket-accept": "Kxep+hNu9n51529fGidYu7a3wO0="}
  95. self.assertEqual(_validate_header(required_header, key, None), (True, None))
  96. header = required_header.copy()
  97. header["upgrade"] = "http"
  98. self.assertEqual(_validate_header(header, key, None), (False, None))
  99. del header["upgrade"]
  100. self.assertEqual(_validate_header(header, key, None), (False, None))
  101. header = required_header.copy()
  102. header["connection"] = "something"
  103. self.assertEqual(_validate_header(header, key, None), (False, None))
  104. del header["connection"]
  105. self.assertEqual(_validate_header(header, key, None), (False, None))
  106. header = required_header.copy()
  107. header["sec-websocket-accept"] = "something"
  108. self.assertEqual(_validate_header(header, key, None), (False, None))
  109. del header["sec-websocket-accept"]
  110. self.assertEqual(_validate_header(header, key, None), (False, None))
  111. header = required_header.copy()
  112. header["sec-websocket-protocol"] = "sub1"
  113. self.assertEqual(_validate_header(header, key, ["sub1", "sub2"]), (True, "sub1"))
  114. # This case will print out a logging error using the error() function, but that is expected
  115. self.assertEqual(_validate_header(header, key, ["sub2", "sub3"]), (False, None))
  116. header = required_header.copy()
  117. header["sec-websocket-protocol"] = "sUb1"
  118. self.assertEqual(_validate_header(header, key, ["Sub1", "suB2"]), (True, "sub1"))
  119. header = required_header.copy()
  120. # This case will print out a logging error using the error() function, but that is expected
  121. self.assertEqual(_validate_header(header, key, ["Sub1", "suB2"]), (False, None))
  122. def testReadHeader(self):
  123. status, header, status_message = read_headers(HeaderSockMock("data/header01.txt"))
  124. self.assertEqual(status, 101)
  125. self.assertEqual(header["connection"], "Upgrade")
  126. status, header, status_message = read_headers(HeaderSockMock("data/header03.txt"))
  127. self.assertEqual(status, 101)
  128. self.assertEqual(header["connection"], "Upgrade, Keep-Alive")
  129. HeaderSockMock("data/header02.txt")
  130. self.assertRaises(ws.WebSocketException, read_headers, HeaderSockMock("data/header02.txt"))
  131. def testSend(self):
  132. # TODO: add longer frame data
  133. sock = ws.WebSocket()
  134. sock.set_mask_key(create_mask_key)
  135. s = sock.sock = HeaderSockMock("data/header01.txt")
  136. sock.send("Hello")
  137. self.assertEqual(s.sent[0], b'\x81\x85abcd)\x07\x0f\x08\x0e')
  138. sock.send("こんにちは")
  139. self.assertEqual(s.sent[1], b'\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc')
  140. # sock.send("x" * 5000)
  141. # self.assertEqual(s.sent[1], b'\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc")
  142. self.assertEqual(sock.send_binary(b'1111111111101'), 19)
  143. def testRecv(self):
  144. # TODO: add longer frame data
  145. sock = ws.WebSocket()
  146. s = sock.sock = SockMock()
  147. something = b'\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc'
  148. s.add_packet(something)
  149. data = sock.recv()
  150. self.assertEqual(data, "こんにちは")
  151. s.add_packet(b'\x81\x85abcd)\x07\x0f\x08\x0e')
  152. data = sock.recv()
  153. self.assertEqual(data, "Hello")
  154. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  155. def testIter(self):
  156. count = 2
  157. s = ws.create_connection('wss://api.bitfinex.com/ws/2')
  158. s.send('{"event": "subscribe", "channel": "ticker"}')
  159. for _ in s:
  160. count -= 1
  161. if count == 0:
  162. break
  163. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  164. def testNext(self):
  165. sock = ws.create_connection('wss://api.bitfinex.com/ws/2')
  166. self.assertEqual(str, type(next(sock)))
  167. def testInternalRecvStrict(self):
  168. sock = ws.WebSocket()
  169. s = sock.sock = SockMock()
  170. s.add_packet(b'foo')
  171. s.add_packet(socket.timeout())
  172. s.add_packet(b'bar')
  173. # s.add_packet(SSLError("The read operation timed out"))
  174. s.add_packet(b'baz')
  175. with self.assertRaises(ws.WebSocketTimeoutException):
  176. sock.frame_buffer.recv_strict(9)
  177. # with self.assertRaises(SSLError):
  178. # data = sock._recv_strict(9)
  179. data = sock.frame_buffer.recv_strict(9)
  180. self.assertEqual(data, b'foobarbaz')
  181. with self.assertRaises(ws.WebSocketConnectionClosedException):
  182. sock.frame_buffer.recv_strict(1)
  183. def testRecvTimeout(self):
  184. sock = ws.WebSocket()
  185. s = sock.sock = SockMock()
  186. s.add_packet(b'\x81')
  187. s.add_packet(socket.timeout())
  188. s.add_packet(b'\x8dabcd\x29\x07\x0f\x08\x0e')
  189. s.add_packet(socket.timeout())
  190. s.add_packet(b'\x4e\x43\x33\x0e\x10\x0f\x00\x40')
  191. with self.assertRaises(ws.WebSocketTimeoutException):
  192. sock.recv()
  193. with self.assertRaises(ws.WebSocketTimeoutException):
  194. sock.recv()
  195. data = sock.recv()
  196. self.assertEqual(data, "Hello, World!")
  197. with self.assertRaises(ws.WebSocketConnectionClosedException):
  198. sock.recv()
  199. def testRecvWithSimpleFragmentation(self):
  200. sock = ws.WebSocket()
  201. s = sock.sock = SockMock()
  202. # OPCODE=TEXT, FIN=0, MSG="Brevity is "
  203. s.add_packet(b'\x01\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C')
  204. # OPCODE=CONT, FIN=1, MSG="the soul of wit"
  205. s.add_packet(b'\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17')
  206. data = sock.recv()
  207. self.assertEqual(data, "Brevity is the soul of wit")
  208. with self.assertRaises(ws.WebSocketConnectionClosedException):
  209. sock.recv()
  210. def testRecvWithFireEventOfFragmentation(self):
  211. sock = ws.WebSocket(fire_cont_frame=True)
  212. s = sock.sock = SockMock()
  213. # OPCODE=TEXT, FIN=0, MSG="Brevity is "
  214. s.add_packet(b'\x01\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C')
  215. # OPCODE=CONT, FIN=0, MSG="Brevity is "
  216. s.add_packet(b'\x00\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C')
  217. # OPCODE=CONT, FIN=1, MSG="the soul of wit"
  218. s.add_packet(b'\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17')
  219. _, data = sock.recv_data()
  220. self.assertEqual(data, b'Brevity is ')
  221. _, data = sock.recv_data()
  222. self.assertEqual(data, b'Brevity is ')
  223. _, data = sock.recv_data()
  224. self.assertEqual(data, b'the soul of wit')
  225. # OPCODE=CONT, FIN=0, MSG="Brevity is "
  226. s.add_packet(b'\x80\x8babcd#\x10\x06\x12\x08\x16\x1aD\x08\x11C')
  227. with self.assertRaises(ws.WebSocketException):
  228. sock.recv_data()
  229. with self.assertRaises(ws.WebSocketConnectionClosedException):
  230. sock.recv()
  231. def testClose(self):
  232. sock = ws.WebSocket()
  233. sock.connected = True
  234. sock.close
  235. sock = ws.WebSocket()
  236. s = sock.sock = SockMock()
  237. sock.connected = True
  238. s.add_packet(b'\x88\x80\x17\x98p\x84')
  239. sock.recv()
  240. self.assertEqual(sock.connected, False)
  241. def testRecvContFragmentation(self):
  242. sock = ws.WebSocket()
  243. s = sock.sock = SockMock()
  244. # OPCODE=CONT, FIN=1, MSG="the soul of wit"
  245. s.add_packet(b'\x80\x8fabcd\x15\n\x06D\x12\r\x16\x08A\r\x05D\x16\x0b\x17')
  246. self.assertRaises(ws.WebSocketException, sock.recv)
  247. def testRecvWithProlongedFragmentation(self):
  248. sock = ws.WebSocket()
  249. s = sock.sock = SockMock()
  250. # OPCODE=TEXT, FIN=0, MSG="Once more unto the breach, "
  251. s.add_packet(b'\x01\x9babcd.\x0c\x00\x01A\x0f\x0c\x16\x04B\x16\n\x15\rC\x10\t\x07C\x06\x13\x07\x02\x07\tNC')
  252. # OPCODE=CONT, FIN=0, MSG="dear friends, "
  253. s.add_packet(b'\x00\x8eabcd\x05\x07\x02\x16A\x04\x11\r\x04\x0c\x07\x17MB')
  254. # OPCODE=CONT, FIN=1, MSG="once more"
  255. s.add_packet(b'\x80\x89abcd\x0e\x0c\x00\x01A\x0f\x0c\x16\x04')
  256. data = sock.recv()
  257. self.assertEqual(
  258. data,
  259. "Once more unto the breach, dear friends, once more")
  260. with self.assertRaises(ws.WebSocketConnectionClosedException):
  261. sock.recv()
  262. def testRecvWithFragmentationAndControlFrame(self):
  263. sock = ws.WebSocket()
  264. sock.set_mask_key(create_mask_key)
  265. s = sock.sock = SockMock()
  266. # OPCODE=TEXT, FIN=0, MSG="Too much "
  267. s.add_packet(b'\x01\x89abcd5\r\x0cD\x0c\x17\x00\x0cA')
  268. # OPCODE=PING, FIN=1, MSG="Please PONG this"
  269. s.add_packet(b'\x89\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17')
  270. # OPCODE=CONT, FIN=1, MSG="of a good thing"
  271. s.add_packet(b'\x80\x8fabcd\x0e\x04C\x05A\x05\x0c\x0b\x05B\x17\x0c\x08\x0c\x04')
  272. data = sock.recv()
  273. self.assertEqual(data, "Too much of a good thing")
  274. with self.assertRaises(ws.WebSocketConnectionClosedException):
  275. sock.recv()
  276. self.assertEqual(
  277. s.sent[0],
  278. b'\x8a\x90abcd1\x0e\x06\x05\x12\x07C4.,$D\x15\n\n\x17')
  279. @unittest.skipUnless(TEST_WITH_LOCAL_SERVER, "Tests using local websocket server are disabled")
  280. def testWebSocket(self):
  281. s = ws.create_connection("ws://127.0.0.1:" + LOCAL_WS_SERVER_PORT)
  282. self.assertNotEqual(s, None)
  283. s.send("Hello, World")
  284. result = s.next()
  285. s.fileno()
  286. self.assertEqual(result, "Hello, World")
  287. s.send("こにゃにゃちは、世界")
  288. result = s.recv()
  289. self.assertEqual(result, "こにゃにゃちは、世界")
  290. self.assertRaises(ValueError, s.send_close, -1, "")
  291. s.close()
  292. @unittest.skipUnless(TEST_WITH_LOCAL_SERVER, "Tests using local websocket server are disabled")
  293. def testPingPong(self):
  294. s = ws.create_connection("ws://127.0.0.1:" + LOCAL_WS_SERVER_PORT)
  295. self.assertNotEqual(s, None)
  296. s.ping("Hello")
  297. s.pong("Hi")
  298. s.close()
  299. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  300. def testSupportRedirect(self):
  301. s = ws.WebSocket()
  302. self.assertRaises(ws._exceptions.WebSocketBadStatusException, s.connect, "ws://google.com/")
  303. # Need to find a URL that has a redirect code leading to a websocket
  304. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  305. def testSecureWebSocket(self):
  306. import ssl
  307. s = ws.create_connection("wss://api.bitfinex.com/ws/2")
  308. self.assertNotEqual(s, None)
  309. self.assertTrue(isinstance(s.sock, ssl.SSLSocket))
  310. self.assertEqual(s.getstatus(), 101)
  311. self.assertNotEqual(s.getheaders(), None)
  312. s.settimeout(10)
  313. self.assertEqual(s.gettimeout(), 10)
  314. self.assertEqual(s.getsubprotocol(), None)
  315. s.abort()
  316. @unittest.skipUnless(TEST_WITH_LOCAL_SERVER, "Tests using local websocket server are disabled")
  317. def testWebSocketWithCustomHeader(self):
  318. s = ws.create_connection("ws://127.0.0.1:" + LOCAL_WS_SERVER_PORT,
  319. headers={"User-Agent": "PythonWebsocketClient"})
  320. self.assertNotEqual(s, None)
  321. self.assertEqual(s.getsubprotocol(), None)
  322. s.send("Hello, World")
  323. result = s.recv()
  324. self.assertEqual(result, "Hello, World")
  325. self.assertRaises(ValueError, s.close, -1, "")
  326. s.close()
  327. @unittest.skipUnless(TEST_WITH_LOCAL_SERVER, "Tests using local websocket server are disabled")
  328. def testAfterClose(self):
  329. s = ws.create_connection("ws://127.0.0.1:" + LOCAL_WS_SERVER_PORT)
  330. self.assertNotEqual(s, None)
  331. s.close()
  332. self.assertRaises(ws.WebSocketConnectionClosedException, s.send, "Hello")
  333. self.assertRaises(ws.WebSocketConnectionClosedException, s.recv)
  334. class SockOptTest(unittest.TestCase):
  335. @unittest.skipUnless(TEST_WITH_LOCAL_SERVER, "Tests using local websocket server are disabled")
  336. def testSockOpt(self):
  337. sockopt = ((socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),)
  338. s = ws.create_connection("ws://127.0.0.1:" + LOCAL_WS_SERVER_PORT, sockopt=sockopt)
  339. self.assertNotEqual(s.sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY), 0)
  340. s.close()
  341. class UtilsTest(unittest.TestCase):
  342. def testUtf8Validator(self):
  343. state = validate_utf8(b'\xf0\x90\x80\x80')
  344. self.assertEqual(state, True)
  345. state = validate_utf8(b'\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5\xed\xa0\x80edited')
  346. self.assertEqual(state, False)
  347. state = validate_utf8(b'')
  348. self.assertEqual(state, True)
  349. class HandshakeTest(unittest.TestCase):
  350. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  351. def test_http_SSL(self):
  352. websock1 = ws.WebSocket(sslopt={"cert_chain": ssl.get_default_verify_paths().capath}, enable_multithread=False)
  353. self.assertRaises(ValueError,
  354. websock1.connect, "wss://api.bitfinex.com/ws/2")
  355. websock2 = ws.WebSocket(sslopt={"certfile": "myNonexistentCertFile"})
  356. self.assertRaises(FileNotFoundError,
  357. websock2.connect, "wss://api.bitfinex.com/ws/2")
  358. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  359. def testManualHeaders(self):
  360. websock3 = ws.WebSocket(sslopt={"ca_certs": ssl.get_default_verify_paths().cafile,
  361. "ca_cert_path": ssl.get_default_verify_paths().capath})
  362. self.assertRaises(ws._exceptions.WebSocketBadStatusException,
  363. websock3.connect, "wss://api.bitfinex.com/ws/2", cookie="chocolate",
  364. origin="testing_websockets.com",
  365. host="echo.websocket.events/websocket-client-test",
  366. subprotocols=["testproto"],
  367. connection="Upgrade",
  368. header={"CustomHeader1":"123",
  369. "Cookie":"TestValue",
  370. "Sec-WebSocket-Key":"k9kFAUWNAMmf5OEMfTlOEA==",
  371. "Sec-WebSocket-Protocol":"newprotocol"})
  372. def testIPv6(self):
  373. websock2 = ws.WebSocket()
  374. self.assertRaises(ValueError, websock2.connect, "2001:4860:4860::8888")
  375. def testBadURLs(self):
  376. websock3 = ws.WebSocket()
  377. self.assertRaises(ValueError, websock3.connect, "ws//example.com")
  378. self.assertRaises(ws.WebSocketAddressException, websock3.connect, "ws://example")
  379. self.assertRaises(ValueError, websock3.connect, "example.com")
  380. if __name__ == "__main__":
  381. unittest.main()