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

     1  package merkle
     2  
     3  import (
     4  	// it is ok to use math/rand here: we do not need a cryptographically secure random
     5  	// number generator here and we can run the tests a bit faster
     6  	"math/rand"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestKeyPath(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	var path KeyPath
    16  	keys := make([][]byte, 10)
    17  	alphanum := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    18  
    19  	for d := 0; d < 1e4; d++ {
    20  		path = nil
    21  
    22  		for i := range keys {
    23  			enc := keyEncoding(rand.Intn(int(KeyEncodingMax)))
    24  			keys[i] = make([]byte, rand.Uint32()%20)
    25  			switch enc {
    26  			case KeyEncodingURL:
    27  				for j := range keys[i] {
    28  					keys[i][j] = alphanum[rand.Intn(len(alphanum))]
    29  				}
    30  			case KeyEncodingHex:
    31  				rand.Read(keys[i])
    32  			default:
    33  				panic("Unexpected encoding")
    34  			}
    35  			path = path.AppendKey(keys[i], enc)
    36  		}
    37  
    38  		res, err := KeyPathToKeys(path.String())
    39  		require.Nil(t, err)
    40  
    41  		for i, key := range keys {
    42  			require.Equal(t, key, res[i])
    43  		}
    44  	}
    45  }