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.

105 lines
2.5 KiB

  1. package types
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. cmn "github.com/tendermint/go-common"
  6. ctest "github.com/tendermint/go-common/test"
  7. wire "github.com/tendermint/go-wire"
  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 TestValidTxProof(t *testing.T) {
  21. assert := assert.New(t)
  22. cases := []struct {
  23. txs Txs
  24. }{
  25. {Txs{{1, 4, 34, 87, 163, 1}}},
  26. {Txs{{5, 56, 165, 2}, {4, 77}}},
  27. {Txs{Tx("foo"), Tx("bar"), Tx("baz")}},
  28. {makeTxs(20, 5)},
  29. {makeTxs(7, 81)},
  30. {makeTxs(61, 15)},
  31. }
  32. for h, tc := range cases {
  33. txs := tc.txs
  34. root := txs.Hash()
  35. // make sure valid proof for every tx
  36. for i := range txs {
  37. leaf := txs[i]
  38. leafHash := leaf.Hash()
  39. proof := txs.Proof(i)
  40. assert.Equal(i, proof.Index, "%d: %d", h, i)
  41. assert.Equal(len(txs), proof.Total, "%d: %d", h, i)
  42. assert.Equal(root, proof.RootHash, "%d: %d", h, i)
  43. assert.Equal(leaf, proof.Data, "%d: %d", h, i)
  44. assert.Equal(leafHash, proof.LeafHash(), "%d: %d", h, i)
  45. assert.Nil(proof.Validate(root), "%d: %d", h, i)
  46. assert.NotNil(proof.Validate([]byte("foobar")), "%d: %d", h, i)
  47. // read-write must also work
  48. var p2 TxProof
  49. bin := wire.BinaryBytes(proof)
  50. err := wire.ReadBinaryBytes(bin, &p2)
  51. if assert.Nil(err, "%d: %d: %+v", h, i, err) {
  52. assert.Nil(p2.Validate(root), "%d: %d", h, i)
  53. }
  54. }
  55. }
  56. }
  57. func TestTxProofUnchangable(t *testing.T) {
  58. // run the other test a bunch...
  59. for i := 0; i < 4; i++ {
  60. testTxProofUnchangable(t)
  61. }
  62. }
  63. func testTxProofUnchangable(t *testing.T) {
  64. assert := assert.New(t)
  65. // make some proof
  66. txs := makeTxs(randInt(2, 100), randInt(16, 128))
  67. root := txs.Hash()
  68. i := randInt(0, len(txs)-1)
  69. proof := txs.Proof(i)
  70. // make sure it is valid to start with
  71. assert.Nil(proof.Validate(root))
  72. bin := wire.BinaryBytes(proof)
  73. // try mutating the data and make sure nothing breaks
  74. for j := 0; j < 50; j++ {
  75. bad := ctest.MutateByteSlice(bin)
  76. assertBadProof(t, root, bad)
  77. }
  78. }
  79. // this make sure the proof doesn't deserialize into something valid
  80. func assertBadProof(t *testing.T, root []byte, bad []byte) {
  81. // we kind of expect this to panic sometimes... (bad, go-wire, bad)
  82. defer func() {
  83. recover()
  84. }()
  85. var proof TxProof
  86. err := wire.ReadBinaryBytes(bad, &proof)
  87. if err == nil {
  88. err = proof.Validate(root)
  89. assert.NotNil(t, err, "%+v", err)
  90. }
  91. }