-
-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use the next available port for wireguard (#2024)
check if WgPort is available, if not find the next free port
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package internal | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
) | ||
|
||
func Test_freePort(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
port int | ||
want int | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "available", | ||
port: 51820, | ||
want: 51820, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "notavailable", | ||
port: 51830, | ||
want: 51831, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "noports", | ||
port: 65535, | ||
want: 0, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
|
||
c1, err := net.ListenUDP("udp", &net.UDPAddr{Port: 51830}) | ||
if err != nil { | ||
t.Errorf("freePort error = %v", err) | ||
} | ||
c2, err := net.ListenUDP("udp", &net.UDPAddr{Port: 65535}) | ||
if err != nil { | ||
t.Errorf("freePort error = %v", err) | ||
} | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := freePort(tt.port) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("freePort() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("freePort() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
c1.Close() | ||
c2.Close() | ||
} | ||
} |