msgfilterload_test.go raw

   1  // Copyright (c) 2014-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  	"io"
  10  	"reflect"
  11  	"testing"
  12  )
  13  
  14  // TestFilterCLearLatest tests the MsgFilterLoad API against the latest protocol
  15  // version.
  16  func TestFilterLoadLatest(t *testing.T) {
  17  	pver := ProtocolVersion
  18  	enc := BaseEncoding
  19  
  20  	data := []byte{0x01, 0x02}
  21  	msg := NewMsgFilterLoad(data, 10, 0, 0)
  22  
  23  	// Ensure the command is expected value.
  24  	wantCmd := "filterload"
  25  	if cmd := msg.Command(); cmd != wantCmd {
  26  		t.Errorf("NewMsgFilterLoad: wrong command - got %v want %v",
  27  			cmd, wantCmd)
  28  	}
  29  
  30  	// Ensure max payload is expected value for latest protocol version.
  31  	wantPayload := uint32(36012)
  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, enc)
  42  	if err != nil {
  43  		t.Errorf("encode of MsgFilterLoad failed %v err <%v>", msg, err)
  44  	}
  45  
  46  	// Test decode with latest protocol version.
  47  	readmsg := MsgFilterLoad{}
  48  	err = readmsg.BtcDecode(&buf, pver, enc)
  49  	if err != nil {
  50  		t.Errorf("decode of MsgFilterLoad failed [%v] err <%v>", buf, err)
  51  	}
  52  }
  53  
  54  // TestFilterLoadCrossProtocol tests the MsgFilterLoad API when encoding with
  55  // the latest protocol version and decoding with BIP0031Version.
  56  func TestFilterLoadCrossProtocol(t *testing.T) {
  57  	data := []byte{0x01, 0x02}
  58  	msg := NewMsgFilterLoad(data, 10, 0, 0)
  59  
  60  	// Encode with latest protocol version.
  61  	var buf bytes.Buffer
  62  	err := msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
  63  	if err != nil {
  64  		t.Errorf("encode of NewMsgFilterLoad failed %v err <%v>", msg,
  65  			err)
  66  	}
  67  
  68  	// Decode with old protocol version.
  69  	var readmsg MsgFilterLoad
  70  	err = readmsg.BtcDecode(&buf, BIP0031Version, BaseEncoding)
  71  	if err == nil {
  72  		t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
  73  			msg)
  74  	}
  75  }
  76  
  77  // TestFilterLoadMaxFilterSize tests the MsgFilterLoad API maximum filter size.
  78  func TestFilterLoadMaxFilterSize(t *testing.T) {
  79  	data := bytes.Repeat([]byte{0xff}, 36001)
  80  	msg := NewMsgFilterLoad(data, 10, 0, 0)
  81  
  82  	// Encode with latest protocol version.
  83  	var buf bytes.Buffer
  84  	err := msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
  85  	if err == nil {
  86  		t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't "+
  87  			"have %v", msg)
  88  	}
  89  
  90  	// Decode with latest protocol version.
  91  	readbuf := bytes.NewReader(data)
  92  	err = msg.BtcDecode(readbuf, ProtocolVersion, BaseEncoding)
  93  	if err == nil {
  94  		t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't "+
  95  			"have %v", msg)
  96  	}
  97  }
  98  
  99  // TestFilterLoadMaxHashFuncsSize tests the MsgFilterLoad API maximum hash functions.
 100  func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
 101  	data := bytes.Repeat([]byte{0xff}, 10)
 102  	msg := NewMsgFilterLoad(data, 61, 0, 0)
 103  
 104  	// Encode with latest protocol version.
 105  	var buf bytes.Buffer
 106  	err := msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
 107  	if err == nil {
 108  		t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't have %v",
 109  			msg)
 110  	}
 111  
 112  	newBuf := []byte{
 113  		0x0a,                                                       // filter size
 114  		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // filter
 115  		0x3d, 0x00, 0x00, 0x00, // max hash funcs
 116  		0x00, 0x00, 0x00, 0x00, // tweak
 117  		0x00, // update Type
 118  	}
 119  	// Decode with latest protocol version.
 120  	readbuf := bytes.NewReader(newBuf)
 121  	err = msg.BtcDecode(readbuf, ProtocolVersion, BaseEncoding)
 122  	if err == nil {
 123  		t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
 124  			msg)
 125  	}
 126  }
 127  
 128  // TestFilterLoadWireErrors performs negative tests against wire encode and decode
 129  // of MsgFilterLoad to confirm error paths work correctly.
 130  func TestFilterLoadWireErrors(t *testing.T) {
 131  	pver := ProtocolVersion
 132  	pverNoFilterLoad := BIP0037Version - 1
 133  	wireErr := &MessageError{}
 134  
 135  	baseFilter := []byte{0x01, 0x02, 0x03, 0x04}
 136  	baseFilterLoad := NewMsgFilterLoad(baseFilter, 10, 0, BloomUpdateNone)
 137  	baseFilterLoadEncoded := append([]byte{0x04}, baseFilter...)
 138  	baseFilterLoadEncoded = append(baseFilterLoadEncoded,
 139  		0x00, 0x00, 0x00, 0x0a, // HashFuncs
 140  		0x00, 0x00, 0x00, 0x00, // Tweak
 141  		0x00) // Flags
 142  
 143  	tests := []struct {
 144  		in       *MsgFilterLoad  // Value to encode
 145  		buf      []byte          // Wire encoding
 146  		pver     uint32          // Protocol version for wire encoding
 147  		enc      MessageEncoding // Message encoding format
 148  		max      int             // Max size of fixed buffer to induce errors
 149  		writeErr error           // Expected write error
 150  		readErr  error           // Expected read error
 151  	}{
 152  		// Latest protocol version with intentional read/write errors.
 153  		// Force error in filter size.
 154  		{
 155  			baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 0,
 156  			io.ErrShortWrite, io.EOF,
 157  		},
 158  		// Force error in filter.
 159  		{
 160  			baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 1,
 161  			io.ErrShortWrite, io.EOF,
 162  		},
 163  		// Force error in hash funcs.
 164  		{
 165  			baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 5,
 166  			io.ErrShortWrite, io.EOF,
 167  		},
 168  		// Force error in tweak.
 169  		{
 170  			baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 9,
 171  			io.ErrShortWrite, io.EOF,
 172  		},
 173  		// Force error in flags.
 174  		{
 175  			baseFilterLoad, baseFilterLoadEncoded, pver, BaseEncoding, 13,
 176  			io.ErrShortWrite, io.EOF,
 177  		},
 178  		// Force error due to unsupported protocol version.
 179  		{
 180  			baseFilterLoad, baseFilterLoadEncoded, pverNoFilterLoad, BaseEncoding,
 181  			10, wireErr, wireErr,
 182  		},
 183  	}
 184  
 185  	t.Logf("Running %d tests", len(tests))
 186  	for i, test := range tests {
 187  		// Encode to wire format.
 188  		w := newFixedWriter(test.max)
 189  		err := test.in.BtcEncode(w, test.pver, test.enc)
 190  		if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
 191  			t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
 192  				i, err, test.writeErr)
 193  			continue
 194  		}
 195  
 196  		// For errors which are not of type MessageError, check them for
 197  		// equality.
 198  		if _, ok := err.(*MessageError); !ok {
 199  			if err != test.writeErr {
 200  				t.Errorf("BtcEncode #%d wrong error got: %v, "+
 201  					"want: %v", i, err, test.writeErr)
 202  				continue
 203  			}
 204  		}
 205  
 206  		// Decode from wire format.
 207  		var msg MsgFilterLoad
 208  		r := newFixedReader(test.max, test.buf)
 209  		err = msg.BtcDecode(r, test.pver, test.enc)
 210  		if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
 211  			t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
 212  				i, err, test.readErr)
 213  			continue
 214  		}
 215  
 216  		// For errors which are not of type MessageError, check them for
 217  		// equality.
 218  		if _, ok := err.(*MessageError); !ok {
 219  			if err != test.readErr {
 220  				t.Errorf("BtcDecode #%d wrong error got: %v, "+
 221  					"want: %v", i, err, test.readErr)
 222  				continue
 223  			}
 224  		}
 225  
 226  	}
 227  }
 228