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.

192 lines
5.3 KiB

  1. package types
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. "github.com/gogo/protobuf/proto"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. "github.com/tendermint/tendermint/libs/protoio"
  11. tmrand "github.com/tendermint/tendermint/libs/rand"
  12. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  13. )
  14. var (
  15. testProposal *Proposal
  16. pbp *tmproto.Proposal
  17. )
  18. func init() {
  19. var stamp, err = time.Parse(TimeFormat, "2018-02-11T07:09:22.765Z")
  20. if err != nil {
  21. panic(err)
  22. }
  23. testProposal = &Proposal{
  24. Height: 12345,
  25. Round: 23456,
  26. BlockID: BlockID{Hash: []byte("--June_15_2020_amino_was_removed"),
  27. PartSetHeader: PartSetHeader{Total: 111, Hash: []byte("--June_15_2020_amino_was_removed")}},
  28. POLRound: -1,
  29. Timestamp: stamp,
  30. }
  31. pbp = testProposal.ToProto()
  32. }
  33. func TestProposalSignable(t *testing.T) {
  34. chainID := "test_chain_id"
  35. signBytes := ProposalSignBytes(chainID, pbp)
  36. pb := CanonicalizeProposal(chainID, pbp)
  37. expected, err := protoio.MarshalDelimited(&pb)
  38. require.NoError(t, err)
  39. require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Proposal")
  40. }
  41. func TestProposalString(t *testing.T) {
  42. str := testProposal.String()
  43. expected := `Proposal{12345/23456 (2D2D4A756E655F31355F323032305F616D696E6F5F7761735F72656D6F766564:111:2D2D4A756E65, -1) 000000000000 @ 2018-02-11T07:09:22.765Z}` //nolint:lll // ignore line length for tests
  44. if str != expected {
  45. t.Errorf("got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
  46. }
  47. }
  48. func TestProposalVerifySignature(t *testing.T) {
  49. privVal := NewMockPV()
  50. pubKey, err := privVal.GetPubKey()
  51. require.NoError(t, err)
  52. prop := NewProposal(
  53. 4, 2, 2,
  54. BlockID{tmrand.Bytes(tmhash.Size), PartSetHeader{777, tmrand.Bytes(tmhash.Size)}})
  55. p := prop.ToProto()
  56. signBytes := ProposalSignBytes("test_chain_id", p)
  57. // sign it
  58. err = privVal.SignProposal("test_chain_id", p)
  59. require.NoError(t, err)
  60. prop.Signature = p.Signature
  61. // verify the same proposal
  62. valid := pubKey.VerifyBytes(signBytes, prop.Signature)
  63. require.True(t, valid)
  64. // serialize, deserialize and verify again....
  65. newProp := new(tmproto.Proposal)
  66. pb := prop.ToProto()
  67. bs, err := proto.Marshal(pb)
  68. require.NoError(t, err)
  69. err = proto.Unmarshal(bs, newProp)
  70. require.NoError(t, err)
  71. np, err := ProposalFromProto(newProp)
  72. require.NoError(t, err)
  73. // verify the transmitted proposal
  74. newSignBytes := ProposalSignBytes("test_chain_id", pb)
  75. require.Equal(t, string(signBytes), string(newSignBytes))
  76. valid = pubKey.VerifyBytes(newSignBytes, np.Signature)
  77. require.True(t, valid)
  78. }
  79. func BenchmarkProposalWriteSignBytes(b *testing.B) {
  80. for i := 0; i < b.N; i++ {
  81. ProposalSignBytes("test_chain_id", pbp)
  82. }
  83. }
  84. func BenchmarkProposalSign(b *testing.B) {
  85. privVal := NewMockPV()
  86. for i := 0; i < b.N; i++ {
  87. err := privVal.SignProposal("test_chain_id", pbp)
  88. if err != nil {
  89. b.Error(err)
  90. }
  91. }
  92. }
  93. func BenchmarkProposalVerifySignature(b *testing.B) {
  94. privVal := NewMockPV()
  95. err := privVal.SignProposal("test_chain_id", pbp)
  96. require.NoError(b, err)
  97. pubKey, err := privVal.GetPubKey()
  98. require.NoError(b, err)
  99. for i := 0; i < b.N; i++ {
  100. pubKey.VerifyBytes(ProposalSignBytes("test_chain_id", pbp), testProposal.Signature)
  101. }
  102. }
  103. func TestProposalValidateBasic(t *testing.T) {
  104. privVal := NewMockPV()
  105. testCases := []struct {
  106. testName string
  107. malleateProposal func(*Proposal)
  108. expectErr bool
  109. }{
  110. {"Good Proposal", func(p *Proposal) {}, false},
  111. {"Invalid Type", func(p *Proposal) { p.Type = tmproto.PrecommitType }, true},
  112. {"Invalid Height", func(p *Proposal) { p.Height = -1 }, true},
  113. {"Invalid Round", func(p *Proposal) { p.Round = -1 }, true},
  114. {"Invalid POLRound", func(p *Proposal) { p.POLRound = -2 }, true},
  115. {"Invalid BlockId", func(p *Proposal) {
  116. p.BlockID = BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}}
  117. }, true},
  118. {"Invalid Signature", func(p *Proposal) {
  119. p.Signature = make([]byte, 0)
  120. }, true},
  121. {"Too big Signature", func(p *Proposal) {
  122. p.Signature = make([]byte, MaxSignatureSize+1)
  123. }, true},
  124. }
  125. blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
  126. for _, tc := range testCases {
  127. tc := tc
  128. t.Run(tc.testName, func(t *testing.T) {
  129. prop := NewProposal(
  130. 4, 2, 2,
  131. blockID)
  132. p := prop.ToProto()
  133. err := privVal.SignProposal("test_chain_id", p)
  134. prop.Signature = p.Signature
  135. require.NoError(t, err)
  136. tc.malleateProposal(prop)
  137. assert.Equal(t, tc.expectErr, prop.ValidateBasic() != nil, "Validate Basic had an unexpected result")
  138. })
  139. }
  140. }
  141. func TestProposalProtoBuf(t *testing.T) {
  142. proposal := NewProposal(1, 2, 3, makeBlockID([]byte("hash"), 2, []byte("part_set_hash")))
  143. proposal.Signature = []byte("sig")
  144. proposal2 := NewProposal(1, 2, 3, BlockID{})
  145. testCases := []struct {
  146. msg string
  147. p1 *Proposal
  148. expPass bool
  149. }{
  150. {"success", proposal, true},
  151. {"success", proposal2, false}, // blcokID cannot be empty
  152. {"empty proposal failure validatebasic", &Proposal{}, false},
  153. {"nil proposal", nil, false},
  154. }
  155. for _, tc := range testCases {
  156. protoProposal := tc.p1.ToProto()
  157. p, err := ProposalFromProto(protoProposal)
  158. if tc.expPass {
  159. require.NoError(t, err)
  160. require.Equal(t, tc.p1, p, tc.msg)
  161. } else {
  162. require.Error(t, err)
  163. }
  164. }
  165. }