You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
999 B

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