aboutsummaryrefslogtreecommitdiff
path: root/internal/game/login_offowner_regression_test.go
blob: 4826c4c95ab7c8cd1f75a4002c5953de149f123b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package game

// Login regressions for the off-owner password path: the production entry
// point verifies passwords and hashes new-account passwords on the
// connection goroutine (never the owner), then completes the state
// transition on the owner. The observable login behavior is unchanged.

import (
	"strings"
	"testing"
	"time"

	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

// waitForOutput polls the queued session writer until substr appears or the
// deadline passes.
func waitForOutput(t *testing.T, conn *chanConn, substr string) {
	t.Helper()
	deadline := time.Now().Add(3 * time.Second)
	for !conn.Contains(substr) && time.Now().Before(deadline) {
		time.Sleep(time.Millisecond)
	}
	if !conn.Contains(substr) {
		t.Fatalf("output %q never arrived; got:\n%s", substr, conn.Bytes())
	}
}

// TestLoginThroughServerEntryUsesOffOwnerPath drives the real server entry
// point (HandleSessionOnOwner) through a WRONG password attempt and a correct
// login, exercising the off-owner verification path.
func TestLoginThroughServerEntryUsesOffOwnerPath(t *testing.T) {
	h := newHarness(t)
	h.seedAccount("alice")
	h.seedCharacter("alice", "alice")

	conn := newChanConn()
	sess := &net.Session{Conn: conn}
	sess.SetState(net.StateAccountName)
	sess.StartWriter()
	lines := make(chan string, 8)
	go h.input(sess, lines)

	lines <- "alice"   // existing account → password prompt (echo off)
	lines <- "wrongpw" // rejected, echo restored, back to account name
	waitForOutput(t, conn, "Wrong password.")
	lines <- "alice" // account name again
	lines <- "pw"    // correct password through the off-owner path
	waitForOutput(t, conn, "Welcome back to Vesta, alice.")

	if sess.Account == nil || sess.Account.Name != "alice" || len(sess.Account.Characters) != 1 {
		t.Fatalf("login did not complete: %+v", sess.Account)
	}
	loaded, err := h.g.AccountStore.LoadAccount("alice")
	if err != nil {
		t.Fatal(err)
	}
	if !player.CheckPassword("pw", loaded.PasswordHash) {
		t.Error("stored hash no longer verifies the password")
	}
}

// TestNewAccountThroughServerEntryHashesOffOwner drives account creation
// through the real server entry point.
func TestNewAccountThroughServerEntryHashesOffOwner(t *testing.T) {
	h := newHarness(t)
	conn := newChanConn()
	sess := &net.Session{Conn: conn}
	sess.SetState(net.StateAccountName)
	sess.StartWriter()
	lines := make(chan string, 8)
	go h.input(sess, lines)

	lines <- "newbie" // unknown name → new-account password prompt
	lines <- "hunter2pass"
	lines <- "hunter2pw" // mismatch → restart account entry (Account reset)
	waitForOutput(t, conn, "Passwords do not match.")
	lines <- "newbie" // account name again
	lines <- "hunter2pw"
	lines <- "hunter2pw"
	lines <- "n" // color prompt
	waitForOutput(t, conn, "Welcome back to Vesta, newbie.")

	created, err := h.g.AccountStore.LoadAccount("newbie")
	if err != nil {
		t.Fatal(err)
	}
	if !strings.HasPrefix(created.PasswordHash, "argon2id:") {
		t.Errorf("created account did not hash with Argon2id: %s", created.PasswordHash[:12])
	}
	if !player.CheckPassword("hunter2pw", created.PasswordHash) {
		t.Error("created hash does not verify the password")
	}
}