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.

140 lines
3.6 KiB

  1. package merkle
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/tendermint/go-amino"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  7. )
  8. const ProofOpDomino = "test:domino"
  9. // Expects given input, produces given output.
  10. // Like the game dominos.
  11. type DominoOp struct {
  12. key string // unexported, may be empty
  13. Input string
  14. Output string
  15. }
  16. func NewDominoOp(key, input, output string) DominoOp {
  17. return DominoOp{
  18. key: key,
  19. Input: input,
  20. Output: output,
  21. }
  22. }
  23. func DominoOpDecoder(pop ProofOp) (ProofOperator, error) {
  24. if pop.Type != ProofOpDomino {
  25. panic("unexpected proof op type")
  26. }
  27. var op DominoOp // a bit strange as we'll discard this, but it works.
  28. err := amino.UnmarshalBinaryLengthPrefixed(pop.Data, &op)
  29. if err != nil {
  30. return nil, cmn.ErrorWrap(err, "decoding ProofOp.Data into SimpleValueOp")
  31. }
  32. return NewDominoOp(string(pop.Key), op.Input, op.Output), nil
  33. }
  34. func (dop DominoOp) ProofOp() ProofOp {
  35. bz := amino.MustMarshalBinaryLengthPrefixed(dop)
  36. return ProofOp{
  37. Type: ProofOpDomino,
  38. Key: []byte(dop.key),
  39. Data: bz,
  40. }
  41. }
  42. func (dop DominoOp) Run(input [][]byte) (output [][]byte, err error) {
  43. if len(input) != 1 {
  44. return nil, cmn.NewError("Expected input of length 1")
  45. }
  46. if string(input[0]) != dop.Input {
  47. return nil, cmn.NewError("Expected input %v, got %v",
  48. dop.Input, string(input[0]))
  49. }
  50. return [][]byte{[]byte(dop.Output)}, nil
  51. }
  52. func (dop DominoOp) GetKey() []byte {
  53. return []byte(dop.key)
  54. }
  55. //----------------------------------------
  56. func TestProofOperators(t *testing.T) {
  57. var err error
  58. // ProofRuntime setup
  59. // TODO test this somehow.
  60. // prt := NewProofRuntime()
  61. // prt.RegisterOpDecoder(ProofOpDomino, DominoOpDecoder)
  62. // ProofOperators setup
  63. op1 := NewDominoOp("KEY1", "INPUT1", "INPUT2")
  64. op2 := NewDominoOp("KEY2", "INPUT2", "INPUT3")
  65. op3 := NewDominoOp("", "INPUT3", "INPUT4")
  66. op4 := NewDominoOp("KEY4", "INPUT4", "OUTPUT4")
  67. // Good
  68. popz := ProofOperators([]ProofOperator{op1, op2, op3, op4})
  69. err = popz.Verify(bz("OUTPUT4"), "/KEY4/KEY2/KEY1", [][]byte{bz("INPUT1")})
  70. assert.Nil(t, err)
  71. err = popz.VerifyValue(bz("OUTPUT4"), "/KEY4/KEY2/KEY1", bz("INPUT1"))
  72. assert.Nil(t, err)
  73. // BAD INPUT
  74. err = popz.Verify(bz("OUTPUT4"), "/KEY4/KEY2/KEY1", [][]byte{bz("INPUT1_WRONG")})
  75. assert.NotNil(t, err)
  76. err = popz.VerifyValue(bz("OUTPUT4"), "/KEY4/KEY2/KEY1", bz("INPUT1_WRONG"))
  77. assert.NotNil(t, err)
  78. // BAD KEY 1
  79. err = popz.Verify(bz("OUTPUT4"), "/KEY3/KEY2/KEY1", [][]byte{bz("INPUT1")})
  80. assert.NotNil(t, err)
  81. // BAD KEY 2
  82. err = popz.Verify(bz("OUTPUT4"), "KEY4/KEY2/KEY1", [][]byte{bz("INPUT1")})
  83. assert.NotNil(t, err)
  84. // BAD KEY 3
  85. err = popz.Verify(bz("OUTPUT4"), "/KEY4/KEY2/KEY1/", [][]byte{bz("INPUT1")})
  86. assert.NotNil(t, err)
  87. // BAD KEY 4
  88. err = popz.Verify(bz("OUTPUT4"), "//KEY4/KEY2/KEY1", [][]byte{bz("INPUT1")})
  89. assert.NotNil(t, err)
  90. // BAD KEY 5
  91. err = popz.Verify(bz("OUTPUT4"), "/KEY2/KEY1", [][]byte{bz("INPUT1")})
  92. assert.NotNil(t, err)
  93. // BAD OUTPUT 1
  94. err = popz.Verify(bz("OUTPUT4_WRONG"), "/KEY4/KEY2/KEY1", [][]byte{bz("INPUT1")})
  95. assert.NotNil(t, err)
  96. // BAD OUTPUT 2
  97. err = popz.Verify(bz(""), "/KEY4/KEY2/KEY1", [][]byte{bz("INPUT1")})
  98. assert.NotNil(t, err)
  99. // BAD POPZ 1
  100. popz = []ProofOperator{op1, op2, op4}
  101. err = popz.Verify(bz("OUTPUT4"), "/KEY4/KEY2/KEY1", [][]byte{bz("INPUT1")})
  102. assert.NotNil(t, err)
  103. // BAD POPZ 2
  104. popz = []ProofOperator{op4, op3, op2, op1}
  105. err = popz.Verify(bz("OUTPUT4"), "/KEY4/KEY2/KEY1", [][]byte{bz("INPUT1")})
  106. assert.NotNil(t, err)
  107. // BAD POPZ 3
  108. popz = []ProofOperator{}
  109. err = popz.Verify(bz("OUTPUT4"), "/KEY4/KEY2/KEY1", [][]byte{bz("INPUT1")})
  110. assert.NotNil(t, err)
  111. }
  112. func bz(s string) []byte {
  113. return []byte(s)
  114. }