MCPcopy Create free account
hub / github.com/CPChain/chain / testStatusMsgErrors

Function testStatusMsgErrors

protocols/cpc/protocol_test.go:26–76  ·  view source on GitHub ↗
(t *testing.T, protocol int)

Source from the content-addressed store, hash-verified

24func TestStatusMsgErrorsCpc1(t *testing.T) { testStatusMsgErrors(t, 1) }
25
26func testStatusMsgErrors(t *testing.T, protocol int) {
27 pm, _ := newTestProtocolManagerMust(t, 0, nil, nil)
28 var (
29 genesis = pm.blockchain.Genesis()
30 head = pm.blockchain.CurrentHeader()
31 ht = pm.blockchain.CurrentBlock().Number()
32 )
33 defer pm.Stop()
34
35 tests := []struct {
36 code uint64
37 data interface{}
38 wantError error
39 }{
40 {
41 code: TxMsg, data: []interface{}{},
42 wantError: errResp(ErrNoStatusMsg, "first msg has code 2 (!= 0)"),
43 },
44 {
45 code: StatusMsg, data: statusData{10, DefaultConfig.NetworkId, ht, head.Hash(), genesis.Hash(), false},
46 wantError: errResp(ErrProtocolVersionMismatch, "10 (!= %d)", protocol),
47 },
48 {
49 code: StatusMsg, data: statusData{uint32(protocol), 999, ht, head.Hash(), genesis.Hash(), false},
50 wantError: errResp(ErrNetworkIdMismatch, "999 (!= 322371584)"),
51 },
52 {
53 code: StatusMsg, data: statusData{uint32(protocol), DefaultConfig.NetworkId, ht, head.Hash(), common.Hash{3}, false},
54 wantError: errResp(ErrGenesisBlockMismatch, "0300000000000000 (!= %x)", genesis.Hash().Bytes()[:8]),
55 },
56 }
57
58 for i, test := range tests {
59 p, errc := newTestPeer("peer", protocol, pm, false)
60 // The send call might hang until reset because
61 // the protocol might not read the payload.
62 go p2p.Send(p.app, test.code, test.data)
63
64 select {
65 case err := <-errc:
66 if err == nil {
67 t.Errorf("test %d: protocol returned nil error, want %q", i, test.wantError)
68 } else if err.Error() != test.wantError.Error() {
69 t.Errorf("test %d: wrong error: got %q, want %q", i, err, test.wantError)
70 }
71 case <-time.After(2 * time.Second):
72 t.Errorf("protocol did not shut down within 2 seconds")
73 }
74 p.close()
75 }
76}
77
78// This test checks that received transactions are added to the local pool.
79func TestRecvTransactionsCpc1(t *testing.T) { testRecvTransactions(t, 1) }

Callers 1

TestStatusMsgErrorsCpc1Function · 0.85

Calls 12

newTestPeerFunction · 0.85
GenesisMethod · 0.80
errRespFunction · 0.70
CurrentHeaderMethod · 0.65
CurrentBlockMethod · 0.65
StopMethod · 0.65
HashMethod · 0.65
BytesMethod · 0.65
ErrorMethod · 0.65
NumberMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected