github.com/Finschia/ostracon@v1.1.5/crypto/merkle/proof_key_path_test.go (about)

     1  package merkle
     2  
     3  import (
     4  	crand "crypto/rand"
     5  	// it is ok to use math/rand here: we do not need a cryptographically secure random
     6  	// number generator here and we can run the tests a bit faster
     7  	"math/rand"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestKeyPath(t *testing.T) {
    14  	var path KeyPath
    15  	keys := make([][]byte, 10)
    16  	alphanum := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    17  
    18  	for d := 0; d < 1e4; d++ {
    19  		path = nil
    20  
    21  		for i := range keys {
    22  			enc := keyEncoding(rand.Intn(int(KeyEncodingMax)))
    23  			keys[i] = make([]byte, rand.Uint32()%20)
    24  			switch enc {
    25  			case KeyEncodingURL:
    26  				for j := range keys[i] {
    27  					keys[i][j] = alphanum[rand.Intn(len(alphanum))]
    28  				}
    29  			case KeyEncodingHex:
    30  				_, err := crand.Read(keys[i])
    31  				require.NoError(t, err)
    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  }