mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 20:38:06 -08:00
The menu comes up full screen on a terminal that can have it (ANSI, or a VT that said what it is, 40x14 at least): the items in a window on a blue desk, a bar moved with the cursor keys, an item's key starting it at once, the welcome at the top of the window, the motd in a box, the user, rate, clock and minutes left on the bars. Others get the menu of lines; the login stays lines, so AutoPPP and the AOL client sniffing are as they were. [menu] fullscreen = auto|no, item help =. modemmenu -setup changes modemmenu.conf on full screens in the style of the 1990s BBS setup programs (the bbs's setup screens, ported): pull-down menus, cascading windows, the sample's help of each key, values typed in a box or picked from a list. Saving checks the file first and keeps its comments and order. An [item] of mode = setup (users or groups needed) gives it to a sysop who calls in. - internal/tui: the screen buffer and its diff rendering (from bbs textui/desk.go), UTF-8, code page 437, the VT line set and ASCII, keys, windows, pull-downs and dialogs - internal/confedit: changes to the file by line, comments kept - internal/confdoc: the sample's help, from tools/confref (shared) - term: a UTF-8 probe for ANSI terminals; [terminal] charset - below 9600 bit/s the desk is plain blue, cleared in a few bytes - the sample: comments for keys that took a neighbour's help - tests: the line menu's tests ask for fullscreen = no; one handoff test waits for the server's last log line Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRUujUBmci66AZkngmh
195 lines
5.3 KiB
Go
195 lines
5.3 KiB
Go
// Copyright (C) 2026 D-Modem Authors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestHeader: the session header written and read back, and the
|
|
// environment a session gets from it, trusted or not
|
|
func TestHeader(t *testing.T) {
|
|
a, b := net.Pipe()
|
|
s := newSession("33600/31200", "555\n1000", "", time.Unix(1791666309, 0))
|
|
s.User, s.Password = "alice", "secret"
|
|
go func() {
|
|
writeHeader(a, s, false)
|
|
a.Write([]byte("data"))
|
|
}()
|
|
h, err := readHeader(b, time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if h["MODEM_USER"] != "alice" || h["MODEM_DIALED"] != "555 1000" || h["MODEM_PASSWORD"] != "" {
|
|
t.Errorf("header %v", h)
|
|
}
|
|
rest := make([]byte, 4)
|
|
b.Read(rest)
|
|
if string(rest) != "data" {
|
|
t.Errorf("after the header: %q", rest)
|
|
}
|
|
env := strings.Join(headerEnv(h, false), "\n")
|
|
if !strings.Contains(env, "DMODEM_RATE=33600/31200") || !strings.Contains(env, "DMODEM_CONNECTED=1791666309") ||
|
|
strings.Contains(env, "UPSTREAM") {
|
|
t.Errorf("env %s", env)
|
|
}
|
|
if env := strings.Join(headerEnv(h, true), "\n"); !strings.Contains(env, "MODEMMENU_UPSTREAM_USER=alice") {
|
|
t.Errorf("trusted env %s", env)
|
|
}
|
|
}
|
|
|
|
// modemmenu -serve on a unix socket: a session from app_dmodem's header,
|
|
// a hangup, and a chain from another modemmenu that logged the caller in
|
|
func TestServe(t *testing.T) {
|
|
dir := t.TempDir()
|
|
bin := filepath.Join(dir, "modemmenu")
|
|
if out, err := exec.Command("go", "build", "-o", bin, ".").CombinedOutput(); err != nil {
|
|
t.Fatalf("build: %v\n%s", err, out)
|
|
}
|
|
if _, _, err := openPty(); err != nil {
|
|
t.Skip("no pty:", err)
|
|
}
|
|
sock := filepath.Join(dir, "menu.sock")
|
|
users := filepath.Join(dir, "users")
|
|
os.WriteFile(users, []byte("alice:{PLAIN}secret\n"), 0o600)
|
|
// the served menu: trusts the user of a header
|
|
conf := filepath.Join(dir, "server.conf")
|
|
os.WriteFile(conf, []byte(fmt.Sprintf(`
|
|
[general]
|
|
log = %s
|
|
auth = file
|
|
fail_delay = 0
|
|
banner = "Served line\r\n"
|
|
[file]
|
|
path = %s
|
|
[server]
|
|
listen = %s
|
|
trust_user = yes
|
|
socket_mode = 0600
|
|
[item info]
|
|
key = I
|
|
label = Info
|
|
command = echo "on $(tty | cut -c1-8) dialed=$MODEM_DIALED rate=$MODEM_RATE date=$MODEM_DATE auth=$MODEM_AUTH user=$MODEM_USER pw=$MODEM_PASSWORD up=$MODEMMENU_UPSTREAM_USER"
|
|
`, filepath.Join(dir, "server.log"), users, sock)), 0o600)
|
|
|
|
srv := exec.Command(bin, "-config", lineMenu(conf), "-serve")
|
|
if err := srv.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer srv.Process.Kill()
|
|
for i := 0; i < 100; i++ {
|
|
if _, err := os.Stat(sock); err == nil {
|
|
break
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
if fi, err := os.Stat(sock); err != nil || fi.Mode().Perm() != 0o600 {
|
|
t.Fatalf("socket: %v %v", fi, err)
|
|
}
|
|
|
|
// as app_dmodem with header = yes: the call's details, then a login
|
|
conn, err := net.Dial("unix", sock)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.Write([]byte("DMODEM_RATE=31200/26400\nDMODEM_CALLERID=5559999\nDMODEM_DIALED=5551000\n" +
|
|
"DMODEM_CONNECTED=1791666309\nDMODEM_CHANNEL=PJSIP/trunk-1\n\n"))
|
|
c := newCaller(t, conn)
|
|
c.expect("Served line")
|
|
c.expect("login: ")
|
|
c.send("alice\r")
|
|
c.expect("Password: ")
|
|
c.send("secret\r")
|
|
c.expect("Selection: ")
|
|
c.send("info\r")
|
|
c.expect("on /dev/pts dialed=5551000 rate=31200/26400 date=2026-10-10 auth=file user=alice pw=secret up=\n")
|
|
c.expect("Goodbye")
|
|
conn.Close()
|
|
|
|
// a caller that hangs up at the login: the session ends
|
|
conn, err = net.Dial("unix", sock)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.Write([]byte("\n"))
|
|
c = newCaller(t, conn)
|
|
c.expect("login: ")
|
|
conn.Close()
|
|
|
|
// another menu (on app_dmodem's pty) logs the caller in and hands
|
|
// them over: no second login
|
|
front := filepath.Join(dir, "front.conf")
|
|
os.WriteFile(front, []byte(fmt.Sprintf(`
|
|
[general]
|
|
log = none
|
|
auth = file
|
|
[file]
|
|
path = %s
|
|
[item more]
|
|
key = M
|
|
label = More services
|
|
mode = unix
|
|
path = %s
|
|
header = yes
|
|
`, users, sock)), 0o600)
|
|
m, s := testPty(t)
|
|
defer m.Close()
|
|
cmd := exec.Command(bin, "-config", lineMenu(front))
|
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = s, s, s
|
|
cmd.Env = append(os.Environ(), "DMODEM_RATE=26400/26400", "DMODEM_DIALED=5552000")
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.Close()
|
|
defer cmd.Process.Kill()
|
|
c = newCaller(t, m)
|
|
c.expect("login: ")
|
|
c.send("alice\r")
|
|
c.expect("Password: ")
|
|
c.send("secret\r")
|
|
c.expect("Selection: ")
|
|
c.send("more\r")
|
|
c.expect("Served line")
|
|
c.expect("Welcome, alice.")
|
|
c.expect("Selection: ")
|
|
c.send("i\r")
|
|
c.expect("dialed=5552000 rate=26400/26400 date=")
|
|
c.expect("auth=upstream:file user=alice pw=secret up=\n")
|
|
c.expect("Goodbye")
|
|
cmd.Wait()
|
|
|
|
// SIGTERM: the socket goes
|
|
srv.Process.Signal(syscall.SIGTERM)
|
|
done := make(chan error, 1)
|
|
go func() { done <- srv.Wait() }()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("the server is still running after SIGTERM")
|
|
}
|
|
if _, err := os.Stat(sock); err == nil {
|
|
t.Error("the socket is still there")
|
|
}
|
|
log, _ := os.ReadFile(filepath.Join(dir, "server.log"))
|
|
for _, w := range []string{"serving on", "login user=alice auth=file", "login user=alice auth=upstream:file",
|
|
"callerid=\"5559999\" dialed=\"5551000\""} {
|
|
if !bytes.Contains(log, []byte(w)) {
|
|
t.Errorf("no %q in the log:\n%s", w, log)
|
|
}
|
|
}
|
|
if n := bytes.Count(log, []byte("ended")); n != 3 {
|
|
t.Errorf("%d sessions ended, want 3:\n%s", n, log)
|
|
}
|
|
}
|