def test_receive_valid_json_valid_message_from_old_peer(self): """ A good message is received then the node handles the message as expected. The cached protocol object for the peer node is expired since a new protocol object is used in this instance. """ nc = NetstringConnector(self.event_loop) old_protocol = mock.MagicMock() network_id = sha512(PUBLIC_KEY.encode('ascii')).hexdigest() nc._connections[network_id] = old_protocol ok = { 'uuid': str(uuid.uuid4()), 'recipient': PUBLIC_KEY, 'sender': PUBLIC_KEY, 'reply_port': 1908, 'version': self.version, } seal = get_seal(ok, PRIVATE_KEY) ok['seal'] = seal ok['message'] = 'ok' raw = json.dumps(ok) sender = '192.168.0.1' handler = Node(PUBLIC_KEY, PRIVATE_KEY, self.event_loop, nc, 1908) handler.message_received = mock.MagicMock() protocol = mock.MagicMock() nc.receive(raw, sender, handler, protocol) self.assertIn(network_id, nc._connections) self.assertEqual(nc._connections[network_id], protocol) msg = from_dict(ok) handler.message_received.assert_called_once_with(msg, 'netstring', sender, msg.reply_port)
def test_send_find_value_unknown(port, version, public_key, private_key): """ Ensures that a "findvalue" message for a non-existent key is sent to the test node and the reply is checked. """ item = { 'uuid': str(uuid.uuid4()), 'recipient': REMOTE_NODE_PUBLIC_KEY, 'sender': public_key, 'reply_port': 1908, 'version': version, 'key': sha512('an un-findable key'.encode('utf-8')).hexdigest(), } 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'] == 'nodes' assert reply['reply_port'] == port assert reply['version'] == version assert 'nodes' in reply assert isinstance(reply['nodes'], list) assert len(reply['nodes']) == 1 # the node only knows about us! assert 'seal' in reply assert check_seal(from_dict(reply))
def test_receive(self): """ The good case. Should return whatever handler.message_received returns. """ connector = HttpConnector(self.event_loop) ok = { 'uuid': str(uuid.uuid4()), 'recipient': PUBLIC_KEY, 'sender': PUBLIC_KEY, 'reply_port': 1908, 'version': self.version, } seal = get_seal(ok, PRIVATE_KEY) ok['seal'] = seal ok['message'] = 'ok' raw = json.dumps(ok).encode('utf-8') sender = '192.168.0.1' handler = Node(PUBLIC_KEY, PRIVATE_KEY, self.event_loop, connector, 1908) handler.message_received = mock.MagicMock() connector.receive(raw, sender, handler) msg = from_dict(ok) handler.message_received.assert_called_once_with(msg, 'http', sender, msg.reply_port)
def test_from_dict_unknown_request(self): """ Ensures the correct exception is raised if the message is not recognised. """ # "pang" is "bang" in Swedish (apparently). mock_message = { 'message': 'pang', 'uuid': self.uuid, 'node': self.node, 'version': self.version } with self.assertRaises(ValueError) as cm: from_dict(mock_message) ex = cm.exception self.assertEqual('pang is not a valid message type.', ex.args[0])
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_from_dict_value(self): """ Ensures a valid value message is correctly parsed. """ mock_message = to_dict(self.mock_message) result = from_dict(mock_message) self.assertIsInstance(result, Value) self.assertEqual(result.uuid, self.uuid) self.assertEqual(result.recipient, self.node) self.assertEqual(result.sender, self.node) self.assertEqual(result.reply_port, self.reply_port) self.assertEqual(result.version, self.version) self.assertEqual(result.seal, self.seal) self.assertEqual(result.key, self.key) self.assertEqual(result.value, self.value) self.assertEqual(result.timestamp, self.timestamp) self.assertEqual(result.expires, self.expires) self.assertEqual(result.created_with, self.created_with) self.assertEqual(result.public_key, self.public_key) self.assertEqual(result.name, self.name) self.assertEqual(result.signature, self.signature)
def test_from_dict_ok(self): """ Ensures a valid OK message is correctly parsed. """ mock_message = { 'message': 'ok', 'uuid': self.uuid, 'recipient': self.node, 'sender': self.node, 'reply_port': self.reply_port, 'version': self.version, 'seal': self.seal } result = from_dict(mock_message) self.assertIsInstance(result, OK) self.assertEqual(result.uuid, self.uuid) self.assertEqual(result.recipient, self.node) self.assertEqual(result.sender, self.node) self.assertEqual(result.reply_port, self.reply_port) self.assertEqual(result.version, self.version) self.assertEqual(result.seal, self.seal)
def test_send_store(port, version, public_key, private_key): """ Sends and "store" message to the test node and check's the reply. """ item = get_signed_item('item_name', "the item's value", public_key, private_key) 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 reply = result.json() assert reply['uuid'] == item['uuid'] assert reply['sender'] == REMOTE_NODE_PUBLIC_KEY assert reply['recipient'] == public_key assert reply['message'] == 'ok' assert reply['reply_port'] == port assert reply['version'] == version assert 'seal' in reply assert check_seal(from_dict(reply))
def test_from_dict_findvalue(self): """ Ensures a valid findvalue message is correctly parsed. """ mock_message = { 'message': 'findvalue', 'uuid': self.uuid, 'recipient': self.node, 'sender': self.node, 'reply_port': self.reply_port, 'version': self.version, 'seal': self.seal, 'key': self.key } result = from_dict(mock_message) self.assertIsInstance(result, FindValue) self.assertEqual(result.uuid, self.uuid) self.assertEqual(result.recipient, self.node) self.assertEqual(result.sender, self.node) self.assertEqual(result.reply_port, self.reply_port) self.assertEqual(result.version, self.version) self.assertEqual(result.seal, self.seal) self.assertEqual(result.key, self.key)
def test_from_dict_store(self): """ Ensures a valid store message is correctly parsed. """ mock_message = { 'message': 'store', 'uuid': self.uuid, 'recipient': self.node, 'sender': self.node, 'reply_port': self.reply_port, 'version': self.version, 'seal': self.seal, 'key': self.key, 'value': self.value, 'timestamp': self.timestamp, 'expires': self.expires, 'created_with': self.created_with, 'public_key': self.public_key, 'name': self.name, 'signature': self.signature, } result = from_dict(mock_message) self.assertIsInstance(result, Store) self.assertEqual(result.uuid, self.uuid) self.assertEqual(result.recipient, self.node) self.assertEqual(result.sender, self.node) self.assertEqual(result.reply_port, self.reply_port) self.assertEqual(result.version, self.version) self.assertEqual(result.seal, self.seal) self.assertEqual(result.key, self.key) self.assertEqual(result.value, self.value) self.assertEqual(result.timestamp, self.timestamp) self.assertEqual(result.expires, self.expires) self.assertEqual(result.created_with, self.created_with) self.assertEqual(result.public_key, self.public_key) self.assertEqual(result.name, self.name) self.assertEqual(result.signature, self.signature)