github.com/decred/dcrlnd@v0.7.6/channeldb/addr_test.go (about)

     1  package channeldb
     2  
     3  import (
     4  	"bytes"
     5  	"net"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/decred/dcrlnd/tor"
    10  )
    11  
    12  type unknownAddrType struct{}
    13  
    14  func (t unknownAddrType) Network() string { return "unknown" }
    15  func (t unknownAddrType) String() string  { return "unknown" }
    16  
    17  var testIP4 = net.ParseIP("192.168.1.1")
    18  var testIP6 = net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329")
    19  
    20  var addrTests = []struct {
    21  	expAddr net.Addr
    22  	serErr  string
    23  }{
    24  	// Valid addresses.
    25  	{
    26  		expAddr: &net.TCPAddr{
    27  			IP:   testIP4,
    28  			Port: 12345,
    29  		},
    30  	},
    31  	{
    32  		expAddr: &net.TCPAddr{
    33  			IP:   testIP6,
    34  			Port: 65535,
    35  		},
    36  	},
    37  	{
    38  		expAddr: &tor.OnionAddr{
    39  			OnionService: "3g2upl4pq6kufc4m.onion",
    40  			Port:         9735,
    41  		},
    42  	},
    43  	{
    44  		expAddr: &tor.OnionAddr{
    45  			OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion",
    46  			Port:         80,
    47  		},
    48  	},
    49  
    50  	// Invalid addresses.
    51  	{
    52  		expAddr: unknownAddrType{},
    53  		serErr:  ErrUnknownAddressType.Error(),
    54  	},
    55  	{
    56  		expAddr: &net.TCPAddr{
    57  			// Remove last byte of IPv4 address.
    58  			IP:   testIP4[:len(testIP4)-1],
    59  			Port: 12345,
    60  		},
    61  		serErr: "unable to encode",
    62  	},
    63  	{
    64  		expAddr: &net.TCPAddr{
    65  			// Add an extra byte of IPv4 address.
    66  			IP:   append(testIP4, 0xff),
    67  			Port: 12345,
    68  		},
    69  		serErr: "unable to encode",
    70  	},
    71  	{
    72  		expAddr: &net.TCPAddr{
    73  			// Remove last byte of IPv6 address.
    74  			IP:   testIP6[:len(testIP6)-1],
    75  			Port: 65535,
    76  		},
    77  		serErr: "unable to encode",
    78  	},
    79  	{
    80  		expAddr: &net.TCPAddr{
    81  			// Add an extra byte to the IPv6 address.
    82  			IP:   append(testIP6, 0xff),
    83  			Port: 65535,
    84  		},
    85  		serErr: "unable to encode",
    86  	},
    87  	{
    88  		expAddr: &tor.OnionAddr{
    89  			// Invalid suffix.
    90  			OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.inion",
    91  			Port:         80,
    92  		},
    93  		serErr: "invalid suffix",
    94  	},
    95  	{
    96  		expAddr: &tor.OnionAddr{
    97  			// Invalid length.
    98  			OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy.onion",
    99  			Port:         80,
   100  		},
   101  		serErr: "unknown onion service length",
   102  	},
   103  	{
   104  		expAddr: &tor.OnionAddr{
   105  			// Invalid encoding.
   106  			OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyA.onion",
   107  			Port:         80,
   108  		},
   109  		serErr: "illegal base32",
   110  	},
   111  }
   112  
   113  // TestAddrSerialization tests that the serialization method used by channeldb
   114  // for net.Addr's works as intended.
   115  func TestAddrSerialization(t *testing.T) {
   116  	t.Parallel()
   117  
   118  	var b bytes.Buffer
   119  	for _, test := range addrTests {
   120  		err := serializeAddr(&b, test.expAddr)
   121  		switch {
   122  		case err == nil && test.serErr != "":
   123  			t.Fatalf("expected serialization err for addr %v",
   124  				test.expAddr)
   125  
   126  		case err != nil && test.serErr == "":
   127  			t.Fatalf("unexpected serialization err for addr %v: %v",
   128  				test.expAddr, err)
   129  
   130  		case err != nil && !strings.Contains(err.Error(), test.serErr):
   131  			t.Fatalf("unexpected serialization err for addr %v, "+
   132  				"want: %v, got %v", test.expAddr, test.serErr,
   133  				err)
   134  
   135  		case err != nil:
   136  			continue
   137  		}
   138  
   139  		addr, err := deserializeAddr(&b)
   140  		if err != nil {
   141  			t.Fatalf("unable to deserialize address: %v", err)
   142  		}
   143  
   144  		if addr.String() != test.expAddr.String() {
   145  			t.Fatalf("expected address %v after serialization, "+
   146  				"got %v", addr, test.expAddr)
   147  		}
   148  	}
   149  }