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.

119 lines
2.8 KiB

  1. package merkle
  2. import (
  3. "bytes"
  4. . "github.com/tendermint/tendermint/common"
  5. . "github.com/tendermint/tendermint/common/test"
  6. "fmt"
  7. "testing"
  8. )
  9. type testItem []byte
  10. func (tI testItem) Hash() []byte {
  11. return []byte(tI)
  12. }
  13. func TestSimpleProof(t *testing.T) {
  14. numItems := 100
  15. items := make([]Hashable, numItems)
  16. for i := 0; i < numItems; i++ {
  17. items[i] = testItem(RandBytes(32))
  18. }
  19. rootHash := SimpleHashFromHashables(items)
  20. proofs := SimpleProofsFromHashables(items)
  21. // For each item, check the trail.
  22. for i, item := range items {
  23. itemHash := item.Hash()
  24. proof := proofs[i]
  25. // Verify success
  26. ok := proof.Verify(itemHash, rootHash)
  27. if !ok {
  28. t.Errorf("Verification failed for index %v.", i)
  29. }
  30. // Wrong item index should make it fail
  31. proof.Index += 1
  32. {
  33. ok = proof.Verify(itemHash, rootHash)
  34. if ok {
  35. t.Errorf("Expected verification to fail for wrong index %v.", i)
  36. }
  37. }
  38. proof.Index -= 1
  39. // Trail too long should make it fail
  40. origInnerHashes := proof.InnerHashes
  41. proof.InnerHashes = append(proof.InnerHashes, RandBytes(32))
  42. {
  43. ok = proof.Verify(itemHash, rootHash)
  44. if ok {
  45. t.Errorf("Expected verification to fail for wrong trail length.")
  46. }
  47. }
  48. proof.InnerHashes = origInnerHashes
  49. // Trail too short should make it fail
  50. proof.InnerHashes = proof.InnerHashes[0 : len(proof.InnerHashes)-1]
  51. {
  52. ok = proof.Verify(itemHash, rootHash)
  53. if ok {
  54. t.Errorf("Expected verification to fail for wrong trail length.")
  55. }
  56. }
  57. proof.InnerHashes = origInnerHashes
  58. // Mutating the itemHash should make it fail.
  59. ok = proof.Verify(MutateByteSlice(itemHash), rootHash)
  60. if ok {
  61. t.Errorf("Expected verification to fail for mutated leaf hash")
  62. }
  63. // Mutating the rootHash should make it fail.
  64. ok = proof.Verify(itemHash, MutateByteSlice(rootHash))
  65. if ok {
  66. t.Errorf("Expected verification to fail for mutated root hash")
  67. }
  68. }
  69. }
  70. func TestKVPairs(t *testing.T) {
  71. // NOTE: in alphabetical order for convenience.
  72. m := map[string]interface{}{}
  73. m["bytez"] = []byte("hizz") // 0
  74. m["light"] = "shadow" // 1
  75. m["one"] = 1 // 2
  76. m["one_u64"] = uint64(1) // 3
  77. m["struct"] = struct { // 4
  78. A int
  79. B int
  80. }{0, 1}
  81. kvPairsH := MakeSortedKVPairs(m)
  82. // rootHash := SimpleHashFromHashables(kvPairsH)
  83. proofs := SimpleProofsFromHashables(kvPairsH)
  84. // Some manual tests
  85. if !bytes.Equal(proofs[1].LeafHash, KVPair{"light", "shadow"}.Hash()) {
  86. t.Errorf("\"light\": proof failed")
  87. fmt.Printf("%v\n%X", proofs[0], KVPair{"light", "shadow"}.Hash())
  88. }
  89. if !bytes.Equal(proofs[2].LeafHash, KVPair{"one", 1}.Hash()) {
  90. t.Errorf("\"one\": proof failed")
  91. }
  92. if !bytes.Equal(proofs[4].LeafHash, KVPair{"struct", struct {
  93. A int
  94. B int
  95. }{0, 1}}.Hash()) {
  96. t.Errorf("\"struct\": proof failed")
  97. }
  98. }