test_abnf.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. #
  3. import unittest
  4. from websocket._abnf import ABNF, frame_buffer
  5. from websocket._exceptions import WebSocketProtocolException
  6. """
  7. test_abnf.py
  8. websocket - WebSocket client library for Python
  9. Copyright 2024 engn33r
  10. Licensed under the Apache License, Version 2.0 (the "License");
  11. you may not use this file except in compliance with the License.
  12. You may obtain a copy of the License at
  13. http://www.apache.org/licenses/LICENSE-2.0
  14. Unless required by applicable law or agreed to in writing, software
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. """
  20. class ABNFTest(unittest.TestCase):
  21. def test_init(self):
  22. a = ABNF(0, 0, 0, 0, opcode=ABNF.OPCODE_PING)
  23. self.assertEqual(a.fin, 0)
  24. self.assertEqual(a.rsv1, 0)
  25. self.assertEqual(a.rsv2, 0)
  26. self.assertEqual(a.rsv3, 0)
  27. self.assertEqual(a.opcode, 9)
  28. self.assertEqual(a.data, "")
  29. a_bad = ABNF(0, 1, 0, 0, opcode=77)
  30. self.assertEqual(a_bad.rsv1, 1)
  31. self.assertEqual(a_bad.opcode, 77)
  32. def test_validate(self):
  33. a_invalid_ping = ABNF(0, 0, 0, 0, opcode=ABNF.OPCODE_PING)
  34. self.assertRaises(
  35. WebSocketProtocolException,
  36. a_invalid_ping.validate,
  37. skip_utf8_validation=False,
  38. )
  39. a_bad_rsv_value = ABNF(0, 1, 0, 0, opcode=ABNF.OPCODE_TEXT)
  40. self.assertRaises(
  41. WebSocketProtocolException,
  42. a_bad_rsv_value.validate,
  43. skip_utf8_validation=False,
  44. )
  45. a_bad_opcode = ABNF(0, 0, 0, 0, opcode=77)
  46. self.assertRaises(
  47. WebSocketProtocolException,
  48. a_bad_opcode.validate,
  49. skip_utf8_validation=False,
  50. )
  51. a_bad_close_frame = ABNF(0, 0, 0, 0, opcode=ABNF.OPCODE_CLOSE, data=b"\x01")
  52. self.assertRaises(
  53. WebSocketProtocolException,
  54. a_bad_close_frame.validate,
  55. skip_utf8_validation=False,
  56. )
  57. a_bad_close_frame_2 = ABNF(
  58. 0, 0, 0, 0, opcode=ABNF.OPCODE_CLOSE, data=b"\x01\x8a\xaa\xff\xdd"
  59. )
  60. self.assertRaises(
  61. WebSocketProtocolException,
  62. a_bad_close_frame_2.validate,
  63. skip_utf8_validation=False,
  64. )
  65. a_bad_close_frame_3 = ABNF(
  66. 0, 0, 0, 0, opcode=ABNF.OPCODE_CLOSE, data=b"\x03\xe7"
  67. )
  68. self.assertRaises(
  69. WebSocketProtocolException,
  70. a_bad_close_frame_3.validate,
  71. skip_utf8_validation=True,
  72. )
  73. def test_mask(self):
  74. abnf_none_data = ABNF(
  75. 0, 0, 0, 0, opcode=ABNF.OPCODE_PING, mask_value=1, data=None
  76. )
  77. bytes_val = b"aaaa"
  78. self.assertEqual(abnf_none_data._get_masked(bytes_val), bytes_val)
  79. abnf_str_data = ABNF(
  80. 0, 0, 0, 0, opcode=ABNF.OPCODE_PING, mask_value=1, data="a"
  81. )
  82. self.assertEqual(abnf_str_data._get_masked(bytes_val), b"aaaa\x00")
  83. def test_format(self):
  84. abnf_bad_rsv_bits = ABNF(2, 0, 0, 0, opcode=ABNF.OPCODE_TEXT)
  85. self.assertRaises(ValueError, abnf_bad_rsv_bits.format)
  86. abnf_bad_opcode = ABNF(0, 0, 0, 0, opcode=5)
  87. self.assertRaises(ValueError, abnf_bad_opcode.format)
  88. abnf_length_10 = ABNF(0, 0, 0, 0, opcode=ABNF.OPCODE_TEXT, data="abcdefghij")
  89. self.assertEqual(b"\x01", abnf_length_10.format()[0].to_bytes(1, "big"))
  90. self.assertEqual(b"\x8a", abnf_length_10.format()[1].to_bytes(1, "big"))
  91. self.assertEqual("fin=0 opcode=1 data=abcdefghij", abnf_length_10.__str__())
  92. abnf_length_20 = ABNF(
  93. 0, 0, 0, 0, opcode=ABNF.OPCODE_BINARY, data="abcdefghijabcdefghij"
  94. )
  95. self.assertEqual(b"\x02", abnf_length_20.format()[0].to_bytes(1, "big"))
  96. self.assertEqual(b"\x94", abnf_length_20.format()[1].to_bytes(1, "big"))
  97. abnf_no_mask = ABNF(
  98. 0, 0, 0, 0, opcode=ABNF.OPCODE_TEXT, mask_value=0, data=b"\x01\x8a\xcc"
  99. )
  100. self.assertEqual(b"\x01\x03\x01\x8a\xcc", abnf_no_mask.format())
  101. def test_frame_buffer(self):
  102. fb = frame_buffer(0, True)
  103. self.assertEqual(fb.recv, 0)
  104. self.assertEqual(fb.skip_utf8_validation, True)
  105. fb.clear
  106. self.assertEqual(fb.header, None)
  107. self.assertEqual(fb.length, None)
  108. self.assertEqual(fb.mask_value, None)
  109. self.assertEqual(fb.has_mask(), False)
  110. if __name__ == "__main__":
  111. unittest.main()