msgfilterload_test.go raw

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