github.com/project-88388/tendermint-v0.34.14-terra.2@v1.0.0/types/validator_test.go (about)

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