def wait_message(self, socket): while (len(self.__buffer) < 2): self.__buffer.extend(socket.recv(1)) wl = Request.calculate_message_length(self.__buffer) while len(self.__buffer) < wl: self.__buffer.extend(socket.recv(1)) msg = self.__buffer[0:wl] self.__buffer = self.__buffer[wl:] req = Request(msg) logging.debug("request: %s" % req) res = self.__protocol.send(req) msg = res.get_message() logging.debug("response: %s" % bytes(msg)) socket.sendall(msg)
def test_create_write_auth_login_0(self): req = Request.create_write( 0x1f0000, b'\xb6\xd16\x04\x08\x0c\x87\xce\x81\xc1\x82\xc6o\xa5\xfb5') self.assertEqual( b'W\x07\x00\x00\x1f\x005z\xb6\xd16\x04\x08\x0c\x87\xce\x81\xc1\x82\xc6o\xa5\xfb5w\xaa', req.get_message())
def test_valid_short_data(self): req = Request.create_query(0xa000, 0) # client hello res = Response(req) res_msg = bytearray(req.get_message()) res_msg.extend(b'\x00\x00') res.set_message(res_msg) self.assertFalse(res.valid())
def test_validate_wrong_data(self): req = Request.create_query(0xa000, 0) # client hello res = Response(req) res_msg = bytearray(req.get_message()) res_msg.extend(b'\x01\x02') res_msg.extend(b'\x03\x04') res.set_message(res_msg) response = b'12345678901234567890123456' with self.assertRaises(ValidationException) as context: res.validate() self.assertEqual('Incorrect CRC (0x4fc5)', context.exception.args[0])
def test_expected_length_0(self): req = Request.create_query(0xa000, 0) self.assertEqual(12, Response(req).expected_length())
def test_valid_no_data(self): req = Request.create_query(0xa000, 0) # client hello res = Response(req) self.assertFalse(res.valid())
def test_write_expected_length_07(self): req = Request.create_write(0x001f, b'00' * 8) self.assertEqual(26, Response(req).expected_length())
def test_str_abcd_4(self): msg = b'\x51\x03\xcd\xab\x00\x00' request = Request(msg) self.assertEqual('Query(0xabcd-abd1)', str(request))
def test_as_bytes_hello(self): req = Request.create_query(0xa000, 0) expected = b'Q\x00\x00\xa0\x00\x00\x9dK' self.assertEqual(expected, req.get_message())
def test_str_hello(self): msg = b'\x51\x00\x00\xa0\x00\x00\x9d\x4b' request = Request(msg) self.assertEqual('Query(0xa000)', str(request))
def test_calculate_message_length(self): self.assertEqual(8, Request.calculate_message_length(b'Q\x00')) self.assertEqual(8, Request.calculate_message_length(b'Q\x0f')) self.assertEqual(8, Request.calculate_message_length(b'Q\xff')) self.assertEqual(16, Request.calculate_message_length(b'W\x02'))
def test_word_length_ff(self): self.assertEqual(256, Request.create_query(0xa000, 0xff).get_word_length())
def test_word_length_0(self): self.assertEqual(16, Request.create_query(0xa000, 15).get_word_length())
def test_as_bytes_load_power(self): req = Request.create_query(0xa093, 3) expected = b'\x51\x03\x93\xa0\x00\x00\x53\x9d' self.assertEqual(expected, req.get_message())