Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion tests/common/test_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extern "C" {
// getKeyshortcut(TEST_P)/getKeyshortcutFromKeymap/fileExists/getFilePath/
// fileIsWritable/fileIsHome/dropShadow(两实现)/detectEncode(6 场景)/
// getEncode(3 场景)/ease*(TEST_P 边界)/getThemeMapFromPath(3 场景)/
// isMimeTypeSupport(4 场景)/isDraftFile/isBackupFile/cleanPath(TEST_P)/
// isMimeTypeSupport(8 场景)/isDraftFile/isBackupFile/cleanPath(TEST_P)/
// localDataPath/getEncodeList/renderSVG(2)/getHiglightColorList/
// clearChildrenFocus/clearChildrenFoucusEx/setChildrenFocus/
// getProcessCountByName(3)/killProcessByName(2)/getStringMD5Hash/
Expand Down Expand Up @@ -675,6 +675,69 @@ TEST_F(UtilsTest, IsMimeTypeSupport_EmptyFile_ReturnsTrue)
EXPECT_EQ(QFileInfo(path).size(), 0); // 空文件(0 字节)走 x-empty/x-zerosize 白名单
}

// --- 扩展名回退分支(commit 0a0fdfe2, PMS BUG-383607)----------------------
// 内容探测(MatchContent)无法识别为文本时,回退到 MatchExtension 检查扩展名;
// 若扩展名 MIME 以 text/ 开头或在白名单中则放行,否则拒绝。
// 前置:上述四条判定路径(text/ 前缀 / .pub 后缀 / 白名单 / inherits)全部失败。

// PMS: https://pms.uniontech.com/bug-view-383607.html commit: 0a0fdfe2
// 场景:git format-patch 生成的 .patch 文件内容探测为 application/mbox,
// 四条判定路径全部失败;扩展名探测为 text/x-patch(text/ 前缀)→ 放行
TEST_F(UtilsTest, IsMimeTypeSupport_PatchFile_ExtensionFallback_ReturnsTrue)
{
// Arrange:git format-patch 文件头,内容探测识别为 application/mbox
const QByteArray patchContent =
"From 1234567890abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001\n"
"From: test@example.com\n"
"Subject: [PATCH] fix example\n\n"
"---\n"
" a.txt | 1 +\n"
" 1 file changed, 1 insertion(+)\n\n"
"diff --git a/a.txt b/a.txt\n"
"--- a/a.txt\n"
"+++ b/a.txt\n"
"@@ -1 +1,2 @@\n"
" hello\n"
"+world\n";
const QString path = tempFile(QString::fromLatin1("fix.patch"), patchContent);

// Act
const bool result = Utils::isMimeTypeSupport(path);

// Assert:扩展名回退 → text/x-patch(text/ 前缀)→ 放行
EXPECT_TRUE(result);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Fallback regression goes unnoticed

When the Qt MIME database classifies the patch fixture's content as a supported MIME type, Utils::isMimeTypeSupport returns true at the content-MIME check before reaching the extension fallback, so EXPECT_TRUE(result) passes even when that fallback is broken and the test reports the behavior as covered.

Use a fixture whose content MIME is unsupported so the assertion can pass only through the extension fallback.

Prompt for AI agents
In `tests/common/test_utils.cpp` at line 708:

**Fallback regression goes unnoticed**

When the Qt MIME database classifies the patch fixture's content as a supported MIME type, `Utils::isMimeTypeSupport` returns true at the content-MIME check before reaching the extension fallback, so `EXPECT_TRUE(result)` passes even when that fallback is broken and the test reports the behavior as covered.

Use a fixture whose content MIME is unsupported so the assertion can pass only through the extension fallback.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Whitelist fallback remains untested

When an extension resolves to a whitelisted MIME type that does not start with text/, a regression that removes the SupportedTextMimeTypes alternative still passes all eight MIME tests, leaving fallback for whitelisted non-text/ extension MIME types broken.

Add a fallback test for an unsupported content MIME paired with a whitelisted, non-text/ extension MIME type.

Prompt for AI agents
In `tests/common/test_utils.cpp` at line 708:

**Whitelist fallback remains untested**

When an extension resolves to a whitelisted MIME type that does not start with `text/`, a regression that removes the `SupportedTextMimeTypes` alternative still passes all eight MIME tests, leaving fallback for whitelisted non-`text/` extension MIME types broken.

Add a fallback test for an unsupported content MIME paired with a whitelisted, non-`text/` extension MIME type.

}

// 扩展名回退守卫:扩展名 MIME 为默认类型(isDefault)时不放行
// 场景:PNG 内容 + 未知扩展名 .zzz → 扩展名 MIME = application/octet-stream(默认)
TEST_F(UtilsTest, IsMimeTypeSupport_BinaryUnknownExt_ExtensionDefaultGuarded_ReturnsFalse)
{
// Arrange:PNG 魔数 + 未知扩展名
const QString path = tempFile(QString::fromLatin1("binary.zzz"),
QByteArray::fromHex("89504e470d0a1a0a0000000d49484452"));

// Act
const bool result = Utils::isMimeTypeSupport(path);

// Assert:内容探测 image/png(非文本),扩展名 MIME 为默认 → 回退守卫拒绝
EXPECT_FALSE(result);
}

// 扩展名回退非文本分支:扩展名 MIME 非默认、但既非 text/ 前缀也不在白名单
// 场景:PNG 内容 + .png 扩展名 → 扩展名 MIME = image/png(非默认、非文本)
TEST_F(UtilsTest, IsMimeTypeSupport_PngFile_NonTextExtension_ReturnsFalse)
{
// Arrange:PNG 魔数 + .png 扩展名
const QString path = tempFile(QString::fromLatin1("image.png"),
QByteArray::fromHex("89504e470d0a1a0a0000000d49484452"));

// Act
const bool result = Utils::isMimeTypeSupport(path);

// Assert:扩展名 MIME = image/png(非默认),但非 text/ 前缀、不在白名单 → 拒绝
EXPECT_FALSE(result);
}

// ===========================================================================
// isDraftFile / isBackupFile / localDataPath(XDG_DATA_HOME 隔离)
// ===========================================================================
Expand Down
Loading