From 272eb26f15bbc2ed4552aa17d88d3e2e8610bd59 Mon Sep 17 00:00:00 2001 From: fuleyi Date: Wed, 19 Aug 2026 16:34:08 +0800 Subject: [PATCH] fix: make getProcessStartTime mockable for unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Add processStartTime function field to allowCallerRegistry struct. 2. Default to getProcessStartTime in newAllowCallerRegistryWithConfig. 3. Replace direct getProcessStartTime calls with r.processStartTime. 4. Set mock processStartTime in tests that use non-existent PIDs. Log: Test-only fix; no functional change Influence: 1. Verify all authorizeRegistrar and addCaller tests pass without /proc access. fix: 将 getProcessStartTime 改为可 mock 结构体字段,修复测试 1. 在 allowCallerRegistry 中添加 processStartTime 函数字段。 2. newAllowCallerRegistryWithConfig 默认赋值为 getProcessStartTime。 3. authorizeRegistrar 中两处调用改为 r.processStartTime。 4. 测试用例设置 mock 返回固定值,避免依赖 /proc。 Log: 仅测试修复,无功能变更 Influence: 1. 验证所有 authorizeRegistrar 和 addCaller 测试无需 /proc 即可通过。 --- locale-helper/allow_caller.go | 6 ++++-- locale-helper/allow_caller_test.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/locale-helper/allow_caller.go b/locale-helper/allow_caller.go index ddc3492..eadcdf8 100644 --- a/locale-helper/allow_caller.go +++ b/locale-helper/allow_caller.go @@ -73,6 +73,7 @@ type allowCallerRegistry struct { privilegedGroupID uint32 processGroups func(uint32) ([]uint32, error) processParent func(uint32) (uint32, error) + processStartTime func(uint32) (uint64, error) mu sync.RWMutex callers map[string]callerInfo @@ -128,6 +129,7 @@ func newAllowCallerRegistryWithConfig(service allowCallerBus, stateFile string, busID: busID, privilegedGroupID: privilegedGroupID, processGroups: getProcessGroups, + processStartTime: getProcessStartTime, processParent: getProcessParentPID, callers: make(map[string]callerInfo), }, nil @@ -336,7 +338,7 @@ func (r *allowCallerRegistry) authorizeRegistrar(sender dbus.Sender, uniqueName } // Capture starttime before reading /proc to detect PID reuse (TOCTOU mitigation). - senderStartTime, err := getProcessStartTime(senderPID) + senderStartTime, err := r.processStartTime(senderPID) if err != nil { return fmt.Errorf("get sender %s start time failed: %w", sender, err) } @@ -347,7 +349,7 @@ func (r *allowCallerRegistry) authorizeRegistrar(sender dbus.Sender, uniqueName } // Verify PID was not recycled during /proc access. - checkStartTime, err := getProcessStartTime(senderPID) + checkStartTime, err := r.processStartTime(senderPID) if err != nil || checkStartTime != senderStartTime { return fmt.Errorf("sender %s PID %d reused during authorization", sender, senderPID) } diff --git a/locale-helper/allow_caller_test.go b/locale-helper/allow_caller_test.go index cc8cebc..c414e6b 100644 --- a/locale-helper/allow_caller_test.go +++ b/locale-helper/allow_caller_test.go @@ -322,6 +322,9 @@ func TestAddCallerAuthorizedSender(t *testing.T) { r := newRegistryForTest(t, bus) r.privilegedGroupID = 42 + r.processStartTime = func(pid uint32) (uint64, error) { + return 12345, nil + } r.processGroups = func(pid uint32) ([]uint32, error) { return []uint32{42}, nil } @@ -604,6 +607,9 @@ func TestAuthorizeRegistrar(t *testing.T) { bus.connPID[":1.0"] = 5 r := newRegistryForTest(t, bus) r.privilegedGroupID = 42 + r.processStartTime = func(pid uint32) (uint64, error) { + return 12345, nil + } r.processGroups = func(pid uint32) ([]uint32, error) { return []uint32{99}, nil } @@ -619,6 +625,9 @@ func TestAuthorizeRegistrar(t *testing.T) { bus.connUID[":1.100"] = 2000 r := newRegistryForTest(t, bus) r.privilegedGroupID = 42 + r.processStartTime = func(pid uint32) (uint64, error) { + return 12345, nil + } r.processGroups = func(pid uint32) ([]uint32, error) { return []uint32{42}, nil } @@ -635,6 +644,9 @@ func TestAuthorizeRegistrar(t *testing.T) { bus.connPID[":1.100"] = 10 r := newRegistryForTest(t, bus) r.privilegedGroupID = 42 + r.processStartTime = func(pid uint32) (uint64, error) { + return 12345, nil + } r.processGroups = func(pid uint32) ([]uint32, error) { return []uint32{42}, nil } @@ -662,6 +674,9 @@ func TestAuthorizeRegistrar(t *testing.T) { bus.connPID[":1.100"] = 10 r := newRegistryForTest(t, bus) r.privilegedGroupID = 42 + r.processStartTime = func(pid uint32) (uint64, error) { + return 12345, nil + } r.processGroups = func(pid uint32) ([]uint32, error) { return []uint32{42}, nil }