From 86b2a188dee1765c6edf361bd55a245855257248 Mon Sep 17 00:00:00 2001 From: qianyong325 Date: Sat, 17 May 2025 22:44:22 +0800 Subject: [PATCH] hdc command Signed-off-by: qianyong325 --- bundle.json | 3 +- .../include/identity_checker.h | 1 + .../include/identity_checker_impl.h | 1 + .../src/identity_checker_impl.cpp | 5 + services/src/input_method_system_ability.cpp | 4 + test/unittest/BUILD.gn | 1 + test/unittest/cpp_test/BUILD.gn | 39 ++++ .../common/include/identity_checker_mock.h | 7 + .../cpp_test/src/identity_checker_test.cpp | 24 ++ .../src/input_method_manager_command_test.cpp | 165 +++++++++++++ .../unittest/resource/ohos_test/ohos_test.xml | 9 + tools/ime/BUILD.gn | 46 ++++ .../include/input_method_manager_command.h | 31 +++ .../ime/src/input_method_manager_command.cpp | 216 ++++++++++++++++++ tools/ime/src/main.cpp | 22 ++ 15 files changed, 573 insertions(+), 1 deletion(-) create mode 100644 test/unittest/cpp_test/src/input_method_manager_command_test.cpp create mode 100644 tools/ime/BUILD.gn create mode 100644 tools/ime/include/input_method_manager_command.h create mode 100644 tools/ime/src/input_method_manager_command.cpp create mode 100644 tools/ime/src/main.cpp diff --git a/bundle.json b/bundle.json index 5959d7e8f..7c0b1bf7b 100644 --- a/bundle.json +++ b/bundle.json @@ -88,7 +88,8 @@ "//base/inputmethod/imf/frameworks/js/napi/keyboardpanelmanager:keyboardpanelmanager", "//base/inputmethod/imf/frameworks/cj:cj_inputmethod_ffi", "//base/inputmethod/imf/seccomp_policy:imf_ext_secure_filter", - "//base/inputmethod/imf/services/dialog:input_method_choose_dialog" + "//base/inputmethod/imf/services/dialog:input_method_choose_dialog", + "//base/inputmethod/imf/tools/ime:ime" ] }, "inner_api": [ diff --git a/services/identity_checker/include/identity_checker.h b/services/identity_checker/include/identity_checker.h index b89ca4260..060d67bb3 100644 --- a/services/identity_checker/include/identity_checker.h +++ b/services/identity_checker/include/identity_checker.h @@ -31,6 +31,7 @@ public: virtual bool HasPermission(uint32_t tokenId, const std::string &permission) = 0; virtual bool IsBroker(Security::AccessToken::AccessTokenID tokenId) = 0; virtual bool IsNativeSa(Security::AccessToken::AccessTokenID tokenId) = 0; + virtual bool IsFormShell(Security::AccessToken::AccessTokenID tokenId) = 0; virtual std::string GetBundleNameByToken(uint32_t tokenId); virtual bool IsFocusedUIExtension(uint32_t callingTokenId, uint64_t displayId = DEFAULT_DISPLAY_ID) { diff --git a/services/identity_checker/include/identity_checker_impl.h b/services/identity_checker/include/identity_checker_impl.h index e63094718..065f4bb90 100644 --- a/services/identity_checker/include/identity_checker_impl.h +++ b/services/identity_checker/include/identity_checker_impl.h @@ -28,6 +28,7 @@ public: bool IsBundleNameValid(uint32_t tokenId, const std::string &validBundleName) override; bool HasPermission(uint32_t tokenId, const std::string &permission) override; bool IsBroker(Security::AccessToken::AccessTokenID tokenId) override; + bool IsFormShell(Security::AccessToken::AccessTokenID tokenId) override; bool IsNativeSa(Security::AccessToken::AccessTokenID tokenId) override; std::string GetBundleNameByToken(uint32_t tokenId) override; bool IsFocusedUIExtension(uint32_t callingTokenId, uint64_t displayId = DEFAULT_DISPLAY_ID) override; diff --git a/services/identity_checker/src/identity_checker_impl.cpp b/services/identity_checker/src/identity_checker_impl.cpp index fcc05dc2d..2ee8a0724 100644 --- a/services/identity_checker/src/identity_checker_impl.cpp +++ b/services/identity_checker/src/identity_checker_impl.cpp @@ -102,6 +102,11 @@ bool IdentityCheckerImpl::IsNativeSa(AccessTokenID tokenId) return AccessTokenKit::GetTokenTypeFlag(tokenId) == TypeATokenTypeEnum::TOKEN_NATIVE; } +bool IdentityCheckerImpl::IsFormShell(AccessTokenID tokenId) +{ + return AccessTokenKit::GetTokenTypeFlag(tokenId) == TypeATokenTypeEnum::TOKEN_SHELL; +} + bool IdentityCheckerImpl::IsFocusedUIExtension(uint32_t callingTokenId, uint64_t displayId) { bool isFocused = false; diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index 5d502e285..4dc7f4eae 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -1981,6 +1981,10 @@ ErrCode InputMethodSystemAbility::UnRegisteredProxyIme(int32_t type, const sptr< int32_t InputMethodSystemAbility::CheckEnableAndSwitchPermission() { + if (identityChecker_->IsFormShell(IPCSkeleton::GetCallingFullTokenID())) { + IMSA_HILOGD("is form shell!"); + return ErrorCode::NO_ERROR; + } if (!identityChecker_->IsNativeSa(IPCSkeleton::GetCallingFullTokenID()) && !identityChecker_->IsSystemApp(IPCSkeleton::GetCallingFullTokenID())) { IMSA_HILOGE("not native sa or system app!"); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index eee417974..170a98ce2 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -50,6 +50,7 @@ group("unittest") { "cpp_test:TextListenerInnerApiTest", "cpp_test:VirtualListenerTest", "cpp_test:WindowAdapterTest", + "cpp_test:InputMethodManagerCommandTest", "cpp_test/common:inputmethod_tdd_util", "napi_test/src:GetInputMethodJsTest", "resource/bundle_dependencies/editorBox:editorBox", diff --git a/test/unittest/cpp_test/BUILD.gn b/test/unittest/cpp_test/BUILD.gn index 9d47bfab7..ff39a6373 100644 --- a/test/unittest/cpp_test/BUILD.gn +++ b/test/unittest/cpp_test/BUILD.gn @@ -1466,6 +1466,45 @@ ohos_unittest("WindowAdapterTest") { } } +ohos_unittest("InputMethodManagerCommandTest") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = module_output_path + + sources = [ + "${inputmethod_path}/tools/ime/src/input_method_manager_command.cpp", + "src/input_method_manager_command_test.cpp", + ] + + include_dirs = [ + "include", + "${inputmethod_path}/tools/ime/include", + "${inputmethod_path}/common/include", + "${inputmethod_path}/interfaces/inner_api/inputmethod_controller/include", + ] + + deps = [ + "${inputmethod_path}/test/unittest/cpp_test/common:inputmethod_tdd_util", + "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client" ] + + external_deps = [ + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "hilog:libhilog", + "input:libmmi-client", + ] + + if (window_manager_use_sceneboard) { + external_deps += [ "window_manager:libwm_lite" ] + } else { + external_deps += [ "window_manager:libwm" ] + } +} + ohos_unittest("ImeEnabledInfoManagerTest") { branch_protector_ret = "pac_ret" sanitize = { diff --git a/test/unittest/cpp_test/common/include/identity_checker_mock.h b/test/unittest/cpp_test/common/include/identity_checker_mock.h index a157be4e7..776358199 100644 --- a/test/unittest/cpp_test/common/include/identity_checker_mock.h +++ b/test/unittest/cpp_test/common/include/identity_checker_mock.h @@ -49,6 +49,10 @@ public: { return isNativeSa_; } + bool IsFormShell(Security::AccessToken::AccessTokenID tokenId) override + { + return IsFormShell_; + } std::string GetBundleNameByToken(uint32_t tokenId) override { return bundleName_; @@ -69,6 +73,7 @@ public: hasPermission_ = false; isBroker_ = false; isNativeSa_ = false; + IsFormShell_ = false; isSpecialSaUid_ = false; bundleName_ = ""; } @@ -113,6 +118,7 @@ private: static bool hasPermission_; static bool isBroker_; static bool isNativeSa_; + static bool IsFormShell_; static bool isSpecialSaUid_; static std::string bundleName_; }; @@ -122,6 +128,7 @@ bool IdentityCheckerMock::isBundleNameValid_ { false }; bool IdentityCheckerMock::hasPermission_ { false }; bool IdentityCheckerMock::isBroker_ { false }; bool IdentityCheckerMock::isNativeSa_ { false }; +bool IdentityCheckerMock::IsFormShell_ { false }; bool IdentityCheckerMock::isSpecialSaUid_ { false }; std::string IdentityCheckerMock::bundleName_; } // namespace MiscServices diff --git a/test/unittest/cpp_test/src/identity_checker_test.cpp b/test/unittest/cpp_test/src/identity_checker_test.cpp index 5c30a7f7e..3003c9602 100644 --- a/test/unittest/cpp_test/src/identity_checker_test.cpp +++ b/test/unittest/cpp_test/src/identity_checker_test.cpp @@ -63,6 +63,10 @@ public: { return isNativeSa_; } + bool IsFormShell(Security::AccessToken::AccessTokenID tokenId) override + { + return isFromShell_; + } std::string GetBundleNameByToken(uint32_t tokenId) override { return ""; @@ -77,6 +81,7 @@ public: static bool hasPermission_; static bool isBroker_; static bool isNativeSa_; + static bool isFromShell_; }; static constexpr uint32_t MAIN_USER_ID = 100; static const constexpr char *CURRENT_IME = "testBundleName/testExtname"; @@ -96,6 +101,7 @@ bool IdentityCheckerTest::IdentityCheckerMock::isBundleNameValid_ = false; bool IdentityCheckerTest::IdentityCheckerMock::hasPermission_ = false; bool IdentityCheckerTest::IdentityCheckerMock::isBroker_ = false; bool IdentityCheckerTest::IdentityCheckerMock::isNativeSa_ = false; +bool IdentityCheckerTest::IdentityCheckerMock::isFromShell_ = false; void IdentityCheckerTest::SetUpTestCase(void) { @@ -715,6 +721,24 @@ HWTEST_F(IdentityCheckerTest, testSwitchInputMethod_003, TestSize.Level1) EXPECT_EQ(ret, ErrorCode::ERROR_IMSA_GET_IME_INFO_FAILED); } +/** + * @tc.name: testSwitchInputMethod_004 + * @tc.desc: has PERMISSION_CONNECT_IME_ABILITY, not currentIme switch subtype + * @tc.type: FUNC + * @tc.require: + * @tc.author: + */ +HWTEST_F(IdentityCheckerTest, testSwitchInputMethod_004, TestSize.Level1) +{ + IMSA_HILOGI("IdentityCheckerTest testSwitchInputMethod_004 start"); + service_->identityChecker_ = identityCheckerImpl_; + IdentityCheckerTest::IdentityCheckerMock::isFromShell_ = true; + IdentityCheckerTest::IdentityCheckerMock::isBundleNameValid_ = false; + int32_t ret = IdentityCheckerTest::service_->SwitchInputMethod( + CURRENT_BUNDLENAME, CURRENT_SUBNAME, static_cast(SwitchTrigger::NATIVE_SA)); + EXPECT_EQ(ret, ErrorCode::ERROR_IMSA_GET_IME_INFO_FAILED); +} + /** * @tc.name: testHideCurrentInputDeprecated_001 * @tc.desc: is broker diff --git a/test/unittest/cpp_test/src/input_method_manager_command_test.cpp b/test/unittest/cpp_test/src/input_method_manager_command_test.cpp new file mode 100644 index 000000000..05ada44fe --- /dev/null +++ b/test/unittest/cpp_test/src/input_method_manager_command_test.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "global.h" +#include "input_method_manager_command.h" +#include "tdd_util.h" + +namespace OHOS { +namespace MiscServices { +namespace { +using namespace testing::ext; +} // namespace +class InputMethodManagerCommandTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void) {} +}; + +void InputMethodManagerCommandTest::SetUpTestCase(void) +{ + IMSA_HILOGI("InputMethodManagerCommandTest::SetUpTestCase"); + sleep(2); +} + +/** + * @tc.name: InputMethodManagerCommand_ExeCmd_001 + * @tc.desc: Test handling missing argument for -e option + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodManagerCommandTest, ExeCmd_001, TestSize.Level1) +{ + IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_001 START"); + std::string result; + auto ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme:InputMethodExtAbility", result); + EXPECT_TRUE(ret); + EXPECT_EQ("Succeeded in enabling IME. IME:com.example.newTestIme:InputMethodExtAbility\n", result); +} + +/** + * @tc.name: InputMethodManagerCommand_ExeCmd_002 + * @tc.desc: Test handling missing argument for -e option + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodManagerCommandTest, ExeCmd_002, TestSize.Level1) +{ + IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_002 START"); + std::string result; + auto ret = TddUtil::ExecuteCmd("ime -e com.example.test:InputMethodExtAbility", result); + EXPECT_TRUE(ret); + EXPECT_EQ("Error: The input method does not exist.\n", result); +} + +/** + * @tc.name: InputMethodManagerCommand_ExeCmd_003 + * @tc.desc: Test handling missing argument for -e option + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodManagerCommandTest, ExeCmd_003, TestSize.Level1) +{ + IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_003 START"); + std::string result; + auto ret = TddUtil::ExecuteCmd("ime -d com.example.newTestIme:InputMethodExtAbility", result); + EXPECT_TRUE(ret); + EXPECT_EQ("Succeeded in disabling IME. IME:com.example.newTestIme:InputMethodExtAbility\n", result); +} + +/** + * @tc.name: InputMethodManagerCommand_ExeCmd_004 + * @tc.desc: Test handling missing argument for -e option + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodManagerCommandTest, ExeCmd_004, TestSize.Level1) +{ + IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_004 START"); + std::string result; + auto ret = TddUtil::ExecuteCmd("ime -f com.example.newTestIme:InputMethodExtAbility", result); + EXPECT_TRUE(ret); + EXPECT_EQ("Succeeded in setting IME to basic mode. IME:com.example.newTestIme:InputMethodExtAbility\n", result); +} + +/** + * @tc.name: InputMethodManagerCommand_ExeCmd_005 + * @tc.desc: Test handling missing argument for -e option + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodManagerCommandTest, ExeCmd_005, TestSize.Level1) +{ + IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_005 START"); + std::string result; + auto ret = TddUtil::ExecuteCmd("ime -b com.example.newTestIme:InputMethodExtAbility", result); + EXPECT_TRUE(ret); + EXPECT_EQ("Succeeded in setting IME to full mode. IME:com.example.newTestIme:InputMethodExtAbility\n", result); +} + +/** + * @tc.name: InputMethodManagerCommand_ExeCmd_006 + * @tc.desc: Test handling missing argument for -e option + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodManagerCommandTest, ExeCmd_006, TestSize.Level1) +{ + IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_006 START"); + std::string result; + auto ret = TddUtil::ExecuteCmd("ime -s com.example.newTestIme:InputMethodExtAbility", result); + EXPECT_TRUE(ret); + EXPECT_EQ("Could not switch the input method. IME:com.example.newTestIme:InputMethodExtAbility\n", result); +} + +/** + * @tc.name: InputMethodManagerCommand_ExeCmd_007 + * @tc.desc: Test handling missing argument for -e option + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodManagerCommandTest, ExeCmd_007, TestSize.Level1) +{ + IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_007 START"); + std::string result; + auto ret = TddUtil::ExecuteCmd("ime -d com.example.newTestIme:InputMethodExtAbility", result); + EXPECT_TRUE(ret); + sleep(1); + ret = TddUtil::ExecuteCmd("ime -e com.example.newTestIme:InputMethodExtAbility", result); + EXPECT_TRUE(ret); + sleep(1); + ret = TddUtil::ExecuteCmd("ime -s com.example.newTestIme:lowerInput", result); + EXPECT_TRUE(ret); + EXPECT_EQ("Succeeded in switching the input method. IME:com.example.newTestIme:lowerInput\n", result); +} + +/** + * @tc.name: InputMethodManagerCommand_ExeCmd_008 + * @tc.desc: Test handling missing argument for -e option + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(InputMethodManagerCommandTest, ExeCmd_008, TestSize.Level1) +{ + IMSA_HILOGI("InputMethodManagerCommandTest ExeCmd_008 START"); + sleep(2); + std::string result; + auto ret = TddUtil::ExecuteCmd("ime -g", result); + EXPECT_TRUE(ret); + EXPECT_EQ("The current input method is: com.example.newTestIme\n", result); +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/resource/ohos_test/ohos_test.xml b/test/unittest/resource/ohos_test/ohos_test.xml index cb9987d54..dbaa6d025 100644 --- a/test/unittest/resource/ohos_test/ohos_test.xml +++ b/test/unittest/resource/ohos_test/ohos_test.xml @@ -133,4 +133,13 @@