fix(test): 修复Linux测试中Unix域套接字路径超出108字节限制问题
Some checks are pending
Security Checks / security (push) Waiting to run

This commit is contained in:
smail 2026-10-18 10:36:54 +08:00
parent 2a8b824969
commit 0f4013ed8a
2 changed files with 38 additions and 7 deletions

View File

@ -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 {

View File

@ -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)