github.com/badrootd/celestia-core@v0.0.0-20240305091328-aa4207a4b25d/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  	crand "crypto/rand"
     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  				_, _ = crand.Read(keys[i])
    31  			default:
    32  				panic("Unexpected encoding")
    33  			}
    34  			path = path.AppendKey(keys[i], enc)
    35  		}
    36  
    37  		res, err := KeyPathToKeys(path.String())
    38  		require.Nil(t, err)
    39  
    40  		for i, key := range keys {
    41  			require.Equal(t, key, res[i])
    42  		}
    43  	}
    44  }