github.com/koko1123/flow-go-1@v0.29.6/ledger/common/pathfinder/pathfinder_test.go (about)

     1  package pathfinder_test
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/koko1123/flow-go-1/ledger"
    10  	"github.com/koko1123/flow-go-1/ledger/common/pathfinder"
    11  	"github.com/koko1123/flow-go-1/ledger/common/testutils"
    12  	"github.com/onflow/flow-go/crypto/hash"
    13  )
    14  
    15  // Test_KeyToPathV0 tests key to path for V0
    16  func Test_KeyToPathV0(t *testing.T) {
    17  
    18  	kp1 := testutils.KeyPartFixture(1, "key part 1")
    19  	kp2 := testutils.KeyPartFixture(22, "key part 2")
    20  	k := ledger.NewKey([]ledger.KeyPart{kp1, kp2})
    21  
    22  	path, err := pathfinder.KeyToPath(k, 0)
    23  	require.NoError(t, err)
    24  
    25  	// compute expected value
    26  	h := sha256.New()
    27  	_, err = h.Write([]byte("key part 1"))
    28  	require.NoError(t, err)
    29  	_, err = h.Write([]byte("key part 2"))
    30  	require.NoError(t, err)
    31  	var expected ledger.Path
    32  	copy(expected[:], h.Sum(nil))
    33  	require.Equal(t, path, expected)
    34  }
    35  
    36  func Test_KeyToPathV1(t *testing.T) {
    37  
    38  	kp1 := testutils.KeyPartFixture(1, "key part 1")
    39  	kp2 := testutils.KeyPartFixture(22, "key part 2")
    40  	k := ledger.NewKey([]ledger.KeyPart{kp1, kp2})
    41  
    42  	path, err := pathfinder.KeyToPath(k, 1)
    43  	require.NoError(t, err)
    44  
    45  	// compute expected value
    46  	hasher := hash.NewSHA3_256()
    47  	_, err = hasher.Write([]byte("/1/key part 1/22/key part 2"))
    48  	require.NoError(t, err)
    49  
    50  	var expected ledger.Path
    51  	copy(expected[:], hasher.SumHash())
    52  	require.Equal(t, path, expected)
    53  }