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.

100 lines
1.8 KiB

  1. package types
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestValidatorProtoBuf(t *testing.T) {
  9. val, _ := RandValidator(true, 100)
  10. testCases := []struct {
  11. msg string
  12. v1 *Validator
  13. expPass1 bool
  14. expPass2 bool
  15. }{
  16. {"success validator", val, true, true},
  17. {"failure empty", &Validator{}, false, false},
  18. {"failure nil", nil, false, false},
  19. }
  20. for _, tc := range testCases {
  21. protoVal, err := tc.v1.ToProto()
  22. if tc.expPass1 {
  23. require.NoError(t, err, tc.msg)
  24. } else {
  25. require.Error(t, err, tc.msg)
  26. }
  27. val, err := ValidatorFromProto(protoVal)
  28. if tc.expPass2 {
  29. require.NoError(t, err, tc.msg)
  30. require.Equal(t, tc.v1, val, tc.msg)
  31. } else {
  32. require.Error(t, err, tc.msg)
  33. }
  34. }
  35. }
  36. func TestValidatorValidateBasic(t *testing.T) {
  37. priv := NewMockPV()
  38. pubKey, _ := priv.GetPubKey(context.Background())
  39. testCases := []struct {
  40. val *Validator
  41. err bool
  42. msg string
  43. }{
  44. {
  45. val: NewValidator(pubKey, 1),
  46. err: false,
  47. msg: "",
  48. },
  49. {
  50. val: nil,
  51. err: true,
  52. msg: "nil validator",
  53. },
  54. {
  55. val: &Validator{
  56. PubKey: nil,
  57. },
  58. err: true,
  59. msg: "validator does not have a public key",
  60. },
  61. {
  62. val: NewValidator(pubKey, -1),
  63. err: true,
  64. msg: "validator has negative voting power",
  65. },
  66. {
  67. val: &Validator{
  68. PubKey: pubKey,
  69. Address: nil,
  70. },
  71. err: true,
  72. msg: "validator address is the wrong size: ",
  73. },
  74. {
  75. val: &Validator{
  76. PubKey: pubKey,
  77. Address: []byte{'a'},
  78. },
  79. err: true,
  80. msg: "validator address is the wrong size: 61",
  81. },
  82. }
  83. for _, tc := range testCases {
  84. err := tc.val.ValidateBasic()
  85. if tc.err {
  86. if assert.Error(t, err) {
  87. assert.Equal(t, tc.msg, err.Error())
  88. }
  89. } else {
  90. assert.NoError(t, err)
  91. }
  92. }
  93. }