invvect_test.go raw

   1  package wire
   2  
   3  import (
   4  	"bytes"
   5  	"reflect"
   6  	"testing"
   7  	
   8  	"github.com/davecgh/go-spew/spew"
   9  	
  10  	"github.com/p9c/p9/pkg/chainhash"
  11  )
  12  
  13  // TestInvVectStringer tests the stringized output for inventory vector types.
  14  func TestInvTypeStringer(t *testing.T) {
  15  	tests := []struct {
  16  		in   InvType
  17  		want string
  18  	}{
  19  		{InvTypeError, "ERROR"},
  20  		{InvTypeTx, "MSG_TX"},
  21  		{InvTypeBlock, "MSG_BLOCK"},
  22  		{0xffffffff, "Unknown InvType (4294967295)"},
  23  	}
  24  	t.Logf("Running %d tests", len(tests))
  25  	for i, test := range tests {
  26  		result := test.in.String()
  27  		if result != test.want {
  28  			t.Errorf("String #%d\n got: %s want: %s", i, result,
  29  				test.want,
  30  			)
  31  			continue
  32  		}
  33  	}
  34  }
  35  
  36  // TestInvVect tests the InvVect API.
  37  func TestInvVect(t *testing.T) {
  38  	ivType := InvTypeBlock
  39  	hash := chainhash.Hash{}
  40  	// Ensure we get the same payload and signature back out.
  41  	iv := NewInvVect(ivType, &hash)
  42  	if iv.Type != ivType {
  43  		t.Errorf("NewInvVect: wrong type - got %v, want %v",
  44  			iv.Type, ivType,
  45  		)
  46  	}
  47  	if !iv.Hash.IsEqual(&hash) {
  48  		t.Errorf("NewInvVect: wrong hash - got %v, want %v",
  49  			spew.Sdump(iv.Hash), spew.Sdump(hash),
  50  		)
  51  	}
  52  }
  53  
  54  // TestInvVectWire tests the InvVect wire encode and decode for various protocol versions and supported inventory vector types.
  55  func TestInvVectWire(t *testing.T) {
  56  	// Block 203707 hash.
  57  	hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
  58  	baseHash, e := chainhash.NewHashFromStr(hashStr)
  59  	if e != nil {
  60  		t.Errorf("NewHashFromStr: %v", e)
  61  	}
  62  	// errInvVect is an inventory vector with an error.
  63  	errInvVect := InvVect{
  64  		Type: InvTypeError,
  65  		Hash: chainhash.Hash{},
  66  	}
  67  	// errInvVectEncoded is the wire encoded bytes of errInvVect.
  68  	errInvVectEncoded := []byte{
  69  		0x00, 0x00, 0x00, 0x00, // InvTypeError
  70  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // No hash
  74  	}
  75  	// txInvVect is an inventory vector representing a transaction.
  76  	txInvVect := InvVect{
  77  		Type: InvTypeTx,
  78  		Hash: *baseHash,
  79  	}
  80  	// txInvVectEncoded is the wire encoded bytes of txInvVect.
  81  	txInvVectEncoded := []byte{
  82  		0x01, 0x00, 0x00, 0x00, // InvTypeTx
  83  		0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
  84  		0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
  85  		0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
  86  		0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash
  87  	}
  88  	// blockInvVect is an inventory vector representing a block.
  89  	blockInvVect := InvVect{
  90  		Type: InvTypeBlock,
  91  		Hash: *baseHash,
  92  	}
  93  	// blockInvVectEncoded is the wire encoded bytes of blockInvVect.
  94  	blockInvVectEncoded := []byte{
  95  		0x02, 0x00, 0x00, 0x00, // InvTypeBlock
  96  		0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
  97  		0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
  98  		0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
  99  		0x26, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Block 203707 hash
 100  	}
 101  	tests := []struct {
 102  		in   InvVect // NetAddress to encode
 103  		out  InvVect // Expected decoded NetAddress
 104  		buf  []byte  // Wire encoding
 105  		pver uint32  // Protocol version for wire encoding
 106  	}{
 107  		// Latest protocol version error inventory vector.
 108  		{
 109  			errInvVect,
 110  			errInvVect,
 111  			errInvVectEncoded,
 112  			ProtocolVersion,
 113  		},
 114  		// Latest protocol version tx inventory vector.
 115  		{
 116  			txInvVect,
 117  			txInvVect,
 118  			txInvVectEncoded,
 119  			ProtocolVersion,
 120  		},
 121  		// Latest protocol version block inventory vector.
 122  		{
 123  			blockInvVect,
 124  			blockInvVect,
 125  			blockInvVectEncoded,
 126  			ProtocolVersion,
 127  		},
 128  		// Protocol version BIP0035Version error inventory vector.
 129  		{
 130  			errInvVect,
 131  			errInvVect,
 132  			errInvVectEncoded,
 133  			BIP0035Version,
 134  		},
 135  		// Protocol version BIP0035Version tx inventory vector.
 136  		{
 137  			txInvVect,
 138  			txInvVect,
 139  			txInvVectEncoded,
 140  			BIP0035Version,
 141  		},
 142  		// Protocol version BIP0035Version block inventory vector.
 143  		{
 144  			blockInvVect,
 145  			blockInvVect,
 146  			blockInvVectEncoded,
 147  			BIP0035Version,
 148  		},
 149  		// Protocol version BIP0031Version error inventory vector.
 150  		{
 151  			errInvVect,
 152  			errInvVect,
 153  			errInvVectEncoded,
 154  			BIP0031Version,
 155  		},
 156  		// Protocol version BIP0031Version tx inventory vector.
 157  		{
 158  			txInvVect,
 159  			txInvVect,
 160  			txInvVectEncoded,
 161  			BIP0031Version,
 162  		},
 163  		// Protocol version BIP0031Version block inventory vector.
 164  		{
 165  			blockInvVect,
 166  			blockInvVect,
 167  			blockInvVectEncoded,
 168  			BIP0031Version,
 169  		},
 170  		// Protocol version NetAddressTimeVersion error inventory vector.
 171  		{
 172  			errInvVect,
 173  			errInvVect,
 174  			errInvVectEncoded,
 175  			NetAddressTimeVersion,
 176  		},
 177  		// Protocol version NetAddressTimeVersion tx inventory vector.
 178  		{
 179  			txInvVect,
 180  			txInvVect,
 181  			txInvVectEncoded,
 182  			NetAddressTimeVersion,
 183  		},
 184  		// Protocol version NetAddressTimeVersion block inventory vector.
 185  		{
 186  			blockInvVect,
 187  			blockInvVect,
 188  			blockInvVectEncoded,
 189  			NetAddressTimeVersion,
 190  		},
 191  		// Protocol version MultipleAddressVersion error inventory vector.
 192  		{
 193  			errInvVect,
 194  			errInvVect,
 195  			errInvVectEncoded,
 196  			MultipleAddressVersion,
 197  		},
 198  		// Protocol version MultipleAddressVersion tx inventory vector.
 199  		{
 200  			txInvVect,
 201  			txInvVect,
 202  			txInvVectEncoded,
 203  			MultipleAddressVersion,
 204  		},
 205  		// Protocol version MultipleAddressVersion block inventory vector.
 206  		{
 207  			blockInvVect,
 208  			blockInvVect,
 209  			blockInvVectEncoded,
 210  			MultipleAddressVersion,
 211  		},
 212  	}
 213  	t.Logf("Running %d tests", len(tests))
 214  	for i, test := range tests {
 215  		// Encode to wire format.
 216  		var buf bytes.Buffer
 217  		e := writeInvVect(&buf, test.pver, &test.in)
 218  		if e != nil {
 219  			t.Errorf("writeInvVect #%d error %v", i, e)
 220  			continue
 221  		}
 222  		if !bytes.Equal(buf.Bytes(), test.buf) {
 223  			t.Errorf("writeInvVect #%d\n got: %s want: %s", i,
 224  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf),
 225  			)
 226  			continue
 227  		}
 228  		// Decode the message from wire format.
 229  		var iv InvVect
 230  		rbuf := bytes.NewReader(test.buf)
 231  		e = readInvVect(rbuf, test.pver, &iv)
 232  		if e != nil {
 233  			t.Errorf("readInvVect #%d error %v", i, e)
 234  			continue
 235  		}
 236  		if !reflect.DeepEqual(iv, test.out) {
 237  			t.Errorf("readInvVect #%d\n got: %s want: %s", i,
 238  				spew.Sdump(iv), spew.Sdump(test.out),
 239  			)
 240  			continue
 241  		}
 242  	}
 243  }
 244