Skip to content

test: add unit tests for SecurityUtils, WakeupUtils, EnableUtils modules - #758

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:agent/9559afe28640-ut
Sep 16, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
add-uos:agent/9559afe28640-ut

Conversation

@add-uos

@add-uos add-uos commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Summary

为 deepin-devicemanager 项目补充 3 个模块的 GTest 单元测试,共 47 个用例,全部编译通过且测试全绿。

Test Coverage

模块 测试文件 用例数
SecurityUtils ut_securityutils.cpp 18
WakeupUtils ut_wakeuputils.cpp 14
EnableUtils ut_enableutils.cpp 15
合计 47

Test Results

  • 编译:✅ 通过
  • 测试运行:✅ 47/47 全部通过
  • 质量审查:✅ PASS 3/3,0 errors,0 warnings
  • 源码缺陷:0

Commits

  • 94907f9a test: add unit tests for securityutils module
  • 56b9ea78 test: add unit tests for wakeuputils module
  • d64079e3 test: add unit tests for enableutils module

Summary by Sourcery

Tests:

  • Add GTest unit coverage for SecurityUtils, WakeupUtils, and EnableUtils, covering validation, path handling, device metadata parsing, wakeup file operations, and network interface behavior.

@sourcery-ai

sourcery-ai Bot commented Sep 16, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds 47 GTest unit cases across SecurityUtils, WakeupUtils, and EnableUtils, using temporary filesystem fixtures and direct access to private helpers to validate normal, invalid, and boundary behaviors without changing production code.

File-Level Changes

Change Details Files
Added GTest coverage for module-name validation, path safety, and install-input validation.
  • Covers valid and invalid module-name formats, including traversal-like characters.
  • Exercises directory containment and sysfs path safety checks using temporary files/directories.
  • Verifies install validation returns errors for invalid or empty module names.
deepin-devicemanager-server/tests/src/SecurityUtils/ut_securityutils.cpp
Added GTest coverage for wakeup-path resolution, wakeup-file writes, device metadata parsing, and PS/2 lookup behavior.
  • Tests valid and malformed sysfs path inputs and expected constructed wakeup paths.
  • Uses temporary files to verify enabled and disabled wakeup values are written correctly.
  • Covers keyboard, mouse, unsupported device classes, short input, quote stripping, and missing PS/2 events.
deepin-devicemanager-server/tests/src/WakeControl/ut_wakeuputils.cpp
Added GTest coverage for USB VID/PID normalization, device metadata parsing, and network-interface enabling validation.
  • Tests hexadecimal normalization and invalid or incomplete VID/PID inputs.
  • Covers insufficient metadata, hub filtering, missing or present SysFS BusID, and quote stripping.
  • Checks empty, overlong, and nonexistent interface names are rejected.
deepin-devicemanager-server/tests/src/EnableControl/ut_enableutils.cpp

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

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="deepin-devicemanager-server/tests/src/SecurityUtils/ut_securityutils.cpp" line_range="193-196" />
<code_context>
+TEST_F(SecurityUtilsTest, isSafeSysfsPath_validDevicesPath_returnsTrue)
+{
+    // Arrange
+    QString path = "/devices/pci0000:00/0000:00:01.0";
+
+    // Act
+    bool result = isSafeSysfsPath(path);
+
+    // Assert
</code_context>
<issue_to_address>
**issue (testing):** The test uses the hard-coded `/sys/devices/pci0000:00/0000:00:01.0` path, but `isSafeSysfsPath` requires the canonical path to exist. On hosts without that exact PCI device, the function returns false and the test fails despite the input being intended as a valid case.

**Triggers:** When the test runs on a machine whose sysfs topology does not contain `/sys/devices/pci0000:00/0000:00:01.0`.

**Suggested fix:** Create or select an actually existing sysfs path at runtime, or mock the filesystem-dependent validation before asserting the positive result.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +193 to +196
QString path = "/devices/pci0000:00/0000:00:01.0";

// Act
bool result = isSafeSysfsPath(path);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (testing): The test uses the hard-coded /sys/devices/pci0000:00/0000:00:01.0 path, but isSafeSysfsPath requires the canonical path to exist. On hosts without that exact PCI device, the function returns false and the test fails despite the input being intended as a valid case.

Triggers: When the test runs on a machine whose sysfs topology does not contain /sys/devices/pci0000:00/0000:00:01.0.

Suggested fix: Create or select an actually existing sysfs path at runtime, or mock the filesystem-dependent validation before asserting the positive result.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

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

…bleUtils modules

- Add ut_securityutils.cpp with 18 test cases covering SecurityUtils functions
- Add ut_wakeuputils.cpp with 14 test cases covering WakeupUtils functions
- Add ut_enableutils.cpp with 15 test cases covering EnableUtils functions

测试(单测): 新增 SecurityUtils、WakeupUtils 和 EnableUtils 模块单元测试

- 新增 ut_securityutils.cpp,包含 18 个测试用例,覆盖 SecurityUtils 各功能函数
- 新增 ut_wakeuputils.cpp,包含 14 个测试用例,覆盖 WakeupUtils 各功能函数
- 新增 ut_enableutils.cpp,包含 15 个测试用例,覆盖 EnableUtils 各功能函数

Log: 为 deepin-devicemanager 的 SecurityUtils、WakeupUtils、EnableUtils 三个工具模块补充单元测试,共新增 47 个测试用例
@add-uos
add-uos force-pushed the agent/9559afe28640-ut branch from d64079e to e8fd8d9 Compare September 16, 2026 06:04
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 99 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 未发现安全问题,代码质量优秀。本次提交为3个模块(SecurityUtils、WakeupUtils、EnableUtils)补充了47个GTest单元测试用例,测试覆盖了输入验证、路径安全校验、设备元数据解析、唤醒文件操作和网络接口行为等场景。代码结构清晰,注释完整,边界条件处理完善。

📋 漏洞对比统计

项目 数量
新增漏洞 0
减少漏洞 0
持平漏洞 0
当前漏洞总数 0

SAST 扫描结果:无(未提供)
OCR 审查结果:无(OCR 未能执行,PR 分支未在克隆代码中检出)
AI 审计结果:0 个安全漏洞


🔍 详细分析

1. 语法逻辑 ✅ (25/25 分)

评价: 语法正确,逻辑清晰 ✅ 通过

分析:

  • GTest 框架使用正确(TEST_F, EXPECT_TRUE, EXPECT_FALSE, EXPECT_EQ, EXPECT_NE, ASSERT_TRUE)
  • #define private public / #define protected public 配合 #undef 正确使用,作用域清晰
  • AAA (Arrange-Act-Assert) 模式一致使用
  • 边界条件处理完善(空输入、过长输入、非法字符、路径穿越等)
  • 无编译错误,无逻辑缺陷

潜在问题:
✅ 未发现明显问题


2. 代码质量 ✅ (24/25 分)

评价: 代码结构清晰,注释完整 ✅ 通过

分析:

  • 注释完整性 (5/5): AAA 模式注释、分节注释(如 // validateAndNormalizeVidPid)、SPDX 许可证头
  • 代码重复 (4/5): getMapInfo 测试中 12 行 QStringList 构建模式重复约 9 次
  • 结构合理性 (5/5): 测试组织清晰,每个测试聚焦单一行为,测试夹具简洁
  • 调试信息清理 (5/5): 无残留调试代码,无敏感注释

潜在问题:

  1. ut_enableutils.cpput_wakeuputils.cppgetMapInfo 测试的 12 行 QStringList 构建模式重复多次,建议提取为辅助方法以提高可维护性

改进建议:

// 可提取为辅助函数,减少重复代码
static QStringList createTestLines(const QString &hardwareClass)
{
    QStringList lines;
    for (int i = 0; i < 12; ++i)
        lines << QString("Key%1: Value%1").arg(i);
    lines[0] = "Hardware Class: " + hardwareClass;
    return lines;
}

3. 代码性能 ✅ (20/20 分)

评价: 性能良好,资源使用合理 ✅ 通过

分析:

  • 单元测试代码,性能非关键关注点
  • QTemporaryDir 自动清理临时文件,无资源泄漏
  • 测试设置简洁,无不必要的操作
  • 无性能瓶颈

潜在问题:
✅ 未发现明显问题


4. 代码安全 🔒 (30/30 分)

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

分析:

  • 测试代码,非生产代码,无安全风险
  • #define private public 是 C++ 单元测试常用技术,仅影响测试编译,不进入生产环境
  • 无硬编码凭证、密钥或敏感信息
  • 无命令注入风险
  • 无路径穿越风险(测试代码本身验证路径穿越防护)
  • 无缓冲区溢出风险

安全漏洞详情:
✅ 未发现安全漏洞

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个


📝 审查总结

本次 PR 为 deepin-devicemanager 项目的 3 个模块补充了 GTest 单元测试:

模块 测试文件 用例数 覆盖函数
SecurityUtils ut_securityutils.cpp 18 isValidModName, isPathWithinDirectory, isSafeSysfsPath, validateModNameForInstall
WakeupUtils ut_wakeuputils.cpp 14 wakeupPath, writeWakeupFile, getMapInfo, getPS2Syspath
EnableUtils ut_enableutils.cpp 15 validateAndNormalizeVidPid, getMapInfo, enableNetworkByIoctl
合计 47

优点:

  • 测试覆盖全面,包含正常路径、边界条件和异常输入
  • AAA 模式注释清晰,测试名称自文档化
  • 安全相关函数(路径穿越防护、模块名校验)测试充分
  • 使用 QTemporaryDir 确保测试资源自动清理

改进建议:

  • getMapInfo 测试中重复的 QStringList 构建逻辑可提取为辅助方法

本报告由 AI 代码审查工具自动生成

@add-uos

add-uos commented Sep 16, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit 8bfa6a6 into linuxdeepin:master Sep 16, 2026
17 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