Files
Claude 7399e0fa36 Point repository references at the D-Modem organisation
The repositories moved from Neo-Desktop to the D-Modem GitHub
organisation: D-Modem-RE is now D-Modem/D-Modem, pico-serial is
D-Modem/hardware, modemmenu (PBX-Modem) is D-Modem/menu, bbs-sec is
D-Modem/bbs and go3270-sec is D-Modem/go3270.  Update links, module
paths, submodule URLs and source-location strings to match.  Copyright
notices still name Neo-Desktop.

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

92 lines
2.8 KiB
Go

package server
import (
"io"
"log"
"os"
"path/filepath"
"strings"
"testing"
"github.com/D-Modem/bbs/internal/backup"
"github.com/D-Modem/bbs/internal/bbs"
"github.com/D-Modem/bbs/internal/store"
)
// TestBackupConfig: [backup]'s defaults (beside the database, 7 kept, the
// files taken), the keys set, and an at = that is not HH:MM refused.
func TestBackupConfig(t *testing.T) {
dir := t.TempDir()
load := func(body string) (*Config, error) {
p := filepath.Join(dir, "bbs.conf")
writeFile(p, "[general]\ndatabase = "+filepath.Join(dir, "bbs.db")+"\n[listen t]\ntype = telnet\naddress = :2300\n"+body)
return Load(p)
}
c, err := load("")
if err != nil {
t.Fatal(err)
}
if b := c.Settings.Backup; b.Dir != filepath.Join(dir, "backups") || b.Keep != 7 || !b.Files || b.At != "" {
t.Errorf("defaults %+v", b)
}
c, err = load("[backup]\ndir = /srv/b\nat = 03:30\nkeep = 0\nfiles = no\n")
if b := c.Settings.Backup; err != nil || b.Dir != "/srv/b" || b.At != "03:30" || b.Keep != 0 || b.Files {
t.Errorf("%+v %v", b, err)
}
if _, err := load("[backup]\nat = 3pm\n"); err == nil || !strings.Contains(err.Error(), "at") {
t.Errorf("at = 3pm: %v", err)
}
}
// TestBackups: a backup of a running BBS as [backup] says (the files
// left out when it says so), listed.
func TestBackups(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "bbs.conf")
files := filepath.Join(dir, "files")
os.MkdirAll(files, 0o700)
os.WriteFile(filepath.Join(files, "1"), []byte("x"), 0o600)
writeFile(p, "[general]\ndatabase = "+filepath.Join(dir, "bbs.db")+"\nfiles_dir = "+files+
"\n[listen t]\ntype = telnet\naddress = :2300\n[listen s]\ntype = ssh\naddress = :2222\n[backup]\nkeep = 1\n")
cfg, err := Load(p)
if err != nil {
t.Fatal(err)
}
st, err := store.Open(cfg.Database)
if err != nil {
t.Fatal(err)
}
defer st.Close()
b := bbs.New(cfg.Settings, st, log.New(io.Discard, "", 0))
for _, kp := range DefaultHostKeys(dir) {
if _, _, err := LoadHostKey(kp); err != nil { // made
t.Fatal(err)
}
}
k := NewBackups(cfg, p, b)
a, err := k.Run()
if err != nil {
t.Fatal(err)
}
m, _ := backup.Manifest(a.Path)
if m["files"] != files || m["config"] != p || m["database"] != "sqlite" {
t.Errorf("manifest %v", m)
}
// files = no, put in force by a reload: the next has none
s := cfg.Settings
s.Backup.Files = false
b.Reconfigure(s)
os.Rename(a.Path, filepath.Join(filepath.Dir(a.Path), "bbs-20000101-000000.tar.gz")) // older: kept 1 takes it away
a2, err := k.Run()
if err != nil {
t.Fatal(err)
}
// the files left out; the host keys not
if m, _ := backup.Manifest(a2.Path); m["files"] != "" || m["key.ssh_host_ed25519_key"] == "" || m["key.ssh_host_rsa_key"] == "" {
t.Errorf("manifest %v", m)
}
if as, _ := k.List(); len(as) != 1 || as[0].Path != a2.Path {
t.Errorf("list %+v", as)
}
}