From f5abe2c188cb76b366b44b347b63d111a9d60645 Mon Sep 17 00:00:00 2001 From: sunyaozu Date: Fri, 26 Jan 2024 15:21:25 +0800 Subject: [PATCH] fix tdd error Signed-off-by: sunyaozu --- frameworks/intl/BUILD.gn | 2 + frameworks/intl/include/utils.h | 2 + frameworks/intl/src/utils.cpp | 36 ++++++++++++++++++ frameworks/intl/test/BUILD.gn | 9 ++++- frameworks/intl/test/unittest/intl_test.cpp | 21 +++++++++++ services/test/BUILD.gn | 5 ++- .../test/unittest/src/i18n_service_test.cpp | 37 ++----------------- 7 files changed, 76 insertions(+), 36 deletions(-) diff --git a/frameworks/intl/BUILD.gn b/frameworks/intl/BUILD.gn index f9c44411..701dcf0f 100644 --- a/frameworks/intl/BUILD.gn +++ b/frameworks/intl/BUILD.gn @@ -160,6 +160,8 @@ ohos_shared_library("intl_util") { use_exceptions = true external_deps = [ "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "access_token:libtokenid_sdk", "c_utils:utils", "hilog:libhilog", diff --git a/frameworks/intl/include/utils.h b/frameworks/intl/include/utils.h index 8e1f5467..7b219e67 100644 --- a/frameworks/intl/include/utils.h +++ b/frameworks/intl/include/utils.h @@ -29,6 +29,8 @@ int32_t ConvertString2Int(const std::string &numberStr, int32_t& status); bool IsValidLocaleTag(icu::Locale &locale); void GetAllValidLocalesTag(std::unordered_set& allValidLocalesLanguageTag); bool CheckTzDataFilePath(const std::string &filePath); +void RemoveNativeTokenTypeAndPermissions(const uint64_t selfTokenId); +void AddNativeTokenTypeAndPermissions(); } // namespace I18n } // namespace Global } // namespace OHOS diff --git a/frameworks/intl/src/utils.cpp b/frameworks/intl/src/utils.cpp index df33e6fd..6159cad5 100644 --- a/frameworks/intl/src/utils.cpp +++ b/frameworks/intl/src/utils.cpp @@ -18,9 +18,13 @@ #include #include #include +#include #include +#include "accesstoken_kit.h" #include "hilog/log.h" +#include "nativetoken_kit.h" #include "parameter.h" +#include "token_setproc.h" #include "utils.h" namespace OHOS { @@ -29,6 +33,9 @@ namespace I18n { using namespace std; static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "Utils" }; using namespace OHOS::HiviewDFX; +static constexpr int32_t I18N_UID = 3013; +static constexpr int32_t ROOT_UID = 0; + void Split(const string &src, const string &sep, vector &dest) { if (src == "") { @@ -118,6 +125,35 @@ bool CheckTzDataFilePath(const std::string &filePath) realpathRes = nullptr; return true; } + +void RemoveNativeTokenTypeAndPermissions(const uint64_t selfTokenId) +{ + SetSelfTokenID(selfTokenId); + seteuid(ROOT_UID); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + seteuid(I18N_UID); +} + +void AddNativeTokenTypeAndPermissions() +{ + const char* permissions[] = { + "ohos.permission.UPDATE_CONFIGURATION" + }; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = sizeof(permissions) / sizeof(permissions[0]), + .aclsNum = 0, + .dcaps = nullptr, + .perms = permissions, + .acls = nullptr, + .aplStr = "system_basic", + }; + infoInstance.processName = "i18n_test"; + SetSelfTokenID(GetAccessTokenId(&infoInstance)); + seteuid(ROOT_UID); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + seteuid(I18N_UID); +} } // namespace I18n } // namespace Global } // namespace OHOS \ No newline at end of file diff --git a/frameworks/intl/test/BUILD.gn b/frameworks/intl/test/BUILD.gn index ae9d0aee..88bb5588 100644 --- a/frameworks/intl/test/BUILD.gn +++ b/frameworks/intl/test/BUILD.gn @@ -30,6 +30,7 @@ ohos_unittest("intl_test") { "//base/global/i18n/frameworks/intl/include", "//base/global/i18n/frameworks/intl/test/unittest/mock/include", "//base/global/i18n/interfaces/native/inner_api/preferred_language/include", + "//base/global/i18n/services/include", "//third_party/icu/icu4c/source/common", "//third_party/icu/icu4c/source/common/unicode", "//third_party/icu/icu4c/source/i18n", @@ -38,10 +39,16 @@ ohos_unittest("intl_test") { "//third_party/protobuf/src", ] - external_deps = [ "init:libbegetutil" ] + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "init:libbegetutil", + ] deps = [ "//base/global/i18n/frameworks/intl:build_module", + "//base/global/i18n/services:i18n_service_ability", "//third_party/googletest:gtest_main", "//third_party/icu/icu4c:shared_icui18n", "//third_party/icu/icu4c:shared_icuuc", diff --git a/frameworks/intl/test/unittest/intl_test.cpp b/frameworks/intl/test/unittest/intl_test.cpp index 5651a08e..5885b628 100644 --- a/frameworks/intl/test/unittest/intl_test.cpp +++ b/frameworks/intl/test/unittest/intl_test.cpp @@ -25,6 +25,7 @@ #include "holiday_manager.h" #include "i18n_break_iterator.h" #include "i18n_calendar.h" +#include "i18n_service_ability_client.h" #include "i18n_timezone.h" #include "i18n_types.h" #include "index_util.h" @@ -41,6 +42,7 @@ #include "system_locale_manager.h" #include "taboo_utils.h" #include "taboo.h" +#include "token_setproc.h" #include "utils.h" #include "intl_test.h" #include "generate_ics_file.h" @@ -53,6 +55,8 @@ using namespace std; namespace OHOS { namespace Global { namespace I18n { +static const uint64_t selfTokenId = GetSelfTokenID(); + class IntlTest : public testing::Test { public: static void SetUpTestCase(void); @@ -73,6 +77,20 @@ void IntlTest::SetUp(void) void IntlTest::TearDown(void) {} +void InitTestEnvironment() +{ + AddNativeTokenTypeAndPermissions(); + I18nServiceAbilityClient::SetSystemLocale("zh-Hans-CN"); + RemoveNativeTokenTypeAndPermissions(selfTokenId); +} + +void RestoreEnvironment(const std::string &originLocaleTag) +{ + AddNativeTokenTypeAndPermissions(); + I18nServiceAbilityClient::SetSystemLocale(originLocaleTag); + RemoveNativeTokenTypeAndPermissions(selfTokenId); +} + /** * @tc.name: IntlFuncTest001 * @tc.desc: Test Intl DateTimeFormat.format @@ -893,6 +911,8 @@ HWTEST_F(IntlTest, IntlFuncTest0024, TestSize.Level1) { // abnormal test cast // normal test case + std::string currentSystemLocale = LocaleConfig::GetSystemLocale(); + InitTestEnvironment(); vector locales; locales.push_back("$$$###"); map options = { @@ -920,6 +940,7 @@ HWTEST_F(IntlTest, IntlFuncTest0024, TestSize.Level1) res = plurals->Select(12.34); EXPECT_EQ(res, "other"); delete plurals; + RestoreEnvironment(currentSystemLocale); } /** diff --git a/services/test/BUILD.gn b/services/test/BUILD.gn index 492abde8..6b10acd7 100644 --- a/services/test/BUILD.gn +++ b/services/test/BUILD.gn @@ -20,10 +20,11 @@ ohos_unittest("i18n_service_test") { sources = [ "unittest/src/i18n_service_test.cpp" ] include_dirs = [ - "../include", + "./unittest/include", "../../frameworks/intl/include", "../../interfaces/native/inner_api/preferred_language/include", "../../services/include", + "//third_party/icu/icu4c/source/common/unicode", ] external_deps = [ @@ -38,6 +39,8 @@ ohos_unittest("i18n_service_test") { "../../services:i18n_sa", "../../services:i18n_sa_client", "//third_party/googletest:gtest_main", + "//third_party/icu/icu4c:shared_icui18n", + "//third_party/icu/icu4c:shared_icuuc", ] defines = [ "SUPPORT_GRAPHICS" ] diff --git a/services/test/unittest/src/i18n_service_test.cpp b/services/test/unittest/src/i18n_service_test.cpp index 6822f4f8..1f3d7453 100644 --- a/services/test/unittest/src/i18n_service_test.cpp +++ b/services/test/unittest/src/i18n_service_test.cpp @@ -14,12 +14,12 @@ */ #include -#include "accesstoken_kit.h" #include "i18n_service_ability_client.h" #include "locale_config.h" -#include "nativetoken_kit.h" #include "preferred_language.h" #include "token_setproc.h" +#include "utils.h" +#include "i18n_service_test.h" using testing::ext::TestSize; using namespace std; @@ -27,8 +27,6 @@ namespace OHOS { namespace Global { namespace I18n { static const uint64_t selfTokenId = GetSelfTokenID(); -static constexpr int32_t I18N_UID = 3013; -static constexpr int32_t ROOT_UID = 0; class I18nServiceTest : public testing::Test { public: @@ -50,35 +48,6 @@ void I18nServiceTest::SetUp(void) void I18nServiceTest::TearDown(void) {} -void RemoveNativeTokenTypeAndPermissions() -{ - SetSelfTokenID(selfTokenId); - seteuid(ROOT_UID); - OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); - seteuid(I18N_UID); -} - -void AddNativeTokenTypeAndPermissions() -{ - const char* permissions[] = { - "ohos.permission.UPDATE_CONFIGURATION" - }; - NativeTokenInfoParams infoInstance = { - .dcapsNum = 0, - .permsNum = sizeof(permissions) / sizeof(permissions[0]), - .aclsNum = 0, - .dcaps = nullptr, - .perms = permissions, - .acls = nullptr, - .aplStr = "system_basic", - }; - infoInstance.processName = "I18nServiceTest"; - SetSelfTokenID(GetAccessTokenId(&infoInstance)); - seteuid(ROOT_UID); - OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); - seteuid(I18N_UID); -} - void InitTestEnvironment() { LocaleConfig::SetSystemLanguage("zh-Hans"); @@ -91,7 +60,7 @@ void InitTestEnvironment() PreferredLanguage::RemovePreferredLanguage(i); } } - RemoveNativeTokenTypeAndPermissions(); + RemoveNativeTokenTypeAndPermissions(selfTokenId); } /**