package main import ( "strings" "testing" ) func TestTerminalMaxSessionsPerUser(t *testing.T) { cases := []struct { raw string want int }{ {"", defaultTerminalMaxSessionsPerUser}, // unset: default {" ", defaultTerminalMaxSessionsPerUser}, {"garbage", defaultTerminalMaxSessionsPerUser}, {"12x", defaultTerminalMaxSessionsPerUser}, {"0", defaultTerminalMaxSessionsPerUser}, // 0 must not disable the cap {"-3", defaultTerminalMaxSessionsPerUser}, {"1", 1}, {"4", 4}, {"16", 16}, {"128", 128}, {"129", maxTerminalSessionsPerUserLimit}, // clamped, never uncapped {"99999", maxTerminalSessionsPerUserLimit}, } for _, tc := range cases { t.Setenv("SSH_TERM_MAX_SESSIONS_PER_USER", tc.raw) if got := terminalMaxSessionsPerUser(); got != tc.want { t.Errorf("terminalMaxSessionsPerUser(%q) = %d, want %d", tc.raw, got, tc.want) } } t.Setenv("SSH_TERM_MAX_SESSIONS_PER_USER", "") if got := terminalMaxSessionsPerUser(); got != defaultTerminalMaxSessionsPerUser { t.Errorf("empty env must yield the default, got %d", got) } } func TestReserveTerminalSessionSlot(t *testing.T) { owner := "cap-test@example.com" other := "cap-test-other@example.com" // The test holds two slots at its end; release both so the global slot // map is left clean for the rest of the suite. t.Cleanup(func() { releaseTerminalSessionSlot(owner) releaseTerminalSessionSlot(owner) }) if !terminalSessionSlotAvailable(owner, 2) { t.Fatal("fresh owner must have a slot available") } if !reserveTerminalSessionSlot(owner, 2) || !reserveTerminalSessionSlot(owner, 2) { t.Fatal("reservations up to the limit must succeed") } if terminalSessionSlotAvailable(owner, 2) || reserveTerminalSessionSlot(owner, 2) { t.Fatal("reserving beyond the limit must fail") } // Other owners are unaffected by one owner filling their slots. if !terminalSessionSlotAvailable(other, 2) || !reserveTerminalSessionSlot(other, 2) { t.Fatal("another owner must be able to reserve while the first is at cap") } releaseTerminalSessionSlot(other) releaseTerminalSessionSlot(owner) if !terminalSessionSlotAvailable(owner, 2) || !reserveTerminalSessionSlot(owner, 2) { t.Fatal("release must free a slot for the next reservation") } } // Double release must never drive the count negative (which would hand an // owner extra slots): the counter deletes at <=1 and is a no-op afterwards. func TestReleaseTerminalSessionSlotFloorsAtZero(t *testing.T) { owner := "cap-floor-test@example.com" releaseTerminalSessionSlot(owner) // release for an owner with no slot releaseTerminalSessionSlot(owner) // repeated: must stay a no-op if !terminalSessionSlotAvailable(owner, 1) { t.Fatal("owner with no slots must have availability") } if !reserveTerminalSessionSlot(owner, 1) { t.Fatal("reservation after no-op releases must succeed") } releaseTerminalSessionSlot(owner) releaseTerminalSessionSlot(owner) if !terminalSessionSlotAvailable(owner, 1) || !reserveTerminalSessionSlot(owner, 1) { t.Fatal("over-release must not leak extra slots to the owner") } releaseTerminalSessionSlot(owner) } // The closeWithMessage teardown must release the slot the connect reserved, // exactly once — nil SSH handles are the point: the release hook has to be // reachable without a live SSH client, and a second close must be a no-op. func TestTerminalSessionCloseReleasesSlot(t *testing.T) { owner := "cap-close-test@example.com" if !reserveTerminalSessionSlot(owner, 1) { t.Fatal("precondition: reservation failed") } ts := &terminalSession{ owner: owner, id: "cap-close-session-1", done: make(chan struct{}), } ts.closeWithMessage("") select { case <-ts.done: default: t.Fatal("closeWithMessage must close the done channel") } if !terminalSessionSlotAvailable(owner, 1) || !reserveTerminalSessionSlot(owner, 1) { t.Fatal("session close must release the per-owner slot exactly once") } // Idempotency: a second close must not release somebody else's slot. releaseTerminalSessionSlot(owner) ts.closeWithMessage("") if !terminalSessionSlotAvailable(owner, 1) { t.Fatal("second close must not release an additional slot") } releaseTerminalSessionSlot(owner) } func TestTerminalSessionCapMessage(t *testing.T) { msg := terminalSessionCapMessage(4) if !strings.Contains(msg, "4") { t.Errorf("cap message must state the limit, got: %s", msg) } if strings.ContainsAny(msg, "\n\r") { t.Errorf("cap message must be single-line, got: %q", msg) } }