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.

134 lines
3.4 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  7. ctest "github.com/tendermint/tendermint/libs/test"
  8. )
  9. func makeTxs(cnt, size int) Txs {
  10. txs := make(Txs, cnt)
  11. for i := 0; i < cnt; i++ {
  12. txs[i] = cmn.RandBytes(size)
  13. }
  14. return txs
  15. }
  16. func randInt(low, high int) int {
  17. off := cmn.RandInt() % (high - low)
  18. return low + off
  19. }
  20. func TestTxIndex(t *testing.T) {
  21. for i := 0; i < 20; i++ {
  22. txs := makeTxs(15, 60)
  23. for j := 0; j < len(txs); j++ {
  24. tx := txs[j]
  25. idx := txs.Index(tx)
  26. assert.Equal(t, j, idx)
  27. }
  28. assert.Equal(t, -1, txs.Index(nil))
  29. assert.Equal(t, -1, txs.Index(Tx("foodnwkf")))
  30. }
  31. }
  32. func TestTxIndexByHash(t *testing.T) {
  33. for i := 0; i < 20; i++ {
  34. txs := makeTxs(15, 60)
  35. for j := 0; j < len(txs); j++ {
  36. tx := txs[j]
  37. idx := txs.IndexByHash(tx.Hash())
  38. assert.Equal(t, j, idx)
  39. }
  40. assert.Equal(t, -1, txs.IndexByHash(nil))
  41. assert.Equal(t, -1, txs.IndexByHash(Tx("foodnwkf").Hash()))
  42. }
  43. }
  44. func TestValidTxProof(t *testing.T) {
  45. cases := []struct {
  46. txs Txs
  47. }{
  48. {Txs{{1, 4, 34, 87, 163, 1}}},
  49. {Txs{{5, 56, 165, 2}, {4, 77}}},
  50. {Txs{Tx("foo"), Tx("bar"), Tx("baz")}},
  51. {makeTxs(20, 5)},
  52. {makeTxs(7, 81)},
  53. {makeTxs(61, 15)},
  54. }
  55. for h, tc := range cases {
  56. txs := tc.txs
  57. root := txs.Hash()
  58. // make sure valid proof for every tx
  59. for i := range txs {
  60. leaf := txs[i]
  61. leafHash := leaf.Hash()
  62. proof := txs.Proof(i)
  63. assert.Equal(t, i, proof.Proof.Index, "%d: %d", h, i)
  64. assert.Equal(t, len(txs), proof.Proof.Total, "%d: %d", h, i)
  65. assert.EqualValues(t, root, proof.RootHash, "%d: %d", h, i)
  66. assert.EqualValues(t, leaf, proof.Data, "%d: %d", h, i)
  67. assert.EqualValues(t, leafHash, proof.LeafHash(), "%d: %d", h, i)
  68. assert.Nil(t, proof.Validate(root), "%d: %d", h, i)
  69. assert.NotNil(t, proof.Validate([]byte("foobar")), "%d: %d", h, i)
  70. // read-write must also work
  71. var p2 TxProof
  72. bin, err := cdc.MarshalBinaryLengthPrefixed(proof)
  73. assert.Nil(t, err)
  74. err = cdc.UnmarshalBinaryLengthPrefixed(bin, &p2)
  75. if assert.Nil(t, err, "%d: %d: %+v", h, i, err) {
  76. assert.Nil(t, p2.Validate(root), "%d: %d", h, i)
  77. }
  78. }
  79. }
  80. }
  81. func TestTxProofUnchangable(t *testing.T) {
  82. // run the other test a bunch...
  83. for i := 0; i < 40; i++ {
  84. testTxProofUnchangable(t)
  85. }
  86. }
  87. func testTxProofUnchangable(t *testing.T) {
  88. // make some proof
  89. txs := makeTxs(randInt(2, 100), randInt(16, 128))
  90. root := txs.Hash()
  91. i := randInt(0, len(txs)-1)
  92. proof := txs.Proof(i)
  93. // make sure it is valid to start with
  94. assert.Nil(t, proof.Validate(root))
  95. bin, err := cdc.MarshalBinaryLengthPrefixed(proof)
  96. assert.Nil(t, err)
  97. // try mutating the data and make sure nothing breaks
  98. for j := 0; j < 500; j++ {
  99. bad := ctest.MutateByteSlice(bin)
  100. if !bytes.Equal(bad, bin) {
  101. assertBadProof(t, root, bad, proof)
  102. }
  103. }
  104. }
  105. // This makes sure that the proof doesn't deserialize into something valid.
  106. func assertBadProof(t *testing.T, root []byte, bad []byte, good TxProof) {
  107. var proof TxProof
  108. err := cdc.UnmarshalBinaryLengthPrefixed(bad, &proof)
  109. if err == nil {
  110. err = proof.Validate(root)
  111. if err == nil {
  112. // XXX Fix simple merkle proofs so the following is *not* OK.
  113. // This can happen if we have a slightly different total (where the
  114. // path ends up the same). If it is something else, we have a real
  115. // problem.
  116. assert.NotEqual(t, proof.Proof.Total, good.Proof.Total, "bad: %#v\ngood: %#v", proof, good)
  117. }
  118. }
  119. }