async def test(): future = MagicMock() # GetChallenge returns error self.manager._get_challenge_done_fail(future.exception) schedule_next_bootstrap_mock.assert_has_calls( [call(hard_failure=False)], ) # Fail to construct response schedule_next_bootstrap_mock.reset_mock() future.exception = lambda: None await self.manager._get_challenge_done_success(future.result) schedule_next_bootstrap_mock.assert_has_calls( [call(hard_failure=True)], ) # No error schedule_next_bootstrap_mock.reset_mock() self.manager._loop = MagicMock() future.result = lambda: Challenge( challenge=b'simple_challenge', key_type=ChallengeKey.ECHO, ) make_awaitable(request_sign_mock) await self.manager._get_challenge_done_success(future.result()) schedule_next_bootstrap_mock.assert_not_called() request_sign_mock.assert_has_calls([call(ANY)])
def test__construct_response(self, load_key_mock): ecdsa_key = ec.generate_private_key(ec.SECP256R1(), default_backend()) key_types = { ChallengeKey.ECHO: None, ChallengeKey.SOFTWARE_ECDSA_SHA256: ecdsa_key, } for key_type, key in key_types.items(): load_key_mock.return_value = key challenge = Challenge(key_type=key_type, challenge=b'challenge') response = self.manager._construct_response(challenge, CSR()) self.assertEqual(response.hw_id.id, self.hw_id) self.assertEqual(response.challenge, challenge.challenge) challenge = Challenge(key_type=5, challenge=b'crap challenge') with self.assertRaises(bm.BootstrapError, msg='Unknown key type: %s' % challenge.key_type): self.manager._construct_response(challenge, CSR())
async def test(): challenge = Challenge(challenge=b'simple_challenge', key_type=ChallengeKey.ECHO) self.manager._gateway_key = ec.generate_private_key( ec.SECP256R1(), default_backend()) csr = self.manager._create_csr() response = self.manager._construct_response(challenge, csr) # test fail to get channel bootstrap_channel_mock.side_effect = ValueError await self.manager._request_sign(response) schedule_next_bootstrap_mock.assert_has_calls( [call(hard_failure=False)])
async def test(): challenge = Challenge(challenge=b'simple_challenge', key_type=ChallengeKey.ECHO) self.manager._gateway_key = ec.generate_private_key( ec.SECP256R1(), default_backend()) csr = self.manager._create_csr() response = self.manager._construct_response(challenge, csr) # test no error schedule_next_bootstrap_mock.reset_mock() bootstrap_channel_mock.reset_mock() bootstrap_channel_mock.side_effect = None bootstrap_channel_mock.return_value = self.channel make_awaitable(self.manager._bootstrap_success_cb) await self.manager._request_sign(response) schedule_next_bootstrap_mock.assert_not_called()
def test__request_sign(self, bootstrap_channel_mock, retry_bootstrap_mock): challenge = Challenge( challenge=b'simple_challenge', key_type=ChallengeKey.ECHO ) self.manager._gateway_key = ec.generate_private_key( ec.SECP384R1(), default_backend()) csr = self.manager._create_csr() response = self.manager._construct_response(challenge, csr) # test fail to get channel bootstrap_channel_mock.side_effect = ValueError self.manager._request_sign(response) retry_bootstrap_mock.assert_has_calls([call(hard_failure=False)]) # test no error retry_bootstrap_mock.reset_mock() bootstrap_channel_mock.reset_mock() bootstrap_channel_mock.side_effect = None bootstrap_channel_mock.return_value = self.channel self.manager._request_sign(response) retry_bootstrap_mock.assert_not_called()
def test__get_challenge_done(self, request_sign_mock, retry_bootstrap_mock): future = MagicMock() # GetChallenge returns error self.manager._get_challenge_done(future) retry_bootstrap_mock.assert_has_calls([call(hard_failure=False)]) # Fail to construct response retry_bootstrap_mock.reset_mock() future.exception = lambda: None self.manager._get_challenge_done(future) retry_bootstrap_mock.assert_has_calls([call(hard_failure=True)]) # No error retry_bootstrap_mock.reset_mock() self.manager._loop = MagicMock() future.result = lambda: Challenge( challenge=b'simple_challenge', key_type=ChallengeKey.ECHO ) self.manager._get_challenge_done(future) retry_bootstrap_mock.assert_not_called() request_sign_mock.assert_has_calls([call(ANY)])
def GetChallenge(self, request, context): challenge = Challenge( challenge=b'simple_challenge', key_type=ChallengeKey.ECHO, ) return challenge