github.com/gopacket/gopacket@v1.1.0/layers/endpoints_test.go (about)

     1  // Copyright 2017, Google, Inc. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the LICENSE file in the root of the source
     5  // tree.
     6  
     7  package layers
     8  
     9  import (
    10  	"net"
    11  	"testing"
    12  
    13  	"github.com/gopacket/gopacket"
    14  )
    15  
    16  func TestNewIPEndpoint(t *testing.T) {
    17  	cases := []struct {
    18  		ip           net.IP
    19  		endpointType gopacket.EndpointType
    20  	}{
    21  		{net.ParseIP("192.168.0.1").To4(), EndpointIPv4},
    22  		{net.ParseIP("192.168.0.1").To16(), EndpointIPv4},
    23  		{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), EndpointIPv6},
    24  	}
    25  
    26  	for _, c := range cases {
    27  		endpoint := NewIPEndpoint(c.ip)
    28  		if endpoint == gopacket.InvalidEndpoint {
    29  			t.Errorf("Failed to create an IP endpoint for %s (%d-bytes)",
    30  				c.ip, len(c.ip))
    31  		}
    32  		if endpoint.EndpointType() != c.endpointType {
    33  			t.Errorf("Wrong endpoint type created for %s (%d-bytes): expected %s, got %s",
    34  				c.ip, len(c.ip), c.endpointType, endpoint.EndpointType())
    35  		}
    36  	}
    37  }