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.

41 lines
810 B

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