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.

125 lines
3.0 KiB

7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. wire "github.com/tendermint/tendermint/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, err := wire.MarshalBinary(proof)
  64. assert.Nil(err)
  65. err = wire.UnmarshalBinary(bin, &p2)
  66. if assert.Nil(err, "%d: %d: %+v", h, i, err) {
  67. assert.Nil(p2.Validate(root), "%d: %d", h, i)
  68. }
  69. }
  70. }
  71. }
  72. func TestTxProofUnchangable(t *testing.T) {
  73. // run the other test a bunch...
  74. for i := 0; i < 40; i++ {
  75. testTxProofUnchangable(t)
  76. }
  77. }
  78. func testTxProofUnchangable(t *testing.T) {
  79. assert := assert.New(t)
  80. // make some proof
  81. txs := makeTxs(randInt(2, 100), randInt(16, 128))
  82. root := txs.Hash()
  83. i := randInt(0, len(txs)-1)
  84. proof := txs.Proof(i)
  85. // make sure it is valid to start with
  86. assert.Nil(proof.Validate(root))
  87. bin, err := wire.MarshalBinary(proof)
  88. assert.Nil(err)
  89. // try mutating the data and make sure nothing breaks
  90. for j := 0; j < 500; j++ {
  91. bad := ctest.MutateByteSlice(bin)
  92. if !bytes.Equal(bad, bin) {
  93. assertBadProof(t, root, bad, proof)
  94. }
  95. }
  96. }
  97. // this make sure the proof doesn't deserialize into something valid
  98. func assertBadProof(t *testing.T, root []byte, bad []byte, good TxProof) {
  99. var proof TxProof
  100. err := wire.UnmarshalBinary(bad, &proof)
  101. if err == nil {
  102. err = proof.Validate(root)
  103. if err == nil {
  104. // okay, this can happen if we have a slightly different total
  105. // (where the path ends up the same), if it is something else, we have
  106. // a real problem
  107. assert.NotEqual(t, proof.Total, good.Total, "bad: %#v\ngood: %#v", proof, good)
  108. }
  109. }
  110. }