diff --git a/frameworks/native/ability/native/js_extension_common.cpp b/frameworks/native/ability/native/js_extension_common.cpp index 878a81161bbe38c03380911c6c735218c6d996b3..8bec649105d82b926b1dd54f753debd72fdb7b48 100644 --- a/frameworks/native/ability/native/js_extension_common.cpp +++ b/frameworks/native/ability/native/js_extension_common.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -97,7 +97,7 @@ napi_value JsExtensionCommon::CallObjectMethod(const char* name, napi_value cons napi_value method = nullptr; napi_get_named_property(env, obj, name, &method); - if (!CheckTypeForNapiValue(env, obj, napi_function)) { + if (!CheckTypeForNapiValue(env, method, napi_function)) { TAG_LOGE(AAFwkTag::EXT, "get '%{public}s' failed", name); return nullptr; } diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index a0c9b57a3b3643f86120ba431b5e0330280a9aa3..6dc32b031f4172e5c95ca41610ae66895eb2e1d7 100644 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -1568,12 +1568,6 @@ int32_t AppMgrService::GetAppRunningUniqueIdByPid(pid_t pid, std::string &appRun TAG_LOGE(AAFwkTag::APPMGR, "not ready"); return ERR_INVALID_OPERATION; } - bool isCallingPermission = AAFwk::PermissionVerification::GetInstance()->IsSACall() && - AAFwk::PermissionVerification::GetInstance()->VerifyRunningInfoPerm(); - if (!isCallingPermission) { - TAG_LOGE(AAFwkTag::APPMGR, "GetAppRunningUniqueIdByPid not SA call or verification failed"); - return ERR_PERMISSION_DENIED; - } return appMgrServiceInner_->GetAppRunningUniqueIdByPid(pid, appRunningUniqueId); } diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index f6f9041179e7e36604fd83b23e9ebe814dcb72c2..1f9d410beb9e410ca5556d6c63b0ca49bcf064fc 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -2488,8 +2488,11 @@ int32_t AppMgrServiceInner::NotifyProcMemoryLevel(const std::mapCheckSpecificSystemAbilityAccessPermission( MEMMGR_PROC_NAME); - if (!isMemmgrCall) { - TAG_LOGE(AAFwkTag::APPMGR, "callerToken not %{public}s", MEMMGR_PROC_NAME); + auto isShellCall = AAFwk::PermissionVerification::GetInstance()->IsShellCall(); + bool isDevelopMode = system::GetBoolParameter(DEVELOPER_MODE_STATE, false); + if (!(isMemmgrCall || (isShellCall && isDevelopMode))) { + TAG_LOGE(AAFwkTag::APPMGR, "Permission check failed: callerToken is not %{public}s, isMemmgrCall=%{public}d, " + "isShellCall=%{public}d, isDevelopMode=%{public}d", MEMMGR_PROC_NAME, isMemmgrCall, isShellCall, isDevelopMode); return ERR_INVALID_VALUE; } if (!appRunningManager_) { @@ -8488,6 +8491,14 @@ int32_t AppMgrServiceInner::GetAppRunningUniqueIdByPid(pid_t pid, std::string &a TAG_LOGE(AAFwkTag::APPMGR, "appRunningManager_ null"); return ERR_NO_INIT; } + bool isCallingPermission = AAFwk::PermissionVerification::GetInstance()->IsSACall() && + AAFwk::PermissionVerification::GetInstance()->VerifyRunningInfoPerm(); + auto isShellCall = AAFwk::PermissionVerification::GetInstance()->IsShellCall(); + bool isDevelopMode = system::GetBoolParameter(DEVELOPER_MODE_STATE, false); + if (!isCallingPermission && !(isShellCall && isDevelopMode)) { + TAG_LOGE(AAFwkTag::APPMGR, "GetAppRunningUniqueIdByPid not SA call or verification failed"); + return ERR_PERMISSION_DENIED; + } return appRunningManager_->GetAppRunningUniqueIdByPid(pid, appRunningUniqueId); } diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index 74aeabc330e8c955533df05681e8dc706416eb56..1678d99f337f0473aa0166bae2275c752d8c03d1 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -41,6 +41,7 @@ const std::string HELP_MSG = "usage: aa \n" " force-stop force stop the process with bundle name\n" " attach attach application to enter debug mode\n" " detach detach application to exit debug mode\n" + " send-memory-level send memory level to application by pid \n" #ifdef ABILITY_COMMAND_FOR_TEST " test start the test framework with options\n" " ApplicationNotResponding Pass in pid with options\n" @@ -129,6 +130,14 @@ const std::string HELP_MSG_TEST = " [-w ]\n" " [-D]\n"; +const std::string HELP_MSG_SEND_MEMORY_LEVEL = + "Usage: aa send-memory-level -p -l \n" + "Mandatory Options:\n" + " -p, --pid Target process ID (integer)\n" + " -l, --level <0|1|2> Memory level (0: Moderate, 1: Low, 2: Critical)\n\n" + "Optional:\n" + " -h, --help Show this help message\n"; + const std::string HELP_MSG_ATTACH_APP_DEBUG = "usage: aa attach \n" "options list:\n" @@ -173,6 +182,9 @@ const std::string STRING_ATTACH_APP_DEBUG_NG = "error: failed to attach app debu const std::string STRING_DETACH_APP_DEBUG_OK = "detach app debug successfully."; const std::string STRING_DETACH_APP_DEBUG_NG = "error: failed to detach app debug."; +const std::string STRING_SEND_MEMORY_LEVEL_OK = "send memory level successfully."; +const std::string STRING_SEND_MEMORY_LEVEL_NG = "error: failed to send memory level."; + const std::string STRING_START_USER_TEST_NG = "error: failed to start user test."; const std::string STRING_USER_TEST_STARTED = "user test started."; const std::string STRING_USER_TEST_FINISHED = "user test finished."; @@ -232,6 +244,8 @@ private: ErrCode RunAsProcessCommand(); ErrCode RunAsAttachDebugCommand(); ErrCode RunAsDetachDebugCommand(); + ErrCode RunAsSendMemoryLevelCommand(); + ErrCode ParsePidMemoryLevel(std::string& pidParse, std::string& memoryLevelParse); bool CheckParameters(int target); ErrCode ParseParam(ParametersInteger& pi); ErrCode ParseParam(ParametersString& ps, bool isNull); diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 0fbfddde1924a1d8e40cbdb92cc9e289d3787b95..0c522fb0532005c6254652be421c78568125680a 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -28,6 +28,7 @@ #include "sa_mgr_client.h" #include "system_ability_definition.h" #include "test_observer.h" +#include "app_mem_info.h" using namespace OHOS::AppExecFwk; @@ -49,6 +50,9 @@ constexpr int OPTION_WINDOW_TOP = 262; constexpr int OPTION_WINDOW_HEIGHT = 263; constexpr int OPTION_WINDOW_WIDTH = 264; +constexpr int INVALID_PID = 10104003; +constexpr int INVALID_LEVEL = 10104004; + constexpr int INNER_ERR_START = 10108101; constexpr int INNER_ERR_TEST = 10108501; constexpr int INNER_ERR_DEBUG = 10108601; @@ -213,6 +217,13 @@ constexpr struct option LONG_OPTIONS_ATTACH[] = { {"bundle", required_argument, nullptr, 'b'}, {nullptr, 0, nullptr, 0}, }; +const std::string SHORT_OPTIONS_SEND_MEMORY_LEVEL = "hp:l:"; +constexpr struct option LONG_OPTIONS_SEND_MEMORY_LEVEL[] = { + {"help", no_argument, nullptr, 'h'}, + {"pid", required_argument, nullptr, 'p' }, + {"level", required_argument, nullptr, 'l' }, + {nullptr, 0, nullptr, 0 }, +}; } // namespace AbilityManagerShellCommand::AbilityManagerShellCommand(int argc, char* argv[]) : ShellCommand(argc, argv, TOOL_NAME) @@ -235,6 +246,7 @@ ErrCode AbilityManagerShellCommand::CreateCommandMap() {"attach", [this]() { return this->RunAsAttachDebugCommand(); }}, {"detach", [this]() { return this->RunAsDetachDebugCommand(); }}, {"appdebug", [this]() { return this->RunAsAppDebugDebugCommand(); }}, + {"send-memory-level", [this]() { return this->RunAsSendMemoryLevelCommand(); }}, #ifdef ABILITY_COMMAND_FOR_TEST {"force-timeout", [this]() { return this->RunForceTimeoutForTest(); }}, #endif @@ -1013,6 +1025,108 @@ ErrCode AbilityManagerShellCommand::RunAsProcessCommand() return result; } +ErrCode AbilityManagerShellCommand::RunAsSendMemoryLevelCommand() +{ + TAG_LOGD(AAFwkTag::AA_TOOL, "sendMemoryLevel"); + std::string pidParse = ""; + std::string memoryLevelParse = ""; + ParsePidMemoryLevel(pidParse, memoryLevelParse); + if(pidParse.empty() || memoryLevelParse.empty()) { + resultReceiver_.append(HELP_MSG_SEND_MEMORY_LEVEL + "\n"); + return OHOS::ERR_INVALID_VALUE; + } + + pid_t inputPid = static_cast(ConvertPid(pidParse)); + MemoryLevel inputLevel = static_cast(ConvertPid(memoryLevelParse)); + + std::string appRunningUniqueId; + ErrCode queryResult = DelayedSingleton::GetInstance()->GetAppRunningUniqueIdByPid( + inputPid, appRunningUniqueId); + if (inputPid <= 0 || queryResult != ERR_OK) { + TAG_LOGE(AAFwkTag::APPMGR, "pid value error. The specified pid does not exist."); + resultReceiver_.append(STRING_SEND_MEMORY_LEVEL_NG + "\n"); + resultReceiver_.append(GetMessageFromCode(INVALID_PID)); + return ERR_INVALID_VALUE; + } + + if (!(inputLevel == OHOS::AppExecFwk::MemoryLevel::MEMORY_LEVEL_MODERATE || + inputLevel == OHOS::AppExecFwk::MemoryLevel::MEMORY_LEVEL_CRITICAL || + inputLevel == OHOS::AppExecFwk::MemoryLevel::MEMORY_LEVEL_LOW)) { + TAG_LOGE(AAFwkTag::APPMGR, "level value error. Valid values: 0-2 (0: Moderate, 1: Low, 2: Critical)"); + resultReceiver_.append(STRING_SEND_MEMORY_LEVEL_NG + "\n"); + resultReceiver_.append(GetMessageFromCode(INVALID_LEVEL)); + return ERR_INVALID_VALUE; + } + + std::map pidMemoryLevelMap; + pidMemoryLevelMap.emplace(inputPid, inputLevel); + auto result = DelayedSingleton::GetInstance()->NotifyProcMemoryLevel(pidMemoryLevelMap); + if (result == OHOS::ERR_OK) { + TAG_LOGI(AAFwkTag::AA_TOOL, "%{public}s", STRING_SEND_MEMORY_LEVEL_OK.c_str()); + resultReceiver_ = STRING_SEND_MEMORY_LEVEL_OK + "\n"; + } else { + TAG_LOGI(AAFwkTag::AA_TOOL, "%{public}s result: %{public}d", STRING_SEND_MEMORY_LEVEL_NG.c_str(), result); + resultReceiver_ = STRING_SEND_MEMORY_LEVEL_NG + "\n"; + resultReceiver_.append(GetMessageFromCode(result)); + } + + return result; +} + +ErrCode AbilityManagerShellCommand::ParsePidMemoryLevel(std::string &pidParse, std::string &memoryLevelParse) +{ + int option = -1; + int counter = 0; + while (true) { + counter++; + option = getopt_long(argc_, argv_, SHORT_OPTIONS_SEND_MEMORY_LEVEL.c_str(), + LONG_OPTIONS_SEND_MEMORY_LEVEL, nullptr); + TAG_LOGD(AAFwkTag::AA_TOOL, "getopt_long option: %{public}d, optopt: %{public}d, optind: %{public}d", + option, optopt, optind); + + if (optind < 0 || optind > argc_) { + return OHOS::ERR_INVALID_VALUE; + } + // aa command without option + if (option == -1) { + if (counter == 1 && strcmp(argv_[optind], cmd_.c_str()) == 0) { + resultReceiver_.append(HELP_MSG_NO_OPTION + "\n"); + } + break; + } + + switch (option) { + case 'h':{ + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s -h' no arg", cmd_.c_str()); + // 'aa send-memory-level -h' no arg + break; + } + case 'p':{ + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s -p' pid", cmd_.c_str()); + // 'aa send-memory-level -p pid' + pidParse = optarg; + break; + } + case 'l':{ + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s -l' level", cmd_.c_str()); + // 'aa send-memory-level -l level' + memoryLevelParse = optarg; + break; + } + case '?':{ + std::string unknownOption = ""; + std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption); + resultReceiver_.append(unknownOptionMsg); + break; + } + default: + break; + } + } + + return OHOS::ERR_OK; +} + bool AbilityManagerShellCommand::MatchOrderString(const std::regex ®exScript, const std::string &orderCmd) { TAG_LOGD(AAFwkTag::AA_TOOL, "orderCmd: %{public}s", orderCmd.c_str()); diff --git a/tools/test/unittest/aa/BUILD.gn b/tools/test/unittest/aa/BUILD.gn index 99ef92ecb1c1fc3789c6f9bc62e0a7f679d1613d..99137c4d4858a5c24fd9cd93e3011eeeba97f512 100644 --- a/tools/test/unittest/aa/BUILD.gn +++ b/tools/test/unittest/aa/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2024 Huawei Device Co., Ltd. +# Copyright (c) 2021-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 @@ -298,6 +298,34 @@ ohos_unittest("aa_command_attach_test") { ] } +ohos_unittest("aa_command_send_memory_level_test") { + module_out_path = module_output_path + + sources = [ "aa_command_send_memory_level_test.cpp" ] + sources += tools_aa_mock_sources + + configs = [ ":tools_aa_config_mock" ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${ability_runtime_path}/tools/aa:tools_aa_source_set", + "${ability_runtime_services_path}/abilitymgr:abilityms", + ] + + external_deps = [ + "ability_base:configuration", + "bundle_framework:appexecfwk_base", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + ] +} + print("accessibility_enable = ", accessibility_enable) if (accessibility_enable) { ohos_unittest("accessibility_ability_utils_test") { @@ -343,6 +371,7 @@ group("unittest") { ":aa_command_stop_service_test", ":aa_command_test", ":aa_command_test_test", + ":aa_command_send_memory_level_test", ] if (accessibility_enable) { deps += [ ":accessibility_ability_utils_test" ] diff --git a/tools/test/unittest/aa/aa_command_send_memory_level_test.cpp b/tools/test/unittest/aa/aa_command_send_memory_level_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e11b29f29d1c60d769d7912de8ec4d9f13875027 --- /dev/null +++ b/tools/test/unittest/aa/aa_command_send_memory_level_test.cpp @@ -0,0 +1,311 @@ +/* + * 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 + +#define protected public +#define private public +#include "ability_command.h" +#undef protected +#undef private + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::AAFwk; + +namespace { +const std::string STRING_VALID_PID = "520"; +const std::string STRING_INVALID_PID = "-1"; +const std::string STRING_VALID_LEVEL = "1"; +const std::string STRING_INVALID_LEVEL = "3"; +} // namespace + +class AaCommandSendMemoryLevelTest : public ::testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + std::string sendMemoryLevelCmd_ = "send-memory-level"; +}; + +void AaCommandSendMemoryLevelTest::SetUpTestCase() +{} + +void AaCommandSendMemoryLevelTest::TearDownTestCase() +{} + +void AaCommandSendMemoryLevelTest::SetUp() +{ + // reset optind to 0 + optind = 0; +} + +void AaCommandSendMemoryLevelTest::TearDown() +{} + +/** + * @tc.number: Aa_Command_ParsePidMemoryLevel_0100 + * @tc.name: Parse Pid and Level from argv[] + * @tc.desc: Verify that send-memory-level command parse Pid and Level normally. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, ParsePidMemoryLevel_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_ParsePidMemoryLevel_0100"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"-p", + (char*)STRING_VALID_PID.c_str(), + (char*)"-l", + (char*)STRING_VALID_LEVEL.c_str(), + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + std::string pid = ""; + std::string level = ""; + cmd.ParsePidMemoryLevel(pid, level); + EXPECT_EQ(pid, STRING_VALID_PID); + EXPECT_EQ(level, STRING_VALID_LEVEL); +} + +/** + * @tc.number: Aa_Command_ParsePidMemoryLevel_0200 + * @tc.name: Parse Pid and Level from argv[] + * @tc.desc: Verify that send-memory-level command parse Pid and Level unormally. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, ParsePidMemoryLevel_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_ParsePidMemoryLevel_0200"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"-p", + (char*)STRING_INVALID_PID.c_str(), + (char*)"-l", + (char*)STRING_INVALID_LEVEL.c_str(), + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + std::string pid = STRING_VALID_PID; + std::string level = STRING_VALID_LEVEL; + cmd.ParsePidMemoryLevel(pid, level); // 解析后pid的值:STRING_VALID_PID ——> STRING_INVALID_PID + EXPECT_EQ(pid, STRING_INVALID_PID); + EXPECT_EQ(level, STRING_INVALID_LEVEL); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0100 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0100"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_SEND_MEMORY_LEVEL + "\n"); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0200 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level xxx" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0200"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"xxx", + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_NO_OPTION + "\n" + HELP_MSG_SEND_MEMORY_LEVEL + "\n"); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0300 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level -x" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0300, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0300"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"-x", + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_EQ(cmd.ExecCommand(), "fail: unknown option.\n" + HELP_MSG_SEND_MEMORY_LEVEL + "\n"); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0400 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level -h" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0400, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0400"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"-h", + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SEND_MEMORY_LEVEL + "\n"); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0500 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level --help" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0500, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0500"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"--help", + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SEND_MEMORY_LEVEL + "\n"); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0600 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level -p -l" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0600, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0600"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"-p", + (char*)"-l", + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_EQ(cmd.ExecCommand(), HELP_MSG_SEND_MEMORY_LEVEL + "\n"); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0700 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level -p -1 -l 3" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0700, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0700"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"-p", + (char*)STRING_INVALID_PID.c_str(), + (char*)"-l", + (char*)STRING_INVALID_LEVEL.c_str(), + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_TRUE(cmd.ExecCommand().find(STRING_SEND_MEMORY_LEVEL_NG) != string::npos); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0800 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level -p -1 -l 1" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0800, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0800"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"-p", + (char*)STRING_INVALID_PID.c_str(), + (char*)"-l", + (char*)STRING_VALID_LEVEL.c_str(), + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_TRUE(cmd.ExecCommand().find(STRING_SEND_MEMORY_LEVEL_NG) != string::npos); +} + +/** + * @tc.number: Aa_Command_SendMemoryLevel_0900 + * @tc.name: ExecCommand + * @tc.desc: Verify the "aa send-memory-level -p -1 -l 3" command. + */ +HWTEST_F(AaCommandSendMemoryLevelTest, Aa_Command_SendMemoryLevel_0900, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "Aa_Command_SendMemoryLevel_0900"; + + char* argv[] = { + (char*)TOOL_NAME.c_str(), + (char*)sendMemoryLevelCmd_.c_str(), + (char*)"-p", + (char*)STRING_INVALID_PID.c_str(), + (char*)"-l", + (char*)STRING_INVALID_LEVEL.c_str(), + (char*)"", + }; + int32_t argc = sizeof(argv) / sizeof(argv[0]) - 1; + + AbilityManagerShellCommand cmd(argc, argv); + EXPECT_TRUE(cmd.ExecCommand().find(STRING_SEND_MEMORY_LEVEL_NG) != string::npos); +} \ No newline at end of file