Files
login/authd/client_test.go
T
Claude 3b86b397ad Rename modemmenu to dmodem-login, the repository to D-Modem/login
The program is dmodem-login (like dmodemd and dmodem-ari), the Go module
github.com/D-Modem/login, the site d-modem.github.io/login.  Its files,
units, sockets, PAM service, pppd plugin, environment (DMODEM_LOGIN_*)
and nftables tables follow.

An old installation keeps working:
- make install puts a modemmenu link to dmodem-login, and a modemmenu.so
  link to the plugin, which also takes its old modemmenu-socket option
- the default configuration, users file, state_dir and PAM service fall
  back to modemmenu's when only those are there (config.Legacy)

docs/rename.md (Simplified Technical English) lists the new names and
what an old installation must change (the -serve socket, the units, the
/etc/modemmenu files, the old nftables tables).  CI's bbs job moves a
bbs that still imports github.com/D-Modem/menu to the new path.  The
pppd plugin builds clean with -Wall -Wextra on GCC and Clang.

COMMERCIAL-LICENSE.md and CONTRIBUTING.md (legal text) are not changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRTD8BSptdpaUWGcs65
2026-11-23 05:04:16 +00:00

68 lines
1.7 KiB
Go

package authd
import (
"bufio"
"encoding/json"
"net"
"path/filepath"
"testing"
"time"
)
// serve answers as dmodem-login'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")
}
}