github.com/okex/exchain@v1.8.0/libs/tendermint/crypto/merkle/simple_proof_test.go (about)

     1  package merkle
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestSimpleProofValidateBasic(t *testing.T) {
    13  	testCases := []struct {
    14  		testName      string
    15  		malleateProof func(*SimpleProof)
    16  		errStr        string
    17  	}{
    18  		{"Good", func(sp *SimpleProof) {}, ""},
    19  		{"Negative Total", func(sp *SimpleProof) { sp.Total = -1 }, "negative Total"},
    20  		{"Negative Index", func(sp *SimpleProof) { sp.Index = -1 }, "negative Index"},
    21  		{"Invalid LeafHash", func(sp *SimpleProof) { sp.LeafHash = make([]byte, 10) },
    22  			"expected LeafHash size to be 32, got 10"},
    23  		{"Too many Aunts", func(sp *SimpleProof) { sp.Aunts = make([][]byte, MaxAunts+1) },
    24  			"expected no more than 100 aunts, got 101"},
    25  		{"Invalid Aunt", func(sp *SimpleProof) { sp.Aunts[0] = make([]byte, 10) },
    26  			"expected Aunts#0 size to be 32, got 10"},
    27  	}
    28  
    29  	for _, tc := range testCases {
    30  		tc := tc
    31  		t.Run(tc.testName, func(t *testing.T) {
    32  			_, proofs := SimpleProofsFromByteSlices([][]byte{
    33  				[]byte("apple"),
    34  				[]byte("watermelon"),
    35  				[]byte("kiwi"),
    36  			})
    37  			tc.malleateProof(proofs[0])
    38  			err := proofs[0].ValidateBasic()
    39  			if tc.errStr != "" {
    40  				assert.Contains(t, err.Error(), tc.errStr)
    41  			}
    42  		})
    43  	}
    44  }
    45  
    46  func TestSimpleProofAmino(t *testing.T) {
    47  	spTestCases := []SimpleProof{
    48  		{},
    49  		{
    50  			Total:    2,
    51  			Index:    1,
    52  			LeafHash: []byte("LeafHash"),
    53  			Aunts:    [][]byte{[]byte("aunt1"), []byte("aunt2")},
    54  		},
    55  		{
    56  			Total:    math.MaxInt,
    57  			Index:    math.MaxInt,
    58  			LeafHash: []byte{},
    59  			Aunts:    [][]byte{},
    60  		},
    61  		{
    62  			Total: math.MinInt,
    63  			Index: math.MinInt,
    64  			Aunts: [][]byte{nil, {}, []byte("uncle")},
    65  		},
    66  	}
    67  
    68  	for _, sp := range spTestCases {
    69  		expectData, err := cdc.MarshalBinaryBare(sp)
    70  		require.NoError(t, err)
    71  		var expectValue SimpleProof
    72  		err = cdc.UnmarshalBinaryBare(expectData, &expectValue)
    73  		require.NoError(t, err)
    74  		var actualValue SimpleProof
    75  		err = actualValue.UnmarshalFromAmino(cdc, expectData)
    76  		require.NoError(t, err)
    77  		require.EqualValues(t, expectValue, actualValue)
    78  	}
    79  }