test_websocket.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. # -*- coding: utf-8 -*-
  2. #
  3. """
  4. """
  5. """
  6. test_websocket.py
  7. websocket - WebSocket client library for Python
  8. Copyright 2021 engn33r
  9. Licensed under the Apache License, Version 2.0 (the "License");
  10. you may not use this file except in compliance with the License.
  11. You may obtain a copy of the License at
  12. http://www.apache.org/licenses/LICENSE-2.0
  13. Unless required by applicable law or agreed to in writing, software
  14. distributed under the License is distributed on an "AS IS" BASIS,
  15. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. See the License for the specific language governing permissions and
  17. limitations under the License.
  18. """
  19. import os
  20. import os.path
  21. import socket
  22. import websocket as ws
  23. from websocket._handshake import _create_sec_websocket_key, \
  24. _validate as _validate_header
  25. from websocket._http import read_headers
  26. from websocket._utils import validate_utf8
  27. from base64 import decodebytes as base64decode
  28. import unittest
  29. try:
  30. import ssl
  31. from ssl import SSLError
  32. except ImportError:
  33. # dummy class of SSLError for ssl none-support environment.
  34. class SSLError(Exception):
  35. pass
  36. # Skip test to access the internet unless TEST_WITH_INTERNET == 1
  37. TEST_WITH_INTERNET = os.environ.get('TEST_WITH_INTERNET', '0') == '1'
  38. # Skip tests relying on local websockets server unless LOCAL_WS_SERVER_PORT != -1
  39. LOCAL_WS_SERVER_PORT = os.environ.get('LOCAL_WS_SERVER_PORT', '-1')
  40. TEST_WITH_LOCAL_SERVER = LOCAL_WS_SERVER_PORT != '-1'
  41. TRACEABLE = True
  42. def create_mask_key(_):
  43. return "abcd"
  44. class SockMock:
  45. def __init__(self):
  46. self.data = []
  47. self.sent = []
  48. def add_packet(self, data):
  49. self.data.append(data)
  50. def gettimeout(self):
  51. return None
  52. def recv(self, bufsize):
  53. if self.data:
  54. e = self.data.pop(0)
  55. if isinstance(e, Exception):
  56. raise e
  57. if len(e) > bufsize:
  58. self.data.insert(0, e[bufsize:])
  59. return e[:bufsize]
  60. def send(self, data):
  61. self.sent.append(data)
  62. return len(data)
  63. def close(self):
  64. pass
  65. class HeaderSockMock(SockMock):
  66. def __init__(self, fname):
  67. SockMock.__init__(self)
  68. path = os.path.join(os.path.dirname(__file__), fname)
  69. with open(path, "rb") as f:
  70. self.add_packet(f.read())
  71. class WebSocketTest(unittest.TestCase):
  72. def setUp(self):
  73. ws.enableTrace(TRACEABLE)
  74. def tearDown(self):
  75. pass
  76. def testDefaultTimeout(self):
  77. self.assertEqual(ws.getdefaulttimeout(), None)
  78. ws.setdefaulttimeout(10)
  79. self.assertEqual(ws.getdefaulttimeout(), 10)
  80. ws.setdefaulttimeout(None)
  81. def testWSKey(self):
  82. key = _create_sec_websocket_key()
  83. self.assertTrue(key != 24)
  84. self.assertTrue(str("¥n") not in key)
  85. def testNonce(self):
  86. """ WebSocket key should be a random 16-byte nonce.
  87. """
  88. key = _create_sec_websocket_key()
  89. nonce = base64decode(key.encode("utf-8"))
  90. self.assertEqual(16, len(nonce))
  91. def testWsUtils(self):
  92. key = "c6b8hTg4EeGb2gQMztV1/g=="
  93. required_header = {
  94. "upgrade": "websocket",
  95. "connection": "upgrade",
  96. "sec-websocket-accept": "Kxep+hNu9n51529fGidYu7a3wO0="}
  97. self.assertEqual(_validate_header(required_header, key, None), (True, None))
  98. header = required_header.copy()
  99. header["upgrade"] = "http"
  100. self.assertEqual(_validate_header(header, key, None), (False, None))
  101. del header["upgrade"]
  102. self.assertEqual(_validate_header(header, key, None), (False, None))
  103. header = required_header.copy()
  104. header["connection"] = "something"
  105. self.assertEqual(_validate_header(header, key, None), (False, None))
  106. del header["connection"]
  107. self.assertEqual(_validate_header(header, key, None), (False, None))
  108. header = required_header.copy()
  109. header["sec-websocket-accept"] = "something"
  110. self.assertEqual(_validate_header(header, key, None), (False, None))
  111. del header["sec-websocket-accept"]
  112. self.assertEqual(_validate_header(header, key, None), (False, None))
  113. header = required_header.copy()
  114. header["sec-websocket-protocol"] = "sub1"
  115. self.assertEqual(_validate_header(header, key, ["sub1", "sub2"]), (True, "sub1"))
  116. # This case will print out a logging error using the error() function, but that is expected
  117. self.assertEqual(_validate_header(header, key, ["sub2", "sub3"]), (False, None))
  118. header = required_header.copy()
  119. header["sec-websocket-protocol"] = "sUb1"
  120. self.assertEqual(_validate_header(header, key, ["Sub1", "suB2"]), (True, "sub1"))
  121. header = required_header.copy()
  122. # This case will print out a logging error using the error() function, but that is expected
  123. self.assertEqual(_validate_header(header, key, ["Sub1", "suB2"]), (False, None))
  124. def testReadHeader(self):
  125. status, header, status_message = read_headers(HeaderSockMock("data/header01.txt"))
  126. self.assertEqual(status, 101)
  127. self.assertEqual(header["connection"], "Upgrade")
  128. status, header, status_message = read_headers(HeaderSockMock("data/header03.txt"))
  129. self.assertEqual(status, 101)
  130. self.assertEqual(header["connection"], "Upgrade, Keep-Alive")
  131. HeaderSockMock("data/header02.txt")
  132. self.assertRaises(ws.WebSocketException, read_headers, HeaderSockMock("data/header02.txt"))
  133. def testSend(self):
  134. # TODO: add longer frame data
  135. sock = ws.WebSocket()
  136. sock.set_mask_key(create_mask_key)
  137. s = sock.sock = HeaderSockMock("data/header01.txt")
  138. sock.send("Hello")
  139. self.assertEqual(s.sent[0], b'\x81\x85abcd)\x07\x0f\x08\x0e')
  140. sock.send("こんにちは")
  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. # sock.send("x" * 5000)
  143. # self.assertEqual(s.sent[1], b'\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc")
  144. self.assertEqual(sock.send_binary(b'1111111111101'), 19)
  145. def testRecv(self):
  146. # TODO: add longer frame data
  147. sock = ws.WebSocket()
  148. s = sock.sock = SockMock()
  149. something = b'\x81\x8fabcd\x82\xe3\xf0\x87\xe3\xf1\x80\xe5\xca\x81\xe2\xc5\x82\xe3\xcc'
  150. s.add_packet(something)
  151. data = sock.recv()
  152. self.assertEqual(data, "こんにちは")
  153. s.add_packet(b'\x81\x85abcd)\x07\x0f\x08\x0e')
  154. data = sock.recv()
  155. self.assertEqual(data, "Hello")
  156. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  157. def testIter(self):
  158. count = 2
  159. for _ in ws.create_connection('wss://stream.meetup.com/2/rsvps'):
  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://stream.meetup.com/2/rsvps')
  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. s.send("Hello, World")
  322. result = s.recv()
  323. self.assertEqual(result, "Hello, World")
  324. self.assertRaises(ValueError, s.close, -1, "")
  325. s.close()
  326. @unittest.skipUnless(TEST_WITH_LOCAL_SERVER, "Tests using local websocket server are disabled")
  327. def testAfterClose(self):
  328. s = ws.create_connection("ws://127.0.0.1:" + LOCAL_WS_SERVER_PORT)
  329. self.assertNotEqual(s, None)
  330. s.close()
  331. self.assertRaises(ws.WebSocketConnectionClosedException, s.send, "Hello")
  332. self.assertRaises(ws.WebSocketConnectionClosedException, s.recv)
  333. class SockOptTest(unittest.TestCase):
  334. @unittest.skipUnless(TEST_WITH_LOCAL_SERVER, "Tests using local websocket server are disabled")
  335. def testSockOpt(self):
  336. sockopt = ((socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),)
  337. s = ws.create_connection("ws://127.0.0.1:" + LOCAL_WS_SERVER_PORT, sockopt=sockopt)
  338. self.assertNotEqual(s.sock.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY), 0)
  339. s.close()
  340. class UtilsTest(unittest.TestCase):
  341. def testUtf8Validator(self):
  342. state = validate_utf8(b'\xf0\x90\x80\x80')
  343. self.assertEqual(state, True)
  344. state = validate_utf8(b'\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5\xed\xa0\x80edited')
  345. self.assertEqual(state, False)
  346. state = validate_utf8(b'')
  347. self.assertEqual(state, True)
  348. class HandshakeTest(unittest.TestCase):
  349. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  350. def test_http_SSL(self):
  351. websock1 = ws.WebSocket(sslopt={"cert_chain": ssl.get_default_verify_paths().capath}, enable_multithread=False)
  352. self.assertRaises(ValueError,
  353. websock1.connect, "wss://api.bitfinex.com/ws/2")
  354. websock2 = ws.WebSocket(sslopt={"certfile": "myNonexistentCertFile"})
  355. self.assertRaises(FileNotFoundError,
  356. websock2.connect, "wss://api.bitfinex.com/ws/2")
  357. @unittest.skipUnless(TEST_WITH_INTERNET, "Internet-requiring tests are disabled")
  358. def testManualHeaders(self):
  359. websock3 = ws.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE,
  360. "ca_certs": ssl.get_default_verify_paths().capath,
  361. "ca_cert_path": ssl.get_default_verify_paths().openssl_cafile})
  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.org/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()