github.com/Aestek/consul@v1.2.4-0.20190309222502-b2c31e33971a/ipaddr/detect_test.go (about)

     1  package ipaddr
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  )
     7  
     8  func TestIsPrivateIP(t *testing.T) {
     9  	tests := []struct {
    10  		ip      string
    11  		private bool
    12  	}{
    13  		// IPv4 private addresses
    14  		{"10.0.0.1", true},    // private network address
    15  		{"100.64.0.1", true},  // shared address space
    16  		{"172.16.0.1", true},  // private network address
    17  		{"192.168.0.1", true}, // private network address
    18  		{"192.0.0.1", true},   // IANA address
    19  		{"192.0.2.1", true},   // documentation address
    20  		{"127.0.0.1", true},   // loopback address
    21  		{"169.254.0.1", true}, // link local address
    22  
    23  		// IPv4 public addresses
    24  		{"1.2.3.4", false},
    25  
    26  		// IPv6 private addresses
    27  		{"::1", true},         // loopback address
    28  		{"fe80::1", true},     // link local address
    29  		{"fc00::1", true},     // unique local address
    30  		{"fec0::1", true},     // site local address
    31  		{"2001:db8::1", true}, // documentation address
    32  
    33  		// IPv6 public addresses
    34  		{"2004:db6::1", false},
    35  	}
    36  
    37  	for _, tt := range tests {
    38  		t.Run(tt.ip, func(t *testing.T) {
    39  			ip := net.ParseIP(tt.ip)
    40  			if ip == nil {
    41  				t.Fatalf("%s is not a valid ip address", tt.ip)
    42  			}
    43  			if got, want := isPrivate(ip), tt.private; got != want {
    44  				t.Fatalf("got %v for %v want %v", got, ip, want)
    45  			}
    46  		})
    47  	}
    48  }