Esempio n. 1
0
    def test_that_when_one_authentication_task_is_successful_remaining_ones_are_cancelled(
            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,
        ):

            async def mock_coro():
                await asyncio.sleep(3)

            some_bytes = b"some bytes"
            mocked_reader = prepare_mocked_reader(some_bytes)
            mocked_writer = prepare_mocked_writer()
            mocked_writer2 = prepare_mocked_writer()
            mock_authentication_task = event_loop.create_task(mock_coro())
            with mock.patch("middleman.middleman_server.is_authenticated",
                            new=async_stream_actor_mock(return_value=True)):
                self.middleman._ss_connection_candidates.append(
                    (mock_authentication_task, mocked_writer2))

                event_loop.run_until_complete(
                    self.middleman._authenticate_signing_service(
                        mocked_reader, mocked_writer))

                assert_that(mock_authentication_task.cancelled()).is_true()
                mocked_writer2.close.assert_called_once_with()
                assert_that(
                    self.middleman._ss_connection_candidates).is_empty()
                assert_that(self.middleman.
                            _is_signing_service_connection_active).is_true()
Esempio 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()
Esempio n. 3
0
    def test_that_when_signing_service_connection_is_active_subsequent_attempts_will_fail(
            self, event_loop):
        self.middleman._is_signing_service_connection_active = True
        mocked_reader = prepare_mocked_reader(b"some bytes")
        mocked_writer = prepare_mocked_writer()

        event_loop.run_until_complete(
            self.middleman._handle_service_connection(mocked_reader,
                                                      mocked_writer))

        mocked_writer.close.assert_called_once_with()
Esempio n. 4
0
    def test_that_when_authentication_is_unsuccessful_then_connection_ends(
            self, event_loop):
        self.middleman._is_signing_service_connection_active = False
        mocked_reader = prepare_mocked_reader(b"some bytes")
        mocked_writer = prepare_mocked_writer()

        with mock.patch.object(
                self.middleman,
                "_authenticate_signing_service",
                new=async_stream_actor_mock(return_value=False)):
            event_loop.run_until_complete(
                self.middleman._handle_service_connection(
                    mocked_reader, mocked_writer))

            mocked_writer.close.assert_called_once_with()
Esempio n. 5
0
 def setUp(self, event_loop):
     self.mocked_writer = prepare_mocked_writer()
     self.message_tracker = OrderedDict({})
     self.golem_message = Ping()
     sign_message(self.golem_message, CONCENT_PRIVATE_KEY)
     self.connection_id = 4
     self.request_id = 888
     self.queue = Queue(loop=event_loop)
     self.queue_pool = QueuePool(
         {self.connection_id: Queue(loop=event_loop)},
         loop=event_loop,
     )
     self.signing_service_request_id = 1
     self.request_queue_item = RequestQueueItem(
         self.connection_id,
         self.request_id,
         self.golem_message,
         FROZEN_TIMESTAMP
     )
Esempio n. 6
0
    def test_that_when_authentication_is_successful_connection_lasts_until_its_end(
            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,
        ):
            self.middleman._is_signing_service_connection_active = False
            some_bytes = b"some bytes"
            mocked_reader = prepare_mocked_reader(
                some_bytes,
                side_effect=asyncio.IncompleteReadError(some_bytes, 123))
            mocked_writer = prepare_mocked_writer()

            with mock.patch.object(
                    self.middleman,
                    "_authenticate_signing_service",
                    new=async_stream_actor_mock(return_value=True)):
                event_loop.run_until_complete(
                    self.middleman._handle_service_connection(
                        mocked_reader, mocked_writer))

                self.crash_logger_mock.assert_not_called()
Esempio n. 7
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)
Esempio n. 8
0
 def setUp(self):
     self.mocked_writer = prepare_mocked_writer()
     self.request_id = 7
     self.wrong_request_id = 8
     self.mocked_challenge = b'f' * AUTHENTICATION_CHALLENGE_SIZE