def test_piece_queue_random(self): test_bytes = '\x00' test_hash = hashlib.sha1(test_bytes).digest() piece = Piece(fake_client, 1, 1, test_hash, test_dir) piece_queue = PieceQueue([piece]) test_piece = piece_queue.get_next_random() self.assertEqual(test_piece.index, piece.index) test_piece.add_block(0, test_bytes) self.assertTrue(piece.check_if_finished()) self.assertTrue(piece.check_info_hash())
def test_piece_info_hash(self): test_bytes = '\x00\x01\x02' test_hash = hashlib.sha1(test_bytes).digest() piece = Piece(fake_client, 1, 3, test_hash, test_dir) piece.write_file = StringIO() self.assertEqual(piece.num_blocks, 1) self.assertFalse(piece.check_if_finished()) self.assertTrue(piece.not_all_blocks_requested()) piece.add_block(0, test_bytes) self.assertTrue(piece.check_if_finished()) self.assertTrue(piece.check_info_hash())
def test_piece_info_hash_out_of_order(self): test_bytes = '\x00\x01\x02' empty_block = '\x00' * 2**14 test_hash = hashlib.sha1(empty_block + test_bytes).digest() piece = Piece(fake_client, 1, 2**14 + 3, test_hash, test_dir) piece.write_file = StringIO() self.assertEqual(piece.num_blocks, 2) self.assertFalse(piece.check_if_finished()) piece.add_block(2**14, test_bytes) self.assertFalse(piece.check_if_finished()) piece.add_block(0, empty_block) self.assertTrue(piece.check_if_finished()) self.assertTrue(piece.check_info_hash())