def setUp(self): """ Gives us some messages to play with. """ self.uuid = str(uuid4()) self.node = PUBLIC_KEY self.recipient = PUBLIC_KEY self.sender = PUBLIC_KEY self.reply_port = 1908 self.value = 1.234 self.public_key = PUBLIC_KEY self.name = 'a_human_readable_key_name' self.key = construct_key(self.public_key, self.name) signed_dict = get_signed_item(self.key, self.value, self.public_key, PRIVATE_KEY, 1000) self.version = get_version() self.timestamp = signed_dict['timestamp'] self.expires = signed_dict['expires'] self.created_with = signed_dict['created_with'] self.signature = signed_dict['signature'] self.message = 'value' self.seal = 'afakesealthatwillnotwork' self.nodes = [[self.node, self.version, 'http://192.168.0.1:8080/'], ] self.mock_message = Value(self.uuid, self.node, self.node, self.reply_port, self.version, self.seal, self.key, self.value, self.timestamp, self.expires, self.created_with, self.public_key, self.name, self.signature)
def test_no_name(self): """ Ensures the DHT key is correct given only a public_key argument. """ pk_hasher = sha512(PUBLIC_KEY.encode('ascii')) expected = pk_hasher.hexdigest() actual = construct_key(PUBLIC_KEY) self.assertEqual(expected, actual)
def test_no_name(self): """ Ensures the DHT key is correct given only a public_key argument. """ pk_hasher = sha512(PUBLIC_KEY.encode("ascii")) expected = pk_hasher.hexdigest() actual = construct_key(PUBLIC_KEY) self.assertEqual(expected, actual)
def test_send_find_value_known(port, version, public_key, private_key): """ Ensures that a "findvalue" message for an existing key is sent to the test node and the reply is checked. """ item = get_signed_item('item_name', "the item's value", public_key, private_key) signature = item['signature'] item['uuid'] = str(uuid4()) item['recipient'] = REMOTE_NODE_PUBLIC_KEY item['sender'] = public_key item['reply_port'] = 1908 item['version'] = version msg = seal_message('store', item, private_key) result = send_message(port, msg) assert result.status_code == 200 item = { 'uuid': str(uuid.uuid4()), 'recipient': REMOTE_NODE_PUBLIC_KEY, 'sender': public_key, 'reply_port': 1908, 'version': version, 'key': construct_key(public_key, 'item_name'), } msg = seal_message('findvalue', item, private_key) result = send_message(port, msg) assert result.status_code == 200 reply = result.json() assert reply['uuid'] == item['uuid'] assert reply['sender'] == REMOTE_NODE_PUBLIC_KEY assert reply['recipient'] == public_key assert reply['message'] == 'value' assert reply['reply_port'] == port assert reply['version'] == version assert reply['name'] == 'item_name' assert reply['value'] == "the item's value" assert reply['key'] == construct_key(public_key, 'item_name') assert reply['public_key'] == public_key assert reply['signature'] == signature assert reply['expires'] == 0.0 assert reply['created_with'] == version assert isinstance(reply['timestamp'], float) assert 'seal' in reply assert check_seal(from_dict(reply))
def test_compound_key(self): """ Ensures the DHT key is constructed correctly given appropriate inputs. """ name = 'foo' pk_hasher = sha512(PUBLIC_KEY.encode('ascii')) name_hasher = sha512(name.encode('utf-8')) hasher = sha512(pk_hasher.digest() + name_hasher.digest()) expected = hasher.hexdigest() actual = construct_key(PUBLIC_KEY, name) self.assertEqual(expected, actual)
def test_compound_key(self): """ Ensures the DHT key is constructed correctly given appropriate inputs. """ name = "foo" pk_hasher = sha512(PUBLIC_KEY.encode("ascii")) name_hasher = sha512(name.encode("utf-8")) hasher = sha512(pk_hasher.digest() + name_hasher.digest()) expected = hasher.hexdigest() actual = construct_key(PUBLIC_KEY, name) self.assertEqual(expected, actual)
def test_get(self): """ Ensure that the node's get method works as expected. """ drog = Drogulus(PRIVATE_KEY, PUBLIC_KEY, self.event_loop, self.connector) result = asyncio.Future() drog._node.retrieve = MagicMock(return_value=result) pending_result = drog.get(PUBLIC_KEY, 'foo') expected = construct_key(PUBLIC_KEY, 'foo') drog._node.retrieve.assert_called_once_with(expected) self.assertEqual(result, pending_result)
def test_set_bespoke_duplication_count(self): """ Ensure the duplication count is passed into the replicate method. """ drog = Drogulus(PRIVATE_KEY, PUBLIC_KEY, self.event_loop, self.connector) result = [] for i in range(20): result.append(asyncio.Future()) drog._node.replicate = MagicMock(return_value=result) pending_result = drog.set('foo', 'bar', duplicate=5) self.assertIsInstance(pending_result, list) self.assertEqual(1, drog._node.replicate.call_count) called_with = drog._node.replicate.call_args_list[0][0] self.assertEqual(called_with[0], 5) self.assertEqual(called_with[1], construct_key(PUBLIC_KEY, 'foo')) self.assertEqual(called_with[2], 'bar') self.assertIsInstance(called_with[3], float) self.assertEqual(called_with[4], 0.0) self.assertEqual(called_with[5], self.version) self.assertEqual(called_with[6], PUBLIC_KEY) self.assertEqual(called_with[7], 'foo') self.assertIsInstance(called_with[8], str)
def test_set(self): """ Ensure a basic set operation works as expected. """ drog = Drogulus(PRIVATE_KEY, PUBLIC_KEY, self.event_loop, self.connector) result = [] for i in range(20): result.append(asyncio.Future()) drog._node.replicate = MagicMock(return_value=result) pending_result = drog.set('foo', 'bar') self.assertIsInstance(pending_result, list) self.assertEqual(1, drog._node.replicate.call_count) called_with = drog._node.replicate.call_args_list[0][0] self.assertEqual(called_with[0], DUPLICATION_COUNT) self.assertEqual(called_with[1], construct_key(PUBLIC_KEY, 'foo')) self.assertEqual(called_with[2], 'bar') self.assertIsInstance(called_with[3], float) self.assertEqual(called_with[4], 0.0) self.assertEqual(called_with[5], self.version) self.assertEqual(called_with[6], PUBLIC_KEY) self.assertEqual(called_with[7], 'foo') self.assertIsInstance(called_with[8], str)
def test_expected_metadata(self): """ Ensure the item (dict) returned from the function contains the expected metadata. """ key = 'key' value = 'value' signed_item = get_signed_item(key, value, PUBLIC_KEY, PRIVATE_KEY) self.assertIn('timestamp', signed_item) self.assertIsInstance(signed_item['timestamp'], float) self.assertIn('expires', signed_item) self.assertIsInstance(signed_item['expires'], float) self.assertIn('created_with', signed_item) self.assertEqual(signed_item['created_with'], get_version()) self.assertIn('public_key', signed_item) self.assertEqual(signed_item['public_key'], PUBLIC_KEY) self.assertIn('signature', signed_item) self.assertIsInstance(signed_item['signature'], str) self.assertIn('key', signed_item) self.assertIsInstance(signed_item['key'], str) self.assertEqual(signed_item['key'], construct_key(PUBLIC_KEY, key)) self.assertEqual(signed_item['name'], key) self.assertEqual(signed_item['value'], value)
def test_expected_metadata(self): """ Ensure the item (dict) returned from the function contains the expected metadata. """ key = "key" value = "value" signed_item = get_signed_item(key, value, PUBLIC_KEY, PRIVATE_KEY) self.assertIn("timestamp", signed_item) self.assertIsInstance(signed_item["timestamp"], float) self.assertIn("expires", signed_item) self.assertIsInstance(signed_item["expires"], float) self.assertIn("created_with", signed_item) self.assertEqual(signed_item["created_with"], get_version()) self.assertIn("public_key", signed_item) self.assertEqual(signed_item["public_key"], PUBLIC_KEY) self.assertIn("signature", signed_item) self.assertIsInstance(signed_item["signature"], str) self.assertIn("key", signed_item) self.assertIsInstance(signed_item["key"], str) self.assertEqual(signed_item["key"], construct_key(PUBLIC_KEY, key)) self.assertEqual(signed_item["name"], key) self.assertEqual(signed_item["value"], value)