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.

99 lines
1.8 KiB

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