github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/wire/msgfilterclear_test.go (about)

     1  // Copyright (c) 2014-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/BlockABC/godash/wire"
    14  	"github.com/davecgh/go-spew/spew"
    15  )
    16  
    17  // TestFilterCLearLatest tests the MsgFilterClear API against the latest
    18  // protocol version.
    19  func TestFilterClearLatest(t *testing.T) {
    20  	pver := wire.ProtocolVersion
    21  
    22  	msg := wire.NewMsgFilterClear()
    23  
    24  	// Ensure the command is expected value.
    25  	wantCmd := "filterclear"
    26  	if cmd := msg.Command(); cmd != wantCmd {
    27  		t.Errorf("NewMsgFilterClear: wrong command - got %v want %v",
    28  			cmd, wantCmd)
    29  	}
    30  
    31  	// Ensure max payload is expected value for latest protocol version.
    32  	wantPayload := uint32(0)
    33  	maxPayload := msg.MaxPayloadLength(pver)
    34  	if maxPayload != wantPayload {
    35  		t.Errorf("MaxPayloadLength: wrong max payload length for "+
    36  			"protocol version %d - got %v, want %v", pver,
    37  			maxPayload, wantPayload)
    38  	}
    39  
    40  	return
    41  }
    42  
    43  // TestFilterClearCrossProtocol tests the MsgFilterClear API when encoding with
    44  // the latest protocol version and decoding with BIP0031Version.
    45  func TestFilterClearCrossProtocol(t *testing.T) {
    46  	msg := wire.NewMsgFilterClear()
    47  
    48  	// Encode with latest protocol version.
    49  	var buf bytes.Buffer
    50  	err := msg.BtcEncode(&buf, wire.ProtocolVersion)
    51  	if err != nil {
    52  		t.Errorf("encode of MsgFilterClear failed %v err <%v>", msg, err)
    53  	}
    54  
    55  	// Decode with old protocol version.
    56  	var readmsg wire.MsgFilterClear
    57  	err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
    58  	if err == nil {
    59  		t.Errorf("decode of MsgFilterClear succeeded when it "+
    60  			"shouldn't have %v", msg)
    61  	}
    62  }
    63  
    64  // TestFilterClearWire tests the MsgFilterClear wire encode and decode for
    65  // various protocol versions.
    66  func TestFilterClearWire(t *testing.T) {
    67  	msgFilterClear := wire.NewMsgFilterClear()
    68  	msgFilterClearEncoded := []byte{}
    69  
    70  	tests := []struct {
    71  		in   *wire.MsgFilterClear // Message to encode
    72  		out  *wire.MsgFilterClear // Expected decoded message
    73  		buf  []byte               // Wire encoding
    74  		pver uint32               // Protocol version for wire encoding
    75  	}{
    76  		// Latest protocol version.
    77  		{
    78  			msgFilterClear,
    79  			msgFilterClear,
    80  			msgFilterClearEncoded,
    81  			wire.ProtocolVersion,
    82  		},
    83  
    84  		// Protocol version BIP0037Version + 1.
    85  		{
    86  			msgFilterClear,
    87  			msgFilterClear,
    88  			msgFilterClearEncoded,
    89  			wire.BIP0037Version + 1,
    90  		},
    91  
    92  		// Protocol version BIP0037Version.
    93  		{
    94  			msgFilterClear,
    95  			msgFilterClear,
    96  			msgFilterClearEncoded,
    97  			wire.BIP0037Version,
    98  		},
    99  	}
   100  
   101  	t.Logf("Running %d tests", len(tests))
   102  	for i, test := range tests {
   103  		// Encode the message to wire format.
   104  		var buf bytes.Buffer
   105  		err := test.in.BtcEncode(&buf, test.pver)
   106  		if err != nil {
   107  			t.Errorf("BtcEncode #%d error %v", i, err)
   108  			continue
   109  		}
   110  		if !bytes.Equal(buf.Bytes(), test.buf) {
   111  			t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
   112  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
   113  			continue
   114  		}
   115  
   116  		// Decode the message from wire format.
   117  		var msg wire.MsgFilterClear
   118  		rbuf := bytes.NewReader(test.buf)
   119  		err = msg.BtcDecode(rbuf, test.pver)
   120  		if err != nil {
   121  			t.Errorf("BtcDecode #%d error %v", i, err)
   122  			continue
   123  		}
   124  		if !reflect.DeepEqual(&msg, test.out) {
   125  			t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
   126  				spew.Sdump(msg), spew.Sdump(test.out))
   127  			continue
   128  		}
   129  	}
   130  }
   131  
   132  // TestFilterClearWireErrors performs negative tests against wire encode and
   133  // decode of MsgFilterClear to confirm error paths work correctly.
   134  func TestFilterClearWireErrors(t *testing.T) {
   135  	pverNoFilterClear := wire.BIP0037Version - 1
   136  	wireErr := &wire.MessageError{}
   137  
   138  	baseFilterClear := wire.NewMsgFilterClear()
   139  	baseFilterClearEncoded := []byte{}
   140  
   141  	tests := []struct {
   142  		in       *wire.MsgFilterClear // Value to encode
   143  		buf      []byte               // Wire encoding
   144  		pver     uint32               // Protocol version for wire encoding
   145  		max      int                  // Max size of fixed buffer to induce errors
   146  		writeErr error                // Expected write error
   147  		readErr  error                // Expected read error
   148  	}{
   149  		// Force error due to unsupported protocol version.
   150  		{
   151  			baseFilterClear, baseFilterClearEncoded,
   152  			pverNoFilterClear, 4, wireErr, wireErr,
   153  		},
   154  	}
   155  
   156  	t.Logf("Running %d tests", len(tests))
   157  	for i, test := range tests {
   158  		// Encode to wire format.
   159  		w := newFixedWriter(test.max)
   160  		err := test.in.BtcEncode(w, test.pver)
   161  		if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
   162  			t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
   163  				i, err, test.writeErr)
   164  			continue
   165  		}
   166  
   167  		// For errors which are not of type wire.MessageError, check
   168  		// them for equality.
   169  		if _, ok := err.(*wire.MessageError); !ok {
   170  			if err != test.writeErr {
   171  				t.Errorf("BtcEncode #%d wrong error got: %v, "+
   172  					"want: %v", i, err, test.writeErr)
   173  				continue
   174  			}
   175  		}
   176  
   177  		// Decode from wire format.
   178  		var msg wire.MsgFilterClear
   179  		r := newFixedReader(test.max, test.buf)
   180  		err = msg.BtcDecode(r, test.pver)
   181  		if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
   182  			t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
   183  				i, err, test.readErr)
   184  			continue
   185  		}
   186  
   187  		// For errors which are not of type wire.MessageError, check
   188  		// them for equality.
   189  		if _, ok := err.(*wire.MessageError); !ok {
   190  			if err != test.readErr {
   191  				t.Errorf("BtcDecode #%d wrong error got: %v, "+
   192  					"want: %v", i, err, test.readErr)
   193  				continue
   194  			}
   195  		}
   196  
   197  	}
   198  }