Skip to content

Commit

Permalink
fix merge errors + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
equals215 committed Sep 26, 2024
1 parent 7d0e9b1 commit 8330bd6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
14 changes: 6 additions & 8 deletions random_local_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,18 @@ func (c *CustomHTTPClient) getAvailableIPs(IPv6AnyIP bool) (IPs []net.IP, err er
}
}
}

if first {
c.interfacesWatcherStarted <- true
close(c.interfacesWatcherStarted)
first = false
}

time.Sleep(time.Second)
}

// Add the new addresses to the list
IPv6.IPs.Store(&newIPv6)
IPv4.IPs.Store(&newIPv4)

if first {
c.interfacesWatcherStarted <- true
close(c.interfacesWatcherStarted)
first = false
}

time.Sleep(time.Second)
}
}
Expand Down
34 changes: 25 additions & 9 deletions random_local_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestGetLocalAddrIPv4TCP(t *testing.T) {
ipList := []net.IPNet{ipNet1}
IPv4.IPs.Store(&ipList)

addr := getLocalAddr("tcp", "192.168.1.2:80")
addr := getLocalAddr("tcp", "192.168.1.2")
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
t.Errorf("Expected *net.TCPAddr, got %T", addr)
Expand All @@ -228,7 +228,7 @@ func TestGetLocalAddrIPv6TCP(t *testing.T) {
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)

addr := getLocalAddr("tcp6", "[2001:db8::2]:80")
addr := getLocalAddr("tcp6", "[2001:db8::2]")
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
t.Errorf("Expected *net.TCPAddr, got %T", addr)
Expand All @@ -245,7 +245,7 @@ func TestGetLocalAddrIPv6TCPAnyIP(t *testing.T) {
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)

addr := getLocalAddr("tcp6", "[2001:db12::20]:80")
addr := getLocalAddr("tcp6", "[2001:db12::20]")
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
t.Errorf("Expected *net.TCPAddr, got %T", addr)
Expand All @@ -263,7 +263,7 @@ func TestGetLocalAddrIPv4UDP(t *testing.T) {
ipList := []net.IPNet{ipNet1}
IPv4.IPs.Store(&ipList)

addr := getLocalAddr("udp", "192.168.1.2:80")
addr := getLocalAddr("udp", "192.168.1.2")
udpAddr, ok := addr.(*net.UDPAddr)
if !ok {
t.Errorf("Expected *net.UDPAddr, got %T", addr)
Expand All @@ -281,7 +281,7 @@ func TestGetLocalAddrIPv6UDP(t *testing.T) {
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)

addr := getLocalAddr("udp", "[2001:db8::2]:80")
addr := getLocalAddr("udp", "[2001:db8::2]")
udpAddr, ok := addr.(*net.UDPAddr)
if !ok {
t.Errorf("Expected *net.UDPAddr, got %T", addr)
Expand All @@ -293,23 +293,23 @@ func TestGetLocalAddrIPv6UDP(t *testing.T) {

// TestGetLocalAddrInvalidIP tests the function with an invalid destination IP.
func TestGetLocalAddrInvalidIP(t *testing.T) {
addr := getLocalAddr("tcp", "invalidIP:80")
addr := getLocalAddr("tcp", "invalidIP")
if addr != nil {
t.Errorf("Expected nil, got %v", addr)
}
}

// TestGetLocalAddrUnknownNetwork tests the function with an unknown network type.
func TestGetLocalAddrUnknownNetwork(t *testing.T) {
addr := getLocalAddr("unknown", "192.168.1.2:80")
addr := getLocalAddr("unknown", "192.168.1.2")
if addr != nil {
t.Errorf("Expected nil, got %v", addr)
}
}

// TestGetLocalAddrNoPort tests the function with an address missing a port.
func TestGetLocalAddrNoPort(t *testing.T) {
addr := getLocalAddr("tcp", "192.168.1.2")
func TestGetLocalAddrWithPort(t *testing.T) {
addr := getLocalAddr("tcp", "192.168.1.2:80")
if addr != nil {
t.Errorf("Expected nil, got %v", addr)
}
Expand All @@ -323,6 +323,22 @@ func TestGetLocalAddrMalformedAddress(t *testing.T) {
}
}

// TestAnyIPIPv6IPv4DisabledRealLife tests the function with IPv6 enabled and IPv4 disabled.
func TestAnyIPIPv6IPv4DisabledRealLife(t *testing.T) {
IPv6 = &availableIPs{AnyIP: true}
IPv4 = &availableIPs{}
ip1 := net.ParseIP("2001:db8::1")
ipNet1 := net.IPNet{IP: ip1, Mask: net.CIDRMask(64, 128)}
ipList := []net.IPNet{ipNet1}
IPv6.IPs.Store(&ipList)

tcpAddr := getLocalAddr("tcp6", "2606:4700:3030::ac43:a86a")
if tcpAddr == nil {
t.Error("Expected non-nil TCP address, got nil")
}
t.Logf("IPv6 TCP address: %v", tcpAddr)
}

// TestGetAvailableIPs is difficult due to its infinite loop and dependency on system interfaces.
func TestGetAvailableIPsAnyIP(t *testing.T) {
if IPv6 == nil {
Expand Down

0 comments on commit 8330bd6

Please sign in to comment.