fix tdd error

Signed-off-by: sunyaozu <sunyaozu@huawei.com>
This commit is contained in:
sunyaozu 2024-01-26 15:21:25 +08:00
parent 4b40ae0876
commit f5abe2c188
7 changed files with 76 additions and 36 deletions

View File

@ -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",

View File

@ -29,6 +29,8 @@ int32_t ConvertString2Int(const std::string &numberStr, int32_t& status);
bool IsValidLocaleTag(icu::Locale &locale);
void GetAllValidLocalesTag(std::unordered_set<std::string>& allValidLocalesLanguageTag);
bool CheckTzDataFilePath(const std::string &filePath);
void RemoveNativeTokenTypeAndPermissions(const uint64_t selfTokenId);
void AddNativeTokenTypeAndPermissions();
} // namespace I18n
} // namespace Global
} // namespace OHOS

View File

@ -18,9 +18,13 @@
#include <fstream>
#include <stdexcept>
#include <string>
#include <unistd.h>
#include <vector>
#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<string> &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

View File

@ -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",

View File

@ -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<string> locales;
locales.push_back("$$$###");
map<string, string> options = {
@ -920,6 +940,7 @@ HWTEST_F(IntlTest, IntlFuncTest0024, TestSize.Level1)
res = plurals->Select(12.34);
EXPECT_EQ(res, "other");
delete plurals;
RestoreEnvironment(currentSystemLocale);
}
/**

View File

@ -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" ]

View File

@ -14,12 +14,12 @@
*/
#include <gtest/gtest.h>
#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);
}
/**