def test_id_invalid(self):
        with self.assertRaises(AssertionError):
            Client('00', 'name', self.correctSecret)

        with self.assertRaises(AssertionError):
            Client(-1, 'name', self.correctSecret)

        with self.assertRaises(AssertionError):
            Client((2 ** 32) + 1, 'name', self.correctSecret)
    def test_secret_invalid(self):
        with self.assertRaises(AssertionError):
            Client(0, 'name', '')

        with self.assertRaises(AssertionError):
            Client(0, 'name', b'0000000000000000000000000000000')   # one byte shorter key

        with self.assertRaises(AssertionError):
            Client(0, 'name', b'000000000000000000000000000000000')   # one byte longer key
    def test_name_invalid(self):
        with self.assertRaises(AssertionError):
            Client(0, 0, self.correctSecret)

        with self.assertRaises(AssertionError):
            Client(0, '', self.correctSecret)

        with self.assertRaises(AssertionError):
            Client(0, '' * 257, self.correctSecret)
Exemplo n.º 4
0
 def test_setting_client_ttl(self):
     c1 = Client(0, 'name', self.correctSecret)
     self.handler.set_ipv4_address(c1, '178.93.23.200', 3000)
     self.assertGreater(self.handler._redis().ttl(c1.id),
                        self.handler.default_address_ttl - 10)
     self.assertGreater(self.handler._redis().ttl(f'{c1.id}:178.93.23.200'),
                        self.handler.default_address_ttl - 10)
    def _set_client(self, client: Client, pipe: Pipeline) -> None:
        if ASSERTS:
            assert '!' not in client.name

        pipe.setex(f'#{client.id}', client.serialize(),
                   self.default_address_ttl)
        pipe.setex(f'!{client.name}', client.id, self.default_address_ttl)
Exemplo n.º 6
0
    def test_setting_ipv4_address(self):
        valid_addresses = ['178.93.23.200', '178.93.23.201', '178.93.23.202']

        c1 = Client(0, 'name', self.correctSecret)
        for address in valid_addresses:
            self.handler.set_ipv4_address(c1, address, 3000)

        ip_addresses = self.handler.get_ipv4_addresses(c1.id)
        self.assertIs(type(ip_addresses), list)

        for address in ip_addresses:
            self.assertIn(address, valid_addresses)
Exemplo n.º 7
0
    def test_setting_ipv4_address_invalid(self):
        c1 = Client(0, 'name', self.correctSecret)

        with self.assertRaises(OSError):
            self.handler.set_ipv4_address(c1, '', 3000)

        with self.assertRaises(OSError):
            self.handler.set_ipv4_address(c1, '178.93.23.2000000', 3000)

        with self.assertRaises(AssertionError):
            self.handler.set_ipv4_address(c1, '178.93.23.200', -1)

        with self.assertRaises(AssertionError):
            self.handler.set_ipv4_address(c1, '178.93.23.200', 300000)
Exemplo n.º 8
0
    def test_setting_client(self):
        def compare_clients(x, y):
            self.assertEqual(x.id, y.id)
            self.assertEqual(x.name, y.name)
            self.assertEqual(x.secret, y.secret)

        c1 = Client(0, 'name', self.correctSecret)
        self.handler.set_ipv4_address(c1, '178.93.23.200', 3000)

        c2 = self.handler.get_by_id(c1.id)
        compare_clients(c1, c2)

        c3 = self.handler.get_by_name('name')
        compare_clients(c1, c3)
    def get_by_id(self, client_id: int) -> Client or None:
        """
        :param client_id: id of the client, that must be fetched from the redis.
        :returns: client instance, or None in case if no such client is present in the redis.
        """
        if ASSERTS:
            assert client_id >= 0
            assert client_id <= 2**32

        raw = self._redis().get(f'#{client_id}')
        if raw is None:
            return None

        return Client.deserialize(raw)
    def test_id_valid(self):
        c1 = Client(0, 'name', self.correctSecret)
        self.assertEqual(c1.id, 0)

        c2 = Client(2 ** 32, 'name', self.correctSecret)
        self.assertEqual(c2.id, 2**32)
    def test_serialize(self):
        def compare_clients(x, y):
            self.assertEqual(x.id, y.id)
            self.assertEqual(x.name, y.name)
            self.assertEqual(x.secret, y.secret)
            self.assertEqual(x.remote_ipv4_address, y.remote_ipv4_address)

        c1 = Client(0, 'name', self.correctSecret)
        compare_clients(c1, Client.deserialize(c1.serialize()))

        c2 = Client(2 ** 32, 'name', self.correctSecret)
        compare_clients(c2, Client.deserialize(c2.serialize()))

        c3 = Client(2 ** 32, 'name', self.correctSecret)
        compare_clients(c3, Client.deserialize(c3.serialize()))
 def test_secret_valid(self):
     c1 = Client(0, 'name', self.correctSecret)
     self.assertEqual(c1.secret, self.correctSecret)
    def test_name_valid(self):
        c1 = Client(0, 'a', self.correctSecret)
        self.assertEqual(c1.name, 'a')

        c2 = Client(0, 'a'*256, self.correctSecret)
        self.assertEqual(c2.name, 'a'*256)
Exemplo n.º 14
0
    def test_getting_client_valid(self):
        c1 = Client(0, 'name', self.correctSecret)
        self.handler.set_ipv4_address(c1, '127.0.0.1', 3000)

        c2 = self.handler.get_by_id(c1.id)
        self.assertIsNotNone(c2)
 def __generate_client(self, id=1, name='test'):
     client = Client(id, name, self.secret)
     self.core._clients_handler.set_ipv4_address(client, '127.0.0.1',
                                                 self.port)
     return client