Files
login/auth_pam_test.go
T
Claude 04f5562196 License: AGPL-3.0-only, with commercial licenses from Neo-Desktop
LICENSE is the GNU Affero General Public License, version 3 (gnu.org's
text); each source file names it (SPDX-License-Identifier:
AGPL-3.0-only) and Neo-Desktop as the copyright holder.

COMMERCIAL-LICENSE.md: for uses the AGPL does not allow, Neo-Desktop
grants commercial licenses on agreed terms; asked for by a GitHub issue.

CONTRIBUTING.md: contributions come with an agreement (Signed-off-by)
that licenses them to Neo-Desktop under the AGPL and other terms,
commercial ones included, so that they can be part of both.

The README has a License section, and says how a modified modemmenu
offers callers its source (the AGPL's section 13).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RvtV8Dg9kqRaakbTEfyH3f
2026-10-15 20:09:02 +00:00

70 lines
1.9 KiB
Go

// Copyright (C) 2026 Neo-Desktop
// SPDX-License-Identifier: AGPL-3.0-only
//go:build pam
package main
import (
"context"
"errors"
"os"
"strings"
"testing"
)
// MODEMMENU_TEST_PAM=user:password of an account the PAM service
// modemmenu (/etc/pam.d/modemmenu) accepts; run as root for pam_unix
func TestPAM(t *testing.T) {
cred := os.Getenv("MODEMMENU_TEST_PAM")
if cred == "" {
t.Skip("MODEMMENU_TEST_PAM is not set")
}
user, pw, _ := strings.Cut(cred, ":")
a, err := newSystemAuth(&section{name: "system", vals: map[string]string{}})
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
if err := a.authenticate(ctx, user, pw); err != nil {
t.Errorf("right password: %v", err)
}
if err := a.authenticate(ctx, user, pw+"x"); !errors.Is(err, errBadPassword) {
t.Errorf("wrong password: %v", err)
}
if err := a.authenticate(ctx, "no-such-user-here", pw); !errors.Is(err, errNoUser) {
t.Errorf("unknown user: %v", err)
}
if err := a.authenticate(ctx, "root", pw); !errors.Is(err, errNoUser) {
t.Errorf("root: %v", err)
}
}
// MODEMMENU_TEST_PAM, a password stack in /etc/pam.d/modemmenu: the
// account's password changed and back
func TestPAMChangePassword(t *testing.T) {
cred := os.Getenv("MODEMMENU_TEST_PAM")
if cred == "" {
t.Skip("MODEMMENU_TEST_PAM is not set")
}
user, pw, _ := strings.Cut(cred, ":")
a, err := newSystemAuth(&section{name: "system", vals: map[string]string{}})
if err != nil {
t.Fatal(err)
}
sa := a.(*systemAuth)
ctx := context.Background()
if err := sa.changePassword(ctx, nil, user, pw, pw+"-2"); err != nil {
t.Fatal(err)
}
if err := a.authenticate(ctx, user, pw+"-2"); err != nil {
t.Errorf("the new password: %v", err)
}
if err := a.authenticate(ctx, user, pw); err == nil {
t.Error("the old password still works")
}
if err := sa.changePassword(ctx, nil, user, pw+"-2", pw); err != nil {
t.Errorf("back: %v", err)
}
}