github.com/palcoin-project/palcd@v1.0.0/wire/msgsendheaders_test.go (about)

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