def setUp(self): self.clock = Clock() monkey_patcher.addPatch(krpc_responder, "time", self.clock) monkey_patcher.patch() self.address = ("127.0.0.1", 5555) # Attach a standard test query query = Query() query._from = 15125 query.rpctype = "get_peers" query.target_id = 90809 self.query = query # The token generator that will be tested self.tgen = _TokenGenerator()
class _TokenGeneratorTestCase(unittest.TestCase): def setUp(self): self.clock = Clock() monkey_patcher.addPatch(krpc_responder, "time", self.clock) monkey_patcher.patch() self.address = ("127.0.0.1", 5555) # Attach a standard test query query = Query() query._from = 15125 query.rpctype = "get_peers" query.target_id = 90809 self.query = query # The token generator that will be tested self.tgen = _TokenGenerator() def tearDown(self): monkey_patcher.restore() def test_generate_sameQueryTwiceSameSecret(self): first_token = self.tgen.generate(self.query, self.address) second_token = self.tgen.generate(self.query, self.address) self.assertEqual(first_token, second_token) def test_generate_sameQueryDifferentSecrets(self): first_token = self.tgen.generate(self.query, self.address) # Force the token generator to make a new secret self.clock.set(constants._secret_timeout) second_token = self.tgen.generate(self.query, self.address) # TODO # it is possible that the hash values could collide # (one suggestion was to run 100 tests and test that # atleast 99 succeeded) self.assertNotEqual(first_token, second_token) def test_verify_sameSecret(self): token = self.tgen.generate(self.query, self.address) self.clock.set(3) self.assertTrue(self.tgen.verify(self.query, self.address, token)) def test_verify_newSecret(self): token = self.tgen.generate(self.query, self.address) self.clock.set(constants._secret_timeout) self.assertTrue(self.tgen.verify(self.query, self.address, token)) def test_verify_tokenTimeout(self): token = self.tgen.generate(self.query, self.address) self.clock.set(constants.token_timeout - 1) self.assertTrue(self.tgen.verify(self.query, self.address, token)) self.clock.set(constants.token_timeout) self.assertFalse(self.tgen.verify(self.query, self.address, token))
def setUp(self): # Reach into the contact module and replace # its time function with a custom one self.clock = Clock() monkey_patcher.addPatch(contact, "time", self.clock) monkey_patcher.patch()
class NodeTestCase(unittest.TestCase): def setUp(self): # Reach into the contact module and replace # its time function with a custom one self.clock = Clock() monkey_patcher.addPatch(contact, "time", self.clock) monkey_patcher.patch() def tearDown(self): # Restore the old time module monkey_patcher.restore() def test_distance(self): node_ids1 = [0, 1024, 2**150, 2**159 + 124, 2**34 - 58] node_ids2 = [0, 857081, 6**7, 8**9 + 7**3, 4**8 + 9**10 + 18] for id1 in node_ids1: for id2 in node_ids2: n = contact.Node(id1, ("127.0.0.1", 8000)) self.assertEqual(id1 ^ id2, n.distance(id2)) n = contact.Node(id2, ("127.0.0.1", 8000)) self.assertEqual(id2 ^ id1, n.distance(id1)) def test_fresh(self): n = contact.Node(2**17, ("127.0.0.1", 8012)) self.assertTrue(n.fresh()) # Simulate that `constants.node_timeout' time has passed self.clock.set(constants.node_timeout + 1) self.assertFalse(n.fresh()) # Refresh the node with a new query n.successful_query(10) self.assertTrue(n.fresh()) def test_better_than_bothOldWithNoQueries(self): self.clock.set(0) n1 = contact.Node(2**1, ("127.0.0.1", 1111)) n2 = contact.Node(2**2, ("127.0.0.1", 2222)) self.clock.set(constants.node_timeout + 1) self.assertFalse(n1.better_than(n2)) self.assertFalse(n2.better_than(n1)) def test_better_than_oneFreshOneOld(self): self.clock.set(0) n_old = contact.Node(2**1, ("127.0.0.1", 1111)) n_fresh = contact.Node(2**2, ("127.0.0.1", 2222)) self.clock.set(constants.node_timeout + 1) n_fresh.successful_query(10) self.assertTrue(n_fresh.better_than(n_old)) self.assertFalse(n_old.better_than(n_fresh)) def test_better_than_oneBetterRTT(self): self.clock.set(0) n_slow = contact.Node(2**1, ("127.0.0.1", 1111)) n_fast = contact.Node(2**2, ("127.0.0.1", 2222)) self.clock.set(10) n_slow.successful_query(0) n_fast.successful_query(5) self.assertTrue(n_fast.better_than(n_slow)) self.assertFalse(n_slow.better_than(n_fast))