def test_invalid_protocol_header(self): with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(b'AMQP\x00\x00\t') self.assertTrue( str(err).startswith( "Could not unmarshal <class 'pamqp.header.ProtocolHeader'> " 'frame: Data did not match the ProtocolHeader format'))
def test_frame_with_no_length(self): frame_data = (b'\x01\x00\x01\x00\x00\x00\x00\x00<\x00P\x00\x00\x00\x00' b'\x00\x00\x00\x01\x00\xce') with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(frame_data) self.assertEqual( str(err), 'Could not unmarshal Unknown frame: No frame size')
def test_frame_malformed_end_byte(self): frame_data = (b'\x01\x00\x01\x00\x00\x00\r\x00<\x00P\x00\x00\x00\x00' b'\x00\x00\x00\x01\x00\x00') with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(frame_data) self.assertEqual( str(err), 'Could not unmarshal Unknown frame: Last byte error')
def test_frame_malformed_length(self): frame_data = (b'\x01\x00\x01\x00\x00\x00\x0c\x00<\x00P\x00\x00\x00\x00' b'\x00\x00\x00\xce') with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(frame_data) self.assertEqual( str(err), 'Could not unmarshal Unknown frame: Not all data received')
def test_invalid_frame_header(self): frame_data = struct.pack('>BI', 255, 0) try: frame.unmarshal(frame_data) except exceptions.UnmarshalingException as err: self.assertEqual( str(err), 'Could not unmarshal Unknown frame: No frame size') else: assert False, 'Failed to raise exception'
def test_invalid_protocol_header(self): try: frame.unmarshal(b'AMQP\x00\x00\t') except exceptions.UnmarshalingException as err: self.assertTrue(str(err).startswith( "Could not unmarshal <class 'pamqp.header.ProtocolHeader'> " 'frame: Data did not match the ProtocolHeader format')) else: assert False, 'Failed to raise exception'
def test_invalid_method_frame_index(self): payload = struct.pack('>L', 42949) frame_value = b''.join([ struct.pack('>BHI', 1, 0, len(payload)), payload, constants.FRAME_END_CHAR ]) with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(frame_value) self.assertEqual(str(err), ('Could not unmarshal Unknown frame: ' 'Unknown method index: 42949'))
def test_invalid_protocol_header(self): try: frame.unmarshal(b'AMQP\x00\x00\t') except exceptions.UnmarshalingException as err: self.assertTrue( str(err).startswith( "Could not unmarshal <class 'pamqp.header.ProtocolHeader'> " "frame: Data did not match the ProtocolHeader format")) else: assert False, 'Failed to raise exception'
def test_frame_with_no_length(self): frame_data = (b'\x01\x00\x01\x00\x00\x00\x00\x00<\x00P\x00\x00\x00\x00' b'\x00\x00\x00\x01\x00\xce') try: frame.unmarshal(frame_data) except exceptions.UnmarshalingException as err: self.assertEqual( str(err), 'Could not unmarshal Unknown frame: No frame size') else: assert False, 'Failed to raise exception'
def test_frame_malformed_end_byte(self): frame_data = (b'\x01\x00\x01\x00\x00\x00\r\x00<\x00P\x00\x00\x00\x00' b'\x00\x00\x00\x01\x00\x00') try: frame.unmarshal(frame_data) except exceptions.UnmarshalingException as err: self.assertEqual( str(err), 'Could not unmarshal Unknown frame: Last byte error') else: assert False, 'Failed to raise exception'
def test_malformed_frame_content(self): payload = struct.pack('>HxxQ', 8192, 32768) frame_value = b''.join([ struct.pack('>BHI', 5, 0, len(payload)), payload, constants.FRAME_END_CHAR ]) with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(frame_value) self.assertEqual( str(err), 'Could not unmarshal Unknown frame: Unknown frame type: 5')
def test_invalid_method_frame_content(self): payload = struct.pack('>L', 0x000A0029) frame_value = b''.join([struct.pack('>BHI', 1, 0, len(payload)), payload, frame.FRAME_END_CHAR]) try: frame.unmarshal(frame_value) except exceptions.UnmarshalingException as err: self.assertTrue(str(err).startswith( 'Could not unmarshal <pamqp.specification.Connection.OpenOk')) else: assert False, 'Failed to raise exception'
def test_frame_malformed_length(self): frame_data = (b'\x01\x00\x01\x00\x00\x00\x0c\x00<\x00P\x00\x00\x00\x00' b'\x00\x00\x00\xce') try: frame.unmarshal(frame_data) except exceptions.UnmarshalingException as err: self.assertEqual( str(err), 'Could not unmarshal Unknown frame: Not all data received') else: assert False, 'Failed to raise exception'
def test_invalid_content_header_frame(self): payload = struct.pack('>L', 0x000A0029) frame_value = b''.join([struct.pack('>BHI', 2, 0, len(payload)), payload, frame.FRAME_END_CHAR]) try: frame.unmarshal(frame_value) except exceptions.UnmarshalingException as err: self.assertTrue(str(err).startswith( 'Could not unmarshal ContentHeader frame:')) else: assert False, 'Failed to raise exception'
def test_invalid_content_header_frame(self): payload = struct.pack('>L', 0x000A0029) frame_value = b''.join([ struct.pack('>BHI', 2, 0, len(payload)), payload, constants.FRAME_END_CHAR ]) with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(frame_value) self.assertTrue( str(err).startswith( 'Could not unmarshal ContentHeader frame:'))
def test_malformed_frame_content(self): payload = struct.pack('>HxxQ', 8192, 32768) frame_value = b''.join([struct.pack('>BHI', 5, 0, len(payload)), payload, frame.FRAME_END_CHAR]) try: frame.unmarshal(frame_value) except exceptions.UnmarshalingException as err: self.assertEqual( str(err), 'Could not unmarshal Unknown frame: Unknown frame type: 5') else: assert False, 'Failed to raise exception'
def test_invalid_method_frame_content(self): payload = struct.pack('>L', 0x000A0029) frame_value = b''.join([ struct.pack('>BHI', 1, 0, len(payload)), payload, constants.FRAME_END_CHAR ]) with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(frame_value) self.assertTrue( str(err).startswith( 'Could not unmarshal <pamqp.specification.Connection.OpenOk' ))
def test_invalid_method_frame_index(self): payload = struct.pack('>L', 42949) frame_value = b''.join([ struct.pack('>BHI', 1, 0, len(payload)), payload, frame.FRAME_END_CHAR ]) try: frame.unmarshal(frame_value) except exceptions.UnmarshalingException as err: self.assertEqual(str(err), ('Could not unmarshal Unknown frame: ' 'Unknown method index: 42949')) else: assert False, 'Failed to raise exception'
def test_invalid_method_frame_index(self): payload = struct.pack('>L', 42949) frame_value = b''.join([struct.pack('>BHI', 1, 0, len(payload)), payload, frame.FRAME_END_CHAR]) try: frame.unmarshal(frame_value) except exceptions.UnmarshalingException as err: self.assertEqual( str(err), ('Could not unmarshal Unknown frame: ' 'Unknown method index: 42949')) else: assert False, 'Failed to raise exception'
def test_malformed_frame_content(self): payload = struct.pack('>HxxQ', 8192, 32768) frame_value = b''.join([ struct.pack('>BHI', 5, 0, len(payload)), payload, frame.FRAME_END_CHAR ]) try: frame.unmarshal(frame_value) except exceptions.UnmarshalingException as err: self.assertEqual( str(err), 'Could not unmarshal Unknown frame: Unknown frame type: 5') else: assert False, 'Failed to raise exception'
def test_invalid_content_header_frame(self): payload = struct.pack('>L', 0x000A0029) frame_value = b''.join([ struct.pack('>BHI', 2, 0, len(payload)), payload, frame.FRAME_END_CHAR ]) try: frame.unmarshal(frame_value) except exceptions.UnmarshalingException as err: self.assertTrue( str(err).startswith( 'Could not unmarshal ContentHeader frame:')) else: assert False, 'Failed to raise exception'
def test_invalid_method_frame_content(self): payload = struct.pack('>L', 0x000A0029) frame_value = b''.join([ struct.pack('>BHI', 1, 0, len(payload)), payload, frame.FRAME_END_CHAR ]) try: frame.unmarshal(frame_value) except exceptions.UnmarshalingException as err: self.assertTrue( str(err).startswith( 'Could not unmarshal <pamqp.specification.Connection.OpenOk' )) else: assert False, 'Failed to raise exception'
def data_received(self, data: bytes) -> None: self.buffer += data while self.buffer: try: count, channel, value = frame.unmarshal(self.buffer) except exceptions.UnmarshalingException as error: LOGGER.warning('Failed to unmarshal a frame: %r', error) LOGGER.debug('Bad frame: %r', self.buffer) break else: self.buffer = self.buffer[count:] self.loop.call_soon(self.on_frame_received, channel, value)
async def _reader_task(self, reader: StreamReader) -> None: buffer = b"" while not reader.at_eof(): chunk = await reader.read(1) if not chunk: break buffer += chunk try: byte_count, channel_id, frame = unmarshal(buffer) except UnmarshalingException: continue else: buffer = b"" _logger.debug(f"<- {frame.name} {channel_id}") await self.dispatch_frame(frame, channel_id)
def _handle_amqp_frame(data_in): """Unmarshal any incoming RabbitMQ frames and return the result. :param data_in: socket data :return: buffer, channel_id, frame """ if not data_in: return data_in, None, None try: byte_count, channel_id, frame_in = pamqp_frame.unmarshal(data_in) return data_in[byte_count:], channel_id, frame_in except pamqp_exception.UnmarshalingException: return data_in, None, None except pamqp_spec.AMQPFrameError as why: LOGGER.error('AMQPFrameError: %r', why, exc_info=True) return data_in, None, None
def _handle_amqp_frame(self, data_in): """Unmarshal a single AMQP frame and return the result. :param data_in: socket data :return: data_in, channel_id, frame """ if not data_in: return data_in, None, None try: byte_count, channel_id, frame_in = pamqp_frame.unmarshal(data_in) return data_in[byte_count:], channel_id, frame_in except pamqp_exception.UnmarshalingException: pass except pamqp_spec.AMQPFrameError as why: LOGGER.error('AMQPFrameError: %r', why, exc_info=True) except ValueError as why: LOGGER.error(why, exc_info=True) self.exceptions.append(AMQPConnectionError(why)) return data_in, None, None
def _handle_amqp_frame(self, data_in): """Unmarshal a single AMQP frame and return the result. :param data_in: socket data :return: data_in, channel_id, frame """ if not data_in: return data_in, None, None try: byte_count, channel_id, frame_in = pamqp_frame.unmarshal(data_in) return data_in[byte_count:], channel_id, frame_in except pamqp_exception.UnmarshalingException: pass except specification.AMQPFrameError as why: LOGGER.error('AMQPFrameError: %r', why, exc_info=True) except ValueError as why: LOGGER.error(why, exc_info=True) self.exceptions.append(AMQPConnectionError(why)) return data_in, None, None
def _get_frame_from_str(value): """Get the pamqp frame from the string value. :param str value: The value to parse for an pamqp frame :return (str, int, pamqp.specification.Frame): Remainder of value, channel id and frame value """ if not value: return value, None, None try: byte_count, channel_id, frame_in = frame.unmarshal(value) except pamqp_exceptions.UnmarshalingException: return value, None, None except specification.AMQPFrameError as error: LOGGER.error('Failed to demarshal: %r', error, exc_info=True) LOGGER.debug(value) return value, None, None return value[byte_count:], channel_id, frame_in
def _transport_write(self, value: bytes) -> typing.NoReturn: count, channel, frame_value = frame.unmarshal(value) self.assertEqual(count, len(value), 'All bytes used') self.assertEqual(channel, 0, 'Frame was published on channel 0') if frame_value.name == 'ProtocolHeader': self.loop.call_soon(self._connection_start) elif frame_value.name == 'Connection.StartOk': self.loop.call_soon(self._connection_tune) elif frame_value.name == 'Connection.TuneOk': pass elif frame_value.name == 'Connection.Open': self.loop.call_soon(self._connection_open_ok) elif frame_value.name == 'Connection.Close': self.loop.call_soon(self._connection_close_ok) elif frame_value.name == 'Connection.CloseOk': pass elif frame_value.name == 'Heartbeat': self.heartbeat.set() else: raise RuntimeError(count, channel, frame_value)
def test_invalid_frame_header(self): frame_data = struct.pack('>BI', 255, 0) with self.assertRaises(exceptions.UnmarshalingException) as err: frame.unmarshal(frame_data) self.assertEqual( str(err), 'Could not unmarshal Unknown frame: No frame size')