From 0f4013ed8a1dace8caaefff6a90467296264da4c Mon Sep 17 00:00:00 2001 From: smail Date: Sun, 18 Oct 2026 10:36:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(test):=20=E4=BF=AE=E5=A4=8DLinux=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=B8=ADUnix=E5=9F=9F=E5=A5=97=E6=8E=A5=E5=AD=97?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E8=B6=85=E5=87=BA108=E5=AD=97=E8=8A=82?= =?UTF-8?q?=E9=99=90=E5=88=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reset_integration_linux_test.go | 15 +++++++++- backend/services/adminreset/transport_test.go | 30 +++++++++++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/backend/services/adminreset/reset_integration_linux_test.go b/backend/services/adminreset/reset_integration_linux_test.go index 12455d5e..aca8e42a 100644 --- a/backend/services/adminreset/reset_integration_linux_test.go +++ b/backend/services/adminreset/reset_integration_linux_test.go @@ -108,6 +108,19 @@ func assertResetUserUnchanged(t *testing.T, engine *xorm.Engine, original models assertResetCredentialState(t, engine, original.ID, false) } +func testIntegrationControlSocketDir(t *testing.T) string { + t.Helper() + dir := t.TempDir() + if len(dir)+len("/control.sock") >= 90 { + shortDir, err := os.MkdirTemp("/tmp", "smail-*") + if err == nil { + t.Cleanup(func() { _ = os.RemoveAll(shortDir) }) + return shortDir + } + } + return dir +} + func TestUnixResetCommitsPasswordAndRevokesAllProtocolCodes(t *testing.T) { engine, users := resetIntegrationDatabase(t) service, err := credentials.NewService(engine) @@ -122,7 +135,7 @@ func TestUnixResetCommitsPasswordAndRevokesAllProtocolCodes(t *testing.T) { t.Fatalf("重置前协议 %s 的客户端码不可用:%v", protocol, err) } } - path := filepath.Join(t.TempDir(), "control.sock") + path := filepath.Join(testIntegrationControlSocketDir(t), "control.sock") // 隔离夹具仅信任测试进程的 UID;不改变生产入口只信任 root 的配置。 server, err := ListenUnix(path, []uint32{uint32(os.Geteuid())}) if err != nil { diff --git a/backend/services/adminreset/transport_test.go b/backend/services/adminreset/transport_test.go index 28e20cae..0644cfa4 100644 --- a/backend/services/adminreset/transport_test.go +++ b/backend/services/adminreset/transport_test.go @@ -10,11 +10,29 @@ import ( "time" ) +func testControlSocketDir(t *testing.T) string { + t.Helper() + dir := t.TempDir() + if len(dir)+len("/control.sock") >= 90 { + shortDir, err := os.MkdirTemp("/tmp", "smail-*") + if err == nil { + t.Cleanup(func() { _ = os.RemoveAll(shortDir) }) + return shortDir + } + } + return dir +} + +func testControlSocketPath(t *testing.T) string { + t.Helper() + return testControlSocketDir(t) + "/control.sock" +} + func TestUnixResetRoundTrip(t *testing.T) { if DefaultControlPath == "" { t.Skip("unsupported platform") } - path := t.TempDir() + "/control.sock" + path := testControlSocketPath(t) server, err := ListenUnix(path, []uint32{uint32(os.Getuid())}) if err != nil { t.Fatal(err) @@ -53,7 +71,7 @@ func TestUnixResetRoundTrip(t *testing.T) { } func TestUnixListenDoesNotReplaceActiveSocket(t *testing.T) { - path := t.TempDir() + "/control.sock" + path := testControlSocketPath(t) server, err := ListenUnix(path, []uint32{uint32(os.Getuid())}) if err != nil { t.Fatal(err) @@ -68,7 +86,7 @@ func TestUnixListenDoesNotReplaceActiveSocket(t *testing.T) { } func TestUnixEmptyAllowedUIDsRejectConnection(t *testing.T) { - path := t.TempDir() + "/control.sock" + path := testControlSocketPath(t) server, err := ListenUnix(path, nil) if err != nil { t.Fatal(err) @@ -98,7 +116,7 @@ func TestUnixEmptyAllowedUIDsRejectConnection(t *testing.T) { } func TestUnixListenRejectsWritableControlDirectory(t *testing.T) { - parent := t.TempDir() + "/control" + parent := testControlSocketDir(t) + "/control" if err := os.Mkdir(parent, 0700); err != nil { t.Fatal(err) } @@ -111,7 +129,7 @@ func TestUnixListenRejectsWritableControlDirectory(t *testing.T) { } func TestUnixControlSocketUsesRestrictedPermissions(t *testing.T) { - parent := t.TempDir() + "/control" + parent := testControlSocketDir(t) + "/control" path := parent + "/control.sock" server, err := ListenUnix(path, []uint32{uint32(os.Getuid())}) if err != nil { @@ -136,7 +154,7 @@ func TestUnixControlSocketUsesRestrictedPermissions(t *testing.T) { } func TestUnixServerStopsWhenContextIsCancelled(t *testing.T) { - path := t.TempDir() + "/control.sock" + path := testControlSocketPath(t) server, err := ListenUnix(path, []uint32{uint32(os.Getuid())}) if err != nil { t.Fatal(err)