Files
login/authd/client_test.go
Claude 80d0fda167 Split the code into packages: cmd/modemmenu, internal/*, authd
All of modemmenu was one package main in the repository's root.  It is
now one module with the program and the pieces it is made of apart:

  cmd/modemmenu/     the program (the login, the menu, access, limits,
                     the selections, -serve, AutoPPP, drop files, the
                     auth socket's server)
  internal/config/   modemmenu.conf's sections and keys, {name} templates
  internal/term/     the caller's line, terminal detection, PETSCII
  internal/auth/     the backends and password changes
  internal/telnet/   the telnet client of mode = telnet items
  authd/             the auth socket's protocol and a client, for a BBS
                     to import (bbs's internal/menuauth is a copy of it)
  samples/           the .conf, .users, .pam and .service samples

make builds ./cmd/modemmenu and installs the samples from samples/; the
docs, confref and the site's reference follow.  The tests are where
their code is, none lost.  TestSampleHasEveryKey now reads the keys of
internal/ too (and the exported Get, Bool...: it would have found none).
.gitignore's "modemmenu" ignored cmd/modemmenu/ as well: it is
/modemmenu now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRRbjf74j8oq9R3PXhA
2026-11-06 03:12:02 +00:00

68 lines
1.7 KiB
Go

package authd
import (
"bufio"
"encoding/json"
"net"
"path/filepath"
"testing"
"time"
)
// serve answers as modemmenu's [authd] would: alice/secret, then a
// refusal for "eve", and silence for "slow".
func serve(t *testing.T) string {
sock := filepath.Join(t.TempDir(), "auth.sock")
ln, err := net.Listen("unix", sock)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() {
defer c.Close()
line, _ := bufio.NewReader(c).ReadBytes('\n')
var r Request
json.Unmarshal(line, &r)
switch {
case r.User == "slow":
time.Sleep(time.Second)
case r.User == "eve":
c.Write([]byte(`{"ok":false,"error":"refused"}` + "\n"))
case r.User == "alice" && r.Password == "secret":
c.Write([]byte(`{"ok":true,"auth":"file"}` + "\n"))
default:
c.Write([]byte(`{"ok":false,"error":"denied"}` + "\n"))
}
}()
}
}()
return sock
}
// TestCheck: the helper's yes (with how), its no, its refusal (an error),
// a helper too slow, and no helper at all.
func TestCheck(t *testing.T) {
c := &Client{Socket: serve(t), Timeout: 300 * time.Millisecond}
if ok, auth, err := c.Check("alice", "secret"); !ok || auth != "file" || err != nil {
t.Errorf("alice: %v %q %v", ok, auth, err)
}
if ok, _, err := c.Check("alice", "wrong"); ok || err != nil {
t.Errorf("wrong: %v %v", ok, err)
}
if _, _, err := c.Check("eve", "x"); err == nil || err.Error() != "refused" {
t.Errorf("eve: %v", err)
}
if _, _, err := c.Check("slow", "x"); err == nil {
t.Error("slow: no time out")
}
if _, _, err := (&Client{Socket: "/nonexistent/auth.sock"}).Check("alice", "secret"); err == nil {
t.Error("no socket")
}
}