Skip to content

fix: make getProcessStartTime mockable for unit tests - #202

Merged
fly602 merged 1 commit into
linuxdeepin:masterfrom
fly602:master
Aug 19, 2026
Merged

fix: make getProcessStartTime mockable for unit tests#202
fly602 merged 1 commit into
linuxdeepin:masterfrom
fly602:master

Conversation

@fly602

@fly602 fly602 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor
  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 即可通过。

Summary by Sourcery

Decouple process start-time checks from direct system access to make unit tests reliable without changing production behavior.

Bug Fixes:

  • Make process start-time lookups mockable so authorization and caller tests do not depend on /proc access.

Enhancements:

  • Route process start-time checks through the registry while preserving the existing production default.

Tests:

  • Use fixed mocked process start times in tests involving non-existent or synthetic PIDs.

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 即可通过。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @fly602, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@sourcery-ai

sourcery-ai Bot commented Aug 19, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR makes the process start time lookup in allowCallerRegistry injectable so tests can run without accessing /proc, by adding a processStartTime function field to the registry, wiring it to getProcessStartTime in production, and overriding it with a fixed mock in unit tests for authorizeRegistrar and addCaller.

File-Level Changes

Change Details Files
Make process start time retrieval injectable on allowCallerRegistry and used via the registry instance.
  • Add a processStartTime function field to allowCallerRegistry to abstract process start time retrieval.
  • Initialize processStartTime to getProcessStartTime in newAllowCallerRegistryWithConfig so production behavior is unchanged.
  • Replace direct calls to getProcessStartTime in authorizeRegistrar with calls to r.processStartTime for both the initial capture and the TOCTOU verification path.
locale-helper/allow_caller.go
Update unit tests to inject a mock processStartTime so they no longer depend on real /proc entries.
  • In addCaller-related tests, set r.processStartTime to a stub that returns a fixed start time and no error.
  • In authorizeRegistrar-related tests, inject the same fixed processStartTime stub across various scenarios that previously relied on real PIDs.
  • Ensure tests for authorizeRegistrar and addCaller pass even when /proc is unavailable or the PIDs do not exist.
locale-helper/allow_caller_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:95分

■ 【总体评价】

代码成功实现了依赖注入重构,解决了单元测试难以Mock的问题,且未破坏原有的TOCTOU防护逻辑
逻辑完全正确且无安全漏洞,因测试代码存在轻微重复扣5分

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

结构体新增的 processStartTime 字段类型为 func(uint32) (uint64, error),与原全局函数 getProcessStartTime 签名完全一致。在 newAllowCallerRegistryWithConfig 中的赋值以及在 authorizeRegistrar 中的调用均符合 Go 语言的函数变量调用规范,未引入空指针或类型不匹配等语法逻辑错误。

  • 2.代码质量(良好)✓

使用依赖注入模式将系统级调用抽象为结构体成员,极大提升了 allowCallerRegistry 的可测试性,符合 Go 语言中处理外部依赖的最佳实践。生产代码中默认注入真实实现,测试代码中注入 Mock,职责分离清晰。
潜在问题:测试文件 allow_caller_test.go 中存在5处完全相同的 processStartTime Mock 函数定义,存在代码冗余。
建议:在测试文件中提取一个公共的 Mock 辅助函数(如 mockProcessStartTime),在各个测试用例中复用,减少重复代码。

  • 3.代码性能(高效)✓

将直接的全局函数调用替换为结构体函数指针调用,在 Go 语言中仅增加极其微小的寻址开销(纳秒级),对整体性能无任何可观测影响。TOCTOU 防护逻辑中两次调用该函数的时机保持不变,性能表现与重构前一致。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次重构仅改变了获取进程启动时间的调用方式,未修改 TOCTOU(Time-of-Check to Time-of-Use)防御逻辑的核心语义。在生产环境中,processStartTime 依然被正确绑定为 getProcessStartTime,保证了在读写 /proc 目录前后校验 PID 是否被回收的安全机制完整有效。测试环境中的硬编码返回值仅限于测试进程内,不存在安全风险。

■ 【改进建议代码示例】

// locale-helper/allow_caller_test.go

// mockProcessStartTime 提取公共的 Mock 函数,避免测试代码重复
func mockProcessStartTime(pid uint32) (uint64, error) {
	return 12345, nil
}

func TestAddCallerAuthorizedSender(t *testing.T) {
	// ... 前置代码 ...
	r := newRegistryForTest(t, bus)
	r.privilegedGroupID = 42
	r.processStartTime = mockProcessStartTime // 使用提取后的公共函数
	r.processGroups = func(pid uint32) ([]uint32, error) {
		return []uint32{42}, nil
	}
	// ... 后续逻辑 ...
}

func TestAuthorizeRegistrar(t *testing.T) {
	// ... 在各个子测试中同样使用 r.processStartTime = mockProcessStartTime 替换原有的内联匿名函数 ...
}

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602, mhduiy

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@fly602
fly602 merged commit df82195 into linuxdeepin:master Aug 19, 2026
28 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants