github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/core/handler_test.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/consensus/istanbul" 25 ) 26 27 // notice: the normal case have been tested in integration tests. 28 func TestHandleMsg(t *testing.T) { 29 N := uint64(4) 30 F := uint64(1) 31 sys := NewTestSystemWithBackend(N, F) 32 33 closer := sys.Run(true) 34 defer closer() 35 36 v0 := sys.backends[0] 37 r0 := v0.engine.(*core) 38 39 m, _ := Encode(&istanbul.Subject{ 40 View: &istanbul.View{ 41 Sequence: big.NewInt(0), 42 Round: big.NewInt(0), 43 }, 44 Digest: common.BytesToHash([]byte("1234567890")), 45 }) 46 // with a matched payload. istanbul.MsgPreprepare should match with *istanbul.Preprepare in normal case. 47 msg := &istanbul.Message{ 48 Code: istanbul.MsgPreprepare, 49 Msg: m, 50 Address: v0.Address(), 51 Signature: []byte{}, 52 } 53 54 _, val := v0.Validators(nil).GetByAddress(v0.Address()) 55 if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePreprepare { 56 t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare) 57 } 58 59 m, _ = Encode(&istanbul.Preprepare{ 60 View: &istanbul.View{ 61 Sequence: big.NewInt(0), 62 Round: big.NewInt(0), 63 }, 64 Proposal: makeBlock(1), 65 }) 66 // with a unmatched payload. istanbul.MsgPrepare should match with *istanbul.Subject in normal case. 67 msg = &istanbul.Message{ 68 Code: istanbul.MsgPrepare, 69 Msg: m, 70 Address: v0.Address(), 71 Signature: []byte{}, 72 } 73 74 _, val = v0.Validators(nil).GetByAddress(v0.Address()) 75 if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePrepare { 76 t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare) 77 } 78 79 m, _ = Encode(&istanbul.Preprepare{ 80 View: &istanbul.View{ 81 Sequence: big.NewInt(0), 82 Round: big.NewInt(0), 83 }, 84 Proposal: makeBlock(2), 85 }) 86 // with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case. 87 msg = &istanbul.Message{ 88 Code: istanbul.MsgCommit, 89 Msg: m, 90 Address: v0.Address(), 91 Signature: []byte{}, 92 } 93 94 _, val = v0.Validators(nil).GetByAddress(v0.Address()) 95 if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit { 96 t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit) 97 } 98 99 m, _ = Encode(&istanbul.Preprepare{ 100 View: &istanbul.View{ 101 Sequence: big.NewInt(0), 102 Round: big.NewInt(0), 103 }, 104 Proposal: makeBlock(3), 105 }) 106 // invalid message code. message code is not exists in list 107 msg = &istanbul.Message{ 108 Code: uint64(99), 109 Msg: m, 110 Address: v0.Address(), 111 Signature: []byte{}, 112 } 113 114 _, val = v0.Validators(nil).GetByAddress(v0.Address()) 115 if err := r0.handleCheckedMsg(msg, val); err == nil { 116 t.Errorf("error mismatch: have %v, want nil", err) 117 } 118 119 // with malicious payload 120 if err := r0.handleMsg([]byte{1}); err == nil { 121 t.Errorf("error mismatch: have %v, want nil", err) 122 } 123 }