From 73b5ac79602651bfbd1f72ebef649181c843062a Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Wed, 31 Jan 2024 22:40:12 -0600 Subject: [PATCH] use distinct types for player and session ids --- game/game_test.go | 36 +++++++++++++++++++----------------- lobby/lobby_test.go | 4 +++- player/auth.go | 2 +- player/auth_test.go | 6 +++--- player/player.go | 4 +++- player/session.go | 6 ++++-- player/session_test.go | 12 ++++++------ serve/proxy.go | 4 +++- 8 files changed, 42 insertions(+), 32 deletions(-) diff --git a/game/game_test.go b/game/game_test.go index a41e90c..73333c7 100644 --- a/game/game_test.go +++ b/game/game_test.go @@ -3,14 +3,16 @@ package game import ( "testing" + "github.com/google/uuid" + "git.sunturtle.xyz/studio/shotgun/player" "git.sunturtle.xyz/studio/shotgun/serve" ) func TestNewGame(t *testing.T) { t.Parallel() - dealer := player.ID{1} - chall := player.ID{2} + dealer := player.ID{UUID: uuid.UUID{1}} + chall := player.ID{UUID: uuid.UUID{2}} for i := 0; i < 10000; i++ { g := New(dealer, chall) checks := []struct { @@ -41,7 +43,7 @@ func TestNewGame(t *testing.T) { func TestGameStartRound(t *testing.T) { t.Parallel() - g := New(player.ID{1}, player.ID{2}) + g := New(player.ID{UUID: uuid.UUID{1}}, player.ID{UUID: uuid.UUID{2}}) for i := int8(1); i < 100; i++ { checks := []struct { failed bool @@ -77,7 +79,7 @@ func TestGameStartRound(t *testing.T) { func TestGameStartGroup(t *testing.T) { t.Parallel() - g := New(player.ID{1}, player.ID{2}) + g := New(player.ID{UUID: uuid.UUID{1}}, player.ID{UUID: uuid.UUID{2}}) for i := uint(1); i < 10000; i++ { checks := []struct { failed bool @@ -114,7 +116,7 @@ func TestGameStartGroup(t *testing.T) { func TestGameNextTurn(t *testing.T) { t.Parallel() - g := New(player.ID{1}, player.ID{2}) + g := New(player.ID{UUID: uuid.UUID{1}}, player.ID{UUID: uuid.UUID{2}}) for i := int8(1); i < 100; i++ { checks := []struct { failed bool @@ -143,8 +145,8 @@ func TestGameNextTurn(t *testing.T) { func TestGamePlayers(t *testing.T) { t.Parallel() - dealer := player.ID{1} - chall := player.ID{2} + dealer := player.ID{UUID: uuid.UUID{1}} + chall := player.ID{UUID: uuid.UUID{2}} g := New(dealer, chall) if g.CurrentPlayer().id != chall { t.Errorf("challenger isn't current player at start") @@ -183,7 +185,7 @@ func TestGamePlayers(t *testing.T) { func TestGamePopShell(t *testing.T) { t.Parallel() - g := New(player.ID{1}, player.ID{2}) + g := New(player.ID{UUID: uuid.UUID{1}}, player.ID{UUID: uuid.UUID{2}}) if live := g.popShell(); live != g.shellArray[0] { t.Errorf("first pop %t, wanted %t", live, g.shellArray[0]) } @@ -208,8 +210,8 @@ func TestGamePopShell(t *testing.T) { } func TestGamePeek(t *testing.T) { - dealer := player.ID{1} - chall := player.ID{2} + dealer := player.ID{UUID: uuid.UUID{1}} + chall := player.ID{UUID: uuid.UUID{2}} t.Run("empty", func(t *testing.T) { t.Parallel() g := New(dealer, chall) @@ -270,8 +272,8 @@ func TestGameEmpty(t *testing.T) { func TestGameWinner(t *testing.T) { t.Parallel() - dealer := player.ID{1} - chall := player.ID{2} + dealer := player.ID{UUID: uuid.UUID{1}} + chall := player.ID{UUID: uuid.UUID{2}} cases := []struct { name string p0, p1 int8 @@ -311,8 +313,8 @@ func TestGameWinner(t *testing.T) { } func TestGameShoot(t *testing.T) { - dealer := player.ID{1} - chall := player.ID{2} + dealer := player.ID{UUID: uuid.UUID{1}} + chall := player.ID{UUID: uuid.UUID{2}} cases := []struct { name string live bool @@ -399,7 +401,7 @@ func TestGameShoot(t *testing.T) { } func TestConcede(t *testing.T) { - dealer, chall := player.ID{1}, player.ID{2} + dealer, chall := player.ID{UUID: uuid.UUID{1}}, player.ID{UUID: uuid.UUID{2}} cases := []struct { name string p player.ID @@ -417,7 +419,7 @@ func TestConcede(t *testing.T) { }, { name: "neither", - p: player.ID{3}, + p: player.ID{UUID: uuid.UUID{3}}, winner: player.ID{}, }, } @@ -436,7 +438,7 @@ func TestConcede(t *testing.T) { func TestGameDTO(t *testing.T) { t.Parallel() - dealer, chall := player.ID{1}, player.ID{2} + dealer, chall := player.ID{UUID: uuid.UUID{1}}, player.ID{UUID: uuid.UUID{2}} g := New(dealer, chall) { want := serve.Game{ diff --git a/lobby/lobby_test.go b/lobby/lobby_test.go index d632194..5626d1c 100644 --- a/lobby/lobby_test.go +++ b/lobby/lobby_test.go @@ -5,6 +5,8 @@ import ( "sync/atomic" "testing" + "github.com/google/uuid" + "git.sunturtle.xyz/studio/shotgun/lobby" "git.sunturtle.xyz/studio/shotgun/player" ) @@ -20,7 +22,7 @@ func TestQueue(t *testing.T) { for i := 0; i < N; i++ { i := i go func() { - id, _, deal := l.Queue(context.Background(), player.ID{uint8(i), uint8(i >> 8)}) + id, _, deal := l.Queue(context.Background(), player.ID{UUID: uuid.UUID{uint8(i), uint8(i >> 8)}}) if deal { dealers.Add(1) } else { diff --git a/player/auth.go b/player/auth.go index 6f1874d..95dfdaf 100644 --- a/player/auth.go +++ b/player/auth.go @@ -85,7 +85,7 @@ func Login(ctx context.Context, db RowQuerier, user, pass string) (ID, error) { slog.ErrorContext(ctx, "login failed", "user", user) return ID{}, fmt.Errorf("invalid credentials") } - return ID(id), nil + return ID{id}, nil } const initUsers = `CREATE TABLE shotgun_users ( diff --git a/player/auth_test.go b/player/auth_test.go index 733205d..c53232a 100644 --- a/player/auth_test.go +++ b/player/auth_test.go @@ -31,7 +31,7 @@ func TestLogin(t *testing.T) { if err == nil { t.Errorf("logging in nonexistent user didn't err") } - if id != uuid.Nil { + if id.UUID != uuid.Nil { t.Errorf("got nonzero user %v before registering", id) } @@ -43,7 +43,7 @@ func TestLogin(t *testing.T) { if err != nil { t.Errorf("couldn't login after registering: %v", err) } - if id == uuid.Nil { + if id.UUID == uuid.Nil { t.Errorf("got zero player id after registering") } @@ -51,7 +51,7 @@ func TestLogin(t *testing.T) { if err == nil { t.Errorf("logged in with wrong password") } - if wrong != uuid.Nil { + if wrong.UUID != uuid.Nil { t.Errorf("got nonzero user %v with wrong password (real is %v)", wrong, id) } } diff --git a/player/player.go b/player/player.go index 36cee9a..aaa97d9 100644 --- a/player/player.go +++ b/player/player.go @@ -4,4 +4,6 @@ package player import "github.com/google/uuid" // ID is a unique ID for a player. -type ID = uuid.UUID +type ID struct { + uuid.UUID +} diff --git a/player/session.go b/player/session.go index b371a4a..c707d43 100644 --- a/player/session.go +++ b/player/session.go @@ -11,7 +11,9 @@ import ( ) // Session is a session ID. -type Session = uuid.UUID +type Session struct { + uuid.UUID +} // InitSessions initializes an SQLite table relating player IDs to sessions. func InitSessions(ctx context.Context, db Execer) error { @@ -32,7 +34,7 @@ func StartSession(ctx context.Context, db Execer, p ID, now time.Time) (Session, return Session{}, fmt.Errorf("couldn't start session: %w", err) } slog.InfoContext(ctx, "session started", "player", p, "session", id, "now", now) - return Session(id), nil + return Session{id}, nil } // FromSession gets the player ID associated with a session and updates the diff --git a/player/session_test.go b/player/session_test.go index 0853cf8..ec03255 100644 --- a/player/session_test.go +++ b/player/session_test.go @@ -27,13 +27,13 @@ func TestSessions(t *testing.T) { t.Fatalf("couldn't init sessions: %v", err) } - p := player.ID{1} + p := player.ID{uuid.UUID{1}} now := time.Unix(0, 0) id, err := player.StartSession(ctx, conn, p, now) if err != nil { t.Fatalf("couldn't start session: %v", err) } - if id == uuid.Nil { + if id.UUID == uuid.Nil { t.Errorf("got zero session id at start") } @@ -41,7 +41,7 @@ func TestSessions(t *testing.T) { if err == nil { t.Errorf("no error on expired session") } - if u != uuid.Nil { + if u.UUID != uuid.Nil { t.Errorf("got nonzero player %v from expired session", u) } @@ -49,7 +49,7 @@ func TestSessions(t *testing.T) { if err != nil { t.Errorf("couldn't get player from session: %v", err) } - if u == uuid.Nil { + if u.UUID == uuid.Nil { t.Errorf("got zero player from session") } @@ -57,7 +57,7 @@ func TestSessions(t *testing.T) { if err != nil { t.Errorf("couldn't get player from extended session: %v", err) } - if u == uuid.Nil { + if u.UUID == uuid.Nil { t.Errorf("got zero player from extended session") } @@ -69,7 +69,7 @@ func TestSessions(t *testing.T) { if err == nil { t.Errorf("no error on logged out session") } - if u != uuid.Nil { + if u.UUID != uuid.Nil { t.Errorf("got nonzero player %v from logged out session", u) } } diff --git a/serve/proxy.go b/serve/proxy.go index f478689..6a89f27 100644 --- a/serve/proxy.go +++ b/serve/proxy.go @@ -6,6 +6,8 @@ import ( "net/netip" "strings" + "github.com/google/uuid" + "git.sunturtle.xyz/studio/shotgun/player" ) @@ -21,7 +23,7 @@ func WithPlayerID(next http.Handler) http.Handler { http.Error(w, "missing or invalid X-Forwarded-For header; check server configuration", http.StatusInternalServerError) return } - id := player.ID(addr.As16()) + id := player.ID{UUID: uuid.UUID(addr.As16())} ctx := ctxWith(r.Context(), id) next.ServeHTTP(w, r.WithContext(ctx)) })