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
2.5 KiB

  1. package types
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto"
  8. )
  9. func TestValidatorProtoBuf(t *testing.T) {
  10. ctx, cancel := context.WithCancel(context.Background())
  11. defer cancel()
  12. val, _, err := randValidator(ctx, true, 100)
  13. require.NoError(t, err)
  14. testCases := []struct {
  15. msg string
  16. v1 *Validator
  17. expPass1 bool
  18. expPass2 bool
  19. }{
  20. {"success validator", val, true, true},
  21. {"failure empty", &Validator{}, false, false},
  22. {"failure nil", nil, false, false},
  23. }
  24. for _, tc := range testCases {
  25. protoVal, err := tc.v1.ToProto()
  26. if tc.expPass1 {
  27. require.NoError(t, err, tc.msg)
  28. } else {
  29. require.Error(t, err, tc.msg)
  30. }
  31. val, err := ValidatorFromProto(protoVal)
  32. if tc.expPass2 {
  33. require.NoError(t, err, tc.msg)
  34. require.Equal(t, tc.v1, val, tc.msg)
  35. } else {
  36. require.Error(t, err, tc.msg)
  37. }
  38. }
  39. }
  40. func TestValidatorValidateBasic(t *testing.T) {
  41. ctx, cancel := context.WithCancel(context.Background())
  42. defer cancel()
  43. priv := NewMockPV()
  44. pubKey, _ := priv.GetPubKey(ctx)
  45. testCases := []struct {
  46. val *Validator
  47. err bool
  48. msg string
  49. }{
  50. {
  51. val: NewValidator(pubKey, 1),
  52. err: false,
  53. msg: "",
  54. },
  55. {
  56. val: nil,
  57. err: true,
  58. msg: "nil validator",
  59. },
  60. {
  61. val: &Validator{
  62. PubKey: nil,
  63. },
  64. err: true,
  65. msg: "validator does not have a public key",
  66. },
  67. {
  68. val: NewValidator(pubKey, -1),
  69. err: true,
  70. msg: "validator has negative voting power",
  71. },
  72. {
  73. val: &Validator{
  74. PubKey: pubKey,
  75. Address: nil,
  76. },
  77. err: true,
  78. msg: "validator address is the wrong size: ",
  79. },
  80. {
  81. val: &Validator{
  82. PubKey: pubKey,
  83. Address: []byte{'a'},
  84. },
  85. err: true,
  86. msg: "validator address is the wrong size: 61",
  87. },
  88. }
  89. for _, tc := range testCases {
  90. err := tc.val.ValidateBasic()
  91. if tc.err {
  92. if assert.Error(t, err) {
  93. assert.Equal(t, tc.msg, err.Error())
  94. }
  95. } else {
  96. assert.NoError(t, err)
  97. }
  98. }
  99. }
  100. // Testing util functions
  101. // deterministicValidator returns a deterministic validator, useful for testing.
  102. // UNSTABLE
  103. func deterministicValidator(ctx context.Context, t *testing.T, key crypto.PrivKey) (*Validator, PrivValidator) {
  104. t.Helper()
  105. privVal := NewMockPV()
  106. privVal.PrivKey = key
  107. var votePower int64 = 50
  108. pubKey, err := privVal.GetPubKey(ctx)
  109. require.NoError(t, err, "could not retrieve pubkey")
  110. val := NewValidator(pubKey, votePower)
  111. return val, privVal
  112. }