unixserver_test.go 3.57 KB
Newer Older
ale's avatar
ale committed
1 2 3 4 5 6
package server

import (
	"context"
	"crypto/rand"
	"fmt"
ale's avatar
ale committed
7
	"io/ioutil"
ale's avatar
ale committed
8
	"net"
ale's avatar
ale committed
9 10
	"os"
	"path/filepath"
ale's avatar
ale committed
11 12 13 14
	"sync"
	"testing"
	"time"

15
	"git.autistici.org/ai3/go-common/unix"
ale's avatar
ale committed
16 17 18 19
	"git.autistici.org/id/auth"
	"git.autistici.org/id/auth/client"
)

ale's avatar
ale committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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)
	}
}

ale's avatar
ale committed
45 46 47 48 49 50 51
func TestAuthServer_UNIX(t *testing.T) {
	s := createTestServer(t, map[string]string{
		"users.yml":  testUsersFileStr,
		"config.yml": testConfigStr,
	})
	defer s.Close()

ale's avatar
ale committed
52 53
	sock, cleanup := newSocketServer(t, unix.NewLineServer(NewSocketServer(s.srv)))
	defer cleanup()
ale's avatar
ale committed
54

ale's avatar
ale committed
55
	c := client.New(sock)
ale's avatar
ale committed
56
	runAuthenticationTest(t, c)
ale's avatar
ale committed
57 58 59 60
}

func TestAuthServer_UNIX_ReuseSocket(t *testing.T) {
	// Leave a stray socket around.
ale's avatar
ale committed
61 62 63 64 65 66 67 68
	dir, err := ioutil.TempDir("", "")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	sock := filepath.Join(dir, "socket")
	addr, _ := net.ResolveUnixAddr("unix", sock)
ale's avatar
ale committed
69 70 71 72 73 74 75 76 77 78
	l, _ := net.ListenUnix("unix", addr)
	l.SetUnlinkOnClose(false)
	l.Close()

	s := createTestServer(t, map[string]string{
		"users.yml":  testUsersFileStr,
		"config.yml": testConfigStr,
	})
	defer s.Close()

ale's avatar
ale committed
79 80
	cleanup := newSocketServerWithPath(t, sock, unix.NewLineServer(NewSocketServer(s.srv)))
	defer cleanup()
ale's avatar
ale committed
81 82 83

	// Verify that a request does not return a connection error
	// (regardless of the response).
ale's avatar
ale committed
84
	c := client.New(sock)
ale's avatar
ale committed
85 86 87 88 89 90 91 92 93 94 95 96 97
	_, err = c.Authenticate(context.Background(), &auth.Request{Username: "foo"})
	if err != nil {
		t.Fatal("Authenticate():", err)
	}
}

func runMany(t testing.TB, concurrency, count int, f func(string) error) {
	s := createTestServer(t, map[string]string{
		"users.yml":  testUsersFileStr,
		"config.yml": testConfigStr,
	})
	defer s.Close()

ale's avatar
ale committed
98 99
	sock, cleanup := newSocketServer(t, unix.NewLineServer(NewSocketServer(s.srv)))
	defer cleanup()
ale's avatar
ale committed
100 101 102 103 104 105 106

	var wg sync.WaitGroup
	for n := 0; n < concurrency; n++ {
		wg.Add(1)
		go func(n int) {
			defer wg.Done()
			for i := 0; i < count; i++ {
ale's avatar
ale committed
107
				if err := f(sock); err != nil {
ale's avatar
ale committed
108 109 110 111 112 113 114 115 116 117
					t.Errorf("thread %d, iteration %d: %v", n, i, err)
				}
				time.Sleep(1 * time.Millisecond)
			}
		}(n)
	}
	wg.Wait()
}

func spewRandomBytes(socketPath string) error {
ale's avatar
ale committed
118
	c, err := net.Dial("unix", socketPath)
ale's avatar
ale committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	if err != nil {
		return err
	}

	defer c.Close()

	var randBytes [1024]byte
	rand.Read(randBytes[:])
	c.Write(randBytes[:])
	c.Write([]byte("\r\n"))
	return nil
}

func TestAuthServer_UNIX_SyntaxError(t *testing.T) {
	// Send random crap, see that it does not crash.
	runMany(t, 10, 100, spewRandomBytes)
}

func trySuccessfulLogin(socketPath string) error {
	c := client.New(socketPath)
	//defer c.Close()
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()
	resp, err := c.Authenticate(ctx, &auth.Request{
		Service:  "test",
		Username: "testuser",
		Password: []byte("password"),
	})
	if err != nil {
		return err
	}
	if resp.Status != auth.StatusOK {
		return fmt.Errorf("unexpected status %s", resp.Status.String())
	}
	return nil
}

func TestAuthServer_UNIX_ConcurrentLogins(t *testing.T) {
	runMany(t, 10, 3, trySuccessfulLogin)
}

func BenchmarkAuthServer_UNIX_ConcurrentLogins(b *testing.B) {
	// This basically measures scrypt performance.
	runMany(b, 4, b.N, trySuccessfulLogin)
}