Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf509c5b06 |
@@ -7,7 +7,6 @@ package handlers_test
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -153,12 +152,9 @@ func newTestServerWith(
|
|||||||
) *testServer {
|
) *testServer {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
// A random name, so that no other test can share it, not
|
|
||||||
// even one that has finished but still holds its database
|
|
||||||
// open.
|
|
||||||
dbURL := fmt.Sprintf(
|
dbURL := fmt.Sprintf(
|
||||||
"file:%s?mode=memory&cache=shared",
|
"file:test_%p?mode=memory&cache=shared",
|
||||||
rand.Text(),
|
t,
|
||||||
)
|
)
|
||||||
copyTemplateDB(t, dbURL)
|
copyTemplateDB(t, dbURL)
|
||||||
|
|
||||||
@@ -671,29 +667,6 @@ func TestCreateSessionDuplicate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestEachTestServerHasItsOwnDatabase checks that starting a
|
|
||||||
// second test server leaves the first one's database alone and
|
|
||||||
// starts on an empty one of its own.
|
|
||||||
func TestEachTestServerHasItsOwnDatabase(t *testing.T) {
|
|
||||||
first := newTestServer(t)
|
|
||||||
first.createSession("samenick")
|
|
||||||
|
|
||||||
second := newTestServer(t)
|
|
||||||
|
|
||||||
resp := postSession(t, first, "samenick")
|
|
||||||
|
|
||||||
defer func() { _ = resp.Body.Close() }()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusConflict {
|
|
||||||
t.Fatalf(
|
|
||||||
"first server: expected 409, got %d",
|
|
||||||
resp.StatusCode,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
second.createSession("samenick")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateSessionEmpty(t *testing.T) {
|
func TestCreateSessionEmpty(t *testing.T) {
|
||||||
tserver := newTestServer(t)
|
tserver := newTestServer(t)
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -101,48 +100,3 @@ func TestDisconnectNotifiesAndClosesVictim(t *testing.T) {
|
|||||||
t.Errorf("missing ERROR line, got: %q", joined)
|
t.Errorf("missing ERROR line, got: %q", joined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestDisconnectReasonCannotStartLine checks that CR, LF and
|
|
||||||
// NUL in a KILL reason are removed from both the KILL line
|
|
||||||
// and the ERROR line, so the victim gets exactly those two
|
|
||||||
// lines. The ERROR line is not built with FormatMessage, so
|
|
||||||
// only the removal in sendWithin protects it.
|
|
||||||
func TestDisconnectReasonCannotStartLine(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
victim, clientSide := newKillVictim(t)
|
|
||||||
received := make(chan []string, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
var lines []string
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(clientSide)
|
|
||||||
for scanner.Scan() {
|
|
||||||
lines = append(lines, scanner.Text())
|
|
||||||
}
|
|
||||||
|
|
||||||
received <- lines
|
|
||||||
}()
|
|
||||||
|
|
||||||
victim.Disconnect("bye\r\nPRIVMSG victim :forged\x00")
|
|
||||||
|
|
||||||
var lines []string
|
|
||||||
|
|
||||||
select {
|
|
||||||
case lines = <-received:
|
|
||||||
case <-time.After(testTimeout):
|
|
||||||
t.Fatal("the victim's connection was not closed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// The test connection has no host name, hence the two
|
|
||||||
// spaces after "Closing Link:".
|
|
||||||
want := []string{
|
|
||||||
":" + testServerName +
|
|
||||||
" KILL victim :byePRIVMSG victim :forged",
|
|
||||||
"ERROR :Closing Link: (byePRIVMSG victim :forged)",
|
|
||||||
}
|
|
||||||
|
|
||||||
if !slices.Equal(lines, want) {
|
|
||||||
t.Errorf("got lines %q, want %q", lines, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ package service_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -44,12 +43,9 @@ type testEnv struct {
|
|||||||
func newTestEnv(t *testing.T) *testEnv {
|
func newTestEnv(t *testing.T) *testEnv {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
// A random name, so that no other test can share it, not
|
|
||||||
// even one that has finished but still holds its database
|
|
||||||
// open.
|
|
||||||
dbURL := fmt.Sprintf(
|
dbURL := fmt.Sprintf(
|
||||||
"file:%s?mode=memory&cache=shared",
|
"file:svc_test_%p?mode=memory&cache=shared",
|
||||||
rand.Text(),
|
t,
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -141,19 +137,6 @@ func createSession(
|
|||||||
return sessionID
|
return sessionID
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestEachTestEnvHasItsOwnDatabase checks that a test
|
|
||||||
// environment starts on an empty database while another one
|
|
||||||
// is still open.
|
|
||||||
func TestEachTestEnvHasItsOwnDatabase(t *testing.T) {
|
|
||||||
ctx := t.Context()
|
|
||||||
|
|
||||||
first := newTestEnv(t)
|
|
||||||
createSession(ctx, t, first.db, "samenick")
|
|
||||||
|
|
||||||
second := newTestEnv(t)
|
|
||||||
createSession(ctx, t, second.db, "samenick")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFanOut(t *testing.T) {
|
func TestFanOut(t *testing.T) {
|
||||||
env := newTestEnv(t)
|
env := newTestEnv(t)
|
||||||
ctx := t.Context()
|
ctx := t.Context()
|
||||||
|
|||||||
Reference in New Issue
Block a user