github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/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  	var path KeyPath
    14  	keys := make([][]byte, 10)
    15  	alphanum := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    16  
    17  	for d := 0; d < 1e4; d++ {
    18  		path = nil
    19  
    20  		for i := range keys {
    21  			enc := keyEncoding(rand.Intn(int(KeyEncodingMax)))
    22  			keys[i] = make([]byte, rand.Uint32()%20)
    23  			switch enc {
    24  			case KeyEncodingURL:
    25  				for j := range keys[i] {
    26  					keys[i][j] = alphanum[rand.Intn(len(alphanum))]
    27  				}
    28  			case KeyEncodingHex:
    29  				rand.Read(keys[i]) //nolint: gosec
    30  			default:
    31  				panic("Unexpected encoding")
    32  			}
    33  			path = path.AppendKey(keys[i], enc)
    34  		}
    35  
    36  		res, err := KeyPathToKeys(path.String())
    37  		require.Nil(t, err)
    38  
    39  		for i, key := range keys {
    40  			require.Equal(t, key, res[i])
    41  		}
    42  	}
    43  }