Skip to content
Draft
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions tests/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,15 @@ add_ut_target(test_eventlog
QT_LIBS
DTK_LIBS
)

# ---------------------------------------------------------------------------
# test_encoding:detectCharset(ESC 序列解析 + 边界检查,commit 4a16a5d 回归)
# ---------------------------------------------------------------------------
set(ENCODING_SOURCES
${CMAKE_SOURCE_DIR}/src/common/encoding.cpp
)
add_ut_target(test_encoding
SOURCES test_encoding.cpp ${ENCODING_SOURCES}
QT_LIBS Qt${QT_VERSION}::Core
DTK_LIBS
)
205 changes: 205 additions & 0 deletions tests/common/test_encoding.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

#include <gtest/gtest.h>

Check warning on line 4 in tests/common/test_encoding.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QByteArray>

Check warning on line 5 in tests/common/test_encoding.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QByteArray> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QString>

Check warning on line 6 in tests/common/test_encoding.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QString> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "encoding.h"

Check warning on line 8 in tests/common/test_encoding.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "encoding.h" not found.

// ---------------------------------------------------------------------------
// 分支清单(来源:src/common/encoding.cpp — detectCharset)
//
// UTF-8 校验通过 (validateUTF8 == true) 后的分支:
// B1: 字节 > 0x7F → charset = "UTF-8"
// B2: ESC $ B (JIS X 0208-1983) → charset = "ISO-2022-JP" (continue)
// B3: ESC $ @ (JIS X 0208-1978) → charset = "ISO-2022-JP" (continue)
// B4: ESC $ A (GB2312-1980) → charset = "ISO-2022-JP-2" (break)
// B5: ESC $ ( C (KSC5601-1987) → charset = "ISO-2022-JP-2"
// B6: ESC $ ( D (JIS X 0212-1990) → charset = "ISO-2022-JP-2"
// B7: ESC $ ) C (KSC5601-1987) → charset = "ISO-2022-KR"
// B8: ESC 后跟非 '$' → 继续扫描,charset 保持空
// B9: ESC $ 后跟未知字符 → break,charset 保持空
// B10: ESC $ ( 后跟未知字符 → break,charset 保持空
// B11: ESC $ ) 后跟非 'C' → charset 保持空
// B12: NUL 字节终止扫描 → charset 保持空
// B13: 纯 ASCII(无高字节、无 ESC) → charset 保持空
// B14: charset 为空 → getDefaultCharset() → 兜底(UTF-8 环境 → "UTF-8")
//
// 边界条件(commit 4a16a5d 修复的越界读):
// E1: 空数组 → 不崩溃,返回 getDefaultCharset()
// E2: 仅 ESC(pos >= size 后 break) → 不崩溃
// E3: ESC $(pos >= size 后 break) → 不崩溃
// E4: ESC $ ((pos >= size 后 break) → 不崩溃
// E5: ESC $ )(pos >= size 后 break) → 不崩溃
//
// 用例映射见各 TEST 名称。
// ---------------------------------------------------------------------------

class DetectCharsetTest : public ::testing::Test
{
protected:
// 在 zh_CN.UTF-8 环境下 getDefaultCharset() 返回 "UTF-8"
QString defaultCharset() const
{
return QStringLiteral("UTF-8");
}
};

// ---- B1: UTF-8 多字节字符 → "UTF-8" ----
TEST_F(DetectCharsetTest, ValidUtf8_HighByte_ReturnsUtf8)
{
// "é" in UTF-8: 0xC3 0xA9
QByteArray input("\xC3\xA9");
EXPECT_EQ(detectCharset(input), QStringLiteral("UTF-8"));
}

// ---- B1: 3 字节 UTF-8 序列 → "UTF-8" ----
TEST_F(DetectCharsetTest, ValidUtf8_ThreeByteSequence_ReturnsUtf8)
{
// "中" in UTF-8: 0xE4 0xB8 0xAD
QByteArray input("\xE4\xB8\xAD");
EXPECT_EQ(detectCharset(input), QStringLiteral("UTF-8"));
}

// ---- B2: ESC $ B → ISO-2022-JP ----
TEST_F(DetectCharsetTest, EscDollarB_ReturnsIso2022Jp)
{
QByteArray input("\x1B$B", 3);
EXPECT_EQ(detectCharset(input), QStringLiteral("ISO-2022-JP"));
}

// ---- B3: ESC $ @ → ISO-2022-JP ----
TEST_F(DetectCharsetTest, EscDollarAt_ReturnsIso2022Jp)
{
QByteArray input("\x1B$@", 3);
EXPECT_EQ(detectCharset(input), QStringLiteral("ISO-2022-JP"));
}

// ---- B4: ESC $ A → ISO-2022-JP-2 ----
TEST_F(DetectCharsetTest, EscDollarA_ReturnsIso2022Jp2)
{
QByteArray input("\x1B$A", 3);
EXPECT_EQ(detectCharset(input), QStringLiteral("ISO-2022-JP-2"));
}

// ---- B5: ESC $ ( C → ISO-2022-JP-2 ----
TEST_F(DetectCharsetTest, EscDollarParenC_ReturnsIso2022Jp2)
{
QByteArray input("\x1B$(C", 4);
EXPECT_EQ(detectCharset(input), QStringLiteral("ISO-2022-JP-2"));
}

// ---- B6: ESC $ ( D → ISO-2022-JP-2 ----
TEST_F(DetectCharsetTest, EscDollarParenD_ReturnsIso2022Jp2)
{
QByteArray input("\x1B$(D", 4);
EXPECT_EQ(detectCharset(input), QStringLiteral("ISO-2022-JP-2"));
}

// ---- B7: ESC $ ) C → ISO-2022-KR ----
TEST_F(DetectCharsetTest, EscDollarParenCloseC_ReturnsIso2022Kr)
{
QByteArray input("\x1B$)C", 4);
EXPECT_EQ(detectCharset(input), QStringLiteral("ISO-2022-KR"));
}

// ---- B8: ESC 后跟非 '$' → 继续扫描 → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EscFollowedByNonDollar_ContinuesScanning)
{
QByteArray input("\x1B ""text", 5);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- B9: ESC $ 后跟未知字符 'X' → break → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EscDollarUnknownChar_ReturnsDefault)
{
QByteArray input("\x1B$X", 3);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- B10: ESC $ ( 后跟未知字符 'X' → break → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EscDollarParenUnknownChar_ReturnsDefault)
{
QByteArray input("\x1B$(X", 4);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- B11: ESC $ ) 后跟非 'C' → charset 保持空 → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EscDollarParenCloseNonC_ReturnsDefault)
{
QByteArray input("\x1B$)X", 4);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- B12: NUL 字节终止扫描 → getDefaultCharset() ----
TEST_F(DetectCharsetTest, NulByte_TerminatesScanning)
{
QByteArray input("AB\x00" "CD", 5);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- B13: 纯 ASCII(无高字节、无 ESC) → getDefaultCharset() ----
TEST_F(DetectCharsetTest, PureAscii_ReturnsDefault)
{
QByteArray input("Hello World");
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- E1: 空数组 → 不崩溃 → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EmptyArray_DoesNotCrash)
{
QByteArray input;
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- E2: 仅 ESC → 不崩溃 → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EscOnly_DoesNotCrash)
{
QByteArray input("\x1B", 1);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- E3: ESC $ → 不崩溃 → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EscDollar_DoesNotCrash)
{
QByteArray input("\x1B$", 2);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- E4: ESC $ ( → 不崩溃 → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EscDollarParen_DoesNotCrash)
{
QByteArray input("\x1B$(", 3);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- E5: ESC $ ) → 不崩溃 → getDefaultCharset() ----
TEST_F(DetectCharsetTest, EscDollarParenClose_DoesNotCrash)
{
QByteArray input("\x1B$)", 3);
EXPECT_EQ(detectCharset(input), defaultCharset());
}

// ---- 回归:ESC $ B 后跟更多 ASCII → 仍为 ISO-2022-JP(continue 后无高字节)----
TEST_F(DetectCharsetTest, EscDollarB_FollowedByAscii_StillIso2022Jp)
{
QByteArray input("\x1B$B""text", 7);
EXPECT_EQ(detectCharset(input), QStringLiteral("ISO-2022-JP"));
}

// ---- 回归:ESC $ B 后跟高字节 → 覆盖为 UTF-8 ----
TEST_F(DetectCharsetTest, EscDollarB_FollowedByHighByte_OverriddenToUtf8)
{
QByteArray input("\x1B$B\xC3\xA9", 5);
EXPECT_EQ(detectCharset(input), QStringLiteral("UTF-8"));
}

// ---- 回归:无效 UTF-8 输入 → 不崩溃,返回非空字符串 ----
TEST_F(DetectCharsetTest, InvalidUtf8_DoesNotCrash_ReturnsNonEmpty)
{
// 0xFF 是无效 UTF-8 起始字节
QByteArray input("\xFF\xFE");
QString result = detectCharset(input);
EXPECT_FALSE(result.isEmpty());
}
Loading