-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(merkle): Add comprehensive unit tests for MerkleTree
Implement various test cases for MerkleTree initialization, authentication path generation, and decommitment verification. Include edge cases and a large tree test for thorough coverage.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import unittest | ||
from hashlib import sha256 | ||
import sys | ||
|
||
sys.path.append("../src") | ||
sys.path.append("src") | ||
from merkle import MerkleTree, verify_decommitment | ||
|
||
class TestMerkleTree(unittest.TestCase): | ||
|
||
def test_init_with_valid_data(self): | ||
data = [1, 2, 3, 4] | ||
tree = MerkleTree(data) | ||
self.assertEqual(len(tree.data), 4) | ||
self.assertEqual(tree.height, 2) | ||
|
||
def test_init_with_non_power_of_two_data(self): | ||
data = [1, 2, 3] | ||
tree = MerkleTree(data) | ||
self.assertEqual(len(tree.data), 4) | ||
self.assertEqual(tree.height, 2) | ||
self.assertEqual(tree.data[-1], 0) # Check padding | ||
|
||
def test_init_with_empty_list(self): | ||
with self.assertRaises(AssertionError): | ||
MerkleTree([]) | ||
|
||
def test_get_authentication_path(self): | ||
data = [1, 2, 3, 4] | ||
tree = MerkleTree(data) | ||
path = tree.get_authentication_path(0) | ||
self.assertEqual(len(path), 2) | ||
|
||
def test_get_authentication_path_out_of_range(self): | ||
data = [1, 2, 3, 4] | ||
tree = MerkleTree(data) | ||
with self.assertRaises(AssertionError): | ||
tree.get_authentication_path(4) | ||
|
||
def test_verify_decommitment(self): | ||
data = [1, 2, 3, 4] | ||
tree = MerkleTree(data) | ||
leaf_id = 2 | ||
leaf_data = data[leaf_id] | ||
path = tree.get_authentication_path(leaf_id) | ||
self.assertTrue(verify_decommitment(leaf_id, leaf_data, path, tree.root)) | ||
|
||
def test_verify_decommitment_with_wrong_leaf_data(self): | ||
data = [1, 2, 3, 4] | ||
tree = MerkleTree(data) | ||
leaf_id = 2 | ||
wrong_leaf_data = 5 | ||
path = tree.get_authentication_path(leaf_id) | ||
self.assertFalse(verify_decommitment(leaf_id, wrong_leaf_data, path, tree.root)) | ||
|
||
def test_verify_decommitment_with_wrong_path(self): | ||
data = [1, 2, 3, 4] | ||
tree = MerkleTree(data) | ||
leaf_id = 2 | ||
leaf_data = data[leaf_id] | ||
path = tree.get_authentication_path(leaf_id) | ||
wrong_path = [sha256(b'wrong').hexdigest() for _ in path] | ||
self.assertFalse(verify_decommitment(leaf_id, leaf_data, wrong_path, tree.root)) | ||
|
||
def test_large_tree(self): | ||
data = list(range(1024)) | ||
tree = MerkleTree(data) | ||
self.assertEqual(tree.height, 10) | ||
for i in range(1024): | ||
path = tree.get_authentication_path(i) | ||
self.assertTrue(verify_decommitment(i, data[i], path, tree.root)) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |