From c8c2167cd1bc422e29ff7b131604b26666ac1ffd Mon Sep 17 00:00:00 2001 From: gaorui Date: Thu, 12 Jun 2025 09:31:53 +0800 Subject: [PATCH] fix: cJSON code optimize and add tests Signed-off-by: gaorui Change-Id: Ieca78bf83ca33d930b1f7fb1e64f416dd72e1d8f --- services/native/include/power_mode_policy.h | 1 + services/native/src/power_mode_policy.cpp | 34 +- .../native/src/shutdown/shutdown_dialog.cpp | 11 +- .../native/src/shutdown/shutdown_dialog.h | 1 + .../src/suspend/suspend_source_parser.cpp | 7 +- .../native/src/wakeup/wakeup_controller.cpp | 35 +- .../native/src/wakeup/wakeup_controller.h | 3 + .../src/wakeup/wakeup_source_parser.cpp | 9 +- .../wakeup_action_source_parser.cpp | 11 +- test/unittest/BUILD.gn | 72 +- .../power_wakeup_controller_test.cpp | 2 +- test/unittest/src/power_config_parse_test.cpp | 637 +++++++++++++++++- .../src/power_config_parse_test_two.cpp | 58 ++ utils/native/include/power_cjson_utils.h | 63 ++ utils/vibrator/src/vibrator_source_parser.cpp | 9 +- 15 files changed, 914 insertions(+), 39 deletions(-) create mode 100644 test/unittest/src/power_config_parse_test_two.cpp create mode 100644 utils/native/include/power_cjson_utils.h diff --git a/services/native/include/power_mode_policy.h b/services/native/include/power_mode_policy.h index fd46f943..dd7e1cf3 100644 --- a/services/native/include/power_mode_policy.h +++ b/services/native/include/power_mode_policy.h @@ -49,6 +49,7 @@ public: void TriggerAllActions(bool isBoot); bool IsValidType(uint32_t type); bool InitRecoverMap(); + bool ParseRecoverJson(std::string& jsonStr); private: std::map actionMap_; diff --git a/services/native/src/power_mode_policy.cpp b/services/native/src/power_mode_policy.cpp index 0b519011..815a9468 100644 --- a/services/native/src/power_mode_policy.cpp +++ b/services/native/src/power_mode_policy.cpp @@ -14,6 +14,7 @@ */ #include +#include "power_cjson_utils.h" #include "power_mode_policy.h" #include "power_mgr_service.h" #include "power_log.h" @@ -70,26 +71,46 @@ void PowerModePolicy::ComparePowerModePolicy() bool PowerModePolicy::InitRecoverMap() { std::string jsonStr = SettingHelper::ReadPowerModeRecoverMap(); + if (!ParseRecoverJson(jsonStr)) { + POWER_HILOGI(FEATURE_POWER_MODE, "init recover map error"); + return false; + } + POWER_HILOGI(FEATURE_POWER_MODE, "init recover map succeed"); + return true; +} +bool PowerModePolicy::ParseRecoverJson(std::string& jsonStr) +{ cJSON* recoverJson = cJSON_Parse(jsonStr.c_str()); if (!recoverJson) { POWER_HILOGW(FEATURE_POWER_MODE, "parse recover json str error"); return false; } - if (!cJSON_IsObject(recoverJson)) { - POWER_HILOGW(FEATURE_POWER_MODE, "recover json root is not an object"); + if (!PowerMgrJsonUtils::IsValidJsonObjectOrJsonArray(recoverJson)) { + POWER_HILOGW(FEATURE_POWER_MODE, "recover json root is not object or array , string:%{public}s", + jsonStr.c_str()); cJSON_Delete(recoverJson); return false; } cJSON* item = nullptr; cJSON_ArrayForEach(item, recoverJson) { const char* keyStr = item->string; - if (!keyStr || !cJSON_IsNumber(item)) { + if (!keyStr || !PowerMgrJsonUtils::IsValidJsonNumber(item)) { continue; } errno = 0; - int32_t key = static_cast(strtol(keyStr, nullptr, KEY_BASE)); + char* endptr = nullptr; + int32_t key = static_cast(strtol(keyStr, &endptr, KEY_BASE)); + if (endptr == keyStr) { + POWER_HILOGW(FEATURE_POWER_MODE, "String have no numbers, string:%{public}s", keyStr); + continue; + } if (errno == ERANGE && (key == INT_MAX || key == INT_MIN)) { + POWER_HILOGW(FEATURE_POWER_MODE, "Transit result out of range, string:%{public}s", keyStr); + continue; + } + if (endptr == nullptr || *endptr != '\0') { + POWER_HILOGW(FEATURE_POWER_MODE, "String contain non-numeric characters, string:%{public}s", keyStr); continue; } int32_t value = static_cast(item->valueint); @@ -97,7 +118,6 @@ bool PowerModePolicy::InitRecoverMap() } cJSON_Delete(recoverJson); - POWER_HILOGI(FEATURE_POWER_MODE, "init recover map succeed"); return true; } @@ -216,7 +236,9 @@ void PowerModePolicy::SavePowerModeRecoverMap() for (const auto& pair : recoverMap_) { std::string keyStr = std::to_string(pair.first); - cJSON_AddNumberToObject(recoverJson, keyStr.c_str(), pair.second); + if (cJSON_AddNumberToObject(recoverJson, keyStr.c_str(), pair.second) == nullptr) { + POWER_HILOGE(FEATURE_POWER_MODE, "Failed to add %{public}s to recoverJson object", keyStr.c_str()); + } } char* jsonStr = cJSON_Print(recoverJson); diff --git a/services/native/src/shutdown/shutdown_dialog.cpp b/services/native/src/shutdown/shutdown_dialog.cpp index ab3d15c1..b04bcb41 100644 --- a/services/native/src/shutdown/shutdown_dialog.cpp +++ b/services/native/src/shutdown/shutdown_dialog.cpp @@ -31,6 +31,7 @@ #include #include "config_policy_utils.h" +#include "power_cjson_utils.h" #include "power_log.h" #include "power_mgr_service.h" #include "power_vibrator.h" @@ -190,7 +191,11 @@ void ShutdownDialog::LoadDialogConfig() POWER_HILOGE(COMP_UTILS, "json file is empty"); return; } + ParseJsonConfig(contentStr); +} +void ShutdownDialog::ParseJsonConfig(std::string& contentStr) +{ cJSON* root = cJSON_Parse(contentStr.c_str()); if (!root) { POWER_HILOGE(COMP_UTILS, "json parse error[%{public}s]", contentStr.c_str()); @@ -206,9 +211,9 @@ void ShutdownDialog::LoadDialogConfig() cJSON* bundleNameItem = cJSON_GetObjectItemCaseSensitive(root, "bundleName"); cJSON* abilityNameItem = cJSON_GetObjectItemCaseSensitive(root, "abilityName"); cJSON* uiExtensionTypeItem = cJSON_GetObjectItemCaseSensitive(root, "uiExtensionType"); - if (!bundleNameItem || !cJSON_IsString(bundleNameItem) || - !abilityNameItem || !cJSON_IsString(abilityNameItem) || - !uiExtensionTypeItem || !cJSON_IsString(uiExtensionTypeItem)) { + if (!PowerMgrJsonUtils::IsValidJsonString(bundleNameItem) || + !PowerMgrJsonUtils::IsValidJsonString(abilityNameItem) || + !PowerMgrJsonUtils::IsValidJsonString(uiExtensionTypeItem)) { POWER_HILOGE(COMP_UTILS, "json variable not supported"); cJSON_Delete(root); return; diff --git a/services/native/src/shutdown/shutdown_dialog.h b/services/native/src/shutdown/shutdown_dialog.h index d495c091..27e3e37e 100644 --- a/services/native/src/shutdown/shutdown_dialog.h +++ b/services/native/src/shutdown/shutdown_dialog.h @@ -35,6 +35,7 @@ public: void ResetLongPressFlag(); void StartVibrator(); void LoadDialogConfig(); + void ParseJsonConfig(std::string& contentStr); static std::string GetBundleName() { return bundleName_; diff --git a/services/native/src/suspend/suspend_source_parser.cpp b/services/native/src/suspend/suspend_source_parser.cpp index 66f9f749..4f27964b 100644 --- a/services/native/src/suspend/suspend_source_parser.cpp +++ b/services/native/src/suspend/suspend_source_parser.cpp @@ -21,6 +21,7 @@ #include #include "config_policy_utils.h" +#include "power_cjson_utils.h" #include "power_log.h" #include "setting_helper.h" #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING @@ -158,7 +159,7 @@ std::shared_ptr SuspendSourceParser::ParseSources(const std::str parseSources->SetParseErrorFlag(true); return parseSources; } - if (!cJSON_IsObject(root)) { + if (!PowerMgrJsonUtils::IsValidJsonObjectOrJsonArray(root)) { POWER_HILOGE(FEATURE_SUSPEND, "json root invalid[%{public}s]", jsonStr.c_str()); parseSources->SetParseErrorFlag(true); cJSON_Delete(root); @@ -199,10 +200,10 @@ bool SuspendSourceParser::ParseSourcesProc( uint32_t action = 0; uint32_t delayMs = 0; - if (valueObj && cJSON_IsObject(valueObj)) { + if (PowerMgrJsonUtils::IsValidJsonObject(valueObj)) { cJSON* actionValue = cJSON_GetObjectItemCaseSensitive(valueObj, SuspendSource::ACTION_KEY); cJSON* delayValue = cJSON_GetObjectItemCaseSensitive(valueObj, SuspendSource::DELAY_KEY); - if (actionValue && cJSON_IsNumber(actionValue) && delayValue && cJSON_IsNumber(delayValue)) { + if (PowerMgrJsonUtils::IsValidJsonNumber(actionValue) && PowerMgrJsonUtils::IsValidJsonNumber(delayValue)) { action = static_cast(actionValue->valueint); delayMs = static_cast(delayValue->valueint); if (action >= ILLEGAL_ACTION) { diff --git a/services/native/src/wakeup/wakeup_controller.cpp b/services/native/src/wakeup/wakeup_controller.cpp index d4e7d527..e8507f2d 100644 --- a/services/native/src/wakeup/wakeup_controller.cpp +++ b/services/native/src/wakeup/wakeup_controller.cpp @@ -28,6 +28,7 @@ #include #include #include "permission.h" +#include "power_cjson_utils.h" #include "power_errors.h" #include "power_log.h" #include "power_mgr_service.h" @@ -198,6 +199,11 @@ void WakeupController::ChangeWakeupSourceConfig(bool updateEnable) return; } POWER_HILOGI(COMP_SVC, "the origin ccmJson is: %{public}s", jsonStr.c_str()); + WakeupParseJsonConfig(updateEnable, jsonStr); +} + +void WakeupController::WakeupParseJsonConfig(bool updateEnable, std::string& jsonStr) +{ cJSON* root = cJSON_Parse(jsonStr.c_str()); if (!root) { POWER_HILOGE(COMP_SVC, "json parse error"); @@ -209,13 +215,13 @@ void WakeupController::ChangeWakeupSourceConfig(bool updateEnable) return; } cJSON* touchscreenNode = cJSON_GetObjectItemCaseSensitive(root, "touchscreen"); - if (!touchscreenNode || !cJSON_IsObject(touchscreenNode)) { + if (!PowerMgrJsonUtils::IsValidJsonObject(touchscreenNode)) { POWER_HILOGE(COMP_SVC, "this touchscreenNode is empty"); cJSON_Delete(root); return; } cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(touchscreenNode, "enable"); - if (!enableNode || !cJSON_IsBool(enableNode)) { + if (!PowerMgrJsonUtils::IsValidJsonBool(enableNode)) { POWER_HILOGE(COMP_SVC, "the touchscreenNode enable value is invalid"); cJSON_Delete(root); return; @@ -226,7 +232,7 @@ void WakeupController::ChangeWakeupSourceConfig(bool updateEnable) cJSON_Delete(root); return; } - enableNode->valueint = updateEnable ? 1 : 0; + cJSON_SetBoolValue(enableNode, updateEnable ? 1 : 0); char* jsonUpdatedStr = cJSON_Print(root); if (!jsonUpdatedStr) { POWER_HILOGI(COMP_SVC, "Failed to print cJSON to string"); @@ -319,6 +325,11 @@ void WakeupController::ChangePickupWakeupSourceConfig(bool updataEnable) POWER_HILOGE(COMP_SVC, "there is no such configuration file available"); return; } + PickupWakeupParseJsonConfig(updataEnable, jsonStr); +} + +void WakeupController::PickupWakeupParseJsonConfig(bool updataEnable, std::string& jsonStr) +{ POWER_HILOGI(COMP_SVC, "%{public}s(%{public}d)", __func__, updataEnable); cJSON* root = cJSON_Parse(jsonStr.c_str()); if (!root) { @@ -331,13 +342,13 @@ void WakeupController::ChangePickupWakeupSourceConfig(bool updataEnable) return; } cJSON* pickupNode = cJSON_GetObjectItemCaseSensitive(root, "pickup"); - if (!pickupNode || !cJSON_IsObject(pickupNode)) { + if (!PowerMgrJsonUtils::IsValidJsonObject(pickupNode)) { POWER_HILOGE(COMP_SVC, "this pickNode is empty"); cJSON_Delete(root); return; } cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(pickupNode, "enable"); - if (!enableNode || !cJSON_IsBool(enableNode)) { + if (!PowerMgrJsonUtils::IsValidJsonBool(enableNode)) { POWER_HILOGE(COMP_SVC, "the pickupNode enable value is invalid"); cJSON_Delete(root); return; @@ -348,7 +359,7 @@ void WakeupController::ChangePickupWakeupSourceConfig(bool updataEnable) cJSON_Delete(root); return; } - enableNode->valueint = updataEnable ? 1 : 0; + cJSON_SetBoolValue(enableNode, updataEnable ? 1 : 0); char* jsonUpdatedStr = cJSON_Print(root); if (!jsonUpdatedStr) { POWER_HILOGI(COMP_SVC, "Failed to print cJSON to string"); @@ -368,7 +379,11 @@ void WakeupController::ChangeLidWakeupSourceConfig(bool updataEnable) std::lock_guard lock(sourceUpdateMutex_); std::string jsonStr = SettingHelper::GetSettingWakeupSources(); POWER_HILOGI(FEATURE_POWER_STATE, "%{public}s", jsonStr.c_str()); + LidWakeupParseJsonConfig(updataEnable, jsonStr); +} +void WakeupController::LidWakeupParseJsonConfig(bool updataEnable, std::string& jsonStr) +{ cJSON* root = cJSON_Parse(jsonStr.c_str()); if (!root) { POWER_HILOGE(FEATURE_POWER_STATE, "Failed to parse json string"); @@ -380,14 +395,14 @@ void WakeupController::ChangeLidWakeupSourceConfig(bool updataEnable) return; } cJSON* lidNode = cJSON_GetObjectItemCaseSensitive(root, "lid"); - if (!lidNode || !cJSON_IsObject(lidNode)) { + if (!PowerMgrJsonUtils::IsValidJsonObject(lidNode)) { POWER_HILOGE(FEATURE_POWER_STATE, "this lidNode is empty or not an object"); cJSON_Delete(root); return; } bool originEnable = true; cJSON* enableNode = cJSON_GetObjectItemCaseSensitive(lidNode, "enable"); - if (enableNode && cJSON_IsBool(enableNode)) { + if (PowerMgrJsonUtils::IsValidJsonBool(enableNode)) { originEnable = cJSON_IsTrue(enableNode); } if (originEnable == updataEnable) { @@ -396,8 +411,8 @@ void WakeupController::ChangeLidWakeupSourceConfig(bool updataEnable) return; } - if (enableNode && cJSON_IsBool(enableNode)) { - enableNode->valueint = updataEnable ? 1 : 0; + if (PowerMgrJsonUtils::IsValidJsonBool(enableNode)) { + cJSON_SetBoolValue(enableNode, updataEnable ? 1 : 0); } char* jsonUpdatedStr = cJSON_Print(root); if (!jsonUpdatedStr) { diff --git a/services/native/src/wakeup/wakeup_controller.h b/services/native/src/wakeup/wakeup_controller.h index baa8f27e..029531ae 100644 --- a/services/native/src/wakeup/wakeup_controller.h +++ b/services/native/src/wakeup/wakeup_controller.h @@ -56,9 +56,12 @@ public: #endif static int32_t SetWakeupDoubleClickSensor(bool enable); static void ChangeWakeupSourceConfig(bool updateEnable); + static void WakeupParseJsonConfig(bool updateEnable, std::string& jsonStr); static void ChangePickupWakeupSourceConfig(bool updataEnable); + static void PickupWakeupParseJsonConfig(bool updataEnable, std::string& jsonStr); static void PickupConnectMotionConfig(bool databaseSwitchValue); static void ChangeLidWakeupSourceConfig(bool updataEnable); + static void LidWakeupParseJsonConfig(bool updataEnable, std::string& jsonStr); std::shared_ptr GetStateMachine() { return stateMachine_; diff --git a/services/native/src/wakeup/wakeup_source_parser.cpp b/services/native/src/wakeup/wakeup_source_parser.cpp index 337a787f..bda74909 100644 --- a/services/native/src/wakeup/wakeup_source_parser.cpp +++ b/services/native/src/wakeup/wakeup_source_parser.cpp @@ -18,6 +18,7 @@ #include #include "config_policy_utils.h" +#include "power_cjson_utils.h" #include "power_log.h" #include "setting_helper.h" #include "wakeup_source_parser.h" @@ -115,7 +116,7 @@ std::shared_ptr WakeupSourceParser::ParseSources(const std::strin return parseSources; } - if (!cJSON_IsObject(root)) { + if (!PowerMgrJsonUtils::IsValidJsonObjectOrJsonArray(root)) { POWER_HILOGE(FEATURE_WAKEUP, "json root invalid[%{public}s]", jsonStr.c_str()); parseSources->SetParseErrorFlag(true); cJSON_Delete(root); @@ -147,13 +148,13 @@ bool WakeupSourceParser::ParseSourcesProc( bool enable = true; uint32_t click = DOUBLE_CLICK; WakeupDeviceType wakeupDeviceType = WakeupDeviceType::WAKEUP_DEVICE_UNKNOWN; - if (valueObj && cJSON_IsObject(valueObj)) { + if (PowerMgrJsonUtils::IsValidJsonObject(valueObj)) { cJSON* enableValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupSource::ENABLE_KEY); - if (enableValue && cJSON_IsBool(enableValue)) { + if (PowerMgrJsonUtils::IsValidJsonBool(enableValue)) { enable = cJSON_IsTrue(enableValue); } cJSON* clickValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupSource::KEYS_KEY); - if (clickValue && cJSON_IsNumber(clickValue)) { + if (PowerMgrJsonUtils::IsValidJsonNumber(clickValue)) { uint32_t clickInt = static_cast(clickValue->valueint); click = (clickInt == SINGLE_CLICK || clickInt == DOUBLE_CLICK) ? clickInt : DOUBLE_CLICK; } diff --git a/services/native/src/wakeup_action/wakeup_action_source_parser.cpp b/services/native/src/wakeup_action/wakeup_action_source_parser.cpp index 5cf07df3..0e7e232c 100644 --- a/services/native/src/wakeup_action/wakeup_action_source_parser.cpp +++ b/services/native/src/wakeup_action/wakeup_action_source_parser.cpp @@ -20,6 +20,7 @@ #include #include "config_policy_utils.h" +#include "power_cjson_utils.h" #include "power_log.h" namespace OHOS { @@ -84,8 +85,8 @@ std::shared_ptr WakeupActionSourceParser::ParseSources(cons return parseSources; } - if (!cJSON_IsObject(root)) { - POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json root invalid"); + if (!PowerMgrJsonUtils::IsValidJsonObjectOrJsonArray(root)) { + POWER_HILOGE(FEATURE_WAKEUP_ACTION, "json root invalid[%{public}s]", jsonStr.c_str()); cJSON_Delete(root); return parseSources; } @@ -115,14 +116,14 @@ bool WakeupActionSourceParser::ParseSourcesProc( { std::string scene{""}; uint32_t action = 0; - if (valueObj && cJSON_IsObject(valueObj)) { + if (PowerMgrJsonUtils::IsValidJsonObject(valueObj)) { cJSON* sceneValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::SCENE_KEY); - if (sceneValue && cJSON_IsString(sceneValue)) { + if (PowerMgrJsonUtils::IsValidJsonString(sceneValue)) { scene = sceneValue->valuestring; POWER_HILOGI(FEATURE_WAKEUP_ACTION, "scene=%{public}s", scene.c_str()); } cJSON* actionValue = cJSON_GetObjectItemCaseSensitive(valueObj, WakeupActionSource::ACTION_KEY); - if (actionValue && cJSON_IsNumber(actionValue)) { + if (PowerMgrJsonUtils::IsValidJsonNumber(actionValue)) { action = static_cast(actionValue->valueint); POWER_HILOGI(FEATURE_WAKEUP_ACTION, "action=%{public}u", action); if (action >= ILLEGAL_ACTION) { diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 50847cb4..3f5b2bf1 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -1309,9 +1309,78 @@ ohos_unittest("test_power_config_parse") { "${powermgr_service_path}:powermgrservice", "${powermgr_service_path}/native/src/actions:powermgr_actions", "${powermgr_utils_path}/setting:power_setting", + "${powermgr_utils_path}/vibrator:power_vibrator", ] - external_deps = deps_ex + external_deps = [ + "ability_base:zuri", + "ability_runtime:ability_manager", + "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + external_deps += deps_ex + + if (power_manager_feature_doubleclick) { + defines += [ "POWER_DOUBLECLICK_ENABLE" ] + } + + if (power_manager_feature_pickup) { + defines += [ "POWER_PICKUP_ENABLE" ] + } +} + +##############################test_power_config_parse_two.############################# +ohos_unittest("test_power_config_parse_two") { + module_out_path = module_output_path + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + + sources = [ + "src/power_config_parse_test_two.cpp", + ] + + configs = [ + "${powermgr_utils_path}:utils_config", + ":module_private_config", + ":module_mock_private_config", + "${powermgr_utils_path}:coverage_flags", + ] + + include_dirs = [ "${powermgr_service_path}/native/src/setting/" ] + + deps = [ + "${powermgr_inner_api}:powermgr_client", + "${powermgr_service_path}:powermgr_stub", + "${powermgr_service_path}:powermgrservice", + "${powermgr_service_path}/native/src/actions:powermgr_actions", + "${powermgr_utils_path}/setting:power_setting", + "${powermgr_utils_path}/vibrator:power_vibrator", + ] + + external_deps = [ + "ability_base:zuri", + "ability_runtime:ability_manager", + "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + external_deps += deps_ex + + if (power_manager_feature_doubleclick) { + defines += [ "POWER_DOUBLECLICK_ENABLE" ] + } + + if (power_manager_feature_pickup) { + defines += [ "POWER_PICKUP_ENABLE" ] + } } group("unittest") { @@ -1324,6 +1393,7 @@ group("unittest") { ":test_mock_peer", ":test_mock_proxy", ":test_power_config_parse", + ":test_power_config_parse_two", ":test_power_coordination_lock", ":test_power_device_mode", ":test_power_getcontroller_mock", diff --git a/test/unittest/src/interface_test/power_wakeup_controller_test.cpp b/test/unittest/src/interface_test/power_wakeup_controller_test.cpp index 6389ced3..9387d517 100644 --- a/test/unittest/src/interface_test/power_wakeup_controller_test.cpp +++ b/test/unittest/src/interface_test/power_wakeup_controller_test.cpp @@ -385,7 +385,7 @@ HWTEST_F(PowerWakeupControllerTest, PowerWakeupControllerTest011, TestSize.Level GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: json parse error"; return; } - if (!cJSON_IsObject(root)) { + if (!cJSON_IsObject(root) || !cJSON_IsArray(root)) { GTEST_LOG_(INFO) << "PowerWakeupControllerTest011: root is not object"; cJSON_Delete(root); return; diff --git a/test/unittest/src/power_config_parse_test.cpp b/test/unittest/src/power_config_parse_test.cpp index d3b56dc6..33534e45 100644 --- a/test/unittest/src/power_config_parse_test.cpp +++ b/test/unittest/src/power_config_parse_test.cpp @@ -15,7 +15,13 @@ #include #include #include "power_mgr_service.h" +#include "power_mode_policy.h" #include "power_log.h" +#include "setting_helper.h" +#include "shutdown_dialog.h" +#include "vibrator_source_parser.h" +#include "wakeup_controller.h" +#include "wakeup_source_parser.h" using namespace testing; using namespace testing::ext; @@ -37,14 +43,29 @@ public: cJSON* root_; }; +void SettingHelper::SetSettingWakeupSources(const std::string& jsonConfig) +{ +} + +void SettingHelper::SavePowerModeRecoverMap(const std::string& jsonConfig) +{ +} namespace { +static sptr g_service; constexpr size_t NUMBER_ONE = 1; constexpr size_t NUMBER_TWO = 2; +constexpr int KEY1 = 10; +constexpr int KEY2 = 20; +constexpr int KEY3 = 30; +constexpr int VALUE1 = 100; +constexpr int VALUE2 = 200; +constexpr int VALUE3 = 300; HWTEST_F(PowerConfigParseTest, PowerConfigParseTest001, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest001 function start!"); + EXPECT_TRUE(root_); cJSON_AddItemToObject(root_, "action", cJSON_CreateNumber(NUMBER_ONE)); cJSON_AddItemToObject(root_, "delayMs", cJSON_CreateNumber(NUMBER_TWO)); @@ -64,6 +85,7 @@ HWTEST_F(PowerConfigParseTest, PowerConfigParseTest001, TestSize.Level0) HWTEST_F(PowerConfigParseTest, PowerConfigParseTest002, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest002 function start!"); + EXPECT_TRUE(root_); cJSON* valueObj = cJSON_CreateNumber(NUMBER_TWO); cJSON_AddItemToObject(root_, "action", valueObj); std::string key = "lid"; @@ -81,6 +103,7 @@ HWTEST_F(PowerConfigParseTest, PowerConfigParseTest002, TestSize.Level0) HWTEST_F(PowerConfigParseTest, PowerConfigParseTest003, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest003 function start!"); + EXPECT_TRUE(root_); std::string key = "lid"; std::shared_ptr parseSources = std::make_shared(); bool result = SuspendSourceParser::ParseSourcesProc(parseSources, nullptr, key); @@ -96,6 +119,7 @@ HWTEST_F(PowerConfigParseTest, PowerConfigParseTest003, TestSize.Level0) HWTEST_F(PowerConfigParseTest, PowerConfigParseTest004, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest004 function start!"); + EXPECT_TRUE(root_); cJSON_AddItemToObject(root_, "action", cJSON_CreateNumber(NUMBER_ONE)); std::string key = "lid"; @@ -114,8 +138,9 @@ HWTEST_F(PowerConfigParseTest, PowerConfigParseTest004, TestSize.Level0) HWTEST_F(PowerConfigParseTest, PowerConfigParseTest005, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest005 function start!"); + EXPECT_TRUE(root_); cJSON_AddItemToObject(root_, "delayMs", cJSON_CreateNumber(NUMBER_TWO)); - + std::string key = "lid"; std::shared_ptr parseSources = std::make_shared(); bool result = SuspendSourceParser::ParseSourcesProc(parseSources, root_, key); @@ -132,6 +157,7 @@ HWTEST_F(PowerConfigParseTest, PowerConfigParseTest005, TestSize.Level0) HWTEST_F(PowerConfigParseTest, PowerConfigParseTest006, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest006 function start!"); + EXPECT_TRUE(root_); const std::string json = R"({"lid": {"action": 1, "delayMs": 2}})"; auto parseSources = SuspendSourceParser::ParseSources(json); std::vector sources = parseSources->GetSourceList(); @@ -139,10 +165,10 @@ HWTEST_F(PowerConfigParseTest, PowerConfigParseTest006, TestSize.Level0) POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest006 function end!"); } - HWTEST_F(PowerConfigParseTest, PowerConfigParseTest007, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest007 function start!"); + EXPECT_TRUE(root_); auto parseSources = SuspendSourceParser::ParseSources(""); std::vector sources = parseSources->GetSourceList(); EXPECT_EQ(sources.size(), 0); @@ -152,9 +178,616 @@ HWTEST_F(PowerConfigParseTest, PowerConfigParseTest007, TestSize.Level0) HWTEST_F(PowerConfigParseTest, PowerConfigParseTest008, TestSize.Level0) { POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest008 function start!"); + EXPECT_TRUE(root_); auto parseSources = SuspendSourceParser::ParseSources("[]"); std::vector sources = parseSources->GetSourceList(); EXPECT_EQ(sources.size(), 0); POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest008 function end!"); } + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest010, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest010 function start!"); + EXPECT_TRUE(root_); + PowerModePolicy powerModePolicy; + powerModePolicy.InitRecoverMap(); + std::string json = ""; + bool ret = powerModePolicy.ParseRecoverJson(json); + EXPECT_FALSE(ret); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest010 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest011, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest011 function start!"); + EXPECT_TRUE(root_); + PowerModePolicy powerModePolicy; + std::string json = R"("this is a string")"; + bool ret = powerModePolicy.ParseRecoverJson(json); + EXPECT_FALSE(ret); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest011 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest012, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest012 function start!"); + EXPECT_TRUE(root_); + PowerModePolicy powerModePolicy; + std::string json = R"({invalid:json)"; + bool ret = powerModePolicy.ParseRecoverJson(json); + EXPECT_FALSE(ret); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest012 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest013, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest013 function start!"); + EXPECT_TRUE(root_); + PowerModePolicy powerModePolicy; + std::string json = R"({"10": 100, "20": 200, "30": 300})"; + bool ret = powerModePolicy.ParseRecoverJson(json); + EXPECT_TRUE(ret); + EXPECT_EQ(powerModePolicy.recoverMap_.size(), 3); + EXPECT_EQ(powerModePolicy.recoverMap_[KEY1], VALUE1); + EXPECT_EQ(powerModePolicy.recoverMap_[KEY2], VALUE2); + EXPECT_EQ(powerModePolicy.recoverMap_[KEY3], VALUE3); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest013 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest014, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest014 function start!"); + EXPECT_TRUE(root_); + PowerModePolicy powerModePolicy; + std::string json = R"({"abc": 100, "10x": 200})"; + bool ret = powerModePolicy.ParseRecoverJson(json); + EXPECT_TRUE(ret); + EXPECT_TRUE(powerModePolicy.recoverMap_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest014 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest015, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest015 function start!"); + EXPECT_TRUE(root_); + PowerModePolicy powerModePolicy; + powerModePolicy.recoverMap_[10] = 20; + powerModePolicy.SavePowerModeRecoverMap(); + std::string json = R"({"22222222222222222222222222": 100, "10x": 200})"; + bool ret = powerModePolicy.ParseRecoverJson(json); + EXPECT_TRUE(ret); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest015 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest016, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest016 function start!"); + EXPECT_TRUE(root_); + PowerModePolicy powerModePolicy; + std::string json = R"({"10": "100", "20": "200", "30": "300"})"; + bool ret = powerModePolicy.ParseRecoverJson(json); + EXPECT_TRUE(ret); + EXPECT_TRUE(powerModePolicy.recoverMap_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest016 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest018, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest018 function start!"); + EXPECT_TRUE(root_); + ShutdownDialog shutdownDialog; + shutdownDialog.LoadDialogConfig(); + std::string json(""); + shutdownDialog.ParseJsonConfig(json); + EXPECT_FALSE(shutdownDialog.bundleName_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest018 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest019, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest019 function start!"); + EXPECT_TRUE(root_); + ShutdownDialog shutdownDialog; + std::string json("[]"); + shutdownDialog.ParseJsonConfig(json); + EXPECT_FALSE(shutdownDialog.bundleName_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest019 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest020, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest020 function start!"); + EXPECT_TRUE(root_); + ShutdownDialog shutdownDialog; + std::string json = R"({"uiExtensionType": "sysDialog/power"})"; + shutdownDialog.ParseJsonConfig(json); + EXPECT_FALSE(shutdownDialog.bundleName_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest020 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest021, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest021 function start!"); + EXPECT_TRUE(root_); + ShutdownDialog shutdownDialog; + std::string json = R"({"bundleName22": "test"})"; + shutdownDialog.ParseJsonConfig(json); + EXPECT_FALSE(shutdownDialog.bundleName_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest021 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest022, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest022 function start!"); + EXPECT_TRUE(root_); + ShutdownDialog shutdownDialog; + std::string json = R"({"bundleName": "com.ohos.powerdialog"})"; + shutdownDialog.ParseJsonConfig(json); + EXPECT_FALSE(shutdownDialog.bundleName_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest022 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest023, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest023 function start!"); + EXPECT_TRUE(root_); + ShutdownDialog shutdownDialog; + std::string json = R"({"abilityName": "PowerUiExtensionAbility"})"; + shutdownDialog.ParseJsonConfig(json); + EXPECT_FALSE(shutdownDialog.bundleName_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest023 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest024, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest024 function start!"); + EXPECT_TRUE(root_); + cJSON* array = cJSON_CreateArray(); + cJSON_AddItemToObject(root_, "points", array); + std::string key = "lid"; + std::shared_ptr parseSources = std::make_shared(); + bool result = SuspendSourceParser::ParseSourcesProc(parseSources, array, key); + + EXPECT_TRUE(result); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), NUMBER_ONE); + EXPECT_EQ(sources[0].reason_, SuspendDeviceType::SUSPEND_DEVICE_REASON_LID); + EXPECT_EQ(sources[0].action_, 0); + EXPECT_EQ(sources[0].delayMs_, 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest024 function end!"); +} +#ifdef POWER_DOUBLECLICK_ENABLE +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest025, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest025 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json(""); + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->WakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest025 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest026, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest026 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json("[]"); + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->WakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest026 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest027, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest027 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json = R"({"touchscreen": []})"; + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->WakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest027 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest028, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest028 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json = R"({"touchscreen": {"enable": "123"}})"; + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->WakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest028 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest029, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest029 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json = R"({"touchscreen": {"enable": false}})"; + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->WakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest029 function end!"); +} +#endif + +#ifdef POWER_PICKUP_ENABLE +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest030, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest030 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json = R"({"pickup": {"enable": "123"}})"; + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->PickupWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest030 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest031, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest031 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json(""); + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->PickupWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest031 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest032, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest032 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json("[]"); + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->PickupWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest032 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest033, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest033 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json = R"({"pickup": []})"; + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->PickupWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest033 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest034, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest034 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json = R"({"pickup": {"enable": false}})"; + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->PickupWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest034 function end!"); +} +#endif + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest035, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest035 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json = R"({"lid": {"enable": "123"}})"; + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->LidWakeupParseJsonConfig(false, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest035 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest036, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest036 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json(""); + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->LidWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest036 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest037, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest037 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json("[]"); + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->LidWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest037 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest038, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest038 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + std::string json = R"({"lid": []})"; + EXPECT_TRUE(wakeupController != nullptr); + wakeupController->LidWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest038 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest039, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest039 function start!"); + EXPECT_TRUE(root_); + g_service = DelayedSpSingleton::GetInstance(); + g_service->OnStart(); + std::shared_ptr stateMachine = g_service->GetPowerStateMachine(); + std::shared_ptr wakeupController = std::make_shared(stateMachine); + EXPECT_TRUE(wakeupController != nullptr); + std::string json = R"({"lid": {"enable": false}})"; + wakeupController->LidWakeupParseJsonConfig(true, json); + g_service->OnStop(); + DelayedSpSingleton::DestroyInstance(); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest039 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest040, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest040 function start!"); + EXPECT_TRUE(root_); + const std::string json = "[]"; + auto parseSources = WakeupSourceParser::ParseSources(json); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest040 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest041, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest041 function start!"); + EXPECT_TRUE(root_); + std::string key = "test"; + std::shared_ptr parseSources = std::make_shared(); + bool result = WakeupSourceParser::ParseSourcesProc(parseSources, nullptr, key); + EXPECT_FALSE(result); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest041 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest042, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest042 function start!"); + EXPECT_TRUE(root_); + cJSON_AddItemToObject(root_, WakeupSource::ENABLE_KEY, cJSON_CreateBool(false)); + cJSON_AddItemToObject(root_, WakeupSource::KEYS_KEY, cJSON_CreateNumber(NUMBER_TWO)); + + std::string key = "test"; + std::shared_ptr parseSources = std::make_shared(); + bool result = WakeupSourceParser::ParseSourcesProc(parseSources, root_, key); + + EXPECT_FALSE(result); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest042 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest043, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest043 function start!"); + EXPECT_TRUE(root_); + cJSON_AddItemToObject(root_, WakeupSource::ENABLE_KEY, cJSON_CreateString("not bool")); + cJSON_AddItemToObject(root_, WakeupSource::KEYS_KEY, cJSON_CreateString("not num")); + + std::string key = "test"; + std::shared_ptr parseSources = std::make_shared(); + bool result = WakeupSourceParser::ParseSourcesProc(parseSources, root_, key); + + EXPECT_FALSE(result); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest043 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest044, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest044 function start!"); + EXPECT_TRUE(root_); + const std::string json = ""; + VibratorSourceParser vibratorSourceParser; + auto parseSources = vibratorSourceParser.ParseSources(json); + EXPECT_EQ(parseSources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest044 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest045, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest045 function start!"); + EXPECT_TRUE(root_); + const std::string json = "[]"; + VibratorSourceParser vibratorSourceParser; + auto parseSources = vibratorSourceParser.ParseSources(json); + EXPECT_EQ(parseSources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest045 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest046, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest046 function start!"); + EXPECT_TRUE(root_); + const std::string json = "123"; + VibratorSourceParser vibratorSourceParser; + auto parseSources = vibratorSourceParser.ParseSources(json); + EXPECT_EQ(parseSources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest046 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest047, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest047 function start!"); + EXPECT_TRUE(root_); + std::string key = "test"; + VibratorSourceParser vibratorSourceParser; + std::vector parseSources; + vibratorSourceParser.ParseSourcesProc(parseSources, nullptr, key); + EXPECT_TRUE(parseSources.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest047 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest048, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest048 function start!"); + EXPECT_TRUE(root_); + std::string key = "test"; + VibratorSourceParser vibratorSourceParser; + std::vector parseSources; + vibratorSourceParser.ParseSourcesProc(parseSources, root_, key); + EXPECT_TRUE(parseSources.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest048 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest049, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest049 function start!"); + EXPECT_TRUE(root_); + std::string key = "test"; + cJSON_AddItemToObject(root_, VibratorSource::ENABLE_KEY, cJSON_CreateBool(false)); + cJSON_AddItemToObject(root_, VibratorSource::TYPE_KEY, cJSON_CreateNumber(NUMBER_TWO)); + VibratorSourceParser vibratorSourceParser; + std::vector parseSources; + vibratorSourceParser.ParseSourcesProc(parseSources, root_, key); + EXPECT_TRUE(parseSources.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest049 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest050, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest050 function start!"); + EXPECT_TRUE(root_); + ShutdownDialog shutdownDialog; + std::string json = R"({"abilityName": "PowerUiExtensionAbility", "bundleName": "com.ohos.powerdialog"})"; + shutdownDialog.ParseJsonConfig(json); + EXPECT_FALSE(shutdownDialog.bundleName_.empty()); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest050 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest051, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest051 function start!"); + EXPECT_TRUE(root_); + const std::string json = R"("this is a string")"; + auto parseSources = SuspendSourceParser::ParseSources(json); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest051 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest052, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest052 function start!"); + EXPECT_TRUE(root_); + const std::string json = R"("this is a string")"; + VibratorSourceParser vibratorSourceParser; + auto parseSources = vibratorSourceParser.ParseSources(json); + EXPECT_EQ(parseSources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest052 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest053, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest053 function start!"); + EXPECT_TRUE(root_); + const std::string json = R"("this is a string")"; + auto parseSources = WakeupSourceParser::ParseSources(json); + std::vector sources = parseSources->GetSourceList(); + EXPECT_EQ(sources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest053 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest054, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest054 function start!"); + EXPECT_TRUE(root_); + ShutdownDialog shutdownDialog; + std::string json = R"({"abilityName": "PowerUiExtensionAbility", "bundleName": "com.ohos.powerdialog", + "uiExtensionType": "sysDialog/power"})"; + shutdownDialog.ParseJsonConfig(json); + EXPECT_EQ(shutdownDialog.bundleName_, "com.ohos.powerdialog"); + EXPECT_EQ(shutdownDialog.abilityName_, "PowerUiExtensionAbility"); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest054 function end!"); +} + +HWTEST_F(PowerConfigParseTest, PowerConfigParseTest055, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest055 function start!"); + EXPECT_TRUE(root_); + const std::string json = R"({"lid": {"enable": false, "type": "123"}})";; + VibratorSourceParser vibratorSourceParser; + auto parseSources = vibratorSourceParser.ParseSources(json); + EXPECT_EQ(parseSources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTest055 function end!"); +} } // namespace diff --git a/test/unittest/src/power_config_parse_test_two.cpp b/test/unittest/src/power_config_parse_test_two.cpp new file mode 100644 index 00000000..e99f217c --- /dev/null +++ b/test/unittest/src/power_config_parse_test_two.cpp @@ -0,0 +1,58 @@ +/* + * 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 +#include "power_mgr_service.h" +#include "power_mode_policy.h" +#include "power_log.h" +#include "setting_helper.h" +#include "shutdown_dialog.h" +#include "vibrator_source_parser.h" +#include "wakeup_controller.h" +#include "wakeup_source_parser.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + +class PowerConfigParseTestTwo : public Test { +public: + void SetUp() override + { + } + + void TearDown() override + { + } +}; + +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +{ + return nullptr; +} + +namespace { +HWTEST_F(PowerConfigParseTestTwo, PowerConfigParseTestTwo001, TestSize.Level0) +{ + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTestTwo001 function start!"); + const std::string json = R"("this is a string")"; + VibratorSourceParser vibratorSourceParser; + auto parseSources = vibratorSourceParser.ParseSources(json); + EXPECT_EQ(parseSources.size(), 0); + POWER_HILOGI(LABEL_TEST, "PowerConfigParseTestTwo001 function end!"); +} +} // namespace diff --git a/utils/native/include/power_cjson_utils.h b/utils/native/include/power_cjson_utils.h new file mode 100644 index 00000000..6c2414b7 --- /dev/null +++ b/utils/native/include/power_cjson_utils.h @@ -0,0 +1,63 @@ +/* + * 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. + */ + +#ifndef POWERMGR_POWER_CJSON_UTILS_H +#define POWERMGR_POWER_CJSON_UTILS_H + +#include +#include + +namespace OHOS { +namespace PowerMgr { +namespace PowerMgrJsonUtils { +inline bool IsValidJsonString(const cJSON* jsonValue) +{ + return jsonValue && cJSON_IsString(jsonValue) && jsonValue->valuestring != nullptr; +} + +inline bool IsValidJsonStringAndNoEmpty(const cJSON* jsonValue) +{ + return jsonValue && cJSON_IsString(jsonValue) && jsonValue->valuestring != nullptr && + strlen(jsonValue->valuestring) > 0; +} + +inline bool IsValidJsonNumber(const cJSON* jsonValue) +{ + return jsonValue && cJSON_IsNumber(jsonValue); +} + +inline bool IsValidJsonObject(const cJSON* jsonValue) +{ + return jsonValue && cJSON_IsObject(jsonValue); +} + +inline bool IsValidJsonArray(const cJSON* jsonValue) +{ + return jsonValue && cJSON_IsArray(jsonValue); +} + +inline bool IsValidJsonBool(const cJSON* jsonValue) +{ + return jsonValue && cJSON_IsBool(jsonValue); +} + +inline bool IsValidJsonObjectOrJsonArray(const cJSON* jsonValue) +{ + return IsValidJsonObject(jsonValue) || IsValidJsonArray(jsonValue); +} +} // namespace PowerMgrJsonUtils +} // namespace PowerMgr +} // namespace OHOS +#endif // POWERMGR_POWER_CJSON_UTILS_H diff --git a/utils/vibrator/src/vibrator_source_parser.cpp b/utils/vibrator/src/vibrator_source_parser.cpp index 51f594fa..0c62c3d8 100644 --- a/utils/vibrator/src/vibrator_source_parser.cpp +++ b/utils/vibrator/src/vibrator_source_parser.cpp @@ -20,6 +20,7 @@ #include #include #include "config_policy_utils.h" +#include "power_cjson_utils.h" #include "power_log.h" namespace OHOS { @@ -81,7 +82,7 @@ std::vector VibratorSourceParser::ParseSources(const std::string POWER_HILOGE(COMP_UTILS, "JSON parse error"); return sources; } - if (!cJSON_IsObject(root)) { + if (!PowerMgrJsonUtils::IsValidJsonObjectOrJsonArray(root)) { POWER_HILOGE(COMP_UTILS, "JSON root is not object"); cJSON_Delete(root); return sources; @@ -106,7 +107,7 @@ std::vector VibratorSourceParser::ParseSources(const std::string void VibratorSourceParser::ParseSourcesProc( std::vector& sources, cJSON* valueObj, std::string& key) { - if (!cJSON_IsObject(valueObj)) { + if (!PowerMgrJsonUtils::IsValidJsonObject(valueObj)) { POWER_HILOGE(COMP_UTILS, "ValueObj is not a json object."); return; } @@ -115,14 +116,14 @@ void VibratorSourceParser::ParseSourcesProc( std::string type; cJSON* enableItem = cJSON_GetObjectItemCaseSensitive(valueObj, VibratorSource::ENABLE_KEY); - if (!enableItem || !cJSON_IsBool(enableItem)) { + if (!PowerMgrJsonUtils::IsValidJsonBool(enableItem)) { POWER_HILOGE(COMP_UTILS, "Parse enable error."); return; } enable = cJSON_IsTrue(enableItem); cJSON* typeItem = cJSON_GetObjectItemCaseSensitive(valueObj, VibratorSource::TYPE_KEY); - if (!typeItem || !cJSON_IsString(typeItem)) { + if (!PowerMgrJsonUtils::IsValidJsonString(typeItem)) { POWER_HILOGE(COMP_UTILS, "Parse type error."); return; } -- Gitee