当前位置: 首页>>代码示例>>Python>>正文


Python Client.generate_own_number方法代码示例

本文整理汇总了Python中Client.Client.generate_own_number方法的典型用法代码示例。如果您正苦于以下问题:Python Client.generate_own_number方法的具体用法?Python Client.generate_own_number怎么用?Python Client.generate_own_number使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Client.Client的用法示例。


在下文中一共展示了Client.generate_own_number方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ClientTests

# 需要导入模块: from Client import Client [as 别名]
# 或者: from Client.Client import generate_own_number [as 别名]
class ClientTests(unittest.TestCase):
    def setUp(self):
        self.client = Client(name='name',
                             g='generator',
                             p='size',
                             input_queue='input_queue',
                             output_queue='output_queue',
                             rsa_obj='rsa-objc',
                             keys='keys',
                             server_id='server_id')

    def test_is_instance_of_thread(self):
        self.assertTrue(isinstance(self.client, threading.Thread))

    def test_create_private_key_returns_a_random_number_smaller_than_p_minus_two(self):
        p = 100
        self.client.p = p
        probe_size = 100
        result = all([0 <= self.client.create_private_key() <= p - 2 for _ in range(probe_size)])

        self.assertTrue(result)

    def test_create_private_key_returns_a_prime_number(self):
        p = 100
        self.client.p = p
        probe_size = 100
        flag = all([is_prime(self.client.create_private_key()) for _ in range(probe_size)])
        self.assertTrue(flag)

    def test_has_a_finished_successfully_property_set_to_None(self):
        self.assertTrue(hasattr(self.client, 'finished_successfully'))
        self.assertEqual(self.client.finished_successfully, None)

    def test_has_send_final_message_method(self):
        self.client.send_final_message()

    def test_create_initial_message_returns_correct_number(self):
        p = 4121
        self.client.p = p
        g = 123
        self.client.g = g
        alpha = 231
        self.client.alpha = alpha
        expected = g ** alpha % p

        actual = self.client.generate_own_number()

        self.assertEqual(actual, expected)

    def test_decode_message_method_returns_exchanged_key_as_first_argument(self):
        p = 41
        self.client.p = p
        alpha = 17
        self.client.alpha = alpha
        message = 16
        expected = alpha ** message % p

        result = self.client.generate_shared_key(message)

        self.assertEqual(result, expected)

    @patch('Client.Cipher')
    def test_run_creates_alpha_by_calling_create_private_key(self, mock_cipher):
        self.client.output_queue = MagicMock()
        MagicMock().attach_mock(self.client.output_queue, 'put')
        value = 'mock_value'
        self.client.create_private_key = MagicMock(return_value=value)
        self.client.generate_own_number = MagicMock()
        self.client.input_queue = MagicMock()
        self.client.rsa_obj = MagicMock()

        self.client.run()

        self.assertEqual(self.client.alpha, value)

    @patch('Client.Cipher')
    def test_run_creates_initial_message_saves_it_and_puts_on_output_queue(self, mock_cipher):
        self.client.create_private_key = MagicMock()
        self.client.output_queue = MagicMock()
        MagicMock().attach_mock(self.client.output_queue, 'put')
        mock_message = 'mock_message'
        self.client.generate_own_number = MagicMock(return_value=('%s' % mock_message))
        self.client.input_queue = MagicMock()
        self.client.rsa_obj = MagicMock()

        self.client.run()

        self.client.output_queue.put.assert_called_once_with(mock_message)
        self.assertEqual(self.client.own_number, mock_message)

    @patch('Client.Cipher')
    def test_run_calls_get_on_input_queue_and_sets_finish_flag_to_false_for_error(self, mock_cipher):
        self.client.create_private_key = MagicMock()
        self.client.output_queue = MagicMock()
        MagicMock().attach_mock(self.client.output_queue, 'put')
        self.client.generate_own_number = MagicMock()
        self.client.input_queue = MagicMock()
        error_signal = 'ERROR'
        self.client.input_queue.get = MagicMock(return_value=error_signal)
        self.client.rsa_obj = MagicMock()
#.........这里部分代码省略.........
开发者ID:jan-osch,项目名称:Station-To-Station-BSK,代码行数:103,代码来源:test_client.py


注:本文中的Client.Client.generate_own_number方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。