github.com/ConsenSys/Quorum@v20.10.0+incompatible/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.StringToHash("1234567890"),
    45  	})
    46  	// with a matched payload. msgPreprepare should match with *istanbul.Preprepare in normal case.
    47  	msg := &message{
    48  		Code:          msgPreprepare,
    49  		Msg:           m,
    50  		Address:       v0.Address(),
    51  		Signature:     []byte{},
    52  		CommittedSeal: []byte{},
    53  	}
    54  
    55  	_, val := v0.Validators(nil).GetByAddress(v0.Address())
    56  	if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePreprepare {
    57  		t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare)
    58  	}
    59  
    60  	m, _ = Encode(&istanbul.Preprepare{
    61  		View: &istanbul.View{
    62  			Sequence: big.NewInt(0),
    63  			Round:    big.NewInt(0),
    64  		},
    65  		Proposal: makeBlock(1),
    66  	})
    67  	// with a unmatched payload. msgPrepare should match with *istanbul.Subject in normal case.
    68  	msg = &message{
    69  		Code:          msgPrepare,
    70  		Msg:           m,
    71  		Address:       v0.Address(),
    72  		Signature:     []byte{},
    73  		CommittedSeal: []byte{},
    74  	}
    75  
    76  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
    77  	if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePrepare {
    78  		t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare)
    79  	}
    80  
    81  	m, _ = Encode(&istanbul.Preprepare{
    82  		View: &istanbul.View{
    83  			Sequence: big.NewInt(0),
    84  			Round:    big.NewInt(0),
    85  		},
    86  		Proposal: makeBlock(2),
    87  	})
    88  	// with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case.
    89  	msg = &message{
    90  		Code:          msgCommit,
    91  		Msg:           m,
    92  		Address:       v0.Address(),
    93  		Signature:     []byte{},
    94  		CommittedSeal: []byte{},
    95  	}
    96  
    97  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
    98  	if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit {
    99  		t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit)
   100  	}
   101  
   102  	m, _ = Encode(&istanbul.Preprepare{
   103  		View: &istanbul.View{
   104  			Sequence: big.NewInt(0),
   105  			Round:    big.NewInt(0),
   106  		},
   107  		Proposal: makeBlock(3),
   108  	})
   109  	// invalid message code. message code is not exists in list
   110  	msg = &message{
   111  		Code:          uint64(99),
   112  		Msg:           m,
   113  		Address:       v0.Address(),
   114  		Signature:     []byte{},
   115  		CommittedSeal: []byte{},
   116  	}
   117  
   118  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
   119  	if err := r0.handleCheckedMsg(msg, val); err == nil {
   120  		t.Errorf("error mismatch: have %v, want nil", err)
   121  	}
   122  
   123  	// with malicious payload
   124  	if err := r0.handleMsg([]byte{1}); err == nil {
   125  		t.Errorf("error mismatch: have %v, want nil", err)
   126  	}
   127  }