github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/consensus/dbft/core/handler_test.go (about)

     1  package core
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/quickchainproject/quickchain/common"
     8  	"github.com/quickchainproject/quickchain/consensus/dbft"
     9  )
    10  
    11  // notice: the normal case have been tested in integration tests.
    12  func TestHandleMsg(t *testing.T) {
    13  	N := uint64(4)
    14  	F := uint64(1)
    15  	sys := NewTestSystemWithBackend(N, F)
    16  
    17  	closer := sys.Run(true)
    18  	defer closer()
    19  
    20  	v0 := sys.backends[0]
    21  	r0 := v0.engine.(*core)
    22  
    23  	m, _ := Encode(&bft.Subject{
    24  		View: &bft.View{
    25  			Sequence: big.NewInt(0),
    26  			Round:    big.NewInt(0),
    27  		},
    28  		Digest: common.StringToHash("1234567890"),
    29  	})
    30  	// with a matched payload. msgPreprepare should match with *bft.Preprepare in normal case.
    31  	msg := &message{
    32  		Code:          msgPreprepare,
    33  		Msg:           m,
    34  		Address:       v0.Address(),
    35  		Signature:     []byte{},
    36  		CommittedSeal: []byte{},
    37  	}
    38  
    39  	_, val := v0.Validators(nil).GetByAddress(v0.Address())
    40  	if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePreprepare {
    41  		t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare)
    42  	}
    43  
    44  	m, _ = Encode(&bft.Preprepare{
    45  		View: &bft.View{
    46  			Sequence: big.NewInt(0),
    47  			Round:    big.NewInt(0),
    48  		},
    49  		Proposal: makeBlock(1),
    50  	})
    51  	// with a unmatched payload. msgPrepare should match with *bft.Subject in normal case.
    52  	msg = &message{
    53  		Code:          msgPrepare,
    54  		Msg:           m,
    55  		Address:       v0.Address(),
    56  		Signature:     []byte{},
    57  		CommittedSeal: []byte{},
    58  	}
    59  
    60  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
    61  	if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodePrepare {
    62  		t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodePreprepare)
    63  	}
    64  
    65  	m, _ = Encode(&bft.Preprepare{
    66  		View: &bft.View{
    67  			Sequence: big.NewInt(0),
    68  			Round:    big.NewInt(0),
    69  		},
    70  		Proposal: makeBlock(2),
    71  	})
    72  	// with a unmatched payload. bft.MsgCommit should match with *bft.Subject in normal case.
    73  	msg = &message{
    74  		Code:          msgCommit,
    75  		Msg:           m,
    76  		Address:       v0.Address(),
    77  		Signature:     []byte{},
    78  		CommittedSeal: []byte{},
    79  	}
    80  
    81  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
    82  	if err := r0.handleCheckedMsg(msg, val); err != errFailedDecodeCommit {
    83  		t.Errorf("error mismatch: have %v, want %v", err, errFailedDecodeCommit)
    84  	}
    85  
    86  	m, _ = Encode(&bft.Preprepare{
    87  		View: &bft.View{
    88  			Sequence: big.NewInt(0),
    89  			Round:    big.NewInt(0),
    90  		},
    91  		Proposal: makeBlock(3),
    92  	})
    93  	// invalid message code. message code is not exists in list
    94  	msg = &message{
    95  		Code:          uint64(99),
    96  		Msg:           m,
    97  		Address:       v0.Address(),
    98  		Signature:     []byte{},
    99  		CommittedSeal: []byte{},
   100  	}
   101  
   102  	_, val = v0.Validators(nil).GetByAddress(v0.Address())
   103  	if err := r0.handleCheckedMsg(msg, val); err == nil {
   104  		t.Errorf("error mismatch: have %v, want nil", err)
   105  	}
   106  
   107  	// with malicious payload
   108  	if err := r0.handleMsg([]byte{1}); err == nil {
   109  		t.Errorf("error mismatch: have %v, want nil", err)
   110  	}
   111  }