github.com/Finschia/ostracon@v1.1.5/types/light_test.go (about) 1 package types 2 3 import ( 4 "math" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 10 tmversion "github.com/tendermint/tendermint/proto/tendermint/version" 11 12 "github.com/Finschia/ostracon/crypto" 13 "github.com/Finschia/ostracon/version" 14 ) 15 16 func TestLightBlockValidateBasic(t *testing.T) { 17 header := makeRandHeader() 18 commit := randCommit(time.Now()) 19 vals, _ := RandValidatorSet(5, 1) 20 header.Height = commit.Height 21 header.LastBlockID = commit.BlockID 22 header.ValidatorsHash = vals.Hash() 23 header.Version.Block = version.BlockProtocol 24 vals2, _ := RandValidatorSet(3, 1) 25 vals3 := vals.Copy() 26 vals3.Validators[2] = &Validator{} 27 commit.BlockID.Hash = header.Hash() 28 29 sh := &SignedHeader{ 30 Header: &header, 31 Commit: commit, 32 } 33 34 testCases := []struct { 35 name string 36 sh *SignedHeader 37 vals *ValidatorSet 38 expectErr bool 39 }{ 40 {"valid light block", sh, vals, false}, 41 {"hashes don't match", sh, vals2, true}, 42 {"invalid validator set", sh, vals3, true}, 43 {"invalid signed header", &SignedHeader{Header: &header, Commit: randCommit(time.Now())}, vals, true}, 44 {"empty signed header", nil, vals, true}, 45 {"empty validator set", sh, nil, true}, 46 } 47 48 for _, tc := range testCases { 49 lightBlock := LightBlock{ 50 SignedHeader: tc.sh, 51 ValidatorSet: tc.vals, 52 } 53 err := lightBlock.ValidateBasic(header.ChainID) 54 if tc.expectErr { 55 assert.Error(t, err, tc.name) 56 } else { 57 assert.NoError(t, err, tc.name) 58 } 59 } 60 61 } 62 63 func TestLightBlockProtobuf(t *testing.T) { 64 header := makeRandHeader() 65 commit := randCommit(time.Now()) 66 vals, _ := RandValidatorSet(5, 1) 67 header.Height = commit.Height 68 header.LastBlockID = commit.BlockID 69 header.Version.Block = version.BlockProtocol 70 header.ValidatorsHash = vals.Hash() 71 commit.BlockID.Hash = header.Hash() 72 73 sh := &SignedHeader{ 74 Header: &header, 75 Commit: commit, 76 } 77 78 testCases := []struct { 79 name string 80 sh *SignedHeader 81 vals *ValidatorSet 82 toProtoErr bool 83 toBlockErr bool 84 }{ 85 {"valid light block", sh, vals, false, false}, 86 {"empty signed header", &SignedHeader{}, vals, false, false}, 87 {"empty validator set", sh, &ValidatorSet{}, false, true}, 88 {"empty light block", &SignedHeader{}, &ValidatorSet{}, false, true}, 89 } 90 91 for _, tc := range testCases { 92 lightBlock := &LightBlock{ 93 SignedHeader: tc.sh, 94 ValidatorSet: tc.vals, 95 } 96 lbp, err := lightBlock.ToProto() 97 if tc.toProtoErr { 98 assert.Error(t, err, tc.name) 99 } else { 100 assert.NoError(t, err, tc.name) 101 } 102 103 lb, err := LightBlockFromProto(lbp) 104 if tc.toBlockErr { 105 assert.Error(t, err, tc.name) 106 } else { 107 assert.NoError(t, err, tc.name) 108 assert.Equal(t, lightBlock, lb) 109 } 110 } 111 112 } 113 114 func TestSignedHeaderValidateBasic(t *testing.T) { 115 commit := randCommit(time.Now()) 116 chainID := "𠜎" 117 timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC) 118 h := Header{ 119 Version: tmversion.Consensus{Block: version.BlockProtocol, App: math.MaxInt64}, 120 ChainID: chainID, 121 Height: commit.Height, 122 Time: timestamp, 123 LastBlockID: commit.BlockID, 124 LastCommitHash: commit.Hash(), 125 DataHash: commit.Hash(), 126 ValidatorsHash: commit.Hash(), 127 NextValidatorsHash: commit.Hash(), 128 ConsensusHash: commit.Hash(), 129 AppHash: commit.Hash(), 130 LastResultsHash: commit.Hash(), 131 EvidenceHash: commit.Hash(), 132 ProposerAddress: crypto.AddressHash([]byte("proposer_address")), 133 } 134 135 validSignedHeader := SignedHeader{Header: &h, Commit: commit} 136 validSignedHeader.Commit.BlockID.Hash = validSignedHeader.Hash() 137 invalidSignedHeader := SignedHeader{} 138 139 testCases := []struct { 140 testName string 141 shHeader *Header 142 shCommit *Commit 143 expectErr bool 144 }{ 145 {"Valid Signed Header", validSignedHeader.Header, validSignedHeader.Commit, false}, 146 {"Invalid Signed Header", invalidSignedHeader.Header, validSignedHeader.Commit, true}, 147 {"Invalid Signed Header", validSignedHeader.Header, invalidSignedHeader.Commit, true}, 148 } 149 150 for _, tc := range testCases { 151 tc := tc 152 t.Run(tc.testName, func(t *testing.T) { 153 sh := SignedHeader{ 154 Header: tc.shHeader, 155 Commit: tc.shCommit, 156 } 157 err := sh.ValidateBasic(validSignedHeader.Header.ChainID) 158 assert.Equalf( 159 t, 160 tc.expectErr, 161 err != nil, 162 "Validate Basic had an unexpected result", 163 err, 164 ) 165 }) 166 } 167 }