github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/crypto/merkle/simple_proof_test.go (about)

     1  package merkle
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestSimpleProofValidateBasic(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	testCases := []struct {
    13  		testName      string
    14  		malleateProof func(*SimpleProof)
    15  		errStr        string
    16  	}{
    17  		{"Good", func(sp *SimpleProof) {}, ""},
    18  		{"Negative Total", func(sp *SimpleProof) { sp.Total = -1 }, "negative Total"},
    19  		{"Negative Index", func(sp *SimpleProof) { sp.Index = -1 }, "negative Index"},
    20  		{"Invalid LeafHash", func(sp *SimpleProof) { sp.LeafHash = make([]byte, 10) }, "expected LeafHash size to be 32, got 10"},
    21  		{"Too many Aunts", func(sp *SimpleProof) { sp.Aunts = make([][]byte, maxAunts+1) }, "expected no more than 100 aunts, got 101"},
    22  		{"Invalid Aunt", func(sp *SimpleProof) { sp.Aunts[0] = make([]byte, 10) }, "expected Aunts#0 size to be 32, got 10"},
    23  	}
    24  
    25  	for _, tc := range testCases {
    26  		tc := tc
    27  		t.Run(tc.testName, func(t *testing.T) {
    28  			t.Parallel()
    29  
    30  			_, proofs := SimpleProofsFromByteSlices([][]byte{
    31  				[]byte("apple"),
    32  				[]byte("watermelon"),
    33  				[]byte("kiwi"),
    34  			})
    35  			tc.malleateProof(proofs[0])
    36  			err := proofs[0].ValidateBasic()
    37  			if tc.errStr != "" {
    38  				assert.Contains(t, err.Error(), tc.errStr)
    39  			}
    40  		})
    41  	}
    42  }