github.com/number571/tendermint@v0.34.11-gost/types/validator_test.go (about)

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