protocol_test.go raw

   1  // Copyright (c) 2013-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  	"testing"
   9  
  10  	"github.com/stretchr/testify/require"
  11  )
  12  
  13  // TestServiceFlagStringer tests the stringized output for service flag types.
  14  func TestServiceFlagStringer(t *testing.T) {
  15  	tests := []struct {
  16  		in   ServiceFlag
  17  		want string
  18  	}{
  19  		{0, "0x0"},
  20  		{SFNodeNetwork, "SFNodeNetwork"},
  21  		{SFNodeGetUTXO, "SFNodeGetUTXO"},
  22  		{SFNodeBloom, "SFNodeBloom"},
  23  		{SFNodeWitness, "SFNodeWitness"},
  24  		{SFNodeXthin, "SFNodeXthin"},
  25  		{SFNodeBit5, "SFNodeBit5"},
  26  		{SFNodeCF, "SFNodeCF"},
  27  		{SFNode2X, "SFNode2X"},
  28  		{SFNodeNetworkLimited, "SFNodeNetworkLimited"},
  29  		{0xffffffff, "SFNodeNetwork|SFNodeGetUTXO|SFNodeBloom|SFNodeWitness|SFNodeXthin|SFNodeBit5|SFNodeCF|SFNode2X|SFNodeNetworkLimited|SFNodeP2PV2|0xfffff300"},
  30  	}
  31  
  32  	t.Logf("Running %d tests", len(tests))
  33  	for i, test := range tests {
  34  		result := test.in.String()
  35  		if result != test.want {
  36  			t.Errorf("String #%d\n got: %s want: %s", i, result,
  37  				test.want)
  38  			continue
  39  		}
  40  	}
  41  }
  42  
  43  // TestBitcoinNetStringer tests the stringized output for bitcoin net types.
  44  func TestBitcoinNetStringer(t *testing.T) {
  45  	tests := []struct {
  46  		in   BitcoinNet
  47  		want string
  48  	}{
  49  		{MainNet, "MainNet"},
  50  		{TestNet, "TestNet"},
  51  		{TestNet3, "TestNet3"},
  52  		{TestNet4, "TestNet4"},
  53  		{SigNet, "SigNet"},
  54  		{SimNet, "SimNet"},
  55  		{0xffffffff, "Unknown BitcoinNet (4294967295)"},
  56  	}
  57  
  58  	t.Logf("Running %d tests", len(tests))
  59  	for i, test := range tests {
  60  		result := test.in.String()
  61  		if result != test.want {
  62  			t.Errorf("String #%d\n got: %s want: %s", i, result,
  63  				test.want)
  64  			continue
  65  		}
  66  	}
  67  }
  68  
  69  func TestHasFlag(t *testing.T) {
  70  	tests := []struct {
  71  		in    ServiceFlag
  72  		check ServiceFlag
  73  		want  bool
  74  	}{
  75  		{0, SFNodeNetwork, false},
  76  		{SFNodeNetwork | SFNodeNetworkLimited | SFNodeWitness, SFNodeBloom, false},
  77  		{SFNodeNetwork | SFNodeNetworkLimited | SFNodeWitness, SFNodeNetworkLimited, true},
  78  	}
  79  
  80  	for _, test := range tests {
  81  		require.Equal(t, test.want, test.in.HasFlag(test.check))
  82  	}
  83  }
  84