diff --git a/tests/common/test_utils.cpp b/tests/common/test_utils.cpp index 5d6de925..2529476f 100644 --- a/tests/common/test_utils.cpp +++ b/tests/common/test_utils.cpp @@ -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/ @@ -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); +} + +// 扩展名回退守卫:扩展名 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 隔离) // ===========================================================================