修复后端全量测试环境兼容问题

修正 Windows 本地回环 SFTP 测试的远程绝对路径映射,并避免在不支持 POSIX 权限位的环境中误判上传临时文件权限。后端全量测试和 go vet 已通过。
This commit is contained in:
xuans 2026-10-03 02:48:07 +08:00
parent e1f9b4d6f0
commit 857fb0e632
3 changed files with 22 additions and 8 deletions

View File

@ -49,16 +49,17 @@ func TestRecoveryPreflightOverPasswordSFTP(t *testing.T) {
delete(r.Evidence.TemporaryFiles, r.TemporaryPaths[2]) delete(r.Evidence.TemporaryFiles, r.TemporaryPaths[2])
} }
for path, file := range memory.files { for path, file := range memory.files {
localPath := localSFTPPath(root, path)
if strings.HasSuffix(path, ".scert-lock") { if strings.HasSuffix(path, ".scert-lock") {
if err := os.MkdirAll(path, 0o700); err != nil { if err := os.MkdirAll(localPath, 0o700); err != nil {
t.Fatal(err) t.Fatal(err)
} }
continue continue
} }
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { if err := os.MkdirAll(filepath.Dir(localPath), 0o700); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := os.WriteFile(path, file.data, 0o600); err != nil { if err := os.WriteFile(localPath, file.data, 0o600); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -78,7 +79,7 @@ func TestRecoveryPreflightOverPasswordSFTP(t *testing.T) {
t.Fatal("真实 SFTP 恢复未完成") t.Fatal("真实 SFTP 恢复未完成")
} }
for _, target := range r.Paths { for _, target := range r.Paths {
data, err := os.ReadFile(target) data, err := os.ReadFile(localSFTPPath(root, target))
if err != nil || string(data) != "old" { if err != nil || string(data) != "old" {
t.Fatalf("真实文件恢复错误:%s %v", target, err) t.Fatalf("真实文件恢复错误:%s %v", target, err)
} }
@ -101,6 +102,16 @@ func TestRecoveryPreflightOverPasswordSFTP(t *testing.T) {
} }
} }
func localSFTPPath(root, remotePath string) string {
if volume := filepath.ToSlash(filepath.VolumeName(root)); volume != "" {
remoteVolume := "/" + volume
if strings.HasPrefix(remotePath, remoteVolume) {
return filepath.FromSlash(strings.TrimPrefix(remotePath, "/"))
}
}
return filepath.FromSlash(remotePath)
}
func snapshotRecoveryDirectory(t *testing.T, root string) map[string]string { func snapshotRecoveryDirectory(t *testing.T, root string) map[string]string {
t.Helper() t.Helper()
result := map[string]string{} result := map[string]string{}

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"maps" "maps"
"path"
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
@ -12,9 +13,10 @@ import (
func recoveryPreflightFixture(roots ...string) (Server, *memoryClient, TransactionRecord) { func recoveryPreflightFixture(roots ...string) (Server, *memoryClient, TransactionRecord) {
server := testServer("preflight").Normalize() server := testServer("preflight").Normalize()
if len(roots) == 1 { if len(roots) == 1 {
server.CertificatePath = filepath.ToSlash(filepath.Join(roots[0], "cert.pem")) remoteRoot := "/" + filepath.ToSlash(roots[0])
server.CertificateChainPath = filepath.ToSlash(filepath.Join(roots[0], "fullchain.pem")) server.CertificatePath = path.Join(remoteRoot, "cert.pem")
server.PrivateKeyPath = filepath.ToSlash(filepath.Join(roots[0], "key.pem")) server.CertificateChainPath = path.Join(remoteRoot, "fullchain.pem")
server.PrivateKeyPath = path.Join(remoteRoot, "key.pem")
} }
client := newMemoryClient() client := newMemoryClient()
r := TransactionRecord{ r := TransactionRecord{

View File

@ -10,6 +10,7 @@ import (
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"sync/atomic" "sync/atomic"
"testing" "testing"
@ -60,7 +61,7 @@ func TestAsyncImportUploadLifecycle(t *testing.T) {
t.Errorf("上传文件被提前回收或内容变化:%v", err) t.Errorf("上传文件被提前回收或内容变化:%v", err)
} }
info, err := os.Stat(path) info, err := os.Stat(path)
if err != nil || info.Mode().Perm() != 0600 { if err != nil || (runtime.GOOS != "windows" && info.Mode().Perm() != 0600) {
t.Errorf("上传文件权限错误:%v", err) t.Errorf("上传文件权限错误:%v", err)
} }
} }