<s> import json <EOL> import unittest <EOL> from chainpoint . MerkleTree import sha256 <EOL> from chainpoint . MerkleTree import is_sha256 <EOL> from chainpoint . MerkleTree import MerkleTree <EOL> from chainpoint . MerkleTree import MerkleBranch <EOL> from chainpoint . MerkleTree import MerkleProof <EOL> class MerkleTreeTest ( unittest . TestCase ) : <EOL> def test_sha256 ( self ) : <EOL> result1 = sha256 ( "<STR_LIT:test>" ) <EOL> result2 = sha256 ( "<STR_LIT>" ) <EOL> ans1 = '<STR_LIT>' <EOL> ans2 = '<STR_LIT>' <EOL> self . assertEqual ( result1 , ans1 ) <EOL> self . assertEqual ( result2 , ans2 ) <EOL> self . assertTrue ( is_sha256 ( result1 ) ) <EOL> self . assertTrue ( is_sha256 ( result2 ) ) <EOL> def test_is_sha256 ( self ) : <EOL> valid_hash = '<STR_LIT>' <EOL> self . assertTrue ( is_sha256 ( valid_hash ) ) <EOL> invalid_hash = '<STR_LIT>' <EOL> self . assertFalse ( is_sha256 ( invalid_hash ) ) <EOL> invalid_hash = '<STR_LIT>' <EOL> self . assertFalse ( is_sha256 ( invalid_hash ) ) <EOL> def test_merkle_branch ( self ) : <EOL> left = sha256 ( "<STR_LIT:test>" ) <EOL> right = sha256 ( "<STR_LIT>" ) <EOL> branch = MerkleBranch ( left , right ) <EOL> left_hash = '<STR_LIT>' <EOL> right_hash = '<STR_LIT>' <EOL> parent = '<STR_LIT>' <EOL> expected_json = { <EOL> "<STR_LIT>" : parent , <EOL> "<STR_LIT:left>" : left_hash , <EOL> "<STR_LIT:right>" : right_hash , <EOL> } <EOL> self . assertEqual ( branch . get_parent ( ) , parent ) <EOL> self . assertEqual ( branch . get_json ( ) , expected_json ) <EOL> def test_two_even_items ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT:test>" ) <EOL> tree . add_hash ( tree . hash_f ( "<STR_LIT>" ) ) <EOL> ans = '<STR_LIT>' <EOL> self . assertEqual ( tree . merkle_root ( ) , ans ) <EOL> def test_tree_odd_items ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT:test>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> ans = '<STR_LIT>' <EOL> self . assertEqual ( tree . merkle_root ( ) , ans ) <EOL> def test_large_tree ( self ) : <EOL> tree = MerkleTree ( ) <EOL> for i in range ( <NUM_LIT> ) : <EOL> tree . add_content ( str ( i ) ) <EOL> ans = '<STR_LIT>' <EOL> self . assertEqual ( tree . merkle_root ( ) , ans ) <EOL> def test_proof_true ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT:test>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> proof = tree . merkle_proof ( sha256 ( "<STR_LIT:test>" ) ) <EOL> self . assertTrue ( proof . is_valid ( ) ) <EOL> def test_proof_false ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> proof = tree . merkle_proof ( sha256 ( "<STR_LIT:test>" ) ) <EOL> self . assertFalse ( proof . is_valid ( ) ) <EOL> def test_proof_single_true ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT:test>" ) <EOL> proof = tree . merkle_proof ( sha256 ( "<STR_LIT:test>" ) ) <EOL> self . assertTrue ( proof . is_valid ( ) ) <EOL> def test_proof_single_false ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT:test>" ) <EOL> proof = tree . merkle_proof ( sha256 ( "<STR_LIT>" ) ) <EOL> self . assertFalse ( proof . is_valid ( ) ) <EOL> def test_merkle_proof_simple_true ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT:test>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> left = sha256 ( "<STR_LIT:test>" ) <EOL> right = sha256 ( "<STR_LIT>" ) <EOL> branch = MerkleBranch ( left , right ) <EOL> target = left <EOL> proof = MerkleProof ( target , tree ) <EOL> proof . add ( branch ) <EOL> self . assertTrue ( proof . is_valid ( ) ) <EOL> def test_merkle_proof_simple_false ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT:test>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> left = sha256 ( "<STR_LIT:test>" ) <EOL> right = sha256 ( "<STR_LIT>" ) <EOL> branch = MerkleBranch ( left , right ) <EOL> target = sha256 ( "<STR_LIT>" ) <EOL> proof = MerkleProof ( target , tree ) <EOL> proof . add ( branch ) <EOL> self . assertFalse ( proof . is_valid ( ) ) <EOL> def test_proof_get_json ( self ) : <EOL> tree = MerkleTree ( ) <EOL> tree . add_content ( "<STR_LIT:test>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> tree . add_content ( "<STR_LIT>" ) <EOL> proof = tree . merkle_proof ( sha256 ( "<STR_LIT:test>" ) ) <EOL> json_data = json . loads ( proof . get_json ( ) ) <EOL> self . assertEqual ( json_data [ <NUM_LIT:0> ] [ "<STR_LIT:right>" ] , '<STR_LIT>' ) <EOL> self . assertEqual ( json_data [ <NUM_LIT:0> ] [ "<STR_LIT:left>" ] , '<STR_LIT>' ) <EOL> self . assertEqual ( json_data [ <NUM_LIT:0> ] [ "<STR_LIT>" ] , '<STR_LIT>' ) <EOL> self . assertEqual ( json_data [ <NUM_LIT:1> ] [ "<STR_LIT:right>" ] , '<STR_LIT>' ) <EOL> self . assertEqual ( json_data [ <NUM_LIT:1> ] [ "<STR_LIT:left>" ] , '<STR_LIT>' ) <EOL> self . assertEqual ( json_data [ <NUM_LIT:1> ] [ "<STR_LIT>" ] , '<STR_LIT>' ) </s>
