wgctrl-go/internal/wguser/conn_unix_test.go
Matt Layher 97bc4ad4a1
FreeBSD kernel support (#128)
* internal/freebsd: add initial version of FreeBSD support
* internal/wguser: Replace deprecated io/ioutil package with io
* internal/freebsd: prepare CI to run tests on FreeBSD
* test: sort AllowedIPs before diffing them
* test: skip integration test configure_peers_update_only on FreeBSD
* test: increase test timeout for slow FreeBSD tests
* add FreeBSD support to README

Signed-off-by: Steffen Vogel <post@steffenvogel.de>

* *: tidy
* go.mod: bump dependencies
* .builds: try to fix OpenBSD

Signed-off-by: Matt Layher <mdlayher@gmail.com>

Co-authored-by: Steffen Vogel <post@steffenvogel.de>
2022-11-04 09:57:56 -04:00

85 lines
1.9 KiB
Go

//go:build !windows
// +build !windows
package wguser
import (
"net"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestUNIX_findUNIXSockets(t *testing.T) {
tmp, err := os.MkdirTemp(os.TempDir(), "wireguardcfg-test")
if err != nil {
t.Fatalf("failed to create temporary directory: %v", err)
}
defer os.RemoveAll(tmp)
// Create a file which is not a device socket.
f, err := os.CreateTemp(tmp, "notwg")
if err != nil {
t.Fatalf("failed to create temporary file: %v", err)
}
_ = f.Close()
// Create a temporary UNIX socket and leave it open so it is picked up
// as a socket file.
path := filepath.Join(tmp, "testwg0.sock")
l, err := net.Listen("unix", path)
if err != nil {
t.Fatalf("failed to create socket: %v", err)
}
defer l.Close()
files, err := findUNIXSockets([]string{
tmp,
// Should gracefully handle non-existent directories and files.
filepath.Join(tmp, "foo"),
"/not/exist",
})
if err != nil {
t.Fatalf("failed to find files: %v", err)
}
if diff := cmp.Diff([]string{path}, files); diff != "" {
t.Fatalf("unexpected output files (-want +got):\n%s", diff)
}
}
// testFind produces a Client.find function for integration tests.
func testFind(dir string) func() ([]string, error) {
return func() ([]string, error) {
return findUNIXSockets([]string{dir})
}
}
// testListen creates a userspace device listener for tests, returning the
// directory where it can be found and a function to clean up its state.
func testListen(t *testing.T, device string) (l net.Listener, dir string, done func()) {
t.Helper()
tmp, err := os.MkdirTemp(os.TempDir(), "wguser-test")
if err != nil {
t.Fatalf("failed to create temporary directory: %v", err)
}
path := filepath.Join(tmp, device)
path += ".sock"
l, err = net.Listen("unix", path)
if err != nil {
t.Fatalf("failed to create UNIX socket: %v", err)
}
done = func() {
_ = l.Close()
_ = os.RemoveAll(tmp)
}
return l, tmp, done
}