From 804305bd4de86586f79b1c61ff259459f4a90b1b Mon Sep 17 00:00:00 2001 From: cy7717 Date: Sun, 25 Jun 2023 10:23:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96tdd=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cy7717 --- .../cpp_test/src/input_method_switch_test.cpp | 6 +++- .../cpp_test/src/newIme_switch_test.cpp | 6 +++- ...permission_verification_exception_test.cpp | 24 ++----------- test/unittest/cpp_test/tdd_util/tdd_util.cpp | 36 ++----------------- test/unittest/cpp_test/tdd_util/tdd_util.h | 1 - 5 files changed, 15 insertions(+), 58 deletions(-) diff --git a/test/unittest/cpp_test/src/input_method_switch_test.cpp b/test/unittest/cpp_test/src/input_method_switch_test.cpp index 59df685d..baf58e30 100644 --- a/test/unittest/cpp_test/src/input_method_switch_test.cpp +++ b/test/unittest/cpp_test/src/input_method_switch_test.cpp @@ -83,7 +83,9 @@ public: void InputMethodSwitchTest::SetUpTestCase(void) { IMSA_HILOGI("InputMethodSwitchTest::SetUpTestCase"); - TddUtil::GrantNativePermission(); + TddUtil::StorageSelfTokenID(); + TddUtil::AllocTestTokenID("ohos.inputMethod.test"); + TddUtil::SetTestTokenID(); imc_ = InputMethodController::GetInstance(); imc_->SetSettingListener(std::make_shared()); imc_->UpdateListenEventFlag("imeChange", true); @@ -93,6 +95,8 @@ void InputMethodSwitchTest::TearDownTestCase(void) { IMSA_HILOGI("InputMethodSwitchTest::TearDownTestCase"); InputMethodController::GetInstance()->Close(); + TddUtil::RestoreSelfTokenID(); + TddUtil::DeleteTestTokenID(); } void InputMethodSwitchTest::SetUp(void) diff --git a/test/unittest/cpp_test/src/newIme_switch_test.cpp b/test/unittest/cpp_test/src/newIme_switch_test.cpp index 760649e2..b3772743 100644 --- a/test/unittest/cpp_test/src/newIme_switch_test.cpp +++ b/test/unittest/cpp_test/src/newIme_switch_test.cpp @@ -79,7 +79,9 @@ public: void NewImeSwitchTest::SetUpTestCase(void) { IMSA_HILOGI("NewImeSwitchTest::SetUpTestCase"); - TddUtil::GrantNativePermission(); + TddUtil::StorageSelfTokenID(); + TddUtil::AllocTestTokenID("ohos.inputMethod.test"); + TddUtil::SetTestTokenID(); imc_ = InputMethodController::GetInstance(); imc_->SetSettingListener(std::make_shared()); imc_->UpdateListenEventFlag("imeChange", true); @@ -89,6 +91,8 @@ void NewImeSwitchTest::TearDownTestCase(void) { IMSA_HILOGI("NewImeSwitchTest::TearDownTestCase"); InputMethodController::GetInstance()->Close(); + TddUtil::RestoreSelfTokenID(); + TddUtil::DeleteTestTokenID(); } void NewImeSwitchTest::SetUp(void) diff --git a/test/unittest/cpp_test/src/permission_verification_exception_test.cpp b/test/unittest/cpp_test/src/permission_verification_exception_test.cpp index ba531901..260e94dd 100644 --- a/test/unittest/cpp_test/src/permission_verification_exception_test.cpp +++ b/test/unittest/cpp_test/src/permission_verification_exception_test.cpp @@ -33,7 +33,6 @@ using namespace testing::ext; namespace OHOS { namespace MiscServices { -constexpr const uint16_t EACH_LINE_LENGTH = 500; class TextListener : public OnTextChangedListener { public: TextListener() = default; @@ -90,7 +89,6 @@ class PermissionVerificationExceptionTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); - static bool ExecuteCmd(const std::string &cmd, std::string &result); void SetUp(); void TearDown(); static void AllocAndSetTestTokenID(const std::string &bundleName); @@ -127,7 +125,7 @@ void PermissionVerificationExceptionTest::TearDownTestCase(void) TddUtil::DeleteTestTokenID(); std::string result; auto property = imc_->GetCurrentInputMethod(); - auto ret = PermissionVerificationExceptionTest::ExecuteCmd("ps -ef| grep " + property->name, result); + auto ret = TddUtil::ExecuteCmd("ps -ef| grep " + property->name, result); IMSA_HILOGI("ret: %{public}d, result is: %{public}s", ret, result.c_str()); std::smatch regResult; std::regex pattern("\\s+\\d{3,6}"); @@ -139,7 +137,7 @@ void PermissionVerificationExceptionTest::TearDownTestCase(void) return; } IMSA_HILOGI("pid is %{public}s.", pid.c_str()); - ret = PermissionVerificationExceptionTest::ExecuteCmd("kill " + pid, result); + ret = TddUtil::ExecuteCmd("kill " + pid, result); IMSA_HILOGI("ret: %{public}d, result is: %{public}s", ret, result.c_str()); } @@ -153,24 +151,6 @@ void PermissionVerificationExceptionTest::TearDown(void) IMSA_HILOGI("PermissionVerificationExceptionTest::TearDown"); } -bool PermissionVerificationExceptionTest::ExecuteCmd(const std::string &cmd, std::string &result) -{ - char buff[EACH_LINE_LENGTH] = { 0x00 }; - std::stringstream output; - FILE *ptr = popen(cmd.c_str(), "r"); - if (ptr != nullptr) { - while (fgets(buff, sizeof(buff), ptr) != nullptr) { - output << buff; - } - pclose(ptr); - ptr = nullptr; - } else { - return false; - } - result = output.str(); - return true; -} - /** * @tc.name: ShowAndHideSoftKeyboard * @tc.desc: PermissionVerificationExceptionTest ShowAndHideSoftKeyboard. diff --git a/test/unittest/cpp_test/tdd_util/tdd_util.cpp b/test/unittest/cpp_test/tdd_util/tdd_util.cpp index 249997ea..2f9a4f7e 100644 --- a/test/unittest/cpp_test/tdd_util/tdd_util.cpp +++ b/test/unittest/cpp_test/tdd_util/tdd_util.cpp @@ -40,7 +40,6 @@ using namespace OHOS::AccountSA; constexpr int32_t INVALID_USER_ID = -1; constexpr int32_t MAIN_USER_ID = 100; constexpr const uint16_t EACH_LINE_LENGTH = 500; -constexpr const uint16_t TOTAL_LENGTH = 4096; uint64_t TddUtil::selfTokenID_ = 0; uint64_t TddUtil::testTokenID_ = 0; int64_t TddUtil::selfUid_ = -1; @@ -135,50 +134,21 @@ void TddUtil::RestoreSelfUid() setuid(selfUid_); } -void TddUtil::GrantNativePermission() -{ - const char **perms = new const char *[1]; - perms[0] = "ohos.permission.CONNECT_IME_ABILITY"; - TokenInfoParams infoInstance = { - .dcapsNum = 0, - .permsNum = 1, - .aclsNum = 0, - .dcaps = nullptr, - .perms = perms, - .acls = nullptr, - .processName = "inputmethod_imf", - .aplStr = "system_core", - }; - uint64_t tokenId = GetAccessTokenId(&infoInstance); - int res = SetSelfTokenID(tokenId); - if (res == 0) { - IMSA_HILOGI("SetSelfTokenID success!"); - } else { - IMSA_HILOGE("SetSelfTokenID fail!"); - } - AccessTokenKit::ReloadNativeTokenInfo(); - delete[] perms; -} - bool TddUtil::ExecuteCmd(const std::string &cmd, std::string &result) { char buff[EACH_LINE_LENGTH] = { 0x00 }; - char output[TOTAL_LENGTH] = { 0x00 }; + std::stringstream output; FILE *ptr = popen(cmd.c_str(), "r"); if (ptr != nullptr) { while (fgets(buff, sizeof(buff), ptr) != nullptr) { - if (strcat_s(output, sizeof(output), buff) != 0) { - pclose(ptr); - ptr = nullptr; - return false; - } + output << buff; } pclose(ptr); ptr = nullptr; } else { return false; } - result = std::string(output); + result = output.str(); return true; } } // namespace MiscServices diff --git a/test/unittest/cpp_test/tdd_util/tdd_util.h b/test/unittest/cpp_test/tdd_util/tdd_util.h index e704083e..e1de12b3 100644 --- a/test/unittest/cpp_test/tdd_util/tdd_util.h +++ b/test/unittest/cpp_test/tdd_util/tdd_util.h @@ -30,7 +30,6 @@ public: static void StorageSelfUid(); static void SetTestUid(); static void RestoreSelfUid(); - static void GrantNativePermission(); static bool ExecuteCmd(const std::string &cmd, std::string &result); private: