From 94c22e6ae02cd29418790d44d8161cf474888141 Mon Sep 17 00:00:00 2001 From: Kengo Ide Date: Wed, 17 Mar 2021 13:40:50 +0900 Subject: [PATCH] =?UTF-8?q?CCodeBase::MIMEHeaderDecode=20=E3=81=AE?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/charset/CCodeBase.h | 4 +- tests/unittests/test-ccodebase.cpp | 66 ++++++++++++++++++++++++++ tests/unittests/tests1.vcxproj | 1 + tests/unittests/tests1.vcxproj.filters | 3 ++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 tests/unittests/test-ccodebase.cpp diff --git a/sakura_core/charset/CCodeBase.h b/sakura_core/charset/CCodeBase.h index c9ad351315..1c244a6b95 100644 --- a/sakura_core/charset/CCodeBase.h +++ b/sakura_core/charset/CCodeBase.h @@ -27,6 +27,8 @@ #define SAKURA_CCODEBASE_1AB194FB_933C_495E_A3A3_62E117C72644_H_ #pragma once +#include "mem/CNativeW.h" + //定数 enum EConvertResult{ RESULT_COMPLETE, //!< データを失うことなく変換が完了した。 @@ -34,8 +36,6 @@ enum EConvertResult{ RESULT_FAILURE, //!< 何らかの原因により失敗した。 }; -class CMemory; -class CNativeW; struct CommonSetting_Statusbar; enum EEolType : char; diff --git a/tests/unittests/test-ccodebase.cpp b/tests/unittests/test-ccodebase.cpp new file mode 100644 index 0000000000..5fe4c60ca9 --- /dev/null +++ b/tests/unittests/test-ccodebase.cpp @@ -0,0 +1,66 @@ +/* + Copyright (C) 2021, Sakura Editor Organization + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; + you must not claim that you wrote the original software. + If you use this software in a product, an acknowledgment + in the product documentation would be appreciated but is + not required. + + 2. Altered source versions must be plainly marked as such, + and must not be misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +*/ + +#include +#include +#include "charset/CCodeBase.h" + +TEST(CCodeBase, MIMEHeaderDecode) +{ + CMemory m; + + // Base64 JIS + std::string source1("From: =?iso-2022-jp?B?GyRCJTUlLyVpGyhC?="); + EXPECT_TRUE(CCodeBase::MIMEHeaderDecode(source1.c_str(), source1.length(), &m, CODE_JIS)); + EXPECT_STREQ(static_cast(m.GetRawPtr()), "From: $B%5%/%i(B"); + + // Base64 UTF-8 + std::string source2("From: =?utf-8?B?44K144Kv44Op?="); + EXPECT_TRUE(CCodeBase::MIMEHeaderDecode(source2.c_str(), source2.length(), &m, CODE_UTF8)); + EXPECT_STREQ(static_cast(m.GetRawPtr()), "From: \xe3\x82\xb5\xe3\x82\xaf\xe3\x83\xa9"); + + // QP UTF-8 + std::string source3("From: =?utf-8?Q?=E3=82=B5=E3=82=AF=E3=83=A9!?="); + EXPECT_TRUE(CCodeBase::MIMEHeaderDecode(source3.c_str(), source3.length(), &m, CODE_UTF8)); + EXPECT_STREQ(static_cast(m.GetRawPtr()), "From: \xe3\x82\xb5\xe3\x82\xaf\xe3\x83\xa9!"); + + // 引数の文字コードとヘッダー内の文字コードが異なる場合は変換しない + EXPECT_TRUE(CCodeBase::MIMEHeaderDecode(source1.c_str(), source1.length(), &m, CODE_UTF8)); + EXPECT_STREQ(static_cast(m.GetRawPtr()), source1.c_str()); + + // 対応していない文字コードなら変換しない + std::string source4("From: =?utf-7?B?+MLUwrzDp-"); + EXPECT_TRUE(CCodeBase::MIMEHeaderDecode(source4.c_str(), source4.length(), &m, CODE_UTF7)); + EXPECT_STREQ(static_cast(m.GetRawPtr()), source4.c_str()); + + // 謎の符号化方式が指定されていたら何もしない + std::string source5("From: =?iso-2022-jp?X?GyRCJTUlLyVpGyhC?="); + EXPECT_TRUE(CCodeBase::MIMEHeaderDecode(source5.c_str(), source5.length(), &m, CODE_JIS)); + EXPECT_STREQ(static_cast(m.GetRawPtr()), source5.c_str()); + + // 末尾の ?= がなければ変換しない + std::string source6("From: =?iso-2022-jp?B?GyRCJTUlLyVpGyhC"); + EXPECT_TRUE(CCodeBase::MIMEHeaderDecode(source6.c_str(), source6.length(), &m, CODE_JIS)); + EXPECT_STREQ(static_cast(m.GetRawPtr()), source6.c_str()); +} diff --git a/tests/unittests/tests1.vcxproj b/tests/unittests/tests1.vcxproj index 4f2bb3870d..00fec312f2 100644 --- a/tests/unittests/tests1.vcxproj +++ b/tests/unittests/tests1.vcxproj @@ -107,6 +107,7 @@ + diff --git a/tests/unittests/tests1.vcxproj.filters b/tests/unittests/tests1.vcxproj.filters index eca9cf0f22..4126fb8cea 100644 --- a/tests/unittests/tests1.vcxproj.filters +++ b/tests/unittests/tests1.vcxproj.filters @@ -119,6 +119,9 @@ Test Files + + Test Files +