Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.

test_abnf.py 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. #
  3. import websocket as ws
  4. from websocket._abnf import *
  5. import unittest
  6. """
  7. test_abnf.py
  8. websocket - WebSocket client library for Python
  9. Copyright 2023 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 testInit(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 testValidate(self):
  33. a_invalid_ping = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING)
  34. self.assertRaises(ws._exceptions.WebSocketProtocolException, a_invalid_ping.validate, skip_utf8_validation=False)
  35. a_bad_rsv_value = ABNF(0,1,0,0, opcode=ABNF.OPCODE_TEXT)
  36. self.assertRaises(ws._exceptions.WebSocketProtocolException, a_bad_rsv_value.validate, skip_utf8_validation=False)
  37. a_bad_opcode = ABNF(0,0,0,0, opcode=77)
  38. self.assertRaises(ws._exceptions.WebSocketProtocolException, a_bad_opcode.validate, skip_utf8_validation=False)
  39. a_bad_close_frame = ABNF(0,0,0,0, opcode=ABNF.OPCODE_CLOSE, data=b'\x01')
  40. self.assertRaises(ws._exceptions.WebSocketProtocolException, a_bad_close_frame.validate, skip_utf8_validation=False)
  41. a_bad_close_frame_2 = ABNF(0,0,0,0, opcode=ABNF.OPCODE_CLOSE, data=b'\x01\x8a\xaa\xff\xdd')
  42. self.assertRaises(ws._exceptions.WebSocketProtocolException, a_bad_close_frame_2.validate, skip_utf8_validation=False)
  43. a_bad_close_frame_3 = ABNF(0,0,0,0, opcode=ABNF.OPCODE_CLOSE, data=b'\x03\xe7')
  44. self.assertRaises(ws._exceptions.WebSocketProtocolException, a_bad_close_frame_3.validate, skip_utf8_validation=True)
  45. def testMask(self):
  46. abnf_none_data = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING, mask=1, data=None)
  47. bytes_val = b"aaaa"
  48. self.assertEqual(abnf_none_data._get_masked(bytes_val), bytes_val)
  49. abnf_str_data = ABNF(0,0,0,0, opcode=ABNF.OPCODE_PING, mask=1, data="a")
  50. self.assertEqual(abnf_str_data._get_masked(bytes_val), b'aaaa\x00')
  51. def testFormat(self):
  52. abnf_bad_rsv_bits = ABNF(2,0,0,0, opcode=ABNF.OPCODE_TEXT)
  53. self.assertRaises(ValueError, abnf_bad_rsv_bits.format)
  54. abnf_bad_opcode = ABNF(0,0,0,0, opcode=5)
  55. self.assertRaises(ValueError, abnf_bad_opcode.format)
  56. abnf_length_10 = ABNF(0,0,0,0, opcode=ABNF.OPCODE_TEXT, data="abcdefghij")
  57. self.assertEqual(b'\x01', abnf_length_10.format()[0].to_bytes(1, 'big'))
  58. self.assertEqual(b'\x8a', abnf_length_10.format()[1].to_bytes(1, 'big'))
  59. self.assertEqual("fin=0 opcode=1 data=abcdefghij", abnf_length_10.__str__())
  60. abnf_length_20 = ABNF(0,0,0,0, opcode=ABNF.OPCODE_BINARY, data="abcdefghijabcdefghij")
  61. self.assertEqual(b'\x02', abnf_length_20.format()[0].to_bytes(1, 'big'))
  62. self.assertEqual(b'\x94', abnf_length_20.format()[1].to_bytes(1, 'big'))
  63. abnf_no_mask = ABNF(0,0,0,0, opcode=ABNF.OPCODE_TEXT, mask=0, data=b'\x01\x8a\xcc')
  64. self.assertEqual(b'\x01\x03\x01\x8a\xcc', abnf_no_mask.format())
  65. def testFrameBuffer(self):
  66. fb = frame_buffer(0, True)
  67. self.assertEqual(fb.recv, 0)
  68. self.assertEqual(fb.skip_utf8_validation, True)
  69. fb.clear
  70. self.assertEqual(fb.header, None)
  71. self.assertEqual(fb.length, None)
  72. self.assertEqual(fb.mask, None)
  73. self.assertEqual(fb.has_mask(), False)
  74. if __name__ == "__main__":
  75. unittest.main()