github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msgsendheaders_test.go (about) 1 // Copyright (c) 2016 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 // TestSendHeaders tests the MsgSendHeaders API against the latest protocol 18 // version. 19 func TestSendHeaders(t *testing.T) { 20 pver := wire.ProtocolVersion 21 22 // Ensure the command is expected value. 23 wantCmd := "sendheaders" 24 msg := wire.NewMsgSendHeaders() 25 if cmd := msg.Command(); cmd != wantCmd { 26 t.Errorf("NewMsgSendHeaders: wrong command - got %v want %v", 27 cmd, wantCmd) 28 } 29 30 // Ensure max payload is expected value. 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 // Test encode with latest protocol version. 40 var buf bytes.Buffer 41 err := msg.BtcEncode(&buf, pver) 42 if err != nil { 43 t.Errorf("encode of MsgSendHeaders failed %v err <%v>", msg, 44 err) 45 } 46 47 // Older protocol versions should fail encode since message didn't 48 // exist yet. 49 oldPver := wire.SendHeadersVersion - 1 50 err = msg.BtcEncode(&buf, oldPver) 51 if err == nil { 52 s := "encode of MsgSendHeaders passed for old protocol " + 53 "version %v err <%v>" 54 t.Errorf(s, msg, err) 55 } 56 57 // Test decode with latest protocol version. 58 readmsg := wire.NewMsgSendHeaders() 59 err = readmsg.BtcDecode(&buf, pver) 60 if err != nil { 61 t.Errorf("decode of MsgSendHeaders failed [%v] err <%v>", buf, 62 err) 63 } 64 65 // Older protocol versions should fail decode since message didn't 66 // exist yet. 67 err = readmsg.BtcDecode(&buf, oldPver) 68 if err == nil { 69 s := "decode of MsgSendHeaders passed for old protocol " + 70 "version %v err <%v>" 71 t.Errorf(s, msg, err) 72 } 73 74 return 75 } 76 77 // TestSendHeadersBIP0130 tests the MsgSendHeaders API against the protocol 78 // prior to version SendHeadersVersion. 79 func TestSendHeadersBIP0130(t *testing.T) { 80 // Use the protocol version just prior to SendHeadersVersion changes. 81 pver := wire.SendHeadersVersion - 1 82 83 msg := wire.NewMsgSendHeaders() 84 85 // Test encode with old protocol version. 86 var buf bytes.Buffer 87 err := msg.BtcEncode(&buf, pver) 88 if err == nil { 89 t.Errorf("encode of MsgSendHeaders succeeded when it should " + 90 "have failed") 91 } 92 93 // Test decode with old protocol version. 94 readmsg := wire.NewMsgSendHeaders() 95 err = readmsg.BtcDecode(&buf, pver) 96 if err == nil { 97 t.Errorf("decode of MsgSendHeaders succeeded when it should " + 98 "have failed") 99 } 100 101 return 102 } 103 104 // TestSendHeadersCrossProtocol tests the MsgSendHeaders API when encoding with 105 // the latest protocol version and decoding with SendHeadersVersion. 106 func TestSendHeadersCrossProtocol(t *testing.T) { 107 msg := wire.NewMsgSendHeaders() 108 109 // Encode with latest protocol version. 110 var buf bytes.Buffer 111 err := msg.BtcEncode(&buf, wire.ProtocolVersion) 112 if err != nil { 113 t.Errorf("encode of MsgSendHeaders failed %v err <%v>", msg, 114 err) 115 } 116 117 // Decode with old protocol version. 118 readmsg := wire.NewMsgSendHeaders() 119 err = readmsg.BtcDecode(&buf, wire.SendHeadersVersion) 120 if err != nil { 121 t.Errorf("decode of MsgSendHeaders failed [%v] err <%v>", buf, 122 err) 123 } 124 } 125 126 // TestSendHeadersWire tests the MsgSendHeaders wire encode and decode for 127 // various protocol versions. 128 func TestSendHeadersWire(t *testing.T) { 129 msgSendHeaders := wire.NewMsgSendHeaders() 130 msgSendHeadersEncoded := []byte{} 131 132 tests := []struct { 133 in *wire.MsgSendHeaders // Message to encode 134 out *wire.MsgSendHeaders // Expected decoded message 135 buf []byte // Wire encoding 136 pver uint32 // Protocol version for wire encoding 137 }{ 138 // Latest protocol version. 139 { 140 msgSendHeaders, 141 msgSendHeaders, 142 msgSendHeadersEncoded, 143 wire.ProtocolVersion, 144 }, 145 146 // Protocol version SendHeadersVersion+1 147 { 148 msgSendHeaders, 149 msgSendHeaders, 150 msgSendHeadersEncoded, 151 wire.SendHeadersVersion + 1, 152 }, 153 154 // Protocol version SendHeadersVersion 155 { 156 msgSendHeaders, 157 msgSendHeaders, 158 msgSendHeadersEncoded, 159 wire.SendHeadersVersion, 160 }, 161 } 162 163 t.Logf("Running %d tests", len(tests)) 164 for i, test := range tests { 165 // Encode the message to wire format. 166 var buf bytes.Buffer 167 err := test.in.BtcEncode(&buf, test.pver) 168 if err != nil { 169 t.Errorf("BtcEncode #%d error %v", i, err) 170 continue 171 } 172 if !bytes.Equal(buf.Bytes(), test.buf) { 173 t.Errorf("BtcEncode #%d\n got: %s want: %s", i, 174 spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) 175 continue 176 } 177 178 // Decode the message from wire format. 179 var msg wire.MsgSendHeaders 180 rbuf := bytes.NewReader(test.buf) 181 err = msg.BtcDecode(rbuf, test.pver) 182 if err != nil { 183 t.Errorf("BtcDecode #%d error %v", i, err) 184 continue 185 } 186 if !reflect.DeepEqual(&msg, test.out) { 187 t.Errorf("BtcDecode #%d\n got: %s want: %s", i, 188 spew.Sdump(msg), spew.Sdump(test.out)) 189 continue 190 } 191 } 192 }