Commit 8a838fb9 authored by ale's avatar ale

Tests shouldn't use the local directory

parent 73ff044c
......@@ -4,7 +4,10 @@ import (
"context"
"crypto/rand"
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync"
"testing"
"time"
......@@ -14,6 +17,31 @@ import (
"git.autistici.org/id/auth/client"
)
func newSocketServerWithPath(t testing.TB, path string, h unix.Handler) func() {
ss, err := unix.NewUNIXSocketServer(path, h)
if err != nil {
t.Fatal(err)
}
go ss.Serve()
return func() {
ss.Close()
os.Remove(path)
}
}
func newSocketServer(t testing.TB, h unix.Handler) (string, func()) {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, "socket")
cleanup := newSocketServerWithPath(t, path, h)
return path, func() {
cleanup()
os.RemoveAll(dir)
}
}
func TestAuthServer_UNIX(t *testing.T) {
s := createTestServer(t, map[string]string{
"users.yml": testUsersFileStr,
......@@ -21,20 +49,23 @@ func TestAuthServer_UNIX(t *testing.T) {
})
defer s.Close()
ss, err := unix.NewUNIXSocketServer(".socket", unix.NewLineServer(NewSocketServer(s.srv)))
if err != nil {
t.Fatal(err)
}
go ss.Serve()
defer ss.Close()
sock, cleanup := newSocketServer(t, unix.NewLineServer(NewSocketServer(s.srv)))
defer cleanup()
c := client.New(".socket")
c := client.New(sock)
runAuthenticationTest(t, c)
}
func TestAuthServer_UNIX_ReuseSocket(t *testing.T) {
// Leave a stray socket around.
addr, _ := net.ResolveUnixAddr("unix", ".socket")
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
sock := filepath.Join(dir, "socket")
addr, _ := net.ResolveUnixAddr("unix", sock)
l, _ := net.ListenUnix("unix", addr)
l.SetUnlinkOnClose(false)
l.Close()
......@@ -45,16 +76,12 @@ func TestAuthServer_UNIX_ReuseSocket(t *testing.T) {
})
defer s.Close()
ss, err := unix.NewUNIXSocketServer(".socket", unix.NewLineServer(NewSocketServer(s.srv)))
if err != nil {
t.Fatal(err)
}
go ss.Serve()
defer ss.Close()
cleanup := newSocketServerWithPath(t, sock, unix.NewLineServer(NewSocketServer(s.srv)))
defer cleanup()
// Verify that a request does not return a connection error
// (regardless of the response).
c := client.New(".socket")
c := client.New(sock)
_, err = c.Authenticate(context.Background(), &auth.Request{Username: "foo"})
if err != nil {
t.Fatal("Authenticate():", err)
......@@ -68,12 +95,8 @@ func runMany(t testing.TB, concurrency, count int, f func(string) error) {
})
defer s.Close()
ss, err := unix.NewUNIXSocketServer(".socket", unix.NewLineServer(NewSocketServer(s.srv)))
if err != nil {
t.Fatal(err)
}
go ss.Serve()
defer ss.Close()
sock, cleanup := newSocketServer(t, unix.NewLineServer(NewSocketServer(s.srv)))
defer cleanup()
var wg sync.WaitGroup
for n := 0; n < concurrency; n++ {
......@@ -81,7 +104,7 @@ func runMany(t testing.TB, concurrency, count int, f func(string) error) {
go func(n int) {
defer wg.Done()
for i := 0; i < count; i++ {
if err := f(".socket"); err != nil {
if err := f(sock); err != nil {
t.Errorf("thread %d, iteration %d: %v", n, i, err)
}
time.Sleep(1 * time.Millisecond)
......@@ -92,7 +115,7 @@ func runMany(t testing.TB, concurrency, count int, f func(string) error) {
}
func spewRandomBytes(socketPath string) error {
c, err := net.Dial("unix", ".socket")
c, err := net.Dial("unix", socketPath)
if err != nil {
return err
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment