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.

122 lines
3.0 KiB

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