def test_node_get_next_message(self): # Test get_next_message now = time.time() node = self._create_node() node2 = self._create_node() # No Messages, return None self.assertIsNone(node.get_next_message(now)) msg = Message() msg2 = Message() msg.sign_from_node(node) msg2.sign_from_node(node2) now = time.time() # Add only 1 message node.enqueue_message(msg, now) self.assertEqual(node.get_next_message(now + 5), msg) now = time.time() node.enqueue_message(msg, now) node.enqueue_message(msg2, now) # Should not return anything since the time is the same self.assertIsNone(node.get_next_message(now)) # Take out 1 message self.assertNotIn(node.get_next_message(now + 5), node.MessageQ.Messages) # Take out last message self.assertNotIn(node.get_next_message(now + 5), node.MessageQ.Messages) # No messages left, should return None self.assertEqual(node.get_next_message(now + 5), None)
def test_message_len(self): # Test the overriden len function # First case is when the msg has no data, creates data msg = Message({'__SIGNATURE__': "Test"}) length = len(dict2cbor(msg.dump())) self.assertIsNone(msg._data) self.assertEqual(len(msg), length) # The second case is when the msg does have data msg2 = Message({"__SIGNATURE__": "Test"}) msg2._data = "test data" self.assertEqual(len(msg2), len("test data"))
def test_message_repr(self): # Test the overridden repr function # First case to test is when the msg has no data, creates data msg = Message({'__SIGNATURE__': "Test"}) serMsg = msg.serialize() self.assertIsNone(msg._data) self.assertEqual(repr(msg), serMsg) self.assertEqual(msg._data, serMsg) # Second case is when the msg contains data msg2 = Message({'__SIGNATURE__': "Test"}) msg2._data = "test data" self.assertEqual(repr(msg2), "test data") self.assertEqual(msg2._data, "test data")
def test_message_init(self): # Test default Message intialization # This is a bad for actual use because it does not have a Signature msg = Message() time.sleep(.05) self.assertLess(msg.Nonce, time.time()) self.assertEqual(msg.TimeToLive, (2**31)) self.assertTrue(msg.IsForward) self.assertTrue(msg.IsReliable) self.assertFalse(msg.IsSystemMessage) self.assertIsNone(msg._data) # Add signature and nounce to minfo msg2 = Message({'__SIGNATURE__': "Test", "__NONCE__": 10.02}) self.assertEqual(msg2.Nonce, 10.02)
def test_node_reset_peer_stats(self): # Test that reset_peers does not break now = time.time() node = self._create_node() node2 = self._create_node() node.initialize_stats(node2) msg = Message() msg.sign_from_node(node) stats1 = str( node.Stats.get_stats( ["Address", "MessageQueue", "MessageQueueLength"])) # Nothing changes since all are the original defaults node.reset_peer_stats( ["Address", "MessageQueue", "MessageQueueLength"]) stats2 = str( node.Stats.get_stats( ["Address", "MessageQueue", "MessageQueueLength"])) self.assertEqual(stats1, stats2) node.enqueue_message(msg, now) stats3 = (node.Stats.get_stats( ["Address", "MessageQueue", "MessageQueueLength"])) node.reset_peer_stats( ["Address", "MessageQueue", "MessageQueueLength"]) stats4 = (node.Stats.get_stats( ["Address", "MessageQueue", "MessageQueueLength"])) # All values are either sample or value metrics that are not changed # by the reset_peer_stats self.assertEqual(stats3, stats4)
def test_message_str(self): # Test overridden string method # Will cause warning from SignedObject identifier msg = Message({'__SIGNATURE__': "Test"}) string = str(msg) self.assertEqual( string, "MSG:{0}:{1}".format(msg.OriginatorID[:8], msg._identifier[:8]))
def test_message_dump(self): # Test dump for message msg = Message({'__SIGNATURE__': "Test", "__NONCE__": 1000.3}) expectedDump = { '__SIGNATURE__': "Test", "__NONCE__": 1000.3, "__TYPE__": "/gossip.Message/MessageBase" } self.assertEquals(msg.dump(), expectedDump)
def test_str_assertion(self): # Test if the assert is called within SignedObject, An assert error # Should be called if message does not have a signature msg1 = Message() try: string = str(msg1) self.fail("This should cause an Assert Error") except AssertionError, e: self.assertIsInstance(e, AssertionError)
def test_node_message_dropped(self): # Test behavior if message is believe to be "dropped" node = self._create_node() msg = Message() msg.sign_from_node(node) oldRTO = node.Estimator.RTO node.message_dropped(msg) newRTO = node.Estimator.RTO # Should increase RTO and "re-add" msg to the MessageQueue self.assertLess(oldRTO, newRTO) self.assertIn(msg.Identifier, node.MessageQ.Messages)
def test_node_enqueue_message(self): # Test correct enqueue of a message to MessageQueue node = self._create_node() before = str(node.MessageQ) msg = Message() msg.sign_from_node(node) now = time.time() # Add message node.enqueue_message(msg, now) after = str(node.MessageQ) self.assertNotEqual(before, after) self.assertIn(msg.Identifier, node.MessageQ.Messages)
def test_node_message_delivered(self): # Test behavior if message is "delivered" node = self._create_node() msg = Message() msg.sign_from_node(node) node.enqueue_message(msg, time.time()) oldRTO = node.Estimator.RTO node.message_delivered(msg, 2.0) newRTO = node.Estimator.RTO # Should update RTO and add identifier to MessageQueue self.assertLess(oldRTO, newRTO) self.assertNotIn(msg.Identifier, node.MessageQ.Messages)
def test_node_dequeue_message_no_message(self): # Test that trying to dequeue a message that is not enqueued does not # throw an error node = self._create_node() msg = Message() msg.sign_from_node(node) now = time.time() # Msg was never enqueued before = str(node.MessageQ) node.dequeue_message(msg) after = str(node.MessageQ) self.assertEqual(before, after)
def test_node_dequeue_message(self): # Test correct enqueue of a message from MessageQueue node = self._create_node() msg = Message() msg.sign_from_node(node) now = time.time() # must enqueue message before able to dequeue node.enqueue_message(msg, now) before = str(node.MessageQ) node.dequeue_message(msg) after = str(node.MessageQ) self.assertNotEqual(before, after) self.assertNotIn(msg.Identifier, node.MessageQ.Messages)
def test_add_message(self): # Add a message to a packet, along with source node and destination # Resets the packet attributes with that of the message pak = Packet() # Need signingkey, otherwise throws errors srcNode = Node(identifier="source", signingkey="source") desNode = Node(identifier="destination", signingkey="destination") msg = Message({'__SIGNATURE__': "MsgTestS"}) pak.add_message(msg, srcNode, desNode, 1) # Check that msg and pak have the same attributes self.assertEqual([msg.Identifier, msg.TimeToLive, msg.IsReliable], [pak.Identifier, pak.TimeToLive, pak.IsReliable]) # Check correct SenderID self.assertEqual(srcNode.Identifier, pak.SenderID) # Check correct destinationID and destination RTE self.assertEqual(desNode.Identifier, pak.DestinationID) self.assertEqual(desNode.Estimator.RTO, pak.RoundTripEstimate) # Check correct data and msg self.assertEqual(pak.Message, msg) self.assertEqual(pak.Data, repr(msg))
def test_unpack_message(self): # Make sure that the message can be unpacked after serliazation msg = Message({'__SIGNATURE__': "Test"}) cbor = dict2cbor(msg.dump()) msgdict = unpack_message_data(cbor) self.assertEqual(msg.dump(), msgdict)
def _create_msg(self): msg = Message({'__SIGNATURE__': "test"}) return msg
def _create_msg(self): node = self._create_node() msg = Message() msg.sign_from_node(node) return msg