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.

98 lines
2.7 KiB

9 years ago
  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. )
  7. var testProposal *Proposal
  8. func init() {
  9. var stamp, err = time.Parse(TimeFormat, "2018-02-11T07:09:22.765Z")
  10. if err != nil {
  11. panic(err)
  12. }
  13. testProposal = &Proposal{
  14. Height: 12345,
  15. Round: 23456,
  16. BlockPartsHeader: PartSetHeader{111, []byte("blockparts")},
  17. POLRound: -1,
  18. Timestamp: stamp,
  19. }
  20. }
  21. func TestProposalSignable(t *testing.T) {
  22. signBytes := testProposal.SignBytes("test_chain_id")
  23. signStr := string(signBytes)
  24. expected := `{"@chain_id":"test_chain_id","@type":"proposal","block_parts_header":{"hash":"626C6F636B7061727473","total":111},"height":12345,"pol_block_id":{},"pol_round":-1,"round":23456,"timestamp":"2018-02-11T07:09:22.765Z"}`
  25. if signStr != expected {
  26. t.Errorf("Got unexpected sign string for Proposal. Expected:\n%v\nGot:\n%v", expected, signStr)
  27. }
  28. }
  29. func TestProposalString(t *testing.T) {
  30. str := testProposal.String()
  31. expected := `Proposal{12345/23456 111:626C6F636B70 (-1,:0:000000000000) <nil> @ 2018-02-11T07:09:22.765Z}`
  32. if str != expected {
  33. t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
  34. }
  35. }
  36. func TestProposalVerifySignature(t *testing.T) {
  37. privVal := NewMockPV()
  38. pubKey := privVal.GetPubKey()
  39. prop := NewProposal(4, 2, PartSetHeader{777, []byte("proper")}, 2, BlockID{})
  40. signBytes := prop.SignBytes("test_chain_id")
  41. // sign it
  42. err := privVal.SignProposal("test_chain_id", prop)
  43. require.NoError(t, err)
  44. // verify the same proposal
  45. valid := pubKey.VerifyBytes(signBytes, prop.Signature)
  46. require.True(t, valid)
  47. // serialize, deserialize and verify again....
  48. newProp := new(Proposal)
  49. bs, err := cdc.MarshalBinary(prop)
  50. require.NoError(t, err)
  51. err = cdc.UnmarshalBinary(bs, &newProp)
  52. require.NoError(t, err)
  53. // verify the transmitted proposal
  54. newSignBytes := newProp.SignBytes("test_chain_id")
  55. require.Equal(t, string(signBytes), string(newSignBytes))
  56. valid = pubKey.VerifyBytes(newSignBytes, newProp.Signature)
  57. require.True(t, valid)
  58. }
  59. func BenchmarkProposalWriteSignBytes(b *testing.B) {
  60. for i := 0; i < b.N; i++ {
  61. testProposal.SignBytes("test_chain_id")
  62. }
  63. }
  64. func BenchmarkProposalSign(b *testing.B) {
  65. privVal := NewMockPV()
  66. for i := 0; i < b.N; i++ {
  67. err := privVal.SignProposal("test_chain_id", testProposal)
  68. if err != nil {
  69. b.Error(err)
  70. }
  71. }
  72. }
  73. func BenchmarkProposalVerifySignature(b *testing.B) {
  74. privVal := NewMockPV()
  75. err := privVal.SignProposal("test_chain_id", testProposal)
  76. require.Nil(b, err)
  77. pubKey := privVal.GetPubKey()
  78. for i := 0; i < b.N; i++ {
  79. pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), testProposal.Signature)
  80. }
  81. }