github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msggetaddr_test.go (about) 1 // Copyright (c) 2013-2015 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package wire_test 7 8 import ( 9 "bytes" 10 "reflect" 11 "testing" 12 13 "github.com/dashpay/godash/wire" 14 "github.com/davecgh/go-spew/spew" 15 ) 16 17 // TestGetAddr tests the MsgGetAddr API. 18 func TestGetAddr(t *testing.T) { 19 pver := wire.ProtocolVersion 20 21 // Ensure the command is expected value. 22 wantCmd := "getaddr" 23 msg := wire.NewMsgGetAddr() 24 if cmd := msg.Command(); cmd != wantCmd { 25 t.Errorf("NewMsgGetAddr: wrong command - got %v want %v", 26 cmd, wantCmd) 27 } 28 29 // Ensure max payload is expected value for latest protocol version. 30 // Num addresses (varInt) + max allowed addresses. 31 wantPayload := uint32(0) 32 maxPayload := msg.MaxPayloadLength(pver) 33 if maxPayload != wantPayload { 34 t.Errorf("MaxPayloadLength: wrong max payload length for "+ 35 "protocol version %d - got %v, want %v", pver, 36 maxPayload, wantPayload) 37 } 38 39 return 40 } 41 42 // TestGetAddrWire tests the MsgGetAddr wire encode and decode for various 43 // protocol versions. 44 func TestGetAddrWire(t *testing.T) { 45 msgGetAddr := wire.NewMsgGetAddr() 46 msgGetAddrEncoded := []byte{} 47 48 tests := []struct { 49 in *wire.MsgGetAddr // Message to encode 50 out *wire.MsgGetAddr // Expected decoded message 51 buf []byte // Wire encoding 52 pver uint32 // Protocol version for wire encoding 53 }{ 54 // Latest protocol version. 55 { 56 msgGetAddr, 57 msgGetAddr, 58 msgGetAddrEncoded, 59 wire.ProtocolVersion, 60 }, 61 62 // Protocol version BIP0035Version. 63 { 64 msgGetAddr, 65 msgGetAddr, 66 msgGetAddrEncoded, 67 wire.BIP0035Version, 68 }, 69 70 // Protocol version BIP0031Version. 71 { 72 msgGetAddr, 73 msgGetAddr, 74 msgGetAddrEncoded, 75 wire.BIP0031Version, 76 }, 77 78 // Protocol version NetAddressTimeVersion. 79 { 80 msgGetAddr, 81 msgGetAddr, 82 msgGetAddrEncoded, 83 wire.NetAddressTimeVersion, 84 }, 85 86 // Protocol version MultipleAddressVersion. 87 { 88 msgGetAddr, 89 msgGetAddr, 90 msgGetAddrEncoded, 91 wire.MultipleAddressVersion, 92 }, 93 } 94 95 t.Logf("Running %d tests", len(tests)) 96 for i, test := range tests { 97 // Encode the message to wire format. 98 var buf bytes.Buffer 99 err := test.in.BtcEncode(&buf, test.pver) 100 if err != nil { 101 t.Errorf("BtcEncode #%d error %v", i, err) 102 continue 103 } 104 if !bytes.Equal(buf.Bytes(), test.buf) { 105 t.Errorf("BtcEncode #%d\n got: %s want: %s", i, 106 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) 107 continue 108 } 109 110 // Decode the message from wire format. 111 var msg wire.MsgGetAddr 112 rbuf := bytes.NewReader(test.buf) 113 err = msg.BtcDecode(rbuf, test.pver) 114 if err != nil { 115 t.Errorf("BtcDecode #%d error %v", i, err) 116 continue 117 } 118 if !reflect.DeepEqual(&msg, test.out) { 119 t.Errorf("BtcDecode #%d\n got: %s want: %s", i, 120 spew.Sdump(msg), spew.Sdump(test.out)) 121 continue 122 } 123 } 124 }