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

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