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.

133 lines
3.4 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. tmrand "github.com/tendermint/tendermint/libs/rand"
  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] = tmrand.Bytes(size)
  13. }
  14. return txs
  15. }
  16. func randInt(low, high int) int {
  17. off := tmrand.Int() % (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. tx := []byte(txs[i])
  61. proof := txs.Proof(i)
  62. assert.EqualValues(t, i, proof.Proof.Index, "%d: %d", h, i)
  63. assert.EqualValues(t, len(txs), proof.Proof.Total, "%d: %d", h, i)
  64. assert.EqualValues(t, root, proof.RootHash, "%d: %d", h, i)
  65. assert.EqualValues(t, tx, proof.Data, "%d: %d", h, i)
  66. assert.EqualValues(t, txs[i].Hash(), proof.Leaf(), "%d: %d", h, i)
  67. assert.Nil(t, proof.Validate(root), "%d: %d", h, i)
  68. assert.NotNil(t, proof.Validate([]byte("foobar")), "%d: %d", h, i)
  69. // read-write must also work
  70. var p2 TxProof
  71. bin, err := cdc.MarshalBinaryLengthPrefixed(proof)
  72. assert.Nil(t, err)
  73. err = cdc.UnmarshalBinaryLengthPrefixed(bin, &p2)
  74. if assert.Nil(t, err, "%d: %d: %+v", h, i, err) {
  75. assert.Nil(t, p2.Validate(root), "%d: %d", h, i)
  76. }
  77. }
  78. }
  79. }
  80. func TestTxProofUnchangable(t *testing.T) {
  81. // run the other test a bunch...
  82. for i := 0; i < 40; i++ {
  83. testTxProofUnchangable(t)
  84. }
  85. }
  86. func testTxProofUnchangable(t *testing.T) {
  87. // make some proof
  88. txs := makeTxs(randInt(2, 100), randInt(16, 128))
  89. root := txs.Hash()
  90. i := randInt(0, len(txs)-1)
  91. proof := txs.Proof(i)
  92. // make sure it is valid to start with
  93. assert.Nil(t, proof.Validate(root))
  94. bin, err := cdc.MarshalBinaryLengthPrefixed(proof)
  95. assert.Nil(t, err)
  96. // try mutating the data and make sure nothing breaks
  97. for j := 0; j < 500; j++ {
  98. bad := ctest.MutateByteSlice(bin)
  99. if !bytes.Equal(bad, bin) {
  100. assertBadProof(t, root, bad, proof)
  101. }
  102. }
  103. }
  104. // This makes sure that the proof doesn't deserialize into something valid.
  105. func assertBadProof(t *testing.T, root []byte, bad []byte, good TxProof) {
  106. var proof TxProof
  107. err := cdc.UnmarshalBinaryLengthPrefixed(bad, &proof)
  108. if err == nil {
  109. err = proof.Validate(root)
  110. if err == nil {
  111. // XXX Fix simple merkle proofs so the following is *not* OK.
  112. // This can happen if we have a slightly different total (where the
  113. // path ends up the same). If it is something else, we have a real
  114. // problem.
  115. assert.NotEqual(t, proof.Proof.Total, good.Proof.Total, "bad: %#v\ngood: %#v", proof, good)
  116. }
  117. }
  118. }