fix: 修复写队列 busy 恢复、关闭安全、备份与配置构建问题

This commit is contained in:
Nixevol
2026-09-30 16:21:05 +08:00
parent 4059a1576b
commit 57c2f70678
23 changed files with 825 additions and 101 deletions
+136
View File
@@ -3,8 +3,13 @@ package main
import (
"bytes"
"context"
"io"
"net/http"
"net/http/cookiejar"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
@@ -108,6 +113,92 @@ func TestBackupVacuumInto(t *testing.T) {
}
}
func TestBackupMissingDB(t *testing.T) {
dir := t.TempDir()
path := writeTestConfig(t, dir)
t.Setenv("NIXMSG_CONFIG", path)
out := filepath.Join(dir, "copy.db")
err := cmdBackup([]string{"--out", out})
if err == nil || !strings.Contains(err.Error(), "database not found") {
t.Fatalf("want database not found, got %v", err)
}
if _, statErr := os.Stat(filepath.Join(dir, store.DBFileName)); !os.IsNotExist(statErr) {
t.Fatalf("backup must not create %s: %v", store.DBFileName, statErr)
}
}
func TestAdminSetPasswordClearsSessions(t *testing.T) {
dir := t.TempDir()
cfgPath := writeTestConfig(t, dir)
initAdminForTest(t, dir)
t.Setenv("NIXMSG_CONFIG", cfgPath)
cfg, err := loadAndValidateConfig()
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errCh := make(chan error, 1)
go func() { errCh <- runServe(ctx, cfg) }()
addr := waitListenAddr(t, dir, 15*time.Second)
base := "http://" + addr
jar, err := cookiejar.New(nil)
if err != nil {
t.Fatal(err)
}
client := &http.Client{Jar: jar, Timeout: 10 * time.Second}
loginBody := `{"username":"admin","password":"test-admin-password-xx"}`
resp, err := client.Post(base+"/api/admin/login", "application/json", strings.NewReader(loginBody))
if err != nil {
cancel()
<-errCh
t.Fatal(err)
}
_, _ = io.ReadAll(resp.Body)
_ = resp.Body.Close()
if resp.StatusCode != http.StatusOK {
cancel()
<-errCh
t.Fatalf("login status=%d", resp.StatusCode)
}
me, err := client.Get(base + "/api/admin/me")
if err != nil {
cancel()
<-errCh
t.Fatal(err)
}
_ = me.Body.Close()
if me.StatusCode != http.StatusOK {
cancel()
<-errCh
t.Fatalf("me before set-password status=%d", me.StatusCode)
}
if err := cmdAdminSetPassword([]string{"--password", "long-enough-password"}); err != nil {
cancel()
<-errCh
t.Fatal(err)
}
me2, err := client.Get(base + "/api/admin/me")
if err != nil {
cancel()
<-errCh
t.Fatal(err)
}
body, _ := io.ReadAll(me2.Body)
_ = me2.Body.Close()
cancel()
<-errCh
if me2.StatusCode != http.StatusUnauthorized {
t.Fatalf("want 401 after set-password, got %d body=%s", me2.StatusCode, body)
}
}
func TestHealthcheck(t *testing.T) {
dir := t.TempDir()
cfgPath := writeTestConfig(t, dir)
@@ -151,3 +242,48 @@ func TestParseSetPasswordArgs(t *testing.T) {
t.Fatalf("pass=%q err=%v", pass, err)
}
}
func TestCmdVersionPrintsInjected(t *testing.T) {
old := Version
Version = "d05-injected"
defer func() { Version = old }()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
oldOut := os.Stdout
os.Stdout = w
cmdVersion(nil)
_ = w.Close()
os.Stdout = oldOut
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
_ = r.Close()
if !strings.Contains(buf.String(), "d05-injected") {
t.Fatalf("version output=%q", buf.String())
}
}
func TestGoBuildInjectsVersion(t *testing.T) {
_, thisFile, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller")
}
pkgDir := filepath.Dir(thisFile)
out := filepath.Join(t.TempDir(), "nixmsg-d05-version.exe")
build := exec.Command("go", "build", "-ldflags", "-X main.Version=d05-ldflags", "-o", out)
build.Dir = pkgDir
build.Env = append(os.Environ(), "CGO_ENABLED=0")
if b, err := build.CombinedOutput(); err != nil {
t.Fatalf("go build: %v\n%s", err, b)
}
run := exec.Command(out, "version")
got, err := run.CombinedOutput()
if err != nil {
t.Fatalf("version: %v\n%s", err, got)
}
if !strings.Contains(string(got), "d05-ldflags") {
t.Fatalf("version output=%q", got)
}
}