Files
login/internal/auth/system_test.go
T
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

69 lines
2.1 KiB
Go

// Copyright (C) 2026 D-Modem Authors
// SPDX-License-Identifier: AGPL-3.0-only
package auth
import (
"context"
"errors"
"testing"
"github.com/D-Modem/menu/internal/config"
)
// the account rules of the system backend, PAM replaced by a stub
func TestSystemRules(t *testing.T) {
accounts := map[string]*Account{
"root": {name: "root", uid: 0},
"daemon": {name: "daemon", uid: 1, gid: 1},
"alice": {name: "alice", uid: 1000, gid: 1000},
"bob": {name: "bob", uid: 1001, gid: 20},
}
dialup := map[string]bool{"bob": true}
newAuth := func(minUID int, root bool, groups []string) *System {
return &System{
service: "modemmenu", minUID: minUID, allowRoot: root, groups: groups,
lookup: func(u string) (*Account, error) {
if a, ok := accounts[u]; ok {
return a, nil
}
return nil, ErrNoUser
},
inGroups: func(a *Account, g []string) (bool, error) { return dialup[a.name], nil },
pam: func(service, user, password string) error {
if password != "pw" {
return ErrBadPassword
}
return nil
},
}
}
ctx := context.Background()
for _, c := range []struct {
a *System
user, pw string
want error
}{
{newAuth(1000, false, nil), "alice", "pw", nil},
{newAuth(1000, false, nil), "alice", "px", ErrBadPassword},
{newAuth(1000, false, nil), "nobody", "pw", ErrNoUser},
{newAuth(1000, false, nil), "daemon", "pw", ErrNoUser},
{newAuth(1000, false, nil), "root", "pw", ErrNoUser},
{newAuth(1000, true, nil), "root", "pw", nil},
{newAuth(1, false, nil), "daemon", "pw", nil},
{newAuth(1000, false, []string{"dialup"}), "alice", "pw", ErrNoUser},
{newAuth(1000, false, []string{"dialup"}), "bob", "pw", nil},
} {
err := c.a.Authenticate(ctx, c.user, c.pw)
if c.want == nil && err != nil || c.want != nil && !errors.Is(err, c.want) {
t.Errorf("%s/%s (min_uid %d, root %v, groups %v): %v, want %v",
c.user, c.pw, c.a.minUID, c.a.allowRoot, c.a.groups, err, c.want)
}
}
if !havePAM {
if _, err := NewSystem(&config.Section{Name: "system", Vals: map[string]string{}}); err == nil {
t.Error("auth = system without PAM: no error")
}
}
}