// TestVerAck tests the MsgVerAck API. func TestVerAck(t *testing.T) { pver := wire.ProtocolVersion // Ensure the command is expected value. wantCmd := "verack" msg := wire.NewMsgVerAck() if cmd := msg.Command(); cmd != wantCmd { t.Errorf("NewMsgVerAck: wrong command - got %v want %v", cmd, wantCmd) } // Ensure max payload is expected value. wantPayload := uint32(0) maxPayload := msg.MaxPayloadLength(pver) if maxPayload != wantPayload { t.Errorf("MaxPayloadLength: wrong max payload length for "+ "protocol version %d - got %v, want %v", pver, maxPayload, wantPayload) } return }
// TestOutboundPeer tests that the outbound peer works as expected. func TestOutboundPeer(t *testing.T) { // Use a mock NewestBlock func to test errs var errBlockNotFound = errors.New("newest block not found") var mockNewestSha = func() (*wire.ShaHash, int32, error) { return nil, 0, errBlockNotFound } peerCfg := &peer.Config{ NewestBlock: mockNewestSha, UserAgentName: "peer", UserAgentVersion: "1.0", ChainParams: &chaincfg.MainNetParams, Services: 0, } r, w := io.Pipe() c := &conn{raddr: "10.0.0.1:6682", Writer: w, Reader: r} p, err := peer.NewOutboundPeer(peerCfg, "10.0.0.1:6682") if err != nil { t.Errorf("NewOutboundPeer: unexpected err - %v\n", err) return } // Test Connect err wantErr := errBlockNotFound if err := p.Connect(c); err != wantErr { t.Errorf("Connect: expected err %v, got %v\n", wantErr, err) return } // Test already connected if err := p.Connect(c); err != nil { t.Errorf("Connect: unexpected err %v\n", err) return } // Test already started if err := p.Start(); err != nil { t.Errorf("Start: unexpected err %v\n", err) return } // Test Queue Inv fakeBlockHash := &wire.ShaHash{0x00, 0x01} fakeInv := wire.NewInvVect(wire.InvTypeBlock, fakeBlockHash) p.QueueInventory(fakeInv) p.AddKnownInventory(fakeInv) p.QueueInventory(fakeInv) // Test Queue Message fakeMsg := wire.NewMsgVerAck() p.QueueMessage(fakeMsg, nil) done := make(chan struct{}) p.QueueMessage(fakeMsg, done) <-done p.Shutdown() // Test NewestBlock var newestBlock = func() (*wire.ShaHash, int32, error) { hashStr := "14a0810ac680a3eb3f82edc878cea25ec41d6b790744e5daeef" hash, err := wire.NewShaHashFromStr(hashStr) if err != nil { return nil, 0, err } return hash, 234439, nil } peerCfg.NewestBlock = newestBlock r1, w1 := io.Pipe() c1 := &conn{raddr: "10.0.0.1:6682", Writer: w1, Reader: r1} p1, err := peer.NewOutboundPeer(peerCfg, "10.0.0.1:6682") if err != nil { t.Errorf("NewOutboundPeer: unexpected err - %v\n", err) return } if err := p1.Connect(c1); err != nil { t.Errorf("Connect: unexpected err %v\n", err) return } // Test update latest block latestBlockSha, err := wire.NewShaHashFromStr("1a63f9cdff1752e6375c8c76e543a71d239e1a2e5c6db1aa679") if err != nil { t.Errorf("NewShaHashFromStr: unexpected err %v\n", err) return } p1.UpdateLastAnnouncedBlock(latestBlockSha) p1.UpdateLastBlockHeight(234440) if p1.LastAnnouncedBlock() != latestBlockSha { t.Errorf("LastAnnouncedBlock: wrong block - got %v, want %v", p1.LastAnnouncedBlock(), latestBlockSha) return } // Test Queue Inv after connection p1.QueueInventory(fakeInv) p1.Shutdown() // Test regression peerCfg.ChainParams = &chaincfg.RegressionNetParams peerCfg.Services = wire.SFNodeBloom r2, w2 := io.Pipe() c2 := &conn{raddr: "10.0.0.1:6682", Writer: w2, Reader: r2} p2, err := peer.NewOutboundPeer(peerCfg, "10.0.0.1:6682") if err != nil { t.Errorf("NewOutboundPeer: unexpected err - %v\n", err) return } if err := p2.Connect(c2); err != nil { t.Errorf("Connect: unexpected err %v\n", err) return } // Test PushXXX var addrs []*wire.NetAddress for i := 0; i < 5; i++ { na := wire.NetAddress{} addrs = append(addrs, &na) } if _, err := p2.PushAddrMsg(addrs); err != nil { t.Errorf("PushAddrMsg: unexpected err %v\n", err) return } if err := p2.PushGetBlocksMsg(nil, &wire.ShaHash{}); err != nil { t.Errorf("PushGetBlocksMsg: unexpected err %v\n", err) return } if err := p2.PushGetHeadersMsg(nil, &wire.ShaHash{}); err != nil { t.Errorf("PushGetHeadersMsg: unexpected err %v\n", err) return } p2.PushRejectMsg("block", wire.RejectMalformed, "malformed", nil, true) p2.PushRejectMsg("block", wire.RejectInvalid, "invalid", nil, false) // Test Queue Messages p2.QueueMessage(wire.NewMsgGetAddr(), nil) p2.QueueMessage(wire.NewMsgPing(1), nil) p2.QueueMessage(wire.NewMsgMemPool(), nil) p2.QueueMessage(wire.NewMsgGetData(), nil) p2.QueueMessage(wire.NewMsgGetHeaders(), nil) p2.Shutdown() }
// TestVerAckWire tests the MsgVerAck wire encode and decode for various // protocol versions. func TestVerAckWire(t *testing.T) { msgVerAck := wire.NewMsgVerAck() msgVerAckEncoded := []byte{} tests := []struct { in *wire.MsgVerAck // Message to encode out *wire.MsgVerAck // Expected decoded message buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding }{ // Latest protocol version. { msgVerAck, msgVerAck, msgVerAckEncoded, wire.ProtocolVersion, }, // Protocol version BIP0035Version. { msgVerAck, msgVerAck, msgVerAckEncoded, wire.BIP0035Version, }, // Protocol version BIP0031Version. { msgVerAck, msgVerAck, msgVerAckEncoded, wire.BIP0031Version, }, // Protocol version NetAddressTimeVersion. { msgVerAck, msgVerAck, msgVerAckEncoded, wire.NetAddressTimeVersion, }, // Protocol version MultipleAddressVersion. { msgVerAck, msgVerAck, msgVerAckEncoded, wire.MultipleAddressVersion, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode the message to wire format. var buf bytes.Buffer err := test.in.BtcEncode(&buf, test.pver) if err != nil { t.Errorf("BtcEncode #%d error %v", i, err) continue } if !bytes.Equal(buf.Bytes(), test.buf) { t.Errorf("BtcEncode #%d\n got: %s want: %s", i, spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } // Decode the message from wire format. var msg wire.MsgVerAck rbuf := bytes.NewReader(test.buf) err = msg.BtcDecode(rbuf, test.pver) if err != nil { t.Errorf("BtcDecode #%d error %v", i, err) continue } if !reflect.DeepEqual(&msg, test.out) { t.Errorf("BtcDecode #%d\n got: %s want: %s", i, spew.Sdump(msg), spew.Sdump(test.out)) continue } } }
// TestMessage tests the Read/WriteMessage and Read/WriteMessageN API. func TestMessage(t *testing.T) { pver := wire.ProtocolVersion // Create the various types of messages to test. // MsgVersion. addrYou := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333} you, err := wire.NewNetAddress(addrYou, wire.SFNodeNetwork) if err != nil { t.Errorf("NewNetAddress: %v", err) } you.Timestamp = time.Time{} // Version message has zero value timestamp. addrMe := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333} me, err := wire.NewNetAddress(addrMe, wire.SFNodeNetwork) if err != nil { t.Errorf("NewNetAddress: %v", err) } me.Timestamp = time.Time{} // Version message has zero value timestamp. msgVersion := wire.NewMsgVersion(me, you, 123123, 0) msgVerack := wire.NewMsgVerAck() msgGetAddr := wire.NewMsgGetAddr() msgAddr := wire.NewMsgAddr() msgGetBlocks := wire.NewMsgGetBlocks(&wire.ShaHash{}) msgBlock := &blockOne msgInv := wire.NewMsgInv() msgGetData := wire.NewMsgGetData() msgNotFound := wire.NewMsgNotFound() msgTx := wire.NewMsgTx() msgPing := wire.NewMsgPing(123123) msgPong := wire.NewMsgPong(123123) msgGetHeaders := wire.NewMsgGetHeaders() msgHeaders := wire.NewMsgHeaders() msgAlert := wire.NewMsgAlert([]byte("payload"), []byte("signature")) msgMemPool := wire.NewMsgMemPool() msgFilterAdd := wire.NewMsgFilterAdd([]byte{0x01}) msgFilterClear := wire.NewMsgFilterClear() msgFilterLoad := wire.NewMsgFilterLoad([]byte{0x01}, 10, 0, wire.BloomUpdateNone) bh := wire.NewBlockHeader(&wire.ShaHash{}, &wire.ShaHash{}, 0, 0) msgMerkleBlock := wire.NewMsgMerkleBlock(bh) msgReject := wire.NewMsgReject("block", wire.RejectDuplicate, "duplicate block") tests := []struct { in wire.Message // Value to encode out wire.Message // Expected decoded value pver uint32 // Protocol version for wire encoding btcnet wire.StonecoinNet // Network to use for wire encoding bytes int // Expected num bytes read/written }{ {msgVersion, msgVersion, pver, wire.MainNet, 125}, {msgVerack, msgVerack, pver, wire.MainNet, 24}, {msgGetAddr, msgGetAddr, pver, wire.MainNet, 24}, {msgAddr, msgAddr, pver, wire.MainNet, 25}, {msgGetBlocks, msgGetBlocks, pver, wire.MainNet, 61}, {msgBlock, msgBlock, pver, wire.MainNet, 239}, {msgInv, msgInv, pver, wire.MainNet, 25}, {msgGetData, msgGetData, pver, wire.MainNet, 25}, {msgNotFound, msgNotFound, pver, wire.MainNet, 25}, {msgTx, msgTx, pver, wire.MainNet, 34}, {msgPing, msgPing, pver, wire.MainNet, 32}, {msgPong, msgPong, pver, wire.MainNet, 32}, {msgGetHeaders, msgGetHeaders, pver, wire.MainNet, 61}, {msgHeaders, msgHeaders, pver, wire.MainNet, 25}, {msgAlert, msgAlert, pver, wire.MainNet, 42}, {msgMemPool, msgMemPool, pver, wire.MainNet, 24}, {msgFilterAdd, msgFilterAdd, pver, wire.MainNet, 26}, {msgFilterClear, msgFilterClear, pver, wire.MainNet, 24}, {msgFilterLoad, msgFilterLoad, pver, wire.MainNet, 35}, {msgMerkleBlock, msgMerkleBlock, pver, wire.MainNet, 110}, {msgReject, msgReject, pver, wire.MainNet, 79}, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. var buf bytes.Buffer nw, err := wire.WriteMessageN(&buf, test.in, test.pver, test.btcnet) if err != nil { t.Errorf("WriteMessage #%d error %v", i, err) continue } // Ensure the number of bytes written match the expected value. if nw != test.bytes { t.Errorf("WriteMessage #%d unexpected num bytes "+ "written - got %d, want %d", i, nw, test.bytes) } // Decode from wire format. rbuf := bytes.NewReader(buf.Bytes()) nr, msg, _, err := wire.ReadMessageN(rbuf, test.pver, test.btcnet) if err != nil { t.Errorf("ReadMessage #%d error %v, msg %v", i, err, spew.Sdump(msg)) continue } if !reflect.DeepEqual(msg, test.out) { t.Errorf("ReadMessage #%d\n got: %v want: %v", i, spew.Sdump(msg), spew.Sdump(test.out)) continue } // Ensure the number of bytes read match the expected value. if nr != test.bytes { t.Errorf("ReadMessage #%d unexpected num bytes read - "+ "got %d, want %d", i, nr, test.bytes) } } // Do the same thing for Read/WriteMessage, but ignore the bytes since // they don't return them. t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. var buf bytes.Buffer err := wire.WriteMessage(&buf, test.in, test.pver, test.btcnet) if err != nil { t.Errorf("WriteMessage #%d error %v", i, err) continue } // Decode from wire format. rbuf := bytes.NewReader(buf.Bytes()) msg, _, err := wire.ReadMessage(rbuf, test.pver, test.btcnet) if err != nil { t.Errorf("ReadMessage #%d error %v, msg %v", i, err, spew.Sdump(msg)) continue } if !reflect.DeepEqual(msg, test.out) { t.Errorf("ReadMessage #%d\n got: %v want: %v", i, spew.Sdump(msg), spew.Sdump(test.out)) continue } } }