def test_tree_get_root_hash_invalid_size(self):
        """Test handling of bad inputs to get root hash.

        Test that an assertion is raised for invalid tree size passed
        into get_root_hash.
        """
        tree = in_memory_merkle_tree.InMemoryMerkleTree(TEST_VECTOR_DATA)
        self.assertRaises(ValueError, tree.get_root_hash, tree.tree_size() + 3)
    def test_tree_consistency_proof_precomputed(self):
        """Test consistency proof generation.

        Test Consistency proof generation correctness test for known-good
        proofs.
        """
        tree = in_memory_merkle_tree.InMemoryMerkleTree(TEST_VECTOR_DATA)
        for v in PRECOMPUTED_PROOF_TEST_VECTORS:
            consistency_proof = tree.get_consistency_proof(
                    v.snapshot_1, v.snapshot_2)
            self.assertEqual(consistency_proof, v.proof)
    def test_tree_snapshot_root_hash(self):
        """Test root hash calculation.

        Test that root hash is calculated correctly when all leaves are added
        at once.
        """
        tree = in_memory_merkle_tree.InMemoryMerkleTree(TEST_VECTOR_DATA)
        hasher = merkle.TreeHasher()
        for i in range(len(TEST_VECTOR_DATA)):
            self.assertEqual(
                    tree.get_root_hash(i),
                    hasher.hash_full_tree(TEST_VECTOR_DATA[0:i]))
    def test_tree_incremental_root_hash(self):
        """Test root hash calculation.

        Test that root hash is calculated correctly when leaves are added
        incrementally.
        """
        tree = in_memory_merkle_tree.InMemoryMerkleTree([])
        hasher = merkle.TreeHasher()
        for i in range(len(TEST_VECTOR_DATA)):
            tree.add_leaf(TEST_VECTOR_DATA[i])
            self.assertEqual(
                    tree.get_root_hash(),
                    hasher.hash_full_tree(TEST_VECTOR_DATA[0:i+1]))
    def test_tree_consistency_proof_bad_indices(self):
        """Test handling of bad inputs into get consistency proof.

        Test that an assertion is raised for invalid tree sizes passed
        into get_consistency_proof.
        """
        tree = in_memory_merkle_tree.InMemoryMerkleTree(TEST_VECTOR_DATA)
        n = tree.tree_size()
        # 2nd tree size too large
        self.assertRaises(ValueError, tree.get_consistency_proof, 1, n + 3)
        # 1st tree size > 2nd tree size
        self.assertRaises(ValueError,
                          tree.get_consistency_proof, n - 1, n - 3)
    def test_tree_inclusion_proof_bad_indices(self):
        """Test handling of bad inputs into get inclusion proof.

        Test that an assertion is raised for invalid tree sizes or invalid
        leaf indices are passed into get_inclusion_proof.
        """
        tree = in_memory_merkle_tree.InMemoryMerkleTree(TEST_VECTOR_DATA)
        n = tree.tree_size()
        # Tree size too large
        self.assertRaises(ValueError, tree.get_inclusion_proof, 0, n + 3)
        # Leaf index too large
        self.assertRaises(ValueError,
                          tree.get_inclusion_proof, n + 3, n - 1)
    def test_tree_consistency_proof_generated(self):
        """Test consistency proof generation.

        Consistency proof generation correctness test for generated proofs.
        """
        leaves = []
        for i in range(128):
            leaves.append(chr(i) * 32)

        tree = in_memory_merkle_tree.InMemoryMerkleTree(leaves)
        verifier = merkle.MerkleVerifier()

        for i in range(1, tree.tree_size()):
            for j in range(i):
                consistency_proof = tree.get_consistency_proof(j, i)
                self.assertTrue(verifier.verify_tree_consistency(
                        j, i, tree.get_root_hash(j), tree.get_root_hash(i),
                        consistency_proof))
    def test_tree_inclusion_proof_generated(self):
        """Test inclusion proof generation.

        Test inclusion proof generation correctness for generated proofs.
        """
        leaves = []
        leaf_hashes = []
        hasher = merkle.TreeHasher()
        for i in range(128):
            leaves.append(chr(i) * 32)
            leaf_hashes.append(hasher.hash_leaf(leaves[-1]))

        tree = in_memory_merkle_tree.InMemoryMerkleTree(leaves)
        verifier = merkle.MerkleVerifier()

        for i in range(1, tree.tree_size()):
            for j in range(i):
                audit_path = tree.get_inclusion_proof(j, i)
                dummy_sth = DummySTH(i, tree.get_root_hash(i))
                verifier.verify_leaf_hash_inclusion(
                        leaf_hashes[j], j, audit_path, dummy_sth)
    def test_tree_inclusion_proof_precomputed(self):
        """Test inclusion proof generation.

        Test inclusion proof generation correctness test for known-good
        proofs.
        """
        tree = in_memory_merkle_tree.InMemoryMerkleTree(TEST_VECTOR_DATA)
        verifier = merkle.MerkleVerifier()
        for v in PRECOMPUTED_PATH_TEST_VECTORS:
            audit_path = tree.get_inclusion_proof(v.leaf, v.tree_size_snapshot)
            self.assertEqual(len(audit_path), v.path_length)
            self.assertEqual(audit_path, v.path)

            leaf_data = TEST_VECTOR_DATA[v.leaf]
            leaf_hash = merkle.TreeHasher().hash_leaf(leaf_data)
            dummy_sth = DummySTH(v.tree_size_snapshot,
                tree.get_root_hash(v.tree_size_snapshot))

            if v.tree_size_snapshot > 0:
                verifier.verify_leaf_hash_inclusion(
                        leaf_hash, v.leaf, audit_path, dummy_sth)