github.com/iotexproject/iotex-core@v1.14.1-rc1/crypto/merkle_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package crypto
     7  
     8  import (
     9  	"encoding/hex"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  
    14  	"github.com/iotexproject/go-pkgs/hash"
    15  )
    16  
    17  func decodeHash(in string) [32]byte {
    18  	hash, _ := hex.DecodeString(in)
    19  	var arr [32]byte
    20  	copy(arr[:], hash[:32])
    21  	return arr
    22  }
    23  
    24  func TestMerkleTree(t *testing.T) {
    25  	var inputs []hash.Hash256
    26  
    27  	// with no leave
    28  	m := NewMerkleTree(inputs)
    29  	assert.Nil(t, m)
    30  
    31  	inputs = []hash.Hash256{
    32  		decodeHash("aeedd06eb44f08abbcc72a2293aff580f13662fa59cc1b0aa4a15ee7c118e4eb"),
    33  		decodeHash("9de6306b08158c423330f7a27243a1a5cbe39bfd764f07818437882d21241567"),
    34  		decodeHash("7959228bfdb316949973c08d8bb7bea2a21227a7b4ed85c35d247bf3d6b15a11"),
    35  		decodeHash("7959228bfdb316949973c08d8bb7bea2a21227a7b4ed85c35d247bf3d6b15a11"),
    36  		decodeHash("6368616e676520746869732070617373776f726420746f206120736563726574"),
    37  	}
    38  	m = NewMerkleTree(inputs)
    39  	rootHash := m.HashTree()
    40  	rootHashHex := hex.EncodeToString(rootHash[:])
    41  	assert.Equal(t, "4de26a6d1d6618f7bfeb3d168e37ef645db94c2d558bf8c3546d1311877ddffa", rootHashHex)
    42  }