Ejemplo n.º 1
0
    def test_that_exceeding_maximum_frame_length_should_treat_exceeded_frame_as_invalid(
            self, unused_tcp_port):
        first_middleman_message = GolemMessageFrame(Ping(), self.request_id)
        first_raw_message = append_frame_separator(
            escape_encode_raw_message(
                first_middleman_message.serialize(
                    private_key=CONCENT_PRIVATE_KEY)))
        second_middleman_message = AuthenticationChallengeFrame(
            payload=b'',
            request_id=100,
        )
        second_raw_message = append_frame_separator(
            escape_encode_raw_message(
                second_middleman_message.serialize(
                    private_key=CONCENT_PRIVATE_KEY)))

        assert len(first_raw_message) > len(second_raw_message) + 10

        with closing(socket.socket(socket.AF_INET,
                                   socket.SOCK_STREAM)) as server_socket:
            server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                                     True)
            with closing(socket.socket(socket.AF_INET,
                                       socket.SOCK_STREAM)) as client_socket:
                client_socket.setsockopt(socket.SOL_SOCKET,
                                         socket.SO_REUSEADDR, True)
                server_socket.bind(('127.0.0.1', unused_tcp_port))
                server_socket.listen(1)

                client_socket.connect(('127.0.0.1', unused_tcp_port))

                (connection, _address) = server_socket.accept()

                client_socket.send(first_raw_message)
                client_socket.send(second_raw_message)

                with mock.patch(
                        'middleman_protocol.stream.MAXIMUM_FRAME_LENGTH',
                        len(first_raw_message) - 10):
                    raw_message_received = next(
                        unescape_stream(connection=connection))

        deserialized_message = AbstractFrame.deserialize(
            raw_message=raw_message_received,
            public_key=CONCENT_PUBLIC_KEY,
        )

        assertpy.assert_that(deserialized_message.request_id).is_equal_to(100)
Ejemplo n.º 2
0
    async def test_that_received_item_received_via_response_queue_is_sent_to_concent(self, event_loop):
        with override_settings(
            CONCENT_PRIVATE_KEY=CONCENT_PRIVATE_KEY,
            CONCENT_PUBLIC_KEY=CONCENT_PUBLIC_KEY,
            SIGNING_SERVICE_PUBLIC_KEY=SIGNING_SERVICE_PUBLIC_KEY
        ):
            connection_id = 11
            concent_request_id = 77
            response_queue = Queue(loop=event_loop)
            golem_message = Ping()
            response_queue_item = ResponseQueueItem(golem_message, concent_request_id, FROZEN_TIMESTAMP)
            expected_data = append_frame_separator(
                escape_encode_raw_message(
                    GolemMessageFrame(golem_message, concent_request_id).serialize(settings.CONCENT_PRIVATE_KEY)
                )
            )
            mocked_writer = prepare_mocked_writer()

            await response_queue.put(response_queue_item)
            consumer_task = event_loop.create_task(
                response_consumer(
                    response_queue,
                    mocked_writer,
                    connection_id
                )
            )
            await response_queue.join()
            consumer_task.cancel()

            mocked_writer.write.assert_called_once_with(expected_data)
            mocked_writer.drain.mock.assert_called_once_with()
Ejemplo n.º 3
0
    async def test_that_when_connection_exists_item_from_the_queue_is_sent_via_writer(self, event_loop):
        with patch("middleman.asynchronous_operations.logger") as mocked_logger:
            with override_settings(
                CONCENT_PRIVATE_KEY=CONCENT_PRIVATE_KEY,
                CONCENT_PUBLIC_KEY=CONCENT_PUBLIC_KEY,
            ):
                expected_data = append_frame_separator(
                    escape_encode_raw_message(
                        GolemMessageFrame(self.golem_message, self.signing_service_request_id).serialize(CONCENT_PRIVATE_KEY)
                    )
                )

                await self.queue.put(self.request_queue_item)
                consumer_task = event_loop.create_task(
                    request_consumer(
                        self.queue,
                        self.queue_pool,
                        self.message_tracker,
                        self.mocked_writer
                    )
                )
                await self.queue.join()
                consumer_task.cancel()

                assert_that(self.message_tracker[self.signing_service_request_id]).is_equal_to(
                    MessageTrackerItem(
                        self.request_id,
                        self.connection_id,
                        self.golem_message,
                        FROZEN_TIMESTAMP
                    )
                )
                mocked_logger.debug.assert_not_called()
                self.mocked_writer.write.assert_called_once_with(expected_data)
                self.mocked_writer.drain.mock.assert_called_once_with()
Ejemplo n.º 4
0
    def test_that_receiving_a_series_of_messages_should_be_handled_correctly(
            self, unused_tcp_port):
        payload = Ping()
        middleman_message = GolemMessageFrame(payload, self.request_id)
        raw_message = escape_encode_raw_message(
            middleman_message.serialize(private_key=CONCENT_PRIVATE_KEY))
        raw_message_with_separator = append_frame_separator(raw_message)

        with closing(socket.socket(socket.AF_INET,
                                   socket.SOCK_STREAM)) as server_socket:
            server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                                     True)
            with closing(socket.socket(socket.AF_INET,
                                       socket.SOCK_STREAM)) as client_socket:
                client_socket.setsockopt(socket.SOL_SOCKET,
                                         socket.SO_REUSEADDR, True)
                server_socket.bind(('127.0.0.1', unused_tcp_port))
                server_socket.listen(1)

                client_socket.connect(('127.0.0.1', unused_tcp_port))

                (connection, _address) = server_socket.accept()

                for _i in range(10):
                    client_socket.send(raw_message_with_separator)

                split_stream_generator = split_stream(connection=connection)

                for _i in range(10):
                    raw_message_received = next(split_stream_generator)

                    assertpy.assert_that(raw_message).is_equal_to(
                        raw_message_received)
Ejemplo n.º 5
0
    def test_that_receiving_wrongly_encoded_message_should_return_none(
            self, unused_tcp_port):
        middleman_message = GolemMessageFrame(Ping(), self.request_id)
        raw_message = middleman_message.serialize(
            private_key=CONCENT_PRIVATE_KEY, )

        raw_message_encoded = escape_encode_raw_message(raw_message)
        raw_message_encoded = raw_message_encoded + ESCAPE_CHARACTER + b'\xff'
        raw_message_encoded = append_frame_separator(raw_message_encoded)

        with closing(socket.socket(socket.AF_INET,
                                   socket.SOCK_STREAM)) as server_socket:
            server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                                     True)
            with closing(socket.socket(socket.AF_INET,
                                       socket.SOCK_STREAM)) as client_socket:
                client_socket.setsockopt(socket.SOL_SOCKET,
                                         socket.SO_REUSEADDR, True)
                server_socket.bind(('127.0.0.1', unused_tcp_port))
                server_socket.listen(1)

                client_socket.connect(('127.0.0.1', unused_tcp_port))

                (connection, _address) = server_socket.accept()

                client_socket.send(raw_message_encoded)
                raw_message_received = next(
                    unescape_stream(connection=connection))

        assertpy.assert_that(raw_message_received).is_none()
Ejemplo n.º 6
0
    def test_that_remove_frame_separator_should_raise_exception_if_separator_is_not_at_the_end(
            self):
        raw_with_separator = append_frame_separator(b'12345') + b'1'

        assert FRAME_SEPARATOR in raw_with_separator

        with self.assertRaises(AssertionError):
            remove_frame_separator(raw_with_separator)
Ejemplo n.º 7
0
def _get_mocked_reader(message, request_id, sign_as, **kwargs):
    protocol_message = create_middleman_protocol_message(PayloadType.GOLEM_MESSAGE, message, request_id)
    data_to_send = append_frame_separator(
        escape_encode_raw_message(
            protocol_message.serialize(sign_as)
        )
    )
    mocked_reader = prepare_mocked_reader(data_to_send, **kwargs)
    return mocked_reader
Ejemplo n.º 8
0
    def test_that_remove_frame_separator_should_remove_frame_separator_from_given_bytes(
            self):
        raw_with_separator = append_frame_separator(b'12345')

        assert FRAME_SEPARATOR in raw_with_separator

        raw = remove_frame_separator(raw_with_separator)

        self.assertNotIn(FRAME_SEPARATOR, raw)
Ejemplo n.º 9
0
    def test_that_append_frame_separator_should_add_frame_separator_to_given_bytes(
            self):
        raw = b'12345'

        assert FRAME_SEPARATOR not in raw

        raw_with_separator = append_frame_separator(raw)

        self.assertIn(FRAME_SEPARATOR, raw_with_separator)
Ejemplo n.º 10
0
    def test_that_remove_frame_separator_should_remove_frame_separator_from_given_bytes_only_at_the_end(
            self):
        raw_with_separator = FRAME_SEPARATOR + append_frame_separator(b'12345')

        assert FRAME_SEPARATOR in raw_with_separator

        raw = remove_frame_separator(raw_with_separator)

        self.assertTrue(raw.startswith(FRAME_SEPARATOR))
        self.assertFalse(raw.endswith(FRAME_SEPARATOR))
Ejemplo n.º 11
0
def test_that_sent_data_is_escaped_and_contains_frame_separator(event_loop):
    frame = GolemMessageFrame(Ping(), 777)
    expected_data = append_frame_separator(
        escape_encode_raw_message(frame.serialize(CONCENT_PRIVATE_KEY)))
    mocked_writer = prepare_mocked_writer()

    task = _run_test_in_event_loop(event_loop, send_over_stream_async, frame,
                                   mocked_writer, CONCENT_PRIVATE_KEY)

    assert_that(task.done()).is_true()
    mocked_writer.write.assert_called_once_with(expected_data)
    mocked_writer.drain.mock.assert_called_once_with()
Ejemplo n.º 12
0
    def test_that_receiving_encoded_message_should_decode_on_the_fly(
            self, unused_tcp_port):
        middleman_message = GolemMessageFrame(Ping(), self.request_id)
        raw_message = append_frame_separator(
            middleman_message.serialize(private_key=CONCENT_PRIVATE_KEY))

        raw_message = raw_message[:10] + ESCAPE_CHARACTER + raw_message[
            len(ESCAPE_CHARACTER) + 10:]
        raw_message_encoded = escape_encode_raw_message(raw_message)

        assert FRAME_SEPARATOR not in raw_message_encoded

        raw_message_encoded = append_frame_separator(raw_message_encoded)

        with closing(socket.socket(socket.AF_INET,
                                   socket.SOCK_STREAM)) as server_socket:
            server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                                     True)
            with closing(socket.socket(socket.AF_INET,
                                       socket.SOCK_STREAM)) as client_socket:
                client_socket.setsockopt(socket.SOL_SOCKET,
                                         socket.SO_REUSEADDR, True)
                server_socket.bind(('127.0.0.1', unused_tcp_port))
                server_socket.listen(1)

                client_socket.connect(('127.0.0.1', unused_tcp_port))

                (connection, _address) = server_socket.accept()

                client_socket.send(raw_message_encoded)
                raw_message_received = next(
                    unescape_stream(connection=connection))

        assertpy_bytes_starts_with(raw_message, raw_message_received)
        assertpy.assert_that(
            len(raw_message_received)).is_greater_than_or_equal_to(
                FRAME_PAYLOAD_STARTING_BYTE)
Ejemplo n.º 13
0
 def setUp(self, unused_tcp_port_factory, event_loop):
     golem_message_frame = GolemMessageFrame(
         Ping(), 777).serialize(CONCENT_PRIVATE_KEY)
     self.patcher = mock.patch("middleman.middleman_server.crash_logger")
     self.crash_logger_mock = self.patcher.start()
     self.internal_port, self.external_port = unused_tcp_port_factory(
     ), unused_tcp_port_factory()
     self.data_to_send = append_frame_separator(
         escape_encode_raw_message(golem_message_frame))
     self.timeout = 0.2
     self.short_delay = 0.1
     self.middleman = MiddleMan(internal_port=self.internal_port,
                                external_port=self.external_port,
                                loop=event_loop)
     yield self.internal_port, self.external_port
     self.patcher.stop()
Ejemplo n.º 14
0
def test_heartbeat_producer_sends_heartbeat_in_time_intervals(event_loop):
    with override_settings(
        CONCENT_PRIVATE_KEY=CONCENT_PRIVATE_KEY
    ):
        mocked_writer = prepare_mocked_writer()
        expected_data = append_frame_separator(
            escape_encode_raw_message(
                HeartbeatFrame(None, HEARTBEAT_REQUEST_ID).serialize(CONCENT_PRIVATE_KEY)
            )
        )
        heartbeat_producer_task = event_loop.create_task(
            heartbeat_producer(
                mocked_writer,
            )
        )
        with patch(
            "middleman.asynchronous_operations.sleep",
            new=async_stream_actor_mock(side_effect=lambda _: heartbeat_producer_task.cancel())
        ) as sleep_mock:
            with suppress(CancelledError):
                event_loop.run_until_complete(heartbeat_producer_task)
            mocked_writer.write.assert_called_with(expected_data)
            mocked_writer.drain.mock.assert_called()
            sleep_mock.mock.assert_called_with(HEARTBEAT_INTERVAL)
Ejemplo n.º 15
0
async def send_over_stream_async(frame: AbstractFrame,
                                 writer: asyncio.StreamWriter,
                                 private_key: bytes) -> None:
    data = frame.serialize(private_key)
    writer.write(append_frame_separator(escape_encode_raw_message(data)))
    await writer.drain()
Ejemplo n.º 16
0
 def _prepare_mocked_reader(frame: AbstractFrame, private_key: bytes = SIGNING_SERVICE_PRIVATE_KEY):
     serialized = frame.serialize(private_key)
     data_to_send = append_frame_separator(escape_encode_raw_message(serialized))
     return prepare_mocked_reader(data_to_send)