From 7006a412ba70893bb1b350bbaca23b48b58228b6 Mon Sep 17 00:00:00 2001 From: yujann Date: Mon, 22 Jan 2024 20:25:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?cJSON=E6=95=B4=E6=94=B9=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8cjson=E6=9B=BF=E6=8D=A2nlohmann::json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ib8734c6f7646aedcf32e7cb674d517bf45c864ff Signed-off-by: yujann --- bundle.json | 5 +- frameworks/BUILD.gn | 6 +- .../include/sec_comp_enhance_adapter.h | 6 +- .../src/sec_comp_enhance_adapter.cpp | 2 +- .../security_component/src/sec_comp_base.cpp | 418 +++++++++++++----- .../inner_api/enhance_kits/test/BUILD.gn | 11 +- .../unittest/src/sec_comp_enhance_test.cpp | 2 +- .../inner_api/security_component/BUILD.gn | 1 - .../security_component/include/cjson_utils.h | 80 ++++ .../include/location_button.h | 4 +- .../security_component/include/paste_button.h | 4 +- .../security_component/include/save_button.h | 4 +- .../include/sec_comp_base.h | 35 +- .../security_component/src/cjson_utils.cpp | 344 ++++++++++++++ .../security_component/test/BUILD.gn | 2 + .../unittest/src/location_button_test.cpp | 232 ++++++---- .../test/unittest/src/paste_button_test.cpp | 47 +- .../test/unittest/src/save_button_test.cpp | 47 +- .../test/unittest/src/sec_comp_kit_test.cpp | 12 +- .../src/sec_comp_register_callback_test.cpp | 91 ++-- .../test/unittest/src/test_common.cpp | 218 ++++----- .../test/unittest/src/test_common.h | 14 +- .../security_component_service/sa/BUILD.gn | 1 + .../sa/sa_main/first_use_dialog.cpp | 100 +++-- .../sa/sa_main/first_use_dialog.h | 11 +- .../sa/sa_main/sec_comp_info_helper.cpp | 2 +- .../sa/sa_main/sec_comp_info_helper.h | 6 +- .../sa/sa_main/sec_comp_manager.cpp | 16 +- .../sa/sa_main/sec_comp_manager.h | 9 +- .../sa/sa_main/sec_comp_service.cpp | 26 +- .../sa/sa_main/sec_comp_service.h | 4 +- .../sa/test/BUILD.gn | 6 +- .../mock/src/sec_comp_enhance_adapter.cpp | 3 +- .../unittest/src/sec_comp_entity_test.cpp | 3 +- .../src/sec_comp_info_helper_test.cpp | 271 ++++++++---- .../unittest/src/sec_comp_manager_test.cpp | 35 +- .../src/sec_comp_service_mock_test.cpp | 25 +- .../unittest/src/sec_comp_service_test.cpp | 8 +- .../test/unittest/src/service_test_common.cpp | 218 ++++----- .../test/unittest/src/service_test_common.h | 10 +- .../BUILD.gn | 1 + .../BUILD.gn | 1 + .../BUILD.gn | 1 + .../BUILD.gn | 1 + .../BUILD.gn | 1 + .../verifysavepermissionInner_fuzzer/BUILD.gn | 1 + 46 files changed, 1596 insertions(+), 749 deletions(-) create mode 100644 interfaces/inner_api/security_component/include/cjson_utils.h create mode 100644 interfaces/inner_api/security_component/src/cjson_utils.cpp diff --git a/bundle.json b/bundle.json index c3244e1..452d97c 100644 --- a/bundle.json +++ b/bundle.json @@ -27,6 +27,7 @@ "ability_base", "ability_runtime", "access_token", + "cJSON", "c_utils", "eventhandler", "hilog", @@ -38,9 +39,7 @@ "samgr", "window_manager" ], - "third_party": [ - "cJSON" - ] + "third_party": [] }, "build": { "sub_component": [ diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 94f7ec0..9dbd350 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -35,10 +35,10 @@ ohos_source_set("security_component_framework_src_set") { "common/include", "security_component/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - "//third_party/json/include", ] sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", "common/src/sec_comp_tool.cpp", "security_component/src/location_button.cpp", "security_component/src/paste_button.cpp", @@ -52,6 +52,7 @@ ohos_source_set("security_component_framework_src_set") { external_deps = [ "c_utils:utils", + "cJSON:cjson", "hilog:libhilog", ] @@ -66,7 +67,6 @@ config("security_component_enhance_adapter_src_set_config") { "common/include", "enhance_adapter/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - "//third_party/json/include", ] } @@ -83,7 +83,6 @@ ohos_source_set("security_component_enhance_adapter_src_set") { "common/include", "enhance_adapter/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - "//third_party/json/include", ] sources = [ "enhance_adapter/src/sec_comp_enhance_adapter.cpp" ] @@ -93,6 +92,7 @@ ohos_source_set("security_component_enhance_adapter_src_set") { external_deps = [ "c_utils:utils", + "cJSON:cjson", "hilog:libhilog", "ipc:ipc_single", ] diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h index 2e96372..ace2e75 100644 --- a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -16,8 +16,8 @@ #define SECURITY_COMPONENT_ENHANCE_ADAPTER_H #include +#include "cjson_utils.h" #include "iremote_object.h" -#include "nlohmann/json.hpp" #include "sec_comp_base.h" #include "sec_comp_info.h" @@ -55,7 +55,7 @@ public: // send component info to enhance service for checking its validity virtual int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, - const nlohmann::json& jsonComponent) = 0; + const CJson* jsonComponent) = 0; // get RemoteObject of enhance service to connect it virtual sptr GetEnhanceRemoteObject() = 0; @@ -97,7 +97,7 @@ public: static int32_t EnableInputEnhance(); static int32_t DisableInputEnhance(); static int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, - const nlohmann::json& jsonComponent); + const CJson* jsonComponent); static sptr GetEnhanceRemoteObject(); static void StartEnhanceService(); static void ExistEnhanceService(); diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp index 2e4c321..d7773b2 100644 --- a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -218,7 +218,7 @@ void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid) } int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid, - std::shared_ptr& compInfo, const nlohmann::json& jsonComponent) + std::shared_ptr& compInfo, const CJson* jsonComponent) { if (!isEnhanceSrvHandlerInit) { InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index 2b6754d..7fd12fc 100644 --- a/frameworks/security_component/src/sec_comp_base.cpp +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -14,6 +14,8 @@ */ #include "sec_comp_base.h" +#include +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" @@ -57,47 +59,49 @@ const std::string JsonTagConstants::JSON_ICON_TAG = "icon"; const std::string JsonTagConstants::JSON_BG_TAG = "bg"; const std::string JsonTagConstants::JSON_WINDOW_ID = "windowId"; -bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res) +bool SecCompBase::ParseDimension(const CJson* json, const std::string& tag, DimensionT& res) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_number_float()) { + double searchRes; + if (GetDoubleFromJson(json, tag, searchRes) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - - res = json.at(tag).get(); + res = searchRes; return true; } -bool SecCompBase::ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res) +bool SecCompBase::ParseColor(const CJson* json, const std::string& tag, SecCompColor& res) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) { + uint32_t searchRes; + if (GetUnsignedIntFromJson(json, tag, searchRes) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - res.value = json.at(tag).get(); + res.value = searchRes; return true; } -bool SecCompBase::ParseBool(const nlohmann::json& json, const std::string& tag, bool& res) +bool SecCompBase::ParseBool(const CJson* json, const std::string& tag, bool& res) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) { + bool searchRes; + if (GetBoolFromJson(json, tag, searchRes) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - - res = json.at(tag).get(); + + res = searchRes; return true; } -bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res) +bool SecCompBase::ParsePadding(const CJson* json, const std::string& tag, PaddingSize& res) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonPadding = GetObjFromJson(json, tag); + if (jsonPadding == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonPadding = json.at(tag); if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, res.top)) { return false; } @@ -113,13 +117,14 @@ bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& ta return true; } -bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseColors(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonColors = GetObjFromJson(json, tag); + if (jsonColors == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonColors = json.at(tag); + if (!ParseColor(jsonColors, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_)) { return false; } @@ -132,24 +137,25 @@ bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag return true; } -bool SecCompBase::ParseBorders(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseBorders(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonBorder = GetObjFromJson(json, tag); + if (jsonBorder == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonBorder = json.at(tag); + return ParseDimension(jsonBorder, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_); } -bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseSize(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonSize = GetObjFromJson(json, tag); + if (jsonSize == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonSize = json.at(tag); if (!ParseDimension(jsonSize, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_)) { return false; } @@ -169,24 +175,25 @@ bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag) return true; } -bool SecCompBase::ParseParent(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseParent(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonParent = GetObjFromJson(json, tag); + if (jsonParent == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonParent = json.at(tag); + return ParseBool(jsonParent, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_); } -bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect) +bool SecCompBase::ParseRect(const CJson* json, const std::string& tag, SecCompRect& rect) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonSize = GetObjFromJson(json, tag); + if (jsonSize == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonSize = json.at(tag); if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_X, rect.x_)) { return false; } @@ -206,15 +213,16 @@ bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, return true; } -bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) +bool SecCompBase::FromJson(const CJson* jsonSrc) { - SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonSrc.dump().c_str()); - if ((jsonSrc.find(JsonTagConstants::JSON_SC_TYPE) == jsonSrc.end()) || - !jsonSrc.at(JsonTagConstants::JSON_SC_TYPE).is_number()) { + std::string jsonStr = PackJsonToString(jsonSrc); + SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonStr.c_str()); + + int32_t value; + if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_SC_TYPE, value) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_SC_TYPE.c_str()); return false; } - int32_t value = jsonSrc.at(JsonTagConstants::JSON_SC_TYPE).get(); if ((value <= static_cast(SecCompType::UNKNOWN_SC_TYPE)) || (value >= static_cast(SecCompType::MAX_SC_TYPE))) { SC_LOG_ERROR(LABEL, "scType value is invalid."); @@ -222,12 +230,10 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) } type_ = static_cast(value); - if ((jsonSrc.find(JsonTagConstants::JSON_NODE_ID) == jsonSrc.end()) || - !jsonSrc.at(JsonTagConstants::JSON_NODE_ID).is_number()) { + if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_NODE_ID, nodeId_) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_NODE_ID.c_str()); return false; } - nodeId_ = jsonSrc.at(JsonTagConstants::JSON_NODE_ID).get(); if (!ParseRect(jsonSrc, JsonTagConstants::JSON_RECT, rect_)) { return false; @@ -250,72 +256,60 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) if (!ParseStyle(jsonSrc, JsonTagConstants::JSON_STYLE_TAG)) { return false; } - - if ((jsonSrc.find(JsonTagConstants::JSON_WINDOW_ID) == jsonSrc.end()) || - !jsonSrc.at(JsonTagConstants::JSON_WINDOW_ID).is_number()) { + if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_WINDOW_ID, windowId_) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_WINDOW_ID.c_str()); return false; } - windowId_ = jsonSrc.at(JsonTagConstants::JSON_WINDOW_ID).get(); return true; } -void SecCompBase::ToJson(nlohmann::json& jsonRes) const +void SecCompBase::ToJson(CJson* jsonRes) const { - jsonRes[JsonTagConstants::JSON_SC_TYPE] = type_; - jsonRes[JsonTagConstants::JSON_NODE_ID] = nodeId_; - jsonRes[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, rect_.x_}, - {JsonTagConstants::JSON_RECT_Y, rect_.y_}, - {JsonTagConstants::JSON_RECT_WIDTH, rect_.width_}, - {JsonTagConstants::JSON_RECT_HEIGHT, rect_.height_} - }; - jsonRes[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, windowRect_.x_}, - {JsonTagConstants::JSON_RECT_Y, windowRect_.y_}, - {JsonTagConstants::JSON_RECT_WIDTH, windowRect_.width_}, - {JsonTagConstants::JSON_RECT_HEIGHT, windowRect_.height_} - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, padding_.top }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding_.right }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding_.bottom }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, padding_.left }, - }; - - jsonRes[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_ }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_ }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_ }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonRes[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value }, - { JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value } - }; - - jsonRes[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_ }, - }; - jsonRes[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_ }, - }; - - jsonRes[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, text_ }, - { JsonTagConstants::JSON_ICON_TAG, icon_ }, - { JsonTagConstants::JSON_BG_TAG, bg_ }, - }; - jsonRes[JsonTagConstants::JSON_WINDOW_ID] = windowId_; + if (AddIntToJson(jsonRes, JsonTagConstants::JSON_SC_TYPE, type_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_SC_TYPE.c_str(), type_); + return; + } + if (AddIntToJson(jsonRes, JsonTagConstants::JSON_NODE_ID, nodeId_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_NODE_ID.c_str(), nodeId_); + return; + } + if (!AddRect(jsonRes, JsonTagConstants::JSON_RECT, rect_)) { + return; + } + if (!AddRect(jsonRes, JsonTagConstants::JSON_WINDOW_RECT, windowRect_)) { + return; + } + + if (!AddSize(jsonRes)) { + return; + } + if (!AddColors(jsonRes)) { + return; + } + if (!AddBorders(jsonRes)) { + return; + } + if (!AddParent(jsonRes)) { + return; + } + if (!AddStyle(jsonRes)) { + return; + } + if (AddIntToJson(jsonRes, JsonTagConstants::JSON_WINDOW_ID, windowId_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_WINDOW_ID.c_str(), windowId_); + return; + } } std::string SecCompBase::ToJsonStr() const { - nlohmann::json json; + CJson* json = CreateJson(); ToJson(json); - return json.dump(); + std::string jsonStr = PackJsonToString(json); + return jsonStr; } bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const @@ -343,27 +337,38 @@ bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck return (leftValue == rightValue); } -bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseStyle(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + CJson* jsonStyle = GetObjFromJson(json, tag); + if (jsonStyle == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonStyle = json.at(tag); - if (!(jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).is_number() && - jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).is_number() && - jsonStyle.at(JsonTagConstants::JSON_BG_TAG).is_number())) { - SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); + + if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_TEXT_TAG, text_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", + tag.c_str(), JsonTagConstants::JSON_TEXT_TAG.c_str()); return false; } - text_ = jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).get(); - icon_ = jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).get(); + if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_ICON_TAG, icon_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", + tag.c_str(), JsonTagConstants::JSON_ICON_TAG.c_str()); + return false; + } + if (!IsTextIconTypeValid()) { SC_LOG_ERROR(LABEL, "text or icon is invalid."); return false; } - bg_ = static_cast(jsonStyle.at(JsonTagConstants::JSON_BG_TAG).get()); + int32_t bg; + if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_BG_TAG, bg) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", + tag.c_str(), JsonTagConstants::JSON_BG_TAG.c_str()); + return false; + } + + bg_ = static_cast(bg); if ((bg_ <= SecCompBackground::UNKNOWN_BG) || (bg_ >= SecCompBackground::MAX_BG_TYPE)) { SC_LOG_ERROR(LABEL, "bg is invalid."); return false; @@ -371,6 +376,217 @@ bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag) return true; } + +bool SecCompBase::AddRect(CJson* json, const std::string jsonTag, const SecCompRect rectObj) const +{ + CJson* childJson = CreateJson(); + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_X, rectObj.x_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_RECT_X.c_str(), rectObj.x_); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_Y, rectObj.y_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_RECT_Y.c_str(), rectObj.y_); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_WIDTH, rectObj.width_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_RECT_WIDTH.c_str(), rectObj.width_); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_HEIGHT, rectObj.height_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_RECT_HEIGHT.c_str(), rectObj.height_); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, jsonTag, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_RECT.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddPadding(CJson* json) const +{ + if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_TOP_TAG, padding_.top) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_PADDING_TOP_TAG.c_str(), padding_.top); + return false; + } + if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding_.right) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_PADDING_RIGHT_TAG.c_str(), padding_.right); + return false; + } + if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding_.bottom) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_PADDING_BOTTOM_TAG.c_str(), padding_.bottom); + return false; + } + if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_LEFT_TAG, padding_.left) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_PADDING_LEFT_TAG.c_str(), padding_.left); + return false; + } + return true; +} + +bool SecCompBase::AddSize(CJson* json) const +{ + CJson* jsonPadding = CreateJson(); + if (!AddPadding(jsonPadding)) { + FreeJson(jsonPadding); + return false; + } + + CJson* childJson = CreateJson(); + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_FONT_SIZE_TAG.c_str(), fontSize_); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_ICON_SIZE_TAG.c_str(), iconSize_); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG.c_str(), textIconSpace_); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + if (AddObjToJson(childJson, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_PADDING_SIZE_TAG.c_str()); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_SIZE_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_SIZE_TAG.c_str()); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + FreeJson(jsonPadding); + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddColors(CJson* json) const +{ + CJson* childJson = CreateJson(); + if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", + JsonTagConstants::JSON_FONT_COLOR_TAG.c_str(), fontColor_.value); + FreeJson(childJson); + return false; + } + if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", + JsonTagConstants::JSON_ICON_COLOR_TAG.c_str(), iconColor_.value); + FreeJson(childJson); + return false; + } + if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", + JsonTagConstants::JSON_BG_COLOR_TAG.c_str(), bgColor_.value); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_COLORS_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_COLORS_TAG.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddBorders(CJson* json) const +{ + CJson* childJson = CreateJson(); + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_BORDER_WIDTH_TAG.c_str(), borderWidth_); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_BORDER_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_BORDER_TAG.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddParent(CJson* json) const +{ + CJson* childJson = CreateJson(); + if (AddBoolToJson(childJson, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_PARENT_EFFECT_TAG.c_str(), parentEffect_); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_PARENT_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_PARENT_TAG.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddStyle(CJson* json) const +{ + CJson* childJson = CreateJson(); + if (AddIntToJson(childJson, JsonTagConstants::JSON_TEXT_TAG, text_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_TEXT_TAG.c_str(), text_); + FreeJson(childJson); + return false; + } + if (AddIntToJson(childJson, JsonTagConstants::JSON_ICON_TAG, icon_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_ICON_TAG.c_str(), icon_); + FreeJson(childJson); + return false; + } + if (AddIntToJson(childJson, JsonTagConstants::JSON_BG_TAG, static_cast(bg_)) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_BG_TAG.c_str(), bg_); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_STYLE_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_STYLE_TAG.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} } // namespace base } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/enhance_kits/test/BUILD.gn b/interfaces/inner_api/enhance_kits/test/BUILD.gn index a82de68..4a34d4b 100644 --- a/interfaces/inner_api/enhance_kits/test/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -25,9 +25,15 @@ ohos_unittest("sec_comp_enhance_sdk_test") { debug = false } branch_protector_ret = "pac_ret" - include_dirs = [ "unittest/src" ] + include_dirs = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + "unittest/src", + ] - sources = [ "unittest/src/sec_comp_enhance_test.cpp" ] + sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", + "unittest/src/sec_comp_enhance_test.cpp", + ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -35,6 +41,7 @@ ohos_unittest("sec_comp_enhance_sdk_test") { deps = [ "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk" ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_single", diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp index d17f3e0..e3a69e7 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #include "sec_comp_enhance_test.h" -#include +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" #include "sec_comp_info.h" diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index 73f98c5..792a4c0 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -22,7 +22,6 @@ config("sec_comp_config") { "${sec_comp_root_dir}/frameworks/common/include", "${sec_comp_root_dir}/frameworks/enhance_adapter/include/", "${sec_comp_root_dir}/interfaces/inner_api/enhance_kits/include", - "//third_party/json/include", ] } diff --git a/interfaces/inner_api/security_component/include/cjson_utils.h b/interfaces/inner_api/security_component/include/cjson_utils.h new file mode 100644 index 0000000..dcdbd69 --- /dev/null +++ b/interfaces/inner_api/security_component/include/cjson_utils.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2024 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 JSON_UTILS_H +#define JSON_UTILS_H + +#include +#include +#include +#include +#include "cJSON.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +typedef cJSON CJson; +enum { + CJSON_SUCCESS = 0, + CJSON_ERR_INVALID_PARAM = -1, + CJSON_ERR_BAD_ALLOC = -2, + CJSON_ERR_NULL_PTR = -3, + CJSON_ERR_JSON_GET = -4, + CJSON_ERR_JSON_ADD = -5, + CJSON_ERR_JSON_REPLACE = -6, + CJSON_ERR_JSON_DUPLICATE = -7, +}; + +/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJson* CreateJsonFromString(const std::string jsonStr); +/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJson* CreateJson(void); +/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJson* CreateJsonArray(void); +void FreeJson(CJson*& jsonObj); + +/* NO Need to call FreeJsonString to free the returned pointer when it's no longer in use. */ +std::string PackJsonToString(const CJson* jsonObj); +void FreeJsonString(char* jsonStr); + +/* + * Can't release the returned pointer, otherwise, an exception may occur. + * It refers to the parent object(param--jsonObj)'s memory. + * It will be recycled along with jsonObj when jsonObj is released. + */ +CJson* GetObjFromJson(const CJson* jsonObj, const std::string key); +CJson* GetArrayFromJson(const CJson* jsonObj, const std::string key); + +/* +* Return a copy of string in jsonObj in std::string +*/ +std::string GetStringFromJson(const CJson* jsonObj, const std::string key); + +int32_t GetIntFromJson(const CJson* jsonObj, const std::string key, int32_t& value); +int32_t GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value); +int32_t GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value); +int32_t GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value); + +int32_t AddObjToJson(CJson* jsonObj, const std::string key, const CJson* childObj); +int32_t AddObjToArray(CJson* jsonArr, CJson* item); +int32_t AddStringToJson(CJson* jsonObj, const std::string key, const std::string value); +int32_t AddBoolToJson(CJson* jsonObj, const std::string key, const bool value); +int32_t AddIntToJson(CJson* jsonObj, const std::string key, const int value); +int32_t AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value); +int32_t AddDoubleToJson(CJson* jsonObj, const std::string key, const double value); +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/inner_api/security_component/include/location_button.h b/interfaces/inner_api/security_component/include/location_button.h index 9788b0c..51684dc 100644 --- a/interfaces/inner_api/security_component/include/location_button.h +++ b/interfaces/inner_api/security_component/include/location_button.h @@ -16,7 +16,7 @@ #define I_SECURITY_COMPONENT_LOCATION_BUTTON_H #include -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_base.h" #include "sec_comp_info.h" @@ -50,7 +50,7 @@ public: virtual bool IsCorrespondenceType() override; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const nlohmann::json& json, const std::string& tag); + bool ParseStyle(const CJson* json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/paste_button.h b/interfaces/inner_api/security_component/include/paste_button.h index 0e43c8f..f8e5f13 100644 --- a/interfaces/inner_api/security_component/include/paste_button.h +++ b/interfaces/inner_api/security_component/include/paste_button.h @@ -16,7 +16,7 @@ #define SECURITY_COMPONENT_PASTE_BUTTON_H #include -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_base.h" namespace OHOS { @@ -40,7 +40,7 @@ public: virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const nlohmann::json& json, const std::string& tag); + bool ParseStyle(const CJson* json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/save_button.h b/interfaces/inner_api/security_component/include/save_button.h index e1f45e6..b636ecd 100644 --- a/interfaces/inner_api/security_component/include/save_button.h +++ b/interfaces/inner_api/security_component/include/save_button.h @@ -16,7 +16,7 @@ #define I_SECURITY_COMPONENT_SAVE_BUTTON_H #include -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_base.h" #include "sec_comp_info.h" @@ -47,7 +47,7 @@ public: virtual bool IsCorrespondenceType() override; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const nlohmann::json& json, const std::string& tag); + bool ParseStyle(const CJson* json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h index 30d15fe..823f907 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_base.h +++ b/interfaces/inner_api/security_component/include/sec_comp_base.h @@ -14,8 +14,8 @@ */ #ifndef SECURITY_COMPONENT_BASE_H #define SECURITY_COMPONENT_BASE_H - -#include "nlohmann/json.hpp" +#include +#include "cjson_utils.h" #include "sec_comp_info.h" namespace OHOS { @@ -78,8 +78,8 @@ class __attribute__((visibility("default"))) SecCompBase { public: SecCompBase() = default; virtual ~SecCompBase() = default; - bool FromJson(const nlohmann::json& jsonSrc); - void ToJson(nlohmann::json& jsonRes) const; + bool FromJson(const CJson* jsonSrc); + void ToJson(CJson* jsonRes) const; std::string ToJsonStr(void) const; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const; void SetValid(bool valid) @@ -124,16 +124,23 @@ protected: virtual bool IsTextIconTypeValid() = 0; virtual bool IsCorrespondenceType() = 0; private: - bool ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res); - bool ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res); - bool ParseBool(const nlohmann::json& json, const std::string& tag, bool& res); - bool ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res); - bool ParseColors(const nlohmann::json& json, const std::string& tag); - bool ParseBorders(const nlohmann::json& json, const std::string& tag); - bool ParseSize(const nlohmann::json& json, const std::string& tag); - bool ParseParent(const nlohmann::json& json, const std::string& tag); - bool ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect); - bool ParseStyle(const nlohmann::json& json, const std::string& tag); + bool ParseDimension(const CJson* json, const std::string& tag, DimensionT& res); + bool ParseColor(const CJson* json, const std::string& tag, SecCompColor& res); + bool ParseBool(const CJson* json, const std::string& tag, bool& res); + bool ParsePadding(const CJson* json, const std::string& tag, PaddingSize& res); + bool ParseColors(const CJson* json, const std::string& tag); + bool ParseBorders(const CJson* json, const std::string& tag); + bool ParseSize(const CJson* json, const std::string& tag); + bool ParseParent(const CJson* json, const std::string& tag); + bool ParseRect(const CJson* json, const std::string& tag, SecCompRect& rect); + bool ParseStyle(const CJson* json, const std::string& tag); + bool AddRect(CJson* json, const std::string jsonTag, const SecCompRect rectObj) const; + bool AddPadding(CJson* json) const; + bool AddSize(CJson* json) const; + bool AddColors(CJson* json) const; + bool AddBorders(CJson* json) const; + bool AddParent(CJson* json) const; + bool AddStyle(CJson* json) const; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/src/cjson_utils.cpp b/interfaces/inner_api/security_component/src/cjson_utils.cpp new file mode 100644 index 0000000..26ae698 --- /dev/null +++ b/interfaces/inner_api/security_component/src/cjson_utils.cpp @@ -0,0 +1,344 @@ +/* + * Copyright (C) 2024 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 "cjson_utils.h" +#include +#include + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +#define RECURSE_FLAG_TRUE 1 +} + +CJson* CreateJsonFromString(const std::string jsonStr) +{ + if (jsonStr.empty()) { + return nullptr; + } + return cJSON_Parse(jsonStr.c_str()); +} + +CJson* CreateJson(void) +{ + return cJSON_CreateObject(); +} + +CJson* CreateJsonArray(void) +{ + return cJSON_CreateArray(); +} + +void FreeJson(CJson*& jsonObj) +{ + cJSON_Delete(jsonObj); + jsonObj = nullptr; +} + +std::string PackJsonToString(const CJson* jsonObj) +{ + if (jsonObj == nullptr) { + return std::string(); + } + char* ptr = cJSON_PrintUnformatted(jsonObj); + if (ptr == nullptr) { + return std::string(); + } + std::string ret = std::string(ptr); + FreeJsonString(ptr); + return ret; +} + +void FreeJsonString(char* jsonStr) +{ + if (jsonStr != nullptr) { + cJSON_free(jsonStr); + } +} + +CJson* GetObjFromJson(const CJson* jsonObj, const std::string key) +{ + if (jsonObj == nullptr || key.empty()) { + return nullptr; + } + + CJson* objValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objValue != nullptr && cJSON_IsObject(objValue)) { + return objValue; + } + return nullptr; +} + +CJson* GetArrayFromJson(const CJson* jsonObj, const std::string key) +{ + if (jsonObj == nullptr || key.empty()) { + return nullptr; + } + + CJson* objValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objValue != nullptr && cJSON_IsArray(objValue)) { + return objValue; + } + return nullptr; +} + +std::string GetStringFromJson(const CJson* jsonObj, const std::string key) +{ + if (jsonObj == nullptr || key.empty()) { + return std::string(); + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsString(jsonObjTmp)) { + char* getStrPtr = cJSON_GetStringValue(jsonObjTmp); + return std::string(getStrPtr); + } + return std::string(); +} + +int32_t GetIntFromJson(const CJson* jsonObj, const std::string key, int32_t& value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { + value = (int)cJSON_GetNumberValue(jsonObjTmp); + return CJSON_SUCCESS; + } + return CJSON_ERR_JSON_GET; +} + +int32_t GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { + double realValue = cJSON_GetNumberValue(jsonObjTmp); + if (realValue < 0) { + int32_t tmpValue = static_cast(realValue); + value = static_cast(tmpValue); + } else { + value = static_cast(realValue); + } + return CJSON_SUCCESS; + } + return CJSON_ERR_JSON_GET; +} + +int32_t GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsBool(jsonObjTmp)) { + value = cJSON_IsTrue(jsonObjTmp) ? true : false; + return CJSON_SUCCESS; + } + return CJSON_ERR_JSON_GET; +} + +int32_t GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { + value = cJSON_GetNumberValue(jsonObjTmp); + return CJSON_SUCCESS; + } + return CJSON_ERR_JSON_GET; +} + +int32_t AddObjToJson(CJson* jsonObj, const std::string key, const CJson* childObj) +{ + if (jsonObj == nullptr || key.empty() || childObj == nullptr) { + return CJSON_ERR_NULL_PTR; + } + + CJson* tmpObj = cJSON_Duplicate(childObj, RECURSE_FLAG_TRUE); + if (tmpObj == nullptr) { + return CJSON_ERR_JSON_DUPLICATE; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddItemToObject(jsonObj, key.c_str(), tmpObj) == false) { + cJSON_Delete(tmpObj); + return CJSON_ERR_JSON_ADD; + } + } else { + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmpObj) == false) { + cJSON_Delete(tmpObj); + return CJSON_ERR_JSON_REPLACE; + } + } + return CJSON_SUCCESS; +} + +int32_t AddObjToArray(CJson* jsonArr, CJson* item) +{ + if (jsonArr == nullptr || item == nullptr) { + return CJSON_ERR_NULL_PTR; + } + + if (cJSON_IsArray(jsonArr) == false) { + return CJSON_ERR_INVALID_PARAM; + } + + bool ret = cJSON_AddItemToArray(jsonArr, item); + if (ret == false) { + return CJSON_ERR_JSON_ADD; + } + + return CJSON_SUCCESS; +} + +int32_t AddStringToJson(CJson* jsonObj, const std::string key, const std::string value) +{ + if (jsonObj == nullptr || key.empty() || value.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddStringToObject(jsonObj, key.c_str(), value.c_str()) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateString(value.c_str()); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + + return CJSON_SUCCESS; +} + +int32_t AddBoolToJson(CJson* jsonObj, const std::string key, const bool value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddBoolToObject(jsonObj, key.c_str(), value) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateBool(value); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + + return CJSON_SUCCESS; +} + +int32_t AddIntToJson(CJson* jsonObj, const std::string key, const int value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddNumberToObject(jsonObj, key.c_str(), value) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateNumber(value); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + + return CJSON_SUCCESS; +} + +int32_t AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + double tmpValue = static_cast(value); + if (objInJson == nullptr) { + if (cJSON_AddNumberToObject(jsonObj, key.c_str(), tmpValue) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateNumber(tmpValue); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + return CJSON_SUCCESS; +} + +int32_t AddDoubleToJson(CJson* jsonObj, const std::string key, const double value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddNumberToObject(jsonObj, key.c_str(), value) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateNumber(value); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + + return CJSON_SUCCESS; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index 4547999..a7ec0dd 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -23,6 +23,7 @@ ohos_unittest("sec_comp_sdk_test") { include_dirs = [ "${sec_comp_root_dir}/frameworks/common/include", + "${sec_comp_root_dir}/frameworks/enhance_adapter/include", "${sec_comp_root_dir}/frameworks/security_component/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", ] @@ -56,6 +57,7 @@ ohos_unittest("sec_comp_sdk_test") { external_deps = [ "access_token:libaccesstoken_sdk", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "hisysevent:libhisysevent", diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 335bb98..0b079ef 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -15,6 +15,7 @@ #include "location_button_test.h" #include +#include "cjson_utils.h" #include "sec_comp_log.h" #include "sec_comp_err.h" #include "test_common.h" @@ -51,11 +52,12 @@ void LocationButtonTest::TearDown() */ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -66,9 +68,10 @@ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -79,16 +82,15 @@ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); - - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = 0; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SecCompType::UNKNOWN_SC_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -99,31 +101,36 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); - jsonComponent[JsonTagConstants::JSON_RECT] = wrongJson; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + CJson* wrongJson = CreateJsonFromString("{"); + ASSERT_EQ(nullptr, wrongJson); + wrongJson = CreateJson(); // add a empty json obj instead + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_RECT, wrongJson)); + FreeJson(wrongJson); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& rectJson = jsonComponent[JsonTagConstants::JSON_RECT]; - rectJson[JsonTagConstants::JSON_RECT_X] = WRONG_TYPE; + CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_RECT); + ASSERT_NE(nullptr, rectJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_X, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_X] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_Y] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_Y, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_HEIGHT] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -134,31 +141,36 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = wrongJson; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + CJson* wrongJson = CreateJsonFromString("{"); + ASSERT_EQ(nullptr, wrongJson); + wrongJson = CreateJson(); // add a empty json obj instead + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT, wrongJson)); + FreeJson(wrongJson); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& rectJson = jsonComponent[JsonTagConstants::JSON_WINDOW_RECT]; - rectJson[JsonTagConstants::JSON_RECT_X] = WRONG_TYPE; + CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); + ASSERT_NE(nullptr, rectJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_X, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_X] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_Y] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_Y, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_HEIGHT] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -169,22 +181,24 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = WRONG_TYPE; + CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, rectJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_FONT_SIZE_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = TestCommon::TEST_SIZE; - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_ICON_SIZE_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = TestCommon::TEST_SIZE; - sizeJson[JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -195,19 +209,23 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = WRONG_TYPE; + CJson* sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + CJson* paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, paddingJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = TestCommon::TEST_DIMENSION; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, + JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -218,19 +236,23 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = WRONG_TYPE; + CJson* sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + CJson* paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, paddingJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = TestCommon::TEST_DIMENSION; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, + JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -241,21 +263,24 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, WRONG_TYPE }, - }; + CJson* borderTag = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(borderTag, JsonTagConstants::JSON_BORDER_WIDTH_TAG, WRONG_TYPE)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTag)); + FreeJson(borderTag); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, - }; + CJson* parentTag = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(parentTag, JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTag)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(parentTag); + FreeJson(jsonComponent); } /** @@ -266,22 +291,26 @@ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = WRONG_TYPE; + CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION; - styleJson[JsonTagConstants::JSON_ICON_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_BG_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -292,22 +321,26 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& colorJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG]; - colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = WRONG_TYPE; + CJson* colorJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); + ASSERT_NE(nullptr, colorJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = TestCommon::TEST_COLOR_RED; - colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + TestCommon::TEST_COLOR_RED)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = TestCommon::TEST_COLOR_BLUE; - colorJson[JsonTagConstants::JSON_BG_COLOR_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + TestCommon::TEST_COLOR_BLUE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_BG_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -318,35 +351,47 @@ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = UNKNOWN_TEXT; + CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION; - styleJson[JsonTagConstants::JSON_ICON_TAG] = UNKNOWN_ICON; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::UNKNOWN_BG; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::CIRCLE; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::MAX_LABEL_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION; - styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::MAX_ICON_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::MAX_BG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -357,12 +402,15 @@ HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) */ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - ASSERT_EQ(jsonComponent.dump(), button.ToJsonStr()); + std::string jsonDump = PackJsonToString(jsonComponent); + ASSERT_FALSE(jsonDump.empty()); + ASSERT_EQ(jsonDump, button.ToJsonStr()); + FreeJson(jsonComponent); } /** @@ -373,11 +421,12 @@ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_FALSE(button.CompareComponentBasicInfo(nullptr, true)); + FreeJson(jsonComponent); } /** @@ -391,7 +440,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) LocationButton button1; LocationButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -411,6 +460,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } /** @@ -421,7 +471,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp1; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -476,6 +526,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) comp1.iconColor_.value = TestCommon::TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); + FreeJson(jsonComponent); } /** @@ -488,9 +539,10 @@ HWTEST_F(LocationButtonTest, CompareLocationButton003, TestSize.Level1) { LocationButton button1; SaveButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, false)); + FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index d79349d..bc34c99 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -15,6 +15,7 @@ #include "paste_button_test.h" #include +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" #include "test_common.h" @@ -50,35 +51,46 @@ void PasteButtonTest::TearDown() */ HWTEST_F(PasteButtonTest, IsParamValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); PasteButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = UNKNOWN_TEXT; + CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = PasteDesc::PASTE; - styleJson[JsonTagConstants::JSON_ICON_TAG] = UNKNOWN_ICON; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(PasteDesc::PASTE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = PasteIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::UNKNOWN_BG; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::CIRCLE; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = PasteDesc::MAX_LABEL_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = PasteDesc::PASTE; - styleJson[JsonTagConstants::JSON_ICON_TAG] = PasteIcon::MAX_ICON_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(PasteDesc::PASTE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(PasteIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = PasteIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::MAX_BG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -92,7 +104,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) PasteButton button1; PasteButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -112,6 +124,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } /** @@ -122,7 +135,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) */ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); PasteButton comp1; TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -145,6 +158,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) comp1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); + FreeJson(jsonComponent); } /** @@ -157,9 +171,10 @@ HWTEST_F(PasteButtonTest, ComparePasteButton003, TestSize.Level1) { PasteButton button1; SaveButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index 6c71088..bdd5df2 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -15,6 +15,7 @@ #include "save_button_test.h" #include +#include "cjson_utils.h" #include "sec_comp_log.h" #include "sec_comp_err.h" #include "test_common.h" @@ -50,35 +51,46 @@ void SaveButtonTest::TearDown() */ HWTEST_F(SaveButtonTest, IsParamValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); SaveButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& stylesJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - stylesJson[JsonTagConstants::JSON_TEXT_TAG] = UNKNOWN_TEXT; + CJson* stylesJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, stylesJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_TEXT_TAG] = SaveDesc::DOWNLOAD; - stylesJson[JsonTagConstants::JSON_ICON_TAG] = UNKNOWN_ICON; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_ICON_TAG] = SaveIcon::LINE_ICON; - stylesJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::UNKNOWN_BG; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::CIRCLE; - stylesJson[JsonTagConstants::JSON_TEXT_TAG] = SaveDesc::MAX_LABEL_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_TEXT_TAG] = SaveDesc::DOWNLOAD; - stylesJson[JsonTagConstants::JSON_ICON_TAG] = SaveIcon::MAX_ICON_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_ICON_TAG] = SaveIcon::LINE_ICON; - stylesJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::MAX_BG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -92,7 +104,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) SaveButton button1; SaveButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -112,6 +124,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } /** @@ -122,7 +135,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) */ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); SaveButton comp1; TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -145,6 +158,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) comp1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); + FreeJson(jsonComponent); } /** @@ -157,9 +171,10 @@ HWTEST_F(SaveButtonTest, CompareSaveButton003, TestSize.Level1) { SaveButton button1; PasteButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 4c82ffc..53cd8ea 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -14,6 +14,7 @@ */ #include "sec_comp_kit_test.h" +#include "cjson_utils.h" #include "location_button.h" #define private public #include "sec_comp_caller_authorization.h" @@ -109,10 +110,12 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) comp.rect_.width_ = TestCommon::TEST_COORDINATE; comp.rect_.height_ = TestCommon::TEST_COORDINATE; - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); comp.ToJson(jsonRes); int32_t scId = -1; - std::string jsonStr = jsonRes.dump(); + std::string jsonStr = PackJsonToString(jsonRes); + FreeJson(jsonRes); + ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonStr, scId)); ASSERT_EQ(-1, scId); ASSERT_NE(SC_OK, SecCompKit::UpdateSecurityComponent(scId, jsonStr)); @@ -183,9 +186,10 @@ HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) */ HWTEST_F(SecCompKitTest, RegisterWithoutCallback001, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); int32_t scId; #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp index aff7413..881b671 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include +#include "cjson_utils.h" #include "i_sec_comp_probe.h" #include "location_button.h" #include "sec_comp_enhance_adapter.h" @@ -127,9 +128,11 @@ void SecCompRegisterCallbackTest::TearDown() */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -148,9 +151,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = -1; @@ -175,9 +180,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -195,9 +202,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -230,9 +239,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -263,9 +274,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; setuid(100); EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -280,9 +293,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -317,9 +332,11 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, Test HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -354,9 +371,11 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, Test */ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -386,9 +405,11 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, Test HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -423,9 +444,11 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -463,9 +486,11 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -498,9 +523,11 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -531,9 +558,10 @@ HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.L */ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); int32_t scId; ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); ASSERT_NE(-1, scId); @@ -549,11 +577,12 @@ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level */ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); - int32_t scId; + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); ASSERT_NE(-1, scId); diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp index e4342c4..0d0ba8d 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp @@ -13,158 +13,110 @@ * limitations under the License. */ #include "test_common.h" +#include "cjson_utils.h" namespace OHOS { namespace Security { namespace SecurityComponent { -void TestCommon::BuildLocationComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::BuildLocationComponentInfo(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); +} - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } - }; +void TestCommon::BuildSaveComponentInfo(CJson* jsonComponent) +{ + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SAVE_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); } -void TestCommon::BuildSaveComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::BuildPasteComponentInfo(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, PASTE_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } - }; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); +} - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; +void TestCommon::AddRectToJson(CJson* jsonComponent, const std::string key) +{ + CJson* rectChild = CreateJson(); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE); + AddObjToJson(jsonComponent, key, rectChild); + FreeJson(rectChild); } -void TestCommon::BuildPasteComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::GenerateCommon(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_NODE_ID, 0); + AddRectToJson(jsonComponent, JsonTagConstants::JSON_RECT); + AddRectToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); + + CJson* jsonPadding = CreateJson(); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION); + + CJson* sizeTagChild = CreateJson(); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE); + AddObjToJson(sizeTagChild, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG, sizeTagChild); + FreeJson(jsonPadding); + FreeJson(sizeTagChild); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; + CJson* colorsTagChild = CreateJson(); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild); + FreeJson(colorsTagChild); - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } - }; + CJson* borderTagChild = CreateJson(); + AddDoubleToJson(borderTagChild, JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTagChild); + FreeJson(borderTagChild); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, - { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; + CJson* parentTagChild = CreateJson(); + AddBoolToJson(parentTagChild, JsonTagConstants::JSON_PARENT_EFFECT_TAG, false); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTagChild); + FreeJson(parentTagChild); } } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.h b/interfaces/inner_api/security_component/test/unittest/src/test_common.h index b42b47a..f890828 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.h +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.h @@ -14,6 +14,9 @@ */ #ifndef TEST_COMMON_H #define TEST_COMMON_H +#include + +#include "cjson_utils.h" #include "location_button.h" #include "paste_button.h" #include "save_button.h" @@ -23,7 +26,7 @@ namespace Security { namespace SecurityComponent { class TestCommon { public: - static constexpr float TEST_SIZE = 100.0; + static constexpr double TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIMENSION = 100.0; static constexpr uint32_t TEST_COLOR = 0xffffffff; @@ -36,9 +39,12 @@ public: static constexpr uint32_t MAX_HMAC_SIZE = 64; static constexpr size_t MAX_CALLER_SIZE = 10; - static void BuildLocationComponentInfo(nlohmann::json& jsonComponent); - static void BuildSaveComponentInfo(nlohmann::json& jsonComponent); - static void BuildPasteComponentInfo(nlohmann::json& jsonComponent); + static void BuildLocationComponentInfo(CJson* jsonComponent); + static void BuildSaveComponentInfo(CJson* jsonComponent); + static void BuildPasteComponentInfo(CJson* jsonComponent); +private: + static void AddRectToJson(CJson* jsonComponent, const std::string key); + static void GenerateCommon(CJson* jsonComponent); }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 7ea81ab..19498a0 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -87,6 +87,7 @@ ohos_source_set("security_component_service_src_set") { "ability_runtime:app_manager", "ability_runtime:runtime", "access_token:libaccesstoken_sdk", + "cJSON:cjson", "c_utils:utils", "eventhandler:libeventhandler", "graphic_2d:librender_service_client", diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.cpp b/services/security_component_service/sa/sa_main/first_use_dialog.cpp index 6840b51..aa098aa 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.cpp +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -14,13 +14,16 @@ */ #include "first_use_dialog.h" +#include #include #include +#include #include #include #include #include "ability_manager_client.h" #include "accesstoken_kit.h" +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" #include "want_params_wrapper.h" @@ -111,43 +114,39 @@ void FirstUseDialog::WriteCfgContent(const std::string& content) out.close(); } -bool FirstUseDialog::ParseRecord(nlohmann::json& jsonRes, - AccessToken::AccessTokenID& id, uint64_t& type) +bool FirstUseDialog::ParseRecord(CJson* jsonRes, + AccessToken::AccessTokenID& id, uint32_t& type) { - if (jsonRes.find(TOKEN_ID_TAG) == jsonRes.end() || - !jsonRes.at(TOKEN_ID_TAG).is_number()) { + if (GetUnsignedIntFromJson(jsonRes, TOKEN_ID_TAG, id) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "parse TokenId failed."); return false; } - id = jsonRes.at(TOKEN_ID_TAG).get(); if (id == AccessToken::INVALID_TOKENID) { SC_LOG_ERROR(LABEL, "TokenId is not invalid."); return false; } - if (jsonRes.find(COMP_TYPE_TAG) == jsonRes.end() || - !jsonRes.at(COMP_TYPE_TAG).is_number()) { + if (GetUnsignedIntFromJson(jsonRes, COMP_TYPE_TAG, type) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "parse CompType failed."); return false; } - type = jsonRes.at(COMP_TYPE_TAG).get(); return true; } -void FirstUseDialog::ParseRecords(nlohmann::json& jsonRes) +void FirstUseDialog::ParseRecords(CJson* jsonRes) { std::unique_lock lock(useMapMutex_); - if (jsonRes.find(FIRST_USE_RECORD_TAG) == jsonRes.end() || - !jsonRes.at(FIRST_USE_RECORD_TAG).is_array()) { + CJson* recordListJson = GetArrayFromJson(jsonRes, FIRST_USE_RECORD_TAG); + if (recordListJson == nullptr) { SC_LOG_ERROR(LABEL, "parse tag failed."); return; } - nlohmann::json recordListJson = jsonRes.at(FIRST_USE_RECORD_TAG); - for (auto& recordJson : recordListJson) { + CJson* iter = nullptr; + cJSON_ArrayForEach(iter, recordListJson) { AccessToken::AccessTokenID id; - uint64_t type; - if (!ParseRecord(recordJson, id, type)) { + uint32_t type; + if (!ParseRecord(iter, id, type)) { SC_LOG_ERROR(LABEL, "parse record failed."); return; } @@ -167,13 +166,14 @@ void FirstUseDialog::LoadFirstUseRecord(void) return; } - nlohmann::json jsonRes = nlohmann::json::parse(content, nullptr, false); - if (jsonRes.is_discarded()) { + CJson* jsonRes = CreateJsonFromString(content); + if (jsonRes == nullptr) { SC_LOG_ERROR(LABEL, "cfg info format is invalid"); return; } ParseRecords(jsonRes); + FreeJson(jsonRes); } void FirstUseDialog::SaveFirstUseRecord(void) @@ -193,26 +193,21 @@ void FirstUseDialog::SaveFirstUseRecord(void) } } - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); { std::unique_lock lock(useMapMutex_); - nlohmann::json recordsJson; - for (auto iter = firstUseMap_.begin(); iter != firstUseMap_.end(); ++iter) { - AccessToken::AccessTokenID id = iter->first; - AccessToken::HapTokenInfo info; - if (AccessToken::AccessTokenKit::GetHapTokenInfo(id, info) != AccessToken::RET_SUCCESS) { - SC_LOG_INFO(LABEL, "token id %{public}d is not exist, do not update it.", id); - continue; - } - nlohmann::json recordJson; - recordJson[TOKEN_ID_TAG] = id; - recordJson[COMP_TYPE_TAG] = iter->second; - recordsJson.emplace_back(recordJson); + if (!GenerateRecordsJson(jsonRes)) { + FreeJson(jsonRes); + return; } - - jsonRes[FIRST_USE_RECORD_TAG] = recordsJson; } - WriteCfgContent(jsonRes.dump()); + std::string jsonStr = PackJsonToString(jsonRes); + FreeJson(jsonRes); + if (jsonStr.empty()) { + SC_LOG_ERROR(LABEL, "json: PackJsonToString return empty."); + return; + } + WriteCfgContent(jsonStr); } void FirstUseDialog::StartDialogAbility(SecCompType type, sptr callerToken) @@ -294,6 +289,45 @@ void FirstUseDialog::Init(std::shared_ptr secHandler) secHandler_ = secHandler; LoadFirstUseRecord(); } + +bool FirstUseDialog::GenerateRecordsJson(CJson* jsonRes) +{ + CJson* recordsJson = CreateJsonArray(); + for (auto iter = firstUseMap_.begin(); iter != firstUseMap_.end(); ++iter) { + AccessToken::AccessTokenID id = iter->first; + AccessToken::HapTokenInfo info; + if (AccessToken::AccessTokenKit::GetHapTokenInfo(id, info) != AccessToken::RET_SUCCESS) { + SC_LOG_INFO(LABEL, "token id %{public}d is not exist, do not update it.", id); + continue; + } + CJson* recordJson = CreateJson(); + if (AddUnsignedIntToJson(recordJson, TOKEN_ID_TAG, id) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", TOKEN_ID_TAG.c_str()); + FreeJson(recordJson); + FreeJson(recordsJson); + return false; + } + if (AddUnsignedIntToJson(recordJson, COMP_TYPE_TAG, iter->second) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", COMP_TYPE_TAG.c_str()); + FreeJson(recordJson); + FreeJson(recordsJson); + return false; + } + if (AddObjToArray(recordsJson, recordJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add array recordsJson error."); + FreeJson(recordJson); + FreeJson(recordsJson); + return false; + } + } + if (AddObjToJson(jsonRes, FIRST_USE_RECORD_TAG, recordsJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", FIRST_USE_RECORD_TAG.c_str()); + FreeJson(recordsJson); + return false; + } + FreeJson(recordsJson); + return true; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h index 753500d..c62cd78 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.h +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -21,7 +21,7 @@ #include #include "access_token.h" #include "iremote_object.h" -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_info.h" #include "sec_event_handler.h" @@ -42,16 +42,17 @@ private: bool IsCfgFileValid(void); bool ReadCfgContent(std::string& content); void WriteCfgContent(const std::string& content); - bool ParseRecord(nlohmann::json& jsonRes, - AccessToken::AccessTokenID& id, uint64_t& type); - void ParseRecords(nlohmann::json& jsonRes); + bool ParseRecord(CJson* jsonRes, + AccessToken::AccessTokenID& id, uint32_t& type); + void ParseRecords(CJson* jsonRes); void LoadFirstUseRecord(void); void SaveFirstUseRecord(void); void StartDialogAbility(SecCompType type, sptr callerToken); void SendSaveEventHandler(void); + bool GenerateRecordsJson(CJson* jsonRes); std::mutex useMapMutex_; - std::unordered_map firstUseMap_; + std::unordered_map firstUseMap_; std::shared_ptr secHandler_; }; } // namespace SecurityComponentEnhance diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 4e4f68e..b9cec84 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -47,7 +47,7 @@ void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, float scale) comp->rect_.x_, comp->rect_.y_, comp->rect_.width_, comp->rect_.height_); } -SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const nlohmann::json& jsonComponent) +SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const CJson* jsonComponent) { SecCompBase* comp = nullptr; switch (type) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h index 7949f4e..a39b2c0 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h @@ -16,7 +16,7 @@ #define I_SECURITY_COMPONENT_INFO_HELPER_H #include "accesstoken_kit.h" -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_base.h" #include "sec_comp_info.h" #include "sec_comp_perm_manager.h" @@ -25,7 +25,7 @@ namespace OHOS { namespace Security { namespace SecurityComponent { template -T* ConstructComponent(const nlohmann::json& jsonComponent) +T* ConstructComponent(const CJson* jsonComponent) { T *componentPtr = new (std::nothrow)T(); if ((componentPtr != nullptr) && !componentPtr->FromJson(jsonComponent)) { @@ -37,7 +37,7 @@ T* ConstructComponent(const nlohmann::json& jsonComponent) class __attribute__((visibility("default"))) SecCompInfoHelper { public: - static SecCompBase* ParseComponent(SecCompType type, const nlohmann::json& jsonComponent); + static SecCompBase* ParseComponent(SecCompType type, const CJson* jsonComponent); static int32_t GrantTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo); static bool CheckComponentValid(SecCompBase* comp); diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 9da222f..4d79122 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -14,6 +14,8 @@ */ #include "sec_comp_manager.h" +#include +#include "cjson_utils.h" #include "delay_exit_task.h" #include "hisysevent.h" #include "i_sec_comp_service.h" @@ -317,7 +319,7 @@ int32_t SecCompManager::AddSecurityComponentProcess(const SecCompCallerInfo& cal } int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) + const CJson* jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) { DelayExitTask::GetInstance().Stop(); SC_LOG_DEBUG(LABEL, "PID: %{public}d, register security component", caller.pid); @@ -357,7 +359,7 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, return ret; } -int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, +int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const CJson* jsonComponent, const SecCompCallerInfo& caller) { SC_LOG_DEBUG(LABEL, "PID: %{public}d, update security component", caller.pid); @@ -407,7 +409,7 @@ int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompC } int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller) + const CJson* jsonComponent, const SecCompCallerInfo& caller) { SC_LOG_DEBUG(LABEL, "PID: %{public}d, Check security component", caller.pid); SecCompBase* report = SecCompInfoHelper::ParseComponent(sc->GetType(), jsonComponent); @@ -442,7 +444,7 @@ int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32 } int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, + const CJson* jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& clickInfo, sptr callerToken) { if (malicious_.IsInMaliciousAppList(caller.pid, caller.uid)) { @@ -493,10 +495,12 @@ void SecCompManager::DumpSecComp(std::string& dumpStr) for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { dumpStr.append("pid:" + std::to_string(iter->first) + "\n"); for (auto compIter = iter->second.compList.begin(); compIter != iter->second.compList.end(); compIter ++) { - nlohmann::json json; + CJson* json = CreateJson(); compIter->GetComponentInfo()->ToJson(json); + std::string jsonStr = PackJsonToString(json); dumpStr.append(" scId:" + std::to_string(compIter->GetScId()) + - ", isGrant:" + std::to_string(compIter->IsGrant()) + ", " + json.dump() + "\n"); + ", isGrant:" + std::to_string(compIter->IsGrant()) + ", " + jsonStr + "\n"); + FreeJson(json); } } } diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index fdf83d0..2d88858 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -22,6 +22,7 @@ #include #include "accesstoken_kit.h" #include "app_state_observer.h" +#include "cjson_utils.h" #include "first_use_dialog.h" #include "nocopyable.h" #include "rwlock.h" @@ -51,12 +52,12 @@ public: static SecCompManager& GetInstance(); virtual ~SecCompManager() = default; - int32_t RegisterSecurityComponent(SecCompType type, const nlohmann::json& jsonComponent, + int32_t RegisterSecurityComponent(SecCompType type, const CJson* jsonComponent, const SecCompCallerInfo& caller, int32_t& scId); - int32_t UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, + int32_t UpdateSecurityComponent(int32_t scId, const CJson* jsonComponent, const SecCompCallerInfo& caller); int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); - int32_t ReportSecurityComponentClickEvent(int32_t scId, const nlohmann::json& jsonComponent, + int32_t ReportSecurityComponentClickEvent(int32_t scId, const CJson* jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& clickInfo, sptr callerToken); void NotifyProcessForeground(int32_t pid); void NotifyProcessBackground(int32_t pid); @@ -76,7 +77,7 @@ private: int32_t DeleteSecurityComponentFromList(int32_t pid, int32_t scId); SecCompEntity* GetSecurityComponentFromList(int32_t pid, int32_t scId); int32_t CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller); + const CJson* jsonComponent, const SecCompCallerInfo& caller); void SendCheckInfoEnhanceSysEvent(int32_t scId, SecCompType type, const std::string& scene, int32_t res); int32_t CreateScId(); diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index ad22a29..941396c 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -14,9 +14,11 @@ */ #include "sec_comp_service.h" +#include #include #include "app_mgr_death_recipient.h" +#include "cjson_utils.h" #include "hisysevent.h" #include "hitrace_meter.h" #include "ipc_skeleton.h" @@ -166,15 +168,14 @@ bool SecCompService::GetCallerInfo(SecCompCallerInfo& caller) } int32_t SecCompService::ParseParams(const std::string& componentInfo, - SecCompCallerInfo& caller, nlohmann::json& jsonRes) + SecCompCallerInfo& caller, CJson*& jsonRes) { if (!GetCallerInfo(caller)) { SC_LOG_ERROR(LABEL, "Check caller failed"); return SC_SERVICE_ERROR_VALUE_INVALID; } - - jsonRes = nlohmann::json::parse(componentInfo, nullptr, false); - if (jsonRes.is_discarded()) { + jsonRes = CreateJsonFromString(componentInfo); + if (jsonRes == nullptr) { SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); return SC_SERVICE_ERROR_VALUE_INVALID; } @@ -195,26 +196,31 @@ int32_t SecCompService::RegisterSecurityComponent(SecCompType type, FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return SC_SERVICE_ERROR_VALUE_INVALID; } - nlohmann::json jsonRes = nlohmann::json::parse(componentInfo, nullptr, false); - if (jsonRes.is_discarded()) { + auto jsonRes = CreateJsonFromString(componentInfo); + if (jsonRes == nullptr) { SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } int32_t res = SecCompManager::GetInstance().RegisterSecurityComponent(type, jsonRes, caller, scId); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + FreeJson(jsonRes); return res; } int32_t SecCompService::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) { SecCompCallerInfo caller; - nlohmann::json jsonRes; + CJson* jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { + FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } - return SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonRes, caller); + int32_t ret = SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonRes, caller); + FreeJson(jsonRes); + return ret; } int32_t SecCompService::UnregisterSecurityComponent(int32_t scId) @@ -232,14 +238,16 @@ int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, { StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentClick"); SecCompCallerInfo caller; - nlohmann::json jsonRes; + CJson* jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } int32_t res = SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, clickInfo, callerToken); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + FreeJson(jsonRes); return res; } diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.h b/services/security_component_service/sa/sa_main/sec_comp_service.h index c7c548e..99bd49b 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.h +++ b/services/security_component_service/sa/sa_main/sec_comp_service.h @@ -18,8 +18,8 @@ #include #include #include "app_state_observer.h" +#include "cjson_utils.h" #include "iremote_object.h" -#include "nlohmann/json.hpp" #include "nocopyable.h" #include "sec_comp_manager.h" #include "sec_comp_stub.h" @@ -52,7 +52,7 @@ public: int Dump(int fd, const std::vector& args) override; private: - int32_t ParseParams(const std::string& componentInfo, SecCompCallerInfo& caller, nlohmann::json& jsonRes); + int32_t ParseParams(const std::string& componentInfo, SecCompCallerInfo& caller, CJson*& jsonRes); bool Initialize() const; bool RegisterAppStateObserver(); void UnregisterAppStateObserver(); diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index 09bc579..e83fb68 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -27,10 +27,10 @@ ohos_unittest("sec_comp_service_test") { "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/include", - "//third_party/json/include", ] sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", @@ -77,6 +77,7 @@ ohos_unittest("sec_comp_service_test") { "ability_runtime:ability_manager", "ability_runtime:runtime", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "eventhandler:libeventhandler", "graphic_2d:librender_service_client", @@ -105,10 +106,10 @@ ohos_unittest("sec_comp_service_mock_test") { "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/include", - "//third_party/json/include", ] sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", @@ -145,6 +146,7 @@ ohos_unittest("sec_comp_service_mock_test") { "ability_runtime:ability_manager", "ability_runtime:runtime", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "eventhandler:libeventhandler", "graphic_2d:librender_service_client", diff --git a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp index 594d42a..17f7561 100644 --- a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp +++ b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp @@ -14,6 +14,7 @@ */ #include "sec_comp_enhance_adapter.h" +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" @@ -94,7 +95,7 @@ void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid) } int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid, - std::shared_ptr& compInfo, const nlohmann::json& jsonComponent) + std::shared_ptr& compInfo, const CJson* jsonComponent) { SC_LOG_DEBUG(LABEL, "CheckComponentInfoEnhnace success"); return SC_OK; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index 0a20e63..099baf9 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -171,11 +171,12 @@ HWTEST_F(SecCompEntityTest, CheckClickInfo002, TestSize.Level1) */ HWTEST_F(SecCompEntityTest, CompareComponentBasicInfo001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* other = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); bool isRectCheck = true; ASSERT_FALSE(entity_->CompareComponentBasicInfo(other, isRectCheck)); + FreeJson(jsonComponent); } /** diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index a14a3e1..97dddb7 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -15,6 +15,7 @@ #include "sec_comp_info_helper_test.h" +#include "cjson_utils.h" #include "display.h" #include "display_info.h" #include "display_manager.h" @@ -84,11 +85,12 @@ void SecCompInfoHelperTest::TearDown() */ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_NE(nullptr, comp); ASSERT_TRUE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -99,12 +101,13 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent)); + FreeJson(jsonComponent); } /** @@ -115,15 +118,16 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, UNKNOWN_SC_TYPE)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_EQ(nullptr, comp); + FreeJson(jsonComponent); } static SecCompRect GetDefaultRect(void) @@ -225,17 +229,20 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, true }, - }; + auto childObj = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddBoolToJson(childObj, JsonTagConstants::JSON_PARENT_EFFECT_TAG, true)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, childObj)); + FreeJson(childObj); + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -246,26 +253,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = ServiceTestCommon::TEST_DIMENSION; - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = ServiceTestCommon::TEST_DIMENSION; - sizeJson[JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -276,21 +290,28 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + ASSERT_NE(nullptr, paddingJson); + + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = ServiceTestCommon::TEST_DIMENSION; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -301,21 +322,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& sizesJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingsJson = sizesJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingsJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + auto paddingsJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - paddingsJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = ServiceTestCommon::TEST_DIMENSION; - paddingsJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -326,20 +351,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& colorJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG]; - colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_INVALID; + auto colorJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); + ASSERT_NE(nullptr, colorJson); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_INVALID)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_RED; - colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_INVALID; + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_INVALID)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -350,18 +380,22 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, NO_TEXT }, - { JsonTagConstants::JSON_ICON_TAG, NO_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; + CJson* styleTagChild = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, NO_TEXT)); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, NO_ICON)); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); + FreeJson(styleTagChild); + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -372,25 +406,32 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& colorsJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG]; - colorsJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_YELLOW; + auto colorsJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); + ASSERT_NE(nullptr, colorsJson); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - colorsJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_RED; - colorsJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_YELLOW; + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE; + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::NO_BG_TYPE))); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -401,27 +442,40 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE; - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = MIN_PADDING_WITHOUT_BG; + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::NO_BG_TYPE))); + + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, paddingJson); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = ServiceTestCommon::TEST_DIMENSION; - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = MIN_PADDING_WITHOUT_BG; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + static_cast(ServiceTestCommon::TEST_DIMENSION))); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -432,28 +486,40 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE; - - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = MIN_PADDING_WITHOUT_BG; + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::NO_BG_TYPE))); + + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, paddingJson); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = ServiceTestCommon::TEST_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + static_cast(ServiceTestCommon::TEST_DIMENSION))); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } @@ -465,23 +531,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE } - }; - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } - }; + + CJson* styleTagChild = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); + FreeJson(styleTagChild); + + CJson* colorsTagChild = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_BLACK)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); + FreeJson(colorsTagChild); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -492,18 +568,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_WHITE } - }; + + CJson* colorsTagChild = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_BLACK)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_WHITE)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); + FreeJson(colorsTagChild); + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } /** @@ -514,7 +597,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->text_ = UNKNOWN_TEXT; @@ -526,6 +609,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) comp->text_ = UNKNOWN_TEXT; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } /** @@ -536,7 +620,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->type_ = SecCompType::UNKNOWN_SC_TYPE; @@ -544,6 +628,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) comp->type_ = SecCompType::MAX_SC_TYPE; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } /** @@ -554,7 +639,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -573,6 +658,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) list.emplace_back(otherWin); Rosen::WindowManager::GetInstance().list_ = list; ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } /** @@ -583,10 +669,11 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid005, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); Rosen::WindowManager::GetInstance().result_ = static_cast(-1); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 25f5d7f..85cbf4f 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -15,6 +15,7 @@ #include "sec_comp_manager_test.h" +#include "cjson_utils.h" #include "sec_comp_log.h" #define private public #include "sec_comp_manager.h" @@ -310,7 +311,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) }; int32_t scId; SecCompManager::GetInstance().malicious_.maliciousAppList_.insert(ServiceTestCommon::TEST_PID_1); - nlohmann::json jsonInvalid; + CJson* jsonInvalid = CreateJson(); EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId)); SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); @@ -320,13 +321,15 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId)); - nlohmann::json jsonValid; + CJson* jsonValid = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); EXPECT_EQ(SC_OK, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId)); SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); + FreeJson(jsonValid); + FreeJson(jsonInvalid); } /** @@ -338,7 +341,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) { SecCompManager::GetInstance().malicious_.maliciousAppList_.insert(ServiceTestCommon::TEST_PID_1); - nlohmann::json jsonValid; + CJson* jsonValid = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); SecCompCallerInfo caller = { @@ -357,7 +360,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity)); // wrong json - nlohmann::json jsonInvalid; + CJson* jsonInvalid = CreateJson(); LocationButton buttonInvalid = BuildInvalidLocationComponent(); buttonInvalid.ToJson(jsonInvalid); EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, @@ -367,6 +370,8 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) EXPECT_EQ(SC_OK, SecCompManager::GetInstance().UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller)); SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); + FreeJson(jsonValid); + FreeJson(jsonInvalid); } /** @@ -399,11 +404,12 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve .pid = ServiceTestCommon::TEST_PID_1 }; SecCompClickEvent clickInfo; - nlohmann::json jsonVaild; + CJson* jsonVaild = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); ASSERT_NE(SC_OK, SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, clickInfo, nullptr)); + FreeJson(jsonVaild); } /** @@ -446,21 +452,27 @@ HWTEST_F(SecCompManagerTest, CheckClickSecurityComponentInfo001, TestSize.Level1 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity)); SecCompClickEvent clickInfo; - nlohmann::json jsonVaild; + CJson* jsonVaild = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); - jsonVaild[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, UNKNOWN_SC_TYPE)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); - jsonVaild[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + + auto rectObj = GetObjFromJson(jsonVaild, JsonTagConstants::JSON_RECT); + ASSERT_NE(nullptr, rectObj); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectObj, JsonTagConstants::JSON_RECT_X, + ServiceTestCommon::TEST_INVALID_DIMENSION)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); - jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_COORDINATE - 1; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectObj, JsonTagConstants::JSON_RECT_X, + ServiceTestCommon::TEST_COORDINATE - 1)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); + FreeJson(jsonVaild); } /** @@ -510,7 +522,7 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList002, TestSize.Level1 */ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonValid; + CJson* jsonValid = CreateJson(); SecCompCallerInfo caller = { .tokenId = ServiceTestCommon::TEST_TOKEN_ID, .pid = ServiceTestCommon::TEST_PID_1 @@ -523,6 +535,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent002, TestSize.Level1) buttonValid.ToJson(jsonValid); ASSERT_NE(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, SecCompManager::GetInstance().UpdateSecurityComponent( ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller)); + FreeJson(jsonValid); } /** diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp index c66b058..c67c16d 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -84,9 +84,9 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "", scId)); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); // parse component json fail ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); @@ -112,6 +112,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) EXPECT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); + FreeJson(jsonRes); } /** @@ -125,9 +126,9 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { @@ -147,6 +148,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); + FreeJson(jsonRes); } /** @@ -160,9 +162,9 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(0)); AppExecFwk::AppStateData stateData = { @@ -183,6 +185,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); + FreeJson(jsonRes); } /** @@ -196,9 +199,9 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { @@ -236,6 +239,7 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. ASSERT_TRUE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); sleep(2); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); + FreeJson(jsonRes); } /** @@ -250,9 +254,9 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { @@ -289,4 +293,5 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0); ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.APPROXIMATELY_LOCATION"), 0); + FreeJson(jsonRes); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 08978d1..faa53f4 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -248,9 +248,9 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId)); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); // parse component json fail ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); @@ -277,6 +277,7 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); setuid(uid); + FreeJson(jsonRes); } /** @@ -357,7 +358,7 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level1) setuid(0); const std::string componentInfo; - nlohmann::json jsonRes; + CJson* jsonRes = nullptr; int32_t scId = 0; EXPECT_EQ(secCompService_->ParseParams(componentInfo, caller, jsonRes), SC_SERVICE_ERROR_VALUE_INVALID); EXPECT_NE(secCompService_->UnregisterSecurityComponent(scId), SC_SERVICE_ERROR_VALUE_INVALID); @@ -366,4 +367,5 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level1) EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo, nullptr), SC_SERVICE_ERROR_VALUE_INVALID); secCompService_->GetEnhanceRemoteObject(); + FreeJson(jsonRes); } diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.cpp b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp index d2358ac..b315ead 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.cpp +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp @@ -13,158 +13,110 @@ * limitations under the License. */ #include "service_test_common.h" +#include "cjson_utils.h" namespace OHOS { namespace Security { namespace SecurityComponent { -void ServiceTestCommon::BuildLocationComponentJson(nlohmann::json& jsonComponent) +void ServiceTestCommon::BuildLocationComponentJson(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - { JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); +} - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } - }; +void ServiceTestCommon::BuildSaveComponentJson(CJson* jsonComponent) +{ + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SAVE_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); } -void ServiceTestCommon::BuildSaveComponentJson(nlohmann::json& jsonComponent) +void ServiceTestCommon::BuildPasteComponentJson(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, PASTE_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } - }; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); +} - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; +void ServiceTestCommon::AddRectToJson(CJson* jsonComponent, const std::string key) +{ + CJson* rectChild = CreateJson(); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE); + AddObjToJson(jsonComponent, key, rectChild); + FreeJson(rectChild); } -void ServiceTestCommon::BuildPasteComponentJson(nlohmann::json& jsonComponent) +void ServiceTestCommon::GenerateCommon(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_NODE_ID, 0); + AddRectToJson(jsonComponent, JsonTagConstants::JSON_RECT); + AddRectToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); + + CJson* jsonPadding = CreateJson(); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION); + + CJson* sizeTagChild = CreateJson(); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE); + AddObjToJson(sizeTagChild, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG, sizeTagChild); + FreeJson(jsonPadding); + FreeJson(sizeTagChild); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; + CJson* colorsTagChild = CreateJson(); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild); + FreeJson(colorsTagChild); - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } - }; + CJson* borderTagChild = CreateJson(); + AddDoubleToJson(borderTagChild, JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTagChild); + FreeJson(borderTagChild); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, - { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; + CJson* parentTagChild = CreateJson(); + AddBoolToJson(parentTagChild, JsonTagConstants::JSON_PARENT_EFFECT_TAG, false); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTagChild); + FreeJson(parentTagChild); } } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.h b/services/security_component_service/sa/test/unittest/src/service_test_common.h index 64989b2..d2635b2 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.h +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.h @@ -34,7 +34,7 @@ public: static constexpr int32_t SC_ID_START = 1000; static constexpr int32_t SA_ID = 3506; - static constexpr float TEST_SIZE = 100.0; + static constexpr double TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIFF_COORDINATE = 200.0; static constexpr double TEST_DIMENSION = 100.0; @@ -55,9 +55,11 @@ public: static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr uint32_t TEST_TOKEN_ID = 1; - static void BuildLocationComponentJson(nlohmann::json& jsonComponent); - static void BuildSaveComponentJson(nlohmann::json& jsonComponent); - static void BuildPasteComponentJson(nlohmann::json& jsonComponent); + static void BuildLocationComponentJson(CJson* jsonComponent); + static void BuildSaveComponentJson(CJson* jsonComponent); + static void BuildPasteComponentJson(CJson* jsonComponent); + static void AddRectToJson(CJson* jsonComponent, const std::string key); + static void GenerateCommon(CJson* jsonComponent); }; } // namespace SecurityComponent } // namespace Security diff --git a/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn index fa31e3c..5ee78a2 100644 --- a/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn +++ b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn @@ -42,6 +42,7 @@ ohos_fuzztest("GetEnhanceRemoteObjectInnerFuzzTest") { "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", diff --git a/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/BUILD.gn index 1174766..c3a34a8 100644 --- a/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/BUILD.gn +++ b/test/fuzztest/security_component/registersecuritycomponentInner_fuzzer/BUILD.gn @@ -42,6 +42,7 @@ ohos_fuzztest("RegisterSecurityComponentInnerFuzzTest") { "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", diff --git a/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/BUILD.gn index 8adc654..0bba978 100644 --- a/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/BUILD.gn +++ b/test/fuzztest/security_component/reportsecuritycomponentclickeventInner_fuzzer/BUILD.gn @@ -42,6 +42,7 @@ ohos_fuzztest("ReportSecurityComponentClickEventInnerFuzzTest") { "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", diff --git a/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/BUILD.gn index bfafdf2..deb8993 100644 --- a/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/BUILD.gn +++ b/test/fuzztest/security_component/unregistersecuritycomponentInner_fuzzer/BUILD.gn @@ -42,6 +42,7 @@ ohos_fuzztest("UnregisterSecurityComponentInnerFuzzTest") { "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", diff --git a/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/BUILD.gn index a46e82b..dad031f 100644 --- a/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/BUILD.gn +++ b/test/fuzztest/security_component/updatesecuritycomponentInner_fuzzer/BUILD.gn @@ -42,6 +42,7 @@ ohos_fuzztest("UpdateSecurityComponentInnerFuzzTest") { "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", diff --git a/test/fuzztest/security_component/verifysavepermissionInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/verifysavepermissionInner_fuzzer/BUILD.gn index 3eaaaf9..7879556 100644 --- a/test/fuzztest/security_component/verifysavepermissionInner_fuzzer/BUILD.gn +++ b/test/fuzztest/security_component/verifysavepermissionInner_fuzzer/BUILD.gn @@ -42,6 +42,7 @@ ohos_fuzztest("VerifySavePermissionInnerFuzzTest") { "ability_runtime:app_manager", "access_token:libaccesstoken_sdk", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", -- Gitee From d258d3091013a8f5715731425817840d53264610 Mon Sep 17 00:00:00 2001 From: yujann Date: Tue, 20 Feb 2024 17:45:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=AF=84=E5=AE=A1=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I442c0fda68826e39129052a886f8e7753048b8a0 Signed-off-by: yujann --- .../include/sec_comp_enhance_adapter.h | 4 +- .../src/sec_comp_enhance_adapter.cpp | 3 +- .../security_component/src/sec_comp_base.cpp | 157 +++++------ .../unittest/src/sec_comp_enhance_test.cpp | 113 ++++++++ .../security_component/include/cjson_utils.h | 63 +++-- .../include/location_button.h | 2 +- .../security_component/include/paste_button.h | 2 +- .../security_component/include/save_button.h | 2 +- .../include/sec_comp_base.h | 30 +- .../security_component/src/cjson_utils.cpp | 259 ++++++++++++------ .../unittest/src/location_button_test.cpp | 195 ++++++------- .../test/unittest/src/paste_button_test.cpp | 34 +-- .../test/unittest/src/save_button_test.cpp | 34 +-- .../test/unittest/src/sec_comp_kit_test.cpp | 6 +- .../src/sec_comp_register_callback_test.cpp | 45 +-- .../test/unittest/src/test_common.cpp | 37 +-- .../test/unittest/src/test_common.h | 12 +- .../sa/sa_main/first_use_dialog.cpp | 35 +-- .../sa/sa_main/first_use_dialog.h | 4 +- .../sa/sa_main/sec_comp_info_helper.cpp | 3 +- .../sa/sa_main/sec_comp_info_helper.h | 4 +- .../sa/sa_main/sec_comp_manager.cpp | 11 +- .../sa/sa_main/sec_comp_manager.h | 8 +- .../sa/sa_main/sec_comp_service.cpp | 13 +- .../sa/sa_main/sec_comp_service.h | 2 +- .../mock/src/sec_comp_enhance_adapter.cpp | 2 +- .../unittest/src/sec_comp_entity_test.cpp | 3 +- .../src/sec_comp_info_helper_test.cpp | 169 +++++------- .../unittest/src/sec_comp_manager_test.cpp | 29 +- .../src/sec_comp_service_mock_test.cpp | 15 +- .../unittest/src/sec_comp_service_test.cpp | 7 +- .../test/unittest/src/service_test_common.cpp | 37 +-- .../test/unittest/src/service_test_common.h | 13 +- 33 files changed, 700 insertions(+), 653 deletions(-) diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h index ace2e75..bfb347c 100644 --- a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -55,7 +55,7 @@ public: // send component info to enhance service for checking its validity virtual int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, - const CJson* jsonComponent) = 0; + CJsonUnique& jsonComponent) = 0; // get RemoteObject of enhance service to connect it virtual sptr GetEnhanceRemoteObject() = 0; @@ -97,7 +97,7 @@ public: static int32_t EnableInputEnhance(); static int32_t DisableInputEnhance(); static int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, - const CJson* jsonComponent); + CJsonUnique& jsonComponent); static sptr GetEnhanceRemoteObject(); static void StartEnhanceService(); static void ExistEnhanceService(); diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp index d7773b2..40052c4 100644 --- a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -15,6 +15,7 @@ #include "sec_comp_enhance_adapter.h" #include +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" @@ -218,7 +219,7 @@ void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid) } int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid, - std::shared_ptr& compInfo, const CJson* jsonComponent) + std::shared_ptr& compInfo, CJsonUnique& jsonComponent) { if (!isEnhanceSrvHandlerInit) { InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index 7fd12fc..faccb76 100644 --- a/frameworks/security_component/src/sec_comp_base.cpp +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -62,7 +62,7 @@ const std::string JsonTagConstants::JSON_WINDOW_ID = "windowId"; bool SecCompBase::ParseDimension(const CJson* json, const std::string& tag, DimensionT& res) { double searchRes; - if (GetDoubleFromJson(json, tag, searchRes) != CJSON_SUCCESS) { + if (!GetDoubleFromJson(json, tag, searchRes)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } @@ -73,7 +73,7 @@ bool SecCompBase::ParseDimension(const CJson* json, const std::string& tag, Dime bool SecCompBase::ParseColor(const CJson* json, const std::string& tag, SecCompColor& res) { uint32_t searchRes; - if (GetUnsignedIntFromJson(json, tag, searchRes) != CJSON_SUCCESS) { + if (!GetUnsignedIntFromJson(json, tag, searchRes)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } @@ -85,7 +85,7 @@ bool SecCompBase::ParseColor(const CJson* json, const std::string& tag, SecCompC bool SecCompBase::ParseBool(const CJson* json, const std::string& tag, bool& res) { bool searchRes; - if (GetBoolFromJson(json, tag, searchRes) != CJSON_SUCCESS) { + if (!GetBoolFromJson(json, tag, searchRes)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } @@ -117,7 +117,7 @@ bool SecCompBase::ParsePadding(const CJson* json, const std::string& tag, Paddin return true; } -bool SecCompBase::ParseColors(const CJson* json, const std::string& tag) +bool SecCompBase::ParseColors(CJsonUnique& json, const std::string& tag) { auto jsonColors = GetObjFromJson(json, tag); if (jsonColors == nullptr) { @@ -137,7 +137,7 @@ bool SecCompBase::ParseColors(const CJson* json, const std::string& tag) return true; } -bool SecCompBase::ParseBorders(const CJson* json, const std::string& tag) +bool SecCompBase::ParseBorders(CJsonUnique& json, const std::string& tag) { auto jsonBorder = GetObjFromJson(json, tag); if (jsonBorder == nullptr) { @@ -148,7 +148,7 @@ bool SecCompBase::ParseBorders(const CJson* json, const std::string& tag) return ParseDimension(jsonBorder, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_); } -bool SecCompBase::ParseSize(const CJson* json, const std::string& tag) +bool SecCompBase::ParseSize(CJsonUnique& json, const std::string& tag) { auto jsonSize = GetObjFromJson(json, tag); if (jsonSize == nullptr) { @@ -175,7 +175,7 @@ bool SecCompBase::ParseSize(const CJson* json, const std::string& tag) return true; } -bool SecCompBase::ParseParent(const CJson* json, const std::string& tag) +bool SecCompBase::ParseParent(CJsonUnique& json, const std::string& tag) { auto jsonParent = GetObjFromJson(json, tag); if (jsonParent == nullptr) { @@ -186,7 +186,7 @@ bool SecCompBase::ParseParent(const CJson* json, const std::string& tag) return ParseBool(jsonParent, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_); } -bool SecCompBase::ParseRect(const CJson* json, const std::string& tag, SecCompRect& rect) +bool SecCompBase::ParseRect(CJsonUnique& json, const std::string& tag, SecCompRect& rect) { auto jsonSize = GetObjFromJson(json, tag); if (jsonSize == nullptr) { @@ -213,13 +213,13 @@ bool SecCompBase::ParseRect(const CJson* json, const std::string& tag, SecCompRe return true; } -bool SecCompBase::FromJson(const CJson* jsonSrc) +bool SecCompBase::FromJson(CJsonUnique& jsonSrc) { std::string jsonStr = PackJsonToString(jsonSrc); SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonStr.c_str()); int32_t value; - if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_SC_TYPE, value) != CJSON_SUCCESS) { + if (!GetIntFromJson(jsonSrc, JsonTagConstants::JSON_SC_TYPE, value)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_SC_TYPE.c_str()); return false; } @@ -230,7 +230,7 @@ bool SecCompBase::FromJson(const CJson* jsonSrc) } type_ = static_cast(value); - if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_NODE_ID, nodeId_) != CJSON_SUCCESS) { + if (!GetIntFromJson(jsonSrc, JsonTagConstants::JSON_NODE_ID, nodeId_)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_NODE_ID.c_str()); return false; } @@ -256,21 +256,21 @@ bool SecCompBase::FromJson(const CJson* jsonSrc) if (!ParseStyle(jsonSrc, JsonTagConstants::JSON_STYLE_TAG)) { return false; } - if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_WINDOW_ID, windowId_) != CJSON_SUCCESS) { + if (!GetIntFromJson(jsonSrc, JsonTagConstants::JSON_WINDOW_ID, windowId_)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_WINDOW_ID.c_str()); return false; } return true; } -void SecCompBase::ToJson(CJson* jsonRes) const +void SecCompBase::ToJson(CJsonUnique& jsonRes) const { - if (AddIntToJson(jsonRes, JsonTagConstants::JSON_SC_TYPE, type_) != CJSON_SUCCESS) { + if (!AddIntToJson(jsonRes, JsonTagConstants::JSON_SC_TYPE, type_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", JsonTagConstants::JSON_SC_TYPE.c_str(), type_); return; } - if (AddIntToJson(jsonRes, JsonTagConstants::JSON_NODE_ID, nodeId_) != CJSON_SUCCESS) { + if (!AddIntToJson(jsonRes, JsonTagConstants::JSON_NODE_ID, nodeId_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", JsonTagConstants::JSON_NODE_ID.c_str(), nodeId_); return; @@ -297,7 +297,7 @@ void SecCompBase::ToJson(CJson* jsonRes) const if (!AddStyle(jsonRes)) { return; } - if (AddIntToJson(jsonRes, JsonTagConstants::JSON_WINDOW_ID, windowId_) != CJSON_SUCCESS) { + if (!AddIntToJson(jsonRes, JsonTagConstants::JSON_WINDOW_ID, windowId_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", JsonTagConstants::JSON_WINDOW_ID.c_str(), windowId_); return; @@ -306,7 +306,7 @@ void SecCompBase::ToJson(CJson* jsonRes) const std::string SecCompBase::ToJsonStr() const { - CJson* json = CreateJson(); + auto json = CreateJson(); ToJson(json); std::string jsonStr = PackJsonToString(json); return jsonStr; @@ -337,7 +337,7 @@ bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck return (leftValue == rightValue); } -bool SecCompBase::ParseStyle(const CJson* json, const std::string& tag) +bool SecCompBase::ParseStyle(CJsonUnique& json, const std::string& tag) { CJson* jsonStyle = GetObjFromJson(json, tag); if (jsonStyle == nullptr) { @@ -345,12 +345,12 @@ bool SecCompBase::ParseStyle(const CJson* json, const std::string& tag) return false; } - if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_TEXT_TAG, text_) != CJSON_SUCCESS) { + if (!GetIntFromJson(jsonStyle, JsonTagConstants::JSON_TEXT_TAG, text_)) { SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", tag.c_str(), JsonTagConstants::JSON_TEXT_TAG.c_str()); return false; } - if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_ICON_TAG, icon_) != CJSON_SUCCESS) { + if (!GetIntFromJson(jsonStyle, JsonTagConstants::JSON_ICON_TAG, icon_)) { SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", tag.c_str(), JsonTagConstants::JSON_ICON_TAG.c_str()); return false; @@ -362,7 +362,7 @@ bool SecCompBase::ParseStyle(const CJson* json, const std::string& tag) } int32_t bg; - if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_BG_TAG, bg) != CJSON_SUCCESS) { + if (!GetIntFromJson(jsonStyle, JsonTagConstants::JSON_BG_TAG, bg)) { SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", tag.c_str(), JsonTagConstants::JSON_BG_TAG.c_str()); return false; @@ -377,61 +377,55 @@ bool SecCompBase::ParseStyle(const CJson* json, const std::string& tag) return true; } -bool SecCompBase::AddRect(CJson* json, const std::string jsonTag, const SecCompRect rectObj) const +bool SecCompBase::AddRect(CJsonUnique& json, const std::string jsonTag, const SecCompRect rectObj) const { - CJson* childJson = CreateJson(); - if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_X, rectObj.x_) != CJSON_SUCCESS) { + auto childJson = CreateJson(); + if (!AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_X, rectObj.x_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_RECT_X.c_str(), rectObj.x_); - FreeJson(childJson); return false; } - if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_Y, rectObj.y_) != CJSON_SUCCESS) { + if (!AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_Y, rectObj.y_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_RECT_Y.c_str(), rectObj.y_); - FreeJson(childJson); return false; } - if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_WIDTH, rectObj.width_) != CJSON_SUCCESS) { + if (!AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_WIDTH, rectObj.width_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_RECT_WIDTH.c_str(), rectObj.width_); - FreeJson(childJson); return false; } - if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_HEIGHT, rectObj.height_) != CJSON_SUCCESS) { + if (!AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_HEIGHT, rectObj.height_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_RECT_HEIGHT.c_str(), rectObj.height_); - FreeJson(childJson); return false; } - if (AddObjToJson(json, jsonTag, childJson) != CJSON_SUCCESS) { + if (!AddObjToJson(json, jsonTag, childJson)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", JsonTagConstants::JSON_RECT.c_str()); - FreeJson(childJson); return false; } - FreeJson(childJson); return true; } -bool SecCompBase::AddPadding(CJson* json) const +bool SecCompBase::AddPadding(CJsonUnique& json) const { - if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_TOP_TAG, padding_.top) != CJSON_SUCCESS) { + if (!AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_TOP_TAG, padding_.top)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_PADDING_TOP_TAG.c_str(), padding_.top); return false; } - if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding_.right) != CJSON_SUCCESS) { + if (!AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding_.right)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_PADDING_RIGHT_TAG.c_str(), padding_.right); return false; } - if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding_.bottom) != CJSON_SUCCESS) { + if (!AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding_.bottom)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_PADDING_BOTTOM_TAG.c_str(), padding_.bottom); return false; } - if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_LEFT_TAG, padding_.left) != CJSON_SUCCESS) { + if (!AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_LEFT_TAG, padding_.left)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_PADDING_LEFT_TAG.c_str(), padding_.left); return false; @@ -439,152 +433,123 @@ bool SecCompBase::AddPadding(CJson* json) const return true; } -bool SecCompBase::AddSize(CJson* json) const +bool SecCompBase::AddSize(CJsonUnique& json) const { - CJson* jsonPadding = CreateJson(); + auto jsonPadding = CreateJson(); if (!AddPadding(jsonPadding)) { - FreeJson(jsonPadding); return false; } - CJson* childJson = CreateJson(); - if (AddDoubleToJson(childJson, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_) != CJSON_SUCCESS) { + auto childJson = CreateJson(); + if (!AddDoubleToJson(childJson, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_FONT_SIZE_TAG.c_str(), fontSize_); - FreeJson(jsonPadding); - FreeJson(childJson); return false; } - if (AddDoubleToJson(childJson, JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_) != CJSON_SUCCESS) { + if (!AddDoubleToJson(childJson, JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_ICON_SIZE_TAG.c_str(), iconSize_); - FreeJson(jsonPadding); - FreeJson(childJson); return false; } - if (AddDoubleToJson(childJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_) != CJSON_SUCCESS) { + if (!AddDoubleToJson(childJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG.c_str(), textIconSpace_); - FreeJson(jsonPadding); - FreeJson(childJson); return false; } - if (AddObjToJson(childJson, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding) != CJSON_SUCCESS) { + if (!AddObjToJson(childJson, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", JsonTagConstants::JSON_PADDING_SIZE_TAG.c_str()); - FreeJson(jsonPadding); - FreeJson(childJson); return false; } - if (AddObjToJson(json, JsonTagConstants::JSON_SIZE_TAG, childJson) != CJSON_SUCCESS) { + if (!AddObjToJson(json, JsonTagConstants::JSON_SIZE_TAG, childJson)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", JsonTagConstants::JSON_SIZE_TAG.c_str()); - FreeJson(jsonPadding); - FreeJson(childJson); return false; } - FreeJson(jsonPadding); - FreeJson(childJson); return true; } -bool SecCompBase::AddColors(CJson* json) const +bool SecCompBase::AddColors(CJsonUnique& json) const { - CJson* childJson = CreateJson(); - if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value) != CJSON_SUCCESS) { + auto childJson = CreateJson(); + if (!AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", JsonTagConstants::JSON_FONT_COLOR_TAG.c_str(), fontColor_.value); - FreeJson(childJson); return false; } - if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value) != CJSON_SUCCESS) { + if (!AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", JsonTagConstants::JSON_ICON_COLOR_TAG.c_str(), iconColor_.value); - FreeJson(childJson); return false; } - if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value) != CJSON_SUCCESS) { + if (!AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", JsonTagConstants::JSON_BG_COLOR_TAG.c_str(), bgColor_.value); - FreeJson(childJson); return false; } - if (AddObjToJson(json, JsonTagConstants::JSON_COLORS_TAG, childJson) != CJSON_SUCCESS) { + if (!AddObjToJson(json, JsonTagConstants::JSON_COLORS_TAG, childJson)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", JsonTagConstants::JSON_COLORS_TAG.c_str()); - FreeJson(childJson); return false; } - FreeJson(childJson); return true; } -bool SecCompBase::AddBorders(CJson* json) const +bool SecCompBase::AddBorders(CJsonUnique& json) const { - CJson* childJson = CreateJson(); - if (AddDoubleToJson(childJson, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_) != CJSON_SUCCESS) { + auto childJson = CreateJson(); + if (!AddDoubleToJson(childJson, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", JsonTagConstants::JSON_BORDER_WIDTH_TAG.c_str(), borderWidth_); - FreeJson(childJson); return false; } - if (AddObjToJson(json, JsonTagConstants::JSON_BORDER_TAG, childJson) != CJSON_SUCCESS) { + if (!AddObjToJson(json, JsonTagConstants::JSON_BORDER_TAG, childJson)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", JsonTagConstants::JSON_BORDER_TAG.c_str()); - FreeJson(childJson); return false; } - FreeJson(childJson); return true; } -bool SecCompBase::AddParent(CJson* json) const +bool SecCompBase::AddParent(CJsonUnique& json) const { - CJson* childJson = CreateJson(); - if (AddBoolToJson(childJson, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_) != CJSON_SUCCESS) { + auto childJson = CreateJson(); + if (!AddBoolToJson(childJson, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", JsonTagConstants::JSON_PARENT_EFFECT_TAG.c_str(), parentEffect_); - FreeJson(childJson); return false; } - if (AddObjToJson(json, JsonTagConstants::JSON_PARENT_TAG, childJson) != CJSON_SUCCESS) { + if (!AddObjToJson(json, JsonTagConstants::JSON_PARENT_TAG, childJson)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", JsonTagConstants::JSON_PARENT_TAG.c_str()); - FreeJson(childJson); return false; } - FreeJson(childJson); return true; } -bool SecCompBase::AddStyle(CJson* json) const +bool SecCompBase::AddStyle(CJsonUnique& json) const { - CJson* childJson = CreateJson(); - if (AddIntToJson(childJson, JsonTagConstants::JSON_TEXT_TAG, text_) != CJSON_SUCCESS) { + auto childJson = CreateJson(); + if (!AddIntToJson(childJson, JsonTagConstants::JSON_TEXT_TAG, text_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", JsonTagConstants::JSON_TEXT_TAG.c_str(), text_); - FreeJson(childJson); return false; } - if (AddIntToJson(childJson, JsonTagConstants::JSON_ICON_TAG, icon_) != CJSON_SUCCESS) { + if (!AddIntToJson(childJson, JsonTagConstants::JSON_ICON_TAG, icon_)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", JsonTagConstants::JSON_ICON_TAG.c_str(), icon_); - FreeJson(childJson); return false; } - if (AddIntToJson(childJson, JsonTagConstants::JSON_BG_TAG, static_cast(bg_)) != CJSON_SUCCESS) { + if (!AddIntToJson(childJson, JsonTagConstants::JSON_BG_TAG, static_cast(bg_))) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", JsonTagConstants::JSON_BG_TAG.c_str(), bg_); - FreeJson(childJson); return false; } - if (AddObjToJson(json, JsonTagConstants::JSON_STYLE_TAG, childJson) != CJSON_SUCCESS) { + if (!AddObjToJson(json, JsonTagConstants::JSON_STYLE_TAG, childJson)) { SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", JsonTagConstants::JSON_STYLE_TAG.c_str()); - FreeJson(childJson); return false; } - FreeJson(childJson); return true; } } // namespace base diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp index e3a69e7..fd051e5 100644 --- a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp +++ b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp @@ -102,3 +102,116 @@ HWTEST_F(SecCompEnhanceTest, GetPoniterEventEnhanceData001, TestSize.Level1) EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); } } + +/** + * @tc.name: EnableInputEnhance001 + * @tc.desc: test UnmarshallEnhanceCfg + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, EnableInputEnhance001, TestSize.Level1) +{ + int32_t result = SecCompEnhanceAdapter::EnableInputEnhance(); + if (g_srvEnhanceExist) { + EXPECT_EQ(result, SC_OK); + } else { + EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); + } +} + +/** + * @tc.name: DisableInputEnhance001 + * @tc.desc: test DisableInputEnhance + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, DisableInputEnhance001, TestSize.Level1) +{ + int32_t result = SecCompEnhanceAdapter::DisableInputEnhance(); + if (g_srvEnhanceExist) { + EXPECT_EQ(result, SC_OK); + } else { + EXPECT_EQ(result, SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE); + } +} + +/** + * @tc.name: InitEnhanceHandler001 + * @tc.desc: test InitEnhanceHandler + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, InitEnhanceHandler001, TestSize.Level1) +{ + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + uint8_t originData[16] = { 0 }; + uint32_t dataLen = 16; + uint8_t* enhanceData = nullptr; + uint32_t enHancedataLen = MAX_HMAC_SIZE; + ASSERT_NE(SC_OK, + SecCompEnhanceAdapter::GetPointerEventEnhanceData(originData, dataLen, enhanceData, enHancedataLen)); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + SecCompClickEvent touchInfo; + ASSERT_NE(SC_OK, SecCompEnhanceAdapter::CheckExtraInfo(touchInfo)); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + + std::string componentInfo; + int32_t scId = 1; + SecCompEnhanceAdapter::EnhanceDataPreprocess(scId, componentInfo); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + SecCompEnhanceAdapter::RegisterScIdEnhance(scId); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + SecCompEnhanceAdapter::DisableInputEnhance(); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + SecCompEnhanceAdapter::StartEnhanceService(); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + SecCompEnhanceAdapter::ExistEnhanceService(); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + SecCompEnhanceAdapter::NotifyProcessDied(scId); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + std::shared_ptr compInfo; + auto jsonComponent = CreateJson(); + SecCompEnhanceAdapter::CheckComponentInfoEnhnace(scId, compInfo, jsonComponent); + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; + SecCompEnhanceAdapter::GetEnhanceRemoteObject(); +} + +/** + * @tc.name: InitEnhanceHandler002 + * @tc.desc: test InitEnhanceHandler + * @tc.type: FUNC + * @tc.require: AR000HO9IN + */ +HWTEST_F(SecCompEnhanceTest, InitEnhanceHandler002, TestSize.Level1) +{ + SecCompEnhanceAdapter::isEnhanceInputHandlerInit = true; + uint8_t cfgData[SEC_COMP_ENHANCE_CFG_SIZE] = { 0 }; + SecCompEnhanceAdapter::inputHandler = nullptr; + ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, + SecCompEnhanceAdapter::SetEnhanceCfg(cfgData, SEC_COMP_ENHANCE_CFG_SIZE)); + uint8_t originData[16] = { 0 }; + uint32_t dataLen = 16; + uint8_t* enhanceData = nullptr; + uint32_t enHancedataLen = MAX_HMAC_SIZE; + ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, + SecCompEnhanceAdapter::GetPointerEventEnhanceData(originData, dataLen, enhanceData, enHancedataLen)); + SecCompClickEvent touchInfo; + SecCompEnhanceAdapter::srvHandler = nullptr; + ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, SecCompEnhanceAdapter::CheckExtraInfo(touchInfo)); + std::string componentInfo; + int32_t scId = 1; + SecCompEnhanceAdapter::clientHandler = nullptr; + ASSERT_TRUE(SecCompEnhanceAdapter::EnhanceDataPreprocess(componentInfo)); + ASSERT_TRUE(SecCompEnhanceAdapter::EnhanceDataPreprocess(scId, componentInfo)); + SecCompEnhanceAdapter::RegisterScIdEnhance(scId); + ASSERT_EQ(SecCompEnhanceAdapter::srvHandler, nullptr); + ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, SecCompEnhanceAdapter::EnableInputEnhance()); + ASSERT_EQ(SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE, SecCompEnhanceAdapter::DisableInputEnhance()); + SecCompEnhanceAdapter::StartEnhanceService(); + SecCompEnhanceAdapter::ExistEnhanceService(); + SecCompEnhanceAdapter::NotifyProcessDied(scId); + std::shared_ptr compInfo; + auto jsonComponent = CreateJson(); + ASSERT_EQ(SC_OK, SecCompEnhanceAdapter::CheckComponentInfoEnhnace(scId, compInfo, jsonComponent)); + ASSERT_EQ(nullptr, SecCompEnhanceAdapter::GetEnhanceRemoteObject()); +} diff --git a/interfaces/inner_api/security_component/include/cjson_utils.h b/interfaces/inner_api/security_component/include/cjson_utils.h index dcdbd69..d4aaf48 100644 --- a/interfaces/inner_api/security_component/include/cjson_utils.h +++ b/interfaces/inner_api/security_component/include/cjson_utils.h @@ -17,6 +17,7 @@ #define JSON_UTILS_H #include +#include #include #include #include @@ -26,27 +27,19 @@ namespace OHOS { namespace Security { namespace SecurityComponent { typedef cJSON CJson; -enum { - CJSON_SUCCESS = 0, - CJSON_ERR_INVALID_PARAM = -1, - CJSON_ERR_BAD_ALLOC = -2, - CJSON_ERR_NULL_PTR = -3, - CJSON_ERR_JSON_GET = -4, - CJSON_ERR_JSON_ADD = -5, - CJSON_ERR_JSON_REPLACE = -6, - CJSON_ERR_JSON_DUPLICATE = -7, -}; +typedef std::unique_ptr> CJsonUnique; -/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ -CJson* CreateJsonFromString(const std::string jsonStr); -/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ -CJson* CreateJson(void); -/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ -CJson* CreateJsonArray(void); -void FreeJson(CJson*& jsonObj); +/* NO Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJsonUnique CreateJsonFromString(const std::string jsonStr); +/* NO Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJsonUnique CreateJson(void); +/* NO Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJsonUnique CreateJsonArray(void); +void FreeJson(CJson* jsonObj); /* NO Need to call FreeJsonString to free the returned pointer when it's no longer in use. */ std::string PackJsonToString(const CJson* jsonObj); +std::string PackJsonToString(CJsonUnique& jsonObj); void FreeJsonString(char* jsonStr); /* @@ -55,25 +48,39 @@ void FreeJsonString(char* jsonStr); * It will be recycled along with jsonObj when jsonObj is released. */ CJson* GetObjFromJson(const CJson* jsonObj, const std::string key); +CJson* GetObjFromJson(CJsonUnique& jsonObj, const std::string key); CJson* GetArrayFromJson(const CJson* jsonObj, const std::string key); +CJson* GetArrayFromJson(CJsonUnique& jsonObj, const std::string key); /* * Return a copy of string in jsonObj in std::string */ std::string GetStringFromJson(const CJson* jsonObj, const std::string key); +std::string GetStringFromJson(CJsonUnique& jsonObj, const std::string key); -int32_t GetIntFromJson(const CJson* jsonObj, const std::string key, int32_t& value); -int32_t GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value); -int32_t GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value); -int32_t GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value); +bool GetIntFromJson(const CJson* jsonObj, const std::string key, int32_t& value); +bool GetIntFromJson(CJsonUnique& jsonObj, const std::string key, int32_t& value); +bool GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value); +bool GetUnsignedIntFromJson(CJsonUnique& jsonObj, const std::string key, uint32_t& value); +bool GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value); +bool GetBoolFromJson(CJsonUnique& jsonObj, const std::string key, bool& value); +bool GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value); +bool GetDoubleFromJson(CJsonUnique& jsonObj, const std::string key, double& value); -int32_t AddObjToJson(CJson* jsonObj, const std::string key, const CJson* childObj); -int32_t AddObjToArray(CJson* jsonArr, CJson* item); -int32_t AddStringToJson(CJson* jsonObj, const std::string key, const std::string value); -int32_t AddBoolToJson(CJson* jsonObj, const std::string key, const bool value); -int32_t AddIntToJson(CJson* jsonObj, const std::string key, const int value); -int32_t AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value); -int32_t AddDoubleToJson(CJson* jsonObj, const std::string key, const double value); +bool AddObjToJson(CJson* jsonObj, const std::string key, const CJson* childObj); +bool AddObjToJson(CJsonUnique& jsonObj, const std::string key, CJsonUnique& childObj); +bool AddObjToArray(CJson* jsonArr, CJson* item); +bool AddObjToArray(CJsonUnique& jsonArr, CJsonUnique& item); +bool AddStringToJson(CJson* jsonObj, const std::string key, const std::string value); +bool AddStringToJson(CJsonUnique& jsonObj, const std::string key, const std::string value); +bool AddBoolToJson(CJson* jsonObj, const std::string key, const bool value); +bool AddBoolToJson(CJsonUnique& jsonObj, const std::string key, const bool value); +bool AddIntToJson(CJson* jsonObj, const std::string key, const int value); +bool AddIntToJson(CJsonUnique& jsonObj, const std::string key, const int value); +bool AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value); +bool AddUnsignedIntToJson(CJsonUnique& jsonObj, const std::string key, const uint32_t value); +bool AddDoubleToJson(CJson* jsonObj, const std::string key, const double value); +bool AddDoubleToJson(CJsonUnique& jsonObj, const std::string key, const double value); } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/include/location_button.h b/interfaces/inner_api/security_component/include/location_button.h index 51684dc..e22b5e4 100644 --- a/interfaces/inner_api/security_component/include/location_button.h +++ b/interfaces/inner_api/security_component/include/location_button.h @@ -50,7 +50,7 @@ public: virtual bool IsCorrespondenceType() override; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const CJson* json, const std::string& tag); + bool ParseStyle(CJsonUnique& json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/paste_button.h b/interfaces/inner_api/security_component/include/paste_button.h index f8e5f13..7f0a490 100644 --- a/interfaces/inner_api/security_component/include/paste_button.h +++ b/interfaces/inner_api/security_component/include/paste_button.h @@ -40,7 +40,7 @@ public: virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const CJson* json, const std::string& tag); + bool ParseStyle(CJsonUnique& json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/save_button.h b/interfaces/inner_api/security_component/include/save_button.h index b636ecd..dfcb61a 100644 --- a/interfaces/inner_api/security_component/include/save_button.h +++ b/interfaces/inner_api/security_component/include/save_button.h @@ -47,7 +47,7 @@ public: virtual bool IsCorrespondenceType() override; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const CJson* json, const std::string& tag); + bool ParseStyle(CJsonUnique& json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h index 823f907..8affc31 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_base.h +++ b/interfaces/inner_api/security_component/include/sec_comp_base.h @@ -78,8 +78,8 @@ class __attribute__((visibility("default"))) SecCompBase { public: SecCompBase() = default; virtual ~SecCompBase() = default; - bool FromJson(const CJson* jsonSrc); - void ToJson(CJson* jsonRes) const; + bool FromJson(CJsonUnique& jsonSrc); + void ToJson(CJsonUnique& jsonRes) const; std::string ToJsonStr(void) const; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const; void SetValid(bool valid) @@ -128,19 +128,19 @@ private: bool ParseColor(const CJson* json, const std::string& tag, SecCompColor& res); bool ParseBool(const CJson* json, const std::string& tag, bool& res); bool ParsePadding(const CJson* json, const std::string& tag, PaddingSize& res); - bool ParseColors(const CJson* json, const std::string& tag); - bool ParseBorders(const CJson* json, const std::string& tag); - bool ParseSize(const CJson* json, const std::string& tag); - bool ParseParent(const CJson* json, const std::string& tag); - bool ParseRect(const CJson* json, const std::string& tag, SecCompRect& rect); - bool ParseStyle(const CJson* json, const std::string& tag); - bool AddRect(CJson* json, const std::string jsonTag, const SecCompRect rectObj) const; - bool AddPadding(CJson* json) const; - bool AddSize(CJson* json) const; - bool AddColors(CJson* json) const; - bool AddBorders(CJson* json) const; - bool AddParent(CJson* json) const; - bool AddStyle(CJson* json) const; + bool ParseColors(CJsonUnique& json, const std::string& tag); + bool ParseBorders(CJsonUnique& json, const std::string& tag); + bool ParseSize(CJsonUnique& json, const std::string& tag); + bool ParseParent(CJsonUnique& json, const std::string& tag); + bool ParseRect(CJsonUnique& json, const std::string& tag, SecCompRect& rect); + bool ParseStyle(CJsonUnique& json, const std::string& tag); + bool AddRect(CJsonUnique& json, const std::string jsonTag, const SecCompRect rectObj) const; + bool AddPadding(CJsonUnique& json) const; + bool AddSize(CJsonUnique& json) const; + bool AddColors(CJsonUnique& json) const; + bool AddBorders(CJsonUnique& json) const; + bool AddParent(CJsonUnique& json) const; + bool AddStyle(CJsonUnique& json) const; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/src/cjson_utils.cpp b/interfaces/inner_api/security_component/src/cjson_utils.cpp index 26ae698..8c12eaa 100644 --- a/interfaces/inner_api/security_component/src/cjson_utils.cpp +++ b/interfaces/inner_api/security_component/src/cjson_utils.cpp @@ -15,6 +15,7 @@ #include "cjson_utils.h" #include +#include #include namespace OHOS { @@ -24,25 +25,28 @@ namespace { #define RECURSE_FLAG_TRUE 1 } -CJson* CreateJsonFromString(const std::string jsonStr) +CJsonUnique CreateJsonFromString(const std::string jsonStr) { if (jsonStr.empty()) { return nullptr; } - return cJSON_Parse(jsonStr.c_str()); + CJsonUnique aPtr(cJSON_Parse(jsonStr.c_str()), FreeJson); + return aPtr; } -CJson* CreateJson(void) +CJsonUnique CreateJson(void) { - return cJSON_CreateObject(); + CJsonUnique aPtr(cJSON_CreateObject(), FreeJson); + return aPtr; } -CJson* CreateJsonArray(void) +CJsonUnique CreateJsonArray(void) { - return cJSON_CreateArray(); + CJsonUnique aPtr(cJSON_CreateArray(), FreeJson); + return aPtr; } -void FreeJson(CJson*& jsonObj) +void FreeJson(CJson* jsonObj) { cJSON_Delete(jsonObj); jsonObj = nullptr; @@ -50,9 +54,6 @@ void FreeJson(CJson*& jsonObj) std::string PackJsonToString(const CJson* jsonObj) { - if (jsonObj == nullptr) { - return std::string(); - } char* ptr = cJSON_PrintUnformatted(jsonObj); if (ptr == nullptr) { return std::string(); @@ -62,6 +63,11 @@ std::string PackJsonToString(const CJson* jsonObj) return ret; } +std::string PackJsonToString(CJsonUnique& jsonObj) +{ + return PackJsonToString(jsonObj.get()); +} + void FreeJsonString(char* jsonStr) { if (jsonStr != nullptr) { @@ -71,7 +77,7 @@ void FreeJsonString(char* jsonStr) CJson* GetObjFromJson(const CJson* jsonObj, const std::string key) { - if (jsonObj == nullptr || key.empty()) { + if (key.empty()) { return nullptr; } @@ -82,9 +88,14 @@ CJson* GetObjFromJson(const CJson* jsonObj, const std::string key) return nullptr; } +CJson* GetObjFromJson(CJsonUnique& jsonObj, const std::string key) +{ + return GetObjFromJson(jsonObj.get(), key); +} + CJson* GetArrayFromJson(const CJson* jsonObj, const std::string key) { - if (jsonObj == nullptr || key.empty()) { + if (key.empty()) { return nullptr; } @@ -95,9 +106,14 @@ CJson* GetArrayFromJson(const CJson* jsonObj, const std::string key) return nullptr; } +CJson* GetArrayFromJson(CJsonUnique& jsonObj, const std::string key) +{ + return GetArrayFromJson(jsonObj.get(), key); +} + std::string GetStringFromJson(const CJson* jsonObj, const std::string key) { - if (jsonObj == nullptr || key.empty()) { + if (key.empty()) { return std::string(); } @@ -109,24 +125,34 @@ std::string GetStringFromJson(const CJson* jsonObj, const std::string key) return std::string(); } -int32_t GetIntFromJson(const CJson* jsonObj, const std::string key, int32_t& value) +std::string GetStringFromJson(CJsonUnique& jsonObj, const std::string key) { - if (jsonObj == nullptr || key.empty()) { - return CJSON_ERR_NULL_PTR; + return GetStringFromJson(jsonObj.get(), key); +} + +bool GetIntFromJson(const CJson* jsonObj, const std::string key, int32_t& value) +{ + if (key.empty()) { + return false; } CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { value = (int)cJSON_GetNumberValue(jsonObjTmp); - return CJSON_SUCCESS; + return true; } - return CJSON_ERR_JSON_GET; + return false; +} + +bool GetIntFromJson(CJsonUnique& jsonObj, const std::string key, int32_t& value) +{ + return GetIntFromJson(jsonObj.get(), key, value); } -int32_t GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value) +bool GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value) { - if (jsonObj == nullptr || key.empty()) { - return CJSON_ERR_NULL_PTR; + if (key.empty()) { + return false; } CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); @@ -138,206 +164,257 @@ int32_t GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint } else { value = static_cast(realValue); } - return CJSON_SUCCESS; + return true; } - return CJSON_ERR_JSON_GET; + return false; +} + +bool GetUnsignedIntFromJson(CJsonUnique& jsonObj, const std::string key, uint32_t& value) +{ + return GetUnsignedIntFromJson(jsonObj.get(), key, value); } -int32_t GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value) +bool GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value) { - if (jsonObj == nullptr || key.empty()) { - return CJSON_ERR_NULL_PTR; + if (key.empty()) { + return false; } CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); if (jsonObjTmp != nullptr && cJSON_IsBool(jsonObjTmp)) { value = cJSON_IsTrue(jsonObjTmp) ? true : false; - return CJSON_SUCCESS; + return true; } - return CJSON_ERR_JSON_GET; + return false; +} + +bool GetBoolFromJson(CJsonUnique& jsonObj, const std::string key, bool& value) +{ + return GetBoolFromJson(jsonObj.get(), key, value); } -int32_t GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value) +bool GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value) { - if (jsonObj == nullptr || key.empty()) { - return CJSON_ERR_NULL_PTR; + if (key.empty()) { + return false; } CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { value = cJSON_GetNumberValue(jsonObjTmp); - return CJSON_SUCCESS; + return true; } - return CJSON_ERR_JSON_GET; + return false; } -int32_t AddObjToJson(CJson* jsonObj, const std::string key, const CJson* childObj) +bool GetDoubleFromJson(CJsonUnique& jsonObj, const std::string key, double& value) { - if (jsonObj == nullptr || key.empty() || childObj == nullptr) { - return CJSON_ERR_NULL_PTR; + return GetDoubleFromJson(jsonObj.get(), key, value); +} + +bool AddObjToJson(CJson* jsonObj, const std::string key, const CJson* childObj) +{ + if (key.empty() || childObj == nullptr) { + return false; } CJson* tmpObj = cJSON_Duplicate(childObj, RECURSE_FLAG_TRUE); if (tmpObj == nullptr) { - return CJSON_ERR_JSON_DUPLICATE; + return false; } CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); if (objInJson == nullptr) { - if (cJSON_AddItemToObject(jsonObj, key.c_str(), tmpObj) == false) { + if (!cJSON_AddItemToObject(jsonObj, key.c_str(), tmpObj)) { cJSON_Delete(tmpObj); - return CJSON_ERR_JSON_ADD; + return false; } } else { - if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmpObj) == false) { + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmpObj)) { cJSON_Delete(tmpObj); - return CJSON_ERR_JSON_REPLACE; + return false; } } - return CJSON_SUCCESS; + return true; +} + +bool AddObjToJson(CJsonUnique& jsonObj, const std::string key, CJsonUnique& childObj) +{ + return AddObjToJson(jsonObj.get(), key, childObj.get()); } -int32_t AddObjToArray(CJson* jsonArr, CJson* item) +bool AddObjToArray(CJson* jsonArr, CJson* item) { - if (jsonArr == nullptr || item == nullptr) { - return CJSON_ERR_NULL_PTR; + if (item == nullptr) { + return false; } - if (cJSON_IsArray(jsonArr) == false) { - return CJSON_ERR_INVALID_PARAM; + if (!cJSON_IsArray(jsonArr)) { + return false; } - bool ret = cJSON_AddItemToArray(jsonArr, item); - if (ret == false) { - return CJSON_ERR_JSON_ADD; + CJson* tmpObj = cJSON_Duplicate(item, RECURSE_FLAG_TRUE); + if (tmpObj == nullptr) { + return false; } - return CJSON_SUCCESS; + bool ret = cJSON_AddItemToArray(jsonArr, tmpObj); + return ret; +} + +bool AddObjToArray(CJsonUnique& jsonArr, CJsonUnique& item) +{ + return AddObjToArray(jsonArr.get(), item.get()); } -int32_t AddStringToJson(CJson* jsonObj, const std::string key, const std::string value) +bool AddStringToJson(CJson* jsonObj, const std::string key, const std::string value) { - if (jsonObj == nullptr || key.empty() || value.empty()) { - return CJSON_ERR_NULL_PTR; + if (key.empty() || value.empty()) { + return false; } CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); if (objInJson == nullptr) { if (cJSON_AddStringToObject(jsonObj, key.c_str(), value.c_str()) == nullptr) { - return CJSON_ERR_JSON_GET; + return false; } } else { CJson* tmp = cJSON_CreateString(value.c_str()); if (tmp == nullptr) { - return CJSON_ERR_BAD_ALLOC; + return false; } - if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { cJSON_Delete(tmp); - return CJSON_ERR_JSON_REPLACE; + return false; } } - return CJSON_SUCCESS; + return true; +} + +bool AddStringToJson(CJsonUnique& jsonObj, const std::string key, const std::string value) +{ + return AddStringToJson(jsonObj.get(), key, value); } -int32_t AddBoolToJson(CJson* jsonObj, const std::string key, const bool value) +bool AddBoolToJson(CJson* jsonObj, const std::string key, const bool value) { - if (jsonObj == nullptr || key.empty()) { - return CJSON_ERR_NULL_PTR; + if (key.empty()) { + return false; } CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); if (objInJson == nullptr) { if (cJSON_AddBoolToObject(jsonObj, key.c_str(), value) == nullptr) { - return CJSON_ERR_JSON_GET; + return false; } } else { CJson* tmp = cJSON_CreateBool(value); if (tmp == nullptr) { - return CJSON_ERR_BAD_ALLOC; + return false; } - if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { cJSON_Delete(tmp); - return CJSON_ERR_JSON_REPLACE; + return false; } } - return CJSON_SUCCESS; + return true; } -int32_t AddIntToJson(CJson* jsonObj, const std::string key, const int value) +bool AddBoolToJson(CJsonUnique& jsonObj, const std::string key, const bool value) { - if (jsonObj == nullptr || key.empty()) { - return CJSON_ERR_NULL_PTR; + return AddBoolToJson(jsonObj.get(), key, value); +} + +bool AddIntToJson(CJson* jsonObj, const std::string key, const int value) +{ + if (key.empty()) { + return false; } CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); if (objInJson == nullptr) { if (cJSON_AddNumberToObject(jsonObj, key.c_str(), value) == nullptr) { - return CJSON_ERR_JSON_GET; + return false; } } else { CJson* tmp = cJSON_CreateNumber(value); if (tmp == nullptr) { - return CJSON_ERR_BAD_ALLOC; + return false; } - if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { cJSON_Delete(tmp); - return CJSON_ERR_JSON_REPLACE; + return false; } } - return CJSON_SUCCESS; + return true; +} + +bool AddIntToJson(CJsonUnique& jsonObj, const std::string key, const int value) +{ + return AddIntToJson(jsonObj.get(), key, value); } -int32_t AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value) +bool AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value) { - if (jsonObj == nullptr || key.empty()) { - return CJSON_ERR_NULL_PTR; + if (key.empty()) { + return false; } CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); double tmpValue = static_cast(value); if (objInJson == nullptr) { if (cJSON_AddNumberToObject(jsonObj, key.c_str(), tmpValue) == nullptr) { - return CJSON_ERR_JSON_GET; + return false; } } else { CJson* tmp = cJSON_CreateNumber(tmpValue); if (tmp == nullptr) { - return CJSON_ERR_BAD_ALLOC; + return false; } - if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { cJSON_Delete(tmp); - return CJSON_ERR_JSON_REPLACE; + return false; } } - return CJSON_SUCCESS; + return true; } -int32_t AddDoubleToJson(CJson* jsonObj, const std::string key, const double value) +bool AddUnsignedIntToJson(CJsonUnique& jsonObj, const std::string key, const uint32_t value) { - if (jsonObj == nullptr || key.empty()) { - return CJSON_ERR_NULL_PTR; + return AddUnsignedIntToJson(jsonObj.get(), key, value); +} + +bool AddDoubleToJson(CJson* jsonObj, const std::string key, const double value) +{ + if (key.empty()) { + return false; } CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); if (objInJson == nullptr) { if (cJSON_AddNumberToObject(jsonObj, key.c_str(), value) == nullptr) { - return CJSON_ERR_JSON_GET; + return false; } } else { CJson* tmp = cJSON_CreateNumber(value); if (tmp == nullptr) { - return CJSON_ERR_BAD_ALLOC; + return false; } - if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { cJSON_Delete(tmp); - return CJSON_ERR_JSON_REPLACE; + return false; } } - return CJSON_SUCCESS; + return true; +} + +bool AddDoubleToJson(CJsonUnique& jsonObj, const std::string key, const double value) +{ + return AddDoubleToJson(jsonObj.get(), key, value); } } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 0b079ef..eae922e 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -52,12 +52,11 @@ void LocationButtonTest::TearDown() */ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -68,10 +67,9 @@ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); LocationButton comp; ASSERT_FALSE(comp.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -82,15 +80,14 @@ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SecCompType::UNKNOWN_SC_TYPE)); + ASSERT_TRUE(AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SecCompType::UNKNOWN_SC_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -101,36 +98,34 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); LocationButton comp; - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); - CJson* wrongJson = CreateJsonFromString("{"); + ASSERT_TRUE(AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + auto wrongJson = CreateJsonFromString("{"); ASSERT_EQ(nullptr, wrongJson); wrongJson = CreateJson(); // add a empty json obj instead - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_RECT, wrongJson)); - FreeJson(wrongJson); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_RECT, wrongJson)); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_RECT); + auto rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_RECT); ASSERT_NE(nullptr, rectJson); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_X, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_X, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_Y, WRONG_TYPE)); + ASSERT_TRUE(AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_Y, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE)); + ASSERT_TRUE(AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE)); + ASSERT_TRUE(AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -141,36 +136,34 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); LocationButton comp; - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); - CJson* wrongJson = CreateJsonFromString("{"); + ASSERT_TRUE(AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + auto wrongJson = CreateJsonFromString("{"); ASSERT_EQ(nullptr, wrongJson); wrongJson = CreateJson(); // add a empty json obj instead - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT, wrongJson)); - FreeJson(wrongJson); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT, wrongJson)); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); + auto rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); ASSERT_NE(nullptr, rectJson); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_X, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_X, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_Y, WRONG_TYPE)); + ASSERT_TRUE(AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_Y, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE)); + ASSERT_TRUE(AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE)); + ASSERT_TRUE(AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -181,24 +174,23 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + auto rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); ASSERT_NE(nullptr, rectJson); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_FONT_SIZE_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_FONT_SIZE_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_ICON_SIZE_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddDoubleToJson(rectJson, JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_ICON_SIZE_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddDoubleToJson(rectJson, JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE)); + ASSERT_TRUE(AddStringToJson(rectJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -209,23 +201,22 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - CJson* sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); ASSERT_NE(nullptr, sizeJson); - CJson* paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); ASSERT_NE(nullptr, paddingJson); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -236,23 +227,22 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - CJson* sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); ASSERT_NE(nullptr, sizeJson); - CJson* paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); ASSERT_NE(nullptr, paddingJson); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -263,24 +253,21 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - CJson* borderTag = CreateJson(); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(borderTag, JsonTagConstants::JSON_BORDER_WIDTH_TAG, WRONG_TYPE)); - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTag)); - FreeJson(borderTag); + auto borderTag = CreateJson(); + ASSERT_TRUE(AddStringToJson(borderTag, JsonTagConstants::JSON_BORDER_WIDTH_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTag)); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); - CJson* parentTag = CreateJson(); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(parentTag, JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE)); - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTag)); + auto parentTag = CreateJson(); + ASSERT_TRUE(AddStringToJson(parentTag, JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTag)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - FreeJson(parentTag); - FreeJson(jsonComponent); } /** @@ -291,26 +278,25 @@ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); ASSERT_NE(nullptr, styleJson); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::SELECT_LOCATION))); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::LINE_ICON))); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_BG_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(styleJson, JsonTagConstants::JSON_BG_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -321,26 +307,25 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - CJson* colorJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); + auto colorJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); ASSERT_NE(nullptr, colorJson); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE)); - ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_BG_COLOR_TAG, WRONG_TYPE)); + ASSERT_TRUE(AddStringToJson(colorJson, JsonTagConstants::JSON_BG_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -351,47 +336,46 @@ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); ASSERT_NE(nullptr, styleJson); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::SELECT_LOCATION))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::LINE_ICON))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::CIRCLE))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::SELECT_LOCATION))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::LINE_ICON))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -402,7 +386,7 @@ HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) */ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; @@ -410,7 +394,6 @@ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) std::string jsonDump = PackJsonToString(jsonComponent); ASSERT_FALSE(jsonDump.empty()); ASSERT_EQ(jsonDump, button.ToJsonStr()); - FreeJson(jsonComponent); } /** @@ -421,12 +404,11 @@ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_FALSE(button.CompareComponentBasicInfo(nullptr, true)); - FreeJson(jsonComponent); } /** @@ -440,7 +422,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) LocationButton button1; LocationButton button2; - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -460,7 +442,6 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); - FreeJson(jsonComponent); } /** @@ -471,7 +452,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); LocationButton comp1; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -526,7 +507,6 @@ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) comp1.iconColor_.value = TestCommon::TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); - FreeJson(jsonComponent); } /** @@ -539,10 +519,9 @@ HWTEST_F(LocationButtonTest, CompareLocationButton003, TestSize.Level1) { LocationButton button1; SaveButton button2; - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, false)); - FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index bc34c99..21d9003 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -51,7 +51,7 @@ void PasteButtonTest::TearDown() */ HWTEST_F(PasteButtonTest, IsParamValid001, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); PasteButton button; @@ -59,38 +59,37 @@ HWTEST_F(PasteButtonTest, IsParamValid001, TestSize.Level1) CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); ASSERT_NE(nullptr, styleJson); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(PasteDesc::PASTE))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::LINE_ICON))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::CIRCLE))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(PasteDesc::PASTE))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, static_cast(PasteIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::LINE_ICON))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -104,7 +103,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) PasteButton button1; PasteButton button2; - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -124,7 +123,6 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); - FreeJson(jsonComponent); } /** @@ -135,7 +133,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) */ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); PasteButton comp1; TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -158,7 +156,6 @@ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) comp1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); - FreeJson(jsonComponent); } /** @@ -171,10 +168,9 @@ HWTEST_F(PasteButtonTest, ComparePasteButton003, TestSize.Level1) { PasteButton button1; SaveButton button2; - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); - FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index bdd5df2..d99e8d1 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -51,7 +51,7 @@ void SaveButtonTest::TearDown() */ HWTEST_F(SaveButtonTest, IsParamValid001, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); SaveButton button; @@ -59,38 +59,37 @@ HWTEST_F(SaveButtonTest, IsParamValid001, TestSize.Level1) CJson* stylesJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); ASSERT_NE(nullptr, stylesJson); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(SaveDesc::DOWNLOAD))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::LINE_ICON))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::CIRCLE))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, static_cast(SaveDesc::DOWNLOAD))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, static_cast(SaveIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::LINE_ICON))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - FreeJson(jsonComponent); } /** @@ -104,7 +103,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) SaveButton button1; SaveButton button2; - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -124,7 +123,6 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); - FreeJson(jsonComponent); } /** @@ -135,7 +133,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) */ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); SaveButton comp1; TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -158,7 +156,6 @@ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) comp1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); - FreeJson(jsonComponent); } /** @@ -171,10 +168,9 @@ HWTEST_F(SaveButtonTest, CompareSaveButton003, TestSize.Level1) { SaveButton button1; PasteButton button2; - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); - FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 53cd8ea..22b09bc 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -110,11 +110,10 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) comp.rect_.width_ = TestCommon::TEST_COORDINATE; comp.rect_.height_ = TestCommon::TEST_COORDINATE; - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); comp.ToJson(jsonRes); int32_t scId = -1; std::string jsonStr = PackJsonToString(jsonRes); - FreeJson(jsonRes); ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonStr, scId)); ASSERT_EQ(-1, scId); @@ -186,10 +185,9 @@ HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) */ HWTEST_F(SecCompKitTest, RegisterWithoutCallback001, TestSize.Level1) { - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp index 881b671..2dcbfa9 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp @@ -128,10 +128,9 @@ void SecCompRegisterCallbackTest::TearDown() */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) { - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -151,10 +150,9 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) { - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = -1; @@ -180,10 +178,9 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level1) { - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -202,10 +199,9 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level1) { system("param set sec.comp.enhance 1"); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; @@ -239,10 +235,9 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1) { system("param set sec.comp.enhance 1"); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; @@ -274,10 +269,9 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1) { - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; setuid(100); @@ -293,10 +287,9 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; @@ -332,10 +325,9 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, Test HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1) { system("param set sec.comp.enhance 1"); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -371,10 +363,9 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, Test */ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) { - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -405,10 +396,9 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, Test HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); std::string locationInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -444,10 +434,9 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -486,10 +475,9 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) { system("param set sec.comp.enhance 1"); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; @@ -523,10 +511,9 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; @@ -558,10 +545,9 @@ HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.L */ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1) { - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); ASSERT_NE(-1, scId); @@ -577,10 +563,9 @@ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level */ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1) { - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); - FreeJson(jsonRes); int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp index 0d0ba8d..fee6273 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp @@ -18,12 +18,12 @@ namespace OHOS { namespace Security { namespace SecurityComponent { -void TestCommon::BuildLocationComponentInfo(CJson* jsonComponent) +void TestCommon::BuildLocationComponentInfo(CJsonUnique& jsonComponent) { AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT); GenerateCommon(jsonComponent); - CJson* styleTagChild = CreateJson(); + auto styleTagChild = CreateJson(); AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::SELECT_LOCATION)); AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, @@ -32,15 +32,14 @@ void TestCommon::BuildLocationComponentInfo(CJson* jsonComponent) static_cast(SecCompBackground::CIRCLE)); AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); - FreeJson(styleTagChild); } -void TestCommon::BuildSaveComponentInfo(CJson* jsonComponent) +void TestCommon::BuildSaveComponentInfo(CJsonUnique& jsonComponent) { AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SAVE_COMPONENT); GenerateCommon(jsonComponent); - CJson* styleTagChild = CreateJson(); + auto styleTagChild = CreateJson(); AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, static_cast(SaveDesc::DOWNLOAD)); AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, @@ -49,15 +48,14 @@ void TestCommon::BuildSaveComponentInfo(CJson* jsonComponent) static_cast(SecCompBackground::CIRCLE)); AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); - FreeJson(styleTagChild); } -void TestCommon::BuildPasteComponentInfo(CJson* jsonComponent) +void TestCommon::BuildPasteComponentInfo(CJsonUnique& jsonComponent) { AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, PASTE_COMPONENT); GenerateCommon(jsonComponent); - CJson* styleTagChild = CreateJson(); + auto styleTagChild = CreateJson(); AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, static_cast(SaveDesc::DOWNLOAD)); AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, @@ -66,57 +64,50 @@ void TestCommon::BuildPasteComponentInfo(CJson* jsonComponent) static_cast(SecCompBackground::CIRCLE)); AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); - FreeJson(styleTagChild); } -void TestCommon::AddRectToJson(CJson* jsonComponent, const std::string key) +void TestCommon::AddRectToJson(CJsonUnique& jsonComponent, const std::string key) { - CJson* rectChild = CreateJson(); + auto rectChild = CreateJson(); AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE); AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE); AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE); AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE); AddObjToJson(jsonComponent, key, rectChild); - FreeJson(rectChild); } -void TestCommon::GenerateCommon(CJson* jsonComponent) +void TestCommon::GenerateCommon(CJsonUnique& jsonComponent) { AddIntToJson(jsonComponent, JsonTagConstants::JSON_NODE_ID, 0); AddRectToJson(jsonComponent, JsonTagConstants::JSON_RECT); AddRectToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); - CJson* jsonPadding = CreateJson(); + auto jsonPadding = CreateJson(); AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION); AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION); AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION); AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION); - CJson* sizeTagChild = CreateJson(); + auto sizeTagChild = CreateJson(); AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE); AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE); AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE); AddObjToJson(sizeTagChild, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding); AddObjToJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG, sizeTagChild); - FreeJson(jsonPadding); - FreeJson(sizeTagChild); - CJson* colorsTagChild = CreateJson(); + auto colorsTagChild = CreateJson(); AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED); AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE); AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW); AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild); - FreeJson(colorsTagChild); - CJson* borderTagChild = CreateJson(); + auto borderTagChild = CreateJson(); AddDoubleToJson(borderTagChild, JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE); AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTagChild); - FreeJson(borderTagChild); - CJson* parentTagChild = CreateJson(); + auto parentTagChild = CreateJson(); AddBoolToJson(parentTagChild, JsonTagConstants::JSON_PARENT_EFFECT_TAG, false); AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTagChild); - FreeJson(parentTagChild); } } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.h b/interfaces/inner_api/security_component/test/unittest/src/test_common.h index f890828..d36adb5 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.h +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.h @@ -26,7 +26,7 @@ namespace Security { namespace SecurityComponent { class TestCommon { public: - static constexpr double TEST_SIZE = 100.0; + static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIMENSION = 100.0; static constexpr uint32_t TEST_COLOR = 0xffffffff; @@ -39,12 +39,12 @@ public: static constexpr uint32_t MAX_HMAC_SIZE = 64; static constexpr size_t MAX_CALLER_SIZE = 10; - static void BuildLocationComponentInfo(CJson* jsonComponent); - static void BuildSaveComponentInfo(CJson* jsonComponent); - static void BuildPasteComponentInfo(CJson* jsonComponent); + static void BuildLocationComponentInfo(CJsonUnique& jsonComponent); + static void BuildSaveComponentInfo(CJsonUnique& jsonComponent); + static void BuildPasteComponentInfo(CJsonUnique& jsonComponent); private: - static void AddRectToJson(CJson* jsonComponent, const std::string key); - static void GenerateCommon(CJson* jsonComponent); + static void AddRectToJson(CJsonUnique& jsonComponent, const std::string key); + static void GenerateCommon(CJsonUnique& jsonComponent); }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.cpp b/services/security_component_service/sa/sa_main/first_use_dialog.cpp index aa098aa..0cad931 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.cpp +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -117,7 +117,7 @@ void FirstUseDialog::WriteCfgContent(const std::string& content) bool FirstUseDialog::ParseRecord(CJson* jsonRes, AccessToken::AccessTokenID& id, uint32_t& type) { - if (GetUnsignedIntFromJson(jsonRes, TOKEN_ID_TAG, id) != CJSON_SUCCESS) { + if (!GetUnsignedIntFromJson(jsonRes, TOKEN_ID_TAG, id)) { SC_LOG_ERROR(LABEL, "parse TokenId failed."); return false; } @@ -126,14 +126,14 @@ bool FirstUseDialog::ParseRecord(CJson* jsonRes, return false; } - if (GetUnsignedIntFromJson(jsonRes, COMP_TYPE_TAG, type) != CJSON_SUCCESS) { + if (!GetUnsignedIntFromJson(jsonRes, COMP_TYPE_TAG, type)) { SC_LOG_ERROR(LABEL, "parse CompType failed."); return false; } return true; } -void FirstUseDialog::ParseRecords(CJson* jsonRes) +void FirstUseDialog::ParseRecords(CJsonUnique& jsonRes) { std::unique_lock lock(useMapMutex_); CJson* recordListJson = GetArrayFromJson(jsonRes, FIRST_USE_RECORD_TAG); @@ -166,14 +166,13 @@ void FirstUseDialog::LoadFirstUseRecord(void) return; } - CJson* jsonRes = CreateJsonFromString(content); + auto jsonRes = CreateJsonFromString(content); if (jsonRes == nullptr) { SC_LOG_ERROR(LABEL, "cfg info format is invalid"); return; } ParseRecords(jsonRes); - FreeJson(jsonRes); } void FirstUseDialog::SaveFirstUseRecord(void) @@ -193,16 +192,14 @@ void FirstUseDialog::SaveFirstUseRecord(void) } } - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); { std::unique_lock lock(useMapMutex_); if (!GenerateRecordsJson(jsonRes)) { - FreeJson(jsonRes); return; } } std::string jsonStr = PackJsonToString(jsonRes); - FreeJson(jsonRes); if (jsonStr.empty()) { SC_LOG_ERROR(LABEL, "json: PackJsonToString return empty."); return; @@ -290,9 +287,9 @@ void FirstUseDialog::Init(std::shared_ptr secHandler) LoadFirstUseRecord(); } -bool FirstUseDialog::GenerateRecordsJson(CJson* jsonRes) +bool FirstUseDialog::GenerateRecordsJson(CJsonUnique& jsonRes) { - CJson* recordsJson = CreateJsonArray(); + auto recordsJson = CreateJsonArray(); for (auto iter = firstUseMap_.begin(); iter != firstUseMap_.end(); ++iter) { AccessToken::AccessTokenID id = iter->first; AccessToken::HapTokenInfo info; @@ -300,32 +297,24 @@ bool FirstUseDialog::GenerateRecordsJson(CJson* jsonRes) SC_LOG_INFO(LABEL, "token id %{public}d is not exist, do not update it.", id); continue; } - CJson* recordJson = CreateJson(); - if (AddUnsignedIntToJson(recordJson, TOKEN_ID_TAG, id) != CJSON_SUCCESS) { + auto recordJson = CreateJson(); + if (!AddUnsignedIntToJson(recordJson, TOKEN_ID_TAG, id)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", TOKEN_ID_TAG.c_str()); - FreeJson(recordJson); - FreeJson(recordsJson); return false; } - if (AddUnsignedIntToJson(recordJson, COMP_TYPE_TAG, iter->second) != CJSON_SUCCESS) { + if (!AddUnsignedIntToJson(recordJson, COMP_TYPE_TAG, iter->second)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", COMP_TYPE_TAG.c_str()); - FreeJson(recordJson); - FreeJson(recordsJson); return false; } - if (AddObjToArray(recordsJson, recordJson) != CJSON_SUCCESS) { + if (!AddObjToArray(recordsJson, recordJson)) { SC_LOG_ERROR(LABEL, "json: add array recordsJson error."); - FreeJson(recordJson); - FreeJson(recordsJson); return false; } } - if (AddObjToJson(jsonRes, FIRST_USE_RECORD_TAG, recordsJson) != CJSON_SUCCESS) { + if (!AddObjToJson(jsonRes, FIRST_USE_RECORD_TAG, recordsJson)) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", FIRST_USE_RECORD_TAG.c_str()); - FreeJson(recordsJson); return false; } - FreeJson(recordsJson); return true; } } // namespace SecurityComponent diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h index c62cd78..e29e0f4 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.h +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -44,12 +44,12 @@ private: void WriteCfgContent(const std::string& content); bool ParseRecord(CJson* jsonRes, AccessToken::AccessTokenID& id, uint32_t& type); - void ParseRecords(CJson* jsonRes); + void ParseRecords(CJsonUnique& jsonRes); void LoadFirstUseRecord(void); void SaveFirstUseRecord(void); void StartDialogAbility(SecCompType type, sptr callerToken); void SendSaveEventHandler(void); - bool GenerateRecordsJson(CJson* jsonRes); + bool GenerateRecordsJson(CJsonUnique& jsonRes); std::mutex useMapMutex_; std::unordered_map firstUseMap_; diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index b9cec84..08c7f6b 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -14,6 +14,7 @@ */ #include "sec_comp_info_helper.h" +#include "cjson_utils.h" #include "display.h" #include "display_info.h" #include "display_manager.h" @@ -47,7 +48,7 @@ void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, float scale) comp->rect_.x_, comp->rect_.y_, comp->rect_.width_, comp->rect_.height_); } -SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const CJson* jsonComponent) +SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, CJsonUnique& jsonComponent) { SecCompBase* comp = nullptr; switch (type) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h index a39b2c0..aed7900 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h @@ -25,7 +25,7 @@ namespace OHOS { namespace Security { namespace SecurityComponent { template -T* ConstructComponent(const CJson* jsonComponent) +T* ConstructComponent(CJsonUnique& jsonComponent) { T *componentPtr = new (std::nothrow)T(); if ((componentPtr != nullptr) && !componentPtr->FromJson(jsonComponent)) { @@ -37,7 +37,7 @@ T* ConstructComponent(const CJson* jsonComponent) class __attribute__((visibility("default"))) SecCompInfoHelper { public: - static SecCompBase* ParseComponent(SecCompType type, const CJson* jsonComponent); + static SecCompBase* ParseComponent(SecCompType type, CJsonUnique& jsonComponent); static int32_t GrantTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo); static bool CheckComponentValid(SecCompBase* comp); diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index 4d79122..27563c5 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp @@ -319,7 +319,7 @@ int32_t SecCompManager::AddSecurityComponentProcess(const SecCompCallerInfo& cal } int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, - const CJson* jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) + CJsonUnique& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) { DelayExitTask::GetInstance().Stop(); SC_LOG_DEBUG(LABEL, "PID: %{public}d, register security component", caller.pid); @@ -359,7 +359,7 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, return ret; } -int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const CJson* jsonComponent, +int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, CJsonUnique& jsonComponent, const SecCompCallerInfo& caller) { SC_LOG_DEBUG(LABEL, "PID: %{public}d, update security component", caller.pid); @@ -409,7 +409,7 @@ int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompC } int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, - const CJson* jsonComponent, const SecCompCallerInfo& caller) + CJsonUnique& jsonComponent, const SecCompCallerInfo& caller) { SC_LOG_DEBUG(LABEL, "PID: %{public}d, Check security component", caller.pid); SecCompBase* report = SecCompInfoHelper::ParseComponent(sc->GetType(), jsonComponent); @@ -444,7 +444,7 @@ int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32 } int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, - const CJson* jsonComponent, const SecCompCallerInfo& caller, + CJsonUnique& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& clickInfo, sptr callerToken) { if (malicious_.IsInMaliciousAppList(caller.pid, caller.uid)) { @@ -495,12 +495,11 @@ void SecCompManager::DumpSecComp(std::string& dumpStr) for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { dumpStr.append("pid:" + std::to_string(iter->first) + "\n"); for (auto compIter = iter->second.compList.begin(); compIter != iter->second.compList.end(); compIter ++) { - CJson* json = CreateJson(); + auto json = CreateJson(); compIter->GetComponentInfo()->ToJson(json); std::string jsonStr = PackJsonToString(json); dumpStr.append(" scId:" + std::to_string(compIter->GetScId()) + ", isGrant:" + std::to_string(compIter->IsGrant()) + ", " + jsonStr + "\n"); - FreeJson(json); } } } diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index 2d88858..64b01ee 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -52,12 +52,12 @@ public: static SecCompManager& GetInstance(); virtual ~SecCompManager() = default; - int32_t RegisterSecurityComponent(SecCompType type, const CJson* jsonComponent, + int32_t RegisterSecurityComponent(SecCompType type, CJsonUnique& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId); - int32_t UpdateSecurityComponent(int32_t scId, const CJson* jsonComponent, + int32_t UpdateSecurityComponent(int32_t scId, CJsonUnique& jsonComponent, const SecCompCallerInfo& caller); int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); - int32_t ReportSecurityComponentClickEvent(int32_t scId, const CJson* jsonComponent, + int32_t ReportSecurityComponentClickEvent(int32_t scId, CJsonUnique& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& clickInfo, sptr callerToken); void NotifyProcessForeground(int32_t pid); void NotifyProcessBackground(int32_t pid); @@ -77,7 +77,7 @@ private: int32_t DeleteSecurityComponentFromList(int32_t pid, int32_t scId); SecCompEntity* GetSecurityComponentFromList(int32_t pid, int32_t scId); int32_t CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, - const CJson* jsonComponent, const SecCompCallerInfo& caller); + CJsonUnique& jsonComponent, const SecCompCallerInfo& caller); void SendCheckInfoEnhanceSysEvent(int32_t scId, SecCompType type, const std::string& scene, int32_t res); int32_t CreateScId(); diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index 941396c..0ba21f8 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -16,6 +16,7 @@ #include #include +#include #include "app_mgr_death_recipient.h" #include "cjson_utils.h" @@ -168,7 +169,7 @@ bool SecCompService::GetCallerInfo(SecCompCallerInfo& caller) } int32_t SecCompService::ParseParams(const std::string& componentInfo, - SecCompCallerInfo& caller, CJson*& jsonRes) + SecCompCallerInfo& caller, CJsonUnique& jsonRes) { if (!GetCallerInfo(caller)) { SC_LOG_ERROR(LABEL, "Check caller failed"); @@ -200,26 +201,22 @@ int32_t SecCompService::RegisterSecurityComponent(SecCompType type, if (jsonRes == nullptr) { SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); - FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } int32_t res = SecCompManager::GetInstance().RegisterSecurityComponent(type, jsonRes, caller, scId); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); - FreeJson(jsonRes); return res; } int32_t SecCompService::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) { SecCompCallerInfo caller; - CJson* jsonRes = nullptr; + CJsonUnique jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { - FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } int32_t ret = SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonRes, caller); - FreeJson(jsonRes); return ret; } @@ -238,16 +235,14 @@ int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, { StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentClick"); SecCompCallerInfo caller; - CJson* jsonRes = nullptr; + CJsonUnique jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { FinishTrace(HITRACE_TAG_ACCESS_CONTROL); - FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } int32_t res = SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, clickInfo, callerToken); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); - FreeJson(jsonRes); return res; } diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.h b/services/security_component_service/sa/sa_main/sec_comp_service.h index 99bd49b..6b18150 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.h +++ b/services/security_component_service/sa/sa_main/sec_comp_service.h @@ -52,7 +52,7 @@ public: int Dump(int fd, const std::vector& args) override; private: - int32_t ParseParams(const std::string& componentInfo, SecCompCallerInfo& caller, CJson*& jsonRes); + int32_t ParseParams(const std::string& componentInfo, SecCompCallerInfo& caller, CJsonUnique& jsonRes); bool Initialize() const; bool RegisterAppStateObserver(); void UnregisterAppStateObserver(); diff --git a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp index 17f7561..f67b7db 100644 --- a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp +++ b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp @@ -95,7 +95,7 @@ void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid) } int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid, - std::shared_ptr& compInfo, const CJson* jsonComponent) + std::shared_ptr& compInfo, CJsonUnique& jsonComponent) { SC_LOG_DEBUG(LABEL, "CheckComponentInfoEnhnace success"); return SC_OK; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp index 099baf9..62a8209 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_entity_test.cpp @@ -171,12 +171,11 @@ HWTEST_F(SecCompEntityTest, CheckClickInfo002, TestSize.Level1) */ HWTEST_F(SecCompEntityTest, CompareComponentBasicInfo001, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* other = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); bool isRectCheck = true; ASSERT_FALSE(entity_->CompareComponentBasicInfo(other, isRectCheck)); - FreeJson(jsonComponent); } /** diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 97dddb7..2d35a2a 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -85,12 +85,11 @@ void SecCompInfoHelperTest::TearDown() */ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_NE(nullptr, comp); ASSERT_TRUE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -101,13 +100,12 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent)); - FreeJson(jsonComponent); } /** @@ -118,16 +116,15 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, UNKNOWN_SC_TYPE)); + ASSERT_TRUE(AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, UNKNOWN_SC_TYPE)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_EQ(nullptr, comp); - FreeJson(jsonComponent); } static SecCompRect GetDefaultRect(void) @@ -236,13 +233,11 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) ASSERT_TRUE(comp->GetValid()); auto childObj = CreateJson(); - ASSERT_EQ(CJSON_SUCCESS, AddBoolToJson(childObj, JsonTagConstants::JSON_PARENT_EFFECT_TAG, true)); - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, childObj)); - FreeJson(childObj); + ASSERT_TRUE(AddBoolToJson(childObj, JsonTagConstants::JSON_PARENT_EFFECT_TAG, true)); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, childObj)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -253,7 +248,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -261,25 +256,24 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); ASSERT_NE(nullptr, sizeJson); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, + ASSERT_TRUE(AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, + ASSERT_TRUE(AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_DIMENSION)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, + ASSERT_TRUE(AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, + ASSERT_TRUE(AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_DIMENSION)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, + ASSERT_TRUE(AddDoubleToJson(sizeJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -300,18 +294,17 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) ASSERT_NE(nullptr, sizeJson); ASSERT_NE(nullptr, paddingJson); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -322,25 +315,24 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); auto paddingsJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -351,25 +343,24 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); auto colorJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); ASSERT_NE(nullptr, colorJson); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_INVALID)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED)); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_INVALID)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -380,22 +371,20 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - CJson* styleTagChild = CreateJson(); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, NO_TEXT)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, NO_ICON)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + auto styleTagChild = CreateJson(); + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, NO_TEXT)); + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, NO_ICON)); + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::CIRCLE))); - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); - FreeJson(styleTagChild); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -406,32 +395,31 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); auto colorsJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); ASSERT_NE(nullptr, colorsJson); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED)); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); ASSERT_NE(nullptr, styleJson); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::NO_BG_TYPE))); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -442,40 +430,39 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); ASSERT_NE(nullptr, styleJson); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::NO_BG_TYPE))); auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); ASSERT_NE(nullptr, sizeJson); auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); ASSERT_NE(nullptr, paddingJson); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, static_cast(ServiceTestCommon::TEST_DIMENSION))); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -486,40 +473,39 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); ASSERT_NE(nullptr, styleJson); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::NO_BG_TYPE))); auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); ASSERT_NE(nullptr, sizeJson); auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); ASSERT_NE(nullptr, paddingJson); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, MIN_PADDING_WITHOUT_BG)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, static_cast(ServiceTestCommon::TEST_DIMENSION))); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } @@ -531,33 +517,30 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - CJson* styleTagChild = CreateJson(); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + auto styleTagChild = CreateJson(); + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::SELECT_LOCATION))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, static_cast(LocationIcon::LINE_ICON))); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, static_cast(SecCompBackground::CIRCLE))); - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); - FreeJson(styleTagChild); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); - CJson* colorsTagChild = CreateJson(); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, + auto colorsTagChild = CreateJson(); + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW)); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK)); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW)); - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); - FreeJson(colorsTagChild); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - FreeJson(jsonComponent); } /** @@ -568,25 +551,23 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); - CJson* colorsTagChild = CreateJson(); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, + auto colorsTagChild = CreateJson(); + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED)); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK)); - ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_WHITE)); - ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); - FreeJson(colorsTagChild); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); - FreeJson(jsonComponent); } /** @@ -597,7 +578,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->text_ = UNKNOWN_TEXT; @@ -609,7 +590,6 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) comp->text_ = UNKNOWN_TEXT; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); - FreeJson(jsonComponent); } /** @@ -620,7 +600,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->type_ = SecCompType::UNKNOWN_SC_TYPE; @@ -628,7 +608,6 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) comp->type_ = SecCompType::MAX_SC_TYPE; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); - FreeJson(jsonComponent); } /** @@ -639,7 +618,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -658,7 +637,6 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) list.emplace_back(otherWin); Rosen::WindowManager::GetInstance().list_ = list; ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); - FreeJson(jsonComponent); } /** @@ -669,11 +647,10 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid005, TestSize.Level1) { - CJson* jsonComponent = CreateJson(); + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); Rosen::WindowManager::GetInstance().result_ = static_cast(-1); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); - FreeJson(jsonComponent); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 85cbf4f..d76cff7 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -311,7 +311,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) }; int32_t scId; SecCompManager::GetInstance().malicious_.maliciousAppList_.insert(ServiceTestCommon::TEST_PID_1); - CJson* jsonInvalid = CreateJson(); + auto jsonInvalid = CreateJson(); EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId)); SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); @@ -321,15 +321,13 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId)); - CJson* jsonValid = CreateJson(); + auto jsonValid = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); EXPECT_EQ(SC_OK, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId)); SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); - FreeJson(jsonValid); - FreeJson(jsonInvalid); } /** @@ -341,7 +339,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) { SecCompManager::GetInstance().malicious_.maliciousAppList_.insert(ServiceTestCommon::TEST_PID_1); - CJson* jsonValid = CreateJson(); + auto jsonValid = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); SecCompCallerInfo caller = { @@ -360,7 +358,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity)); // wrong json - CJson* jsonInvalid = CreateJson(); + auto jsonInvalid = CreateJson(); LocationButton buttonInvalid = BuildInvalidLocationComponent(); buttonInvalid.ToJson(jsonInvalid); EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, @@ -370,8 +368,6 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) EXPECT_EQ(SC_OK, SecCompManager::GetInstance().UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller)); SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); - FreeJson(jsonValid); - FreeJson(jsonInvalid); } /** @@ -404,12 +400,11 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve .pid = ServiceTestCommon::TEST_PID_1 }; SecCompClickEvent clickInfo; - CJson* jsonVaild = CreateJson(); + auto jsonVaild = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); ASSERT_NE(SC_OK, SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, clickInfo, nullptr)); - FreeJson(jsonVaild); } /** @@ -452,27 +447,26 @@ HWTEST_F(SecCompManagerTest, CheckClickSecurityComponentInfo001, TestSize.Level1 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity)); SecCompClickEvent clickInfo; - CJson* jsonVaild = CreateJson(); + auto jsonVaild = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, UNKNOWN_SC_TYPE)); + ASSERT_TRUE(AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, UNKNOWN_SC_TYPE)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); - ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + ASSERT_TRUE(AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); auto rectObj = GetObjFromJson(jsonVaild, JsonTagConstants::JSON_RECT); ASSERT_NE(nullptr, rectObj); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectObj, JsonTagConstants::JSON_RECT_X, + ASSERT_TRUE(AddDoubleToJson(rectObj, JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_INVALID_DIMENSION)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); - ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectObj, JsonTagConstants::JSON_RECT_X, + ASSERT_TRUE(AddDoubleToJson(rectObj, JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE - 1)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); - FreeJson(jsonVaild); } /** @@ -522,7 +516,7 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList002, TestSize.Level1 */ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent002, TestSize.Level1) { - CJson* jsonValid = CreateJson(); + auto jsonValid = CreateJson(); SecCompCallerInfo caller = { .tokenId = ServiceTestCommon::TEST_TOKEN_ID, .pid = ServiceTestCommon::TEST_PID_1 @@ -535,7 +529,6 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent002, TestSize.Level1) buttonValid.ToJson(jsonValid); ASSERT_NE(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, SecCompManager::GetInstance().UpdateSecurityComponent( ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller)); - FreeJson(jsonValid); } /** diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp index c67c16d..e507e3d 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -84,7 +84,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "", scId)); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); @@ -112,7 +112,6 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) EXPECT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); - FreeJson(jsonRes); } /** @@ -126,7 +125,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); @@ -148,7 +147,6 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); - FreeJson(jsonRes); } /** @@ -162,7 +160,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); @@ -185,7 +183,6 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); - FreeJson(jsonRes); } /** @@ -199,7 +196,7 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); std::string saveInfo = PackJsonToString(jsonRes); @@ -239,7 +236,6 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. ASSERT_TRUE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); sleep(2); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); - FreeJson(jsonRes); } /** @@ -254,7 +250,7 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonRes); std::string locationInfo = PackJsonToString(jsonRes); @@ -293,5 +289,4 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0); ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.APPROXIMATELY_LOCATION"), 0); - FreeJson(jsonRes); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index faa53f4..20b8d85 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp @@ -14,6 +14,7 @@ */ #include "sec_comp_service_test.h" +#include "cjson_utils.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "location_button.h" @@ -248,7 +249,7 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId)); - CJson* jsonRes = CreateJson(); + auto jsonRes = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonRes); std::string locationInfo = PackJsonToString(jsonRes); @@ -277,7 +278,6 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve secCompService_->ReportSecurityComponentClickEvent(scId, locationInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); setuid(uid); - FreeJson(jsonRes); } /** @@ -358,7 +358,7 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level1) setuid(0); const std::string componentInfo; - CJson* jsonRes = nullptr; + CJsonUnique jsonRes(nullptr, nullptr); int32_t scId = 0; EXPECT_EQ(secCompService_->ParseParams(componentInfo, caller, jsonRes), SC_SERVICE_ERROR_VALUE_INVALID); EXPECT_NE(secCompService_->UnregisterSecurityComponent(scId), SC_SERVICE_ERROR_VALUE_INVALID); @@ -367,5 +367,4 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level1) EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo, nullptr), SC_SERVICE_ERROR_VALUE_INVALID); secCompService_->GetEnhanceRemoteObject(); - FreeJson(jsonRes); } diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.cpp b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp index b315ead..9df4a92 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.cpp +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp @@ -18,12 +18,12 @@ namespace OHOS { namespace Security { namespace SecurityComponent { -void ServiceTestCommon::BuildLocationComponentJson(CJson* jsonComponent) +void ServiceTestCommon::BuildLocationComponentJson(CJsonUnique& jsonComponent) { AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT); GenerateCommon(jsonComponent); - CJson* styleTagChild = CreateJson(); + auto styleTagChild = CreateJson(); AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, static_cast(LocationDesc::SELECT_LOCATION)); AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, @@ -32,15 +32,14 @@ void ServiceTestCommon::BuildLocationComponentJson(CJson* jsonComponent) static_cast(SecCompBackground::CIRCLE)); AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); - FreeJson(styleTagChild); } -void ServiceTestCommon::BuildSaveComponentJson(CJson* jsonComponent) +void ServiceTestCommon::BuildSaveComponentJson(CJsonUnique& jsonComponent) { AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SAVE_COMPONENT); GenerateCommon(jsonComponent); - CJson* styleTagChild = CreateJson(); + auto styleTagChild = CreateJson(); AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, static_cast(SaveDesc::DOWNLOAD)); AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, @@ -49,15 +48,14 @@ void ServiceTestCommon::BuildSaveComponentJson(CJson* jsonComponent) static_cast(SecCompBackground::CIRCLE)); AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); - FreeJson(styleTagChild); } -void ServiceTestCommon::BuildPasteComponentJson(CJson* jsonComponent) +void ServiceTestCommon::BuildPasteComponentJson(CJsonUnique& jsonComponent) { AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, PASTE_COMPONENT); GenerateCommon(jsonComponent); - CJson* styleTagChild = CreateJson(); + auto styleTagChild = CreateJson(); AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, static_cast(SaveDesc::DOWNLOAD)); AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, @@ -66,57 +64,50 @@ void ServiceTestCommon::BuildPasteComponentJson(CJson* jsonComponent) static_cast(SecCompBackground::CIRCLE)); AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); - FreeJson(styleTagChild); } -void ServiceTestCommon::AddRectToJson(CJson* jsonComponent, const std::string key) +void ServiceTestCommon::AddRectToJson(CJsonUnique& jsonComponent, const std::string key) { - CJson* rectChild = CreateJson(); + auto rectChild = CreateJson(); AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE); AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE); AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE); AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE); AddObjToJson(jsonComponent, key, rectChild); - FreeJson(rectChild); } -void ServiceTestCommon::GenerateCommon(CJson* jsonComponent) +void ServiceTestCommon::GenerateCommon(CJsonUnique& jsonComponent) { AddIntToJson(jsonComponent, JsonTagConstants::JSON_NODE_ID, 0); AddRectToJson(jsonComponent, JsonTagConstants::JSON_RECT); AddRectToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); - CJson* jsonPadding = CreateJson(); + auto jsonPadding = CreateJson(); AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION); AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION); AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION); AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION); - CJson* sizeTagChild = CreateJson(); + auto sizeTagChild = CreateJson(); AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE); AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE); AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE); AddObjToJson(sizeTagChild, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding); AddObjToJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG, sizeTagChild); - FreeJson(jsonPadding); - FreeJson(sizeTagChild); - CJson* colorsTagChild = CreateJson(); + auto colorsTagChild = CreateJson(); AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED); AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE); AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW); AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild); - FreeJson(colorsTagChild); - CJson* borderTagChild = CreateJson(); + auto borderTagChild = CreateJson(); AddDoubleToJson(borderTagChild, JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE); AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTagChild); - FreeJson(borderTagChild); - CJson* parentTagChild = CreateJson(); + auto parentTagChild = CreateJson(); AddBoolToJson(parentTagChild, JsonTagConstants::JSON_PARENT_EFFECT_TAG, false); AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTagChild); - FreeJson(parentTagChild); } } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.h b/services/security_component_service/sa/test/unittest/src/service_test_common.h index d2635b2..2a7a5f7 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.h +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.h @@ -14,6 +14,7 @@ */ #ifndef SERVICE_TEST_COMMON_H #define SERVICE_TEST_COMMON_H +#include "cjson_utils.h" #include "location_button.h" #include "paste_button.h" #include "save_button.h" @@ -34,7 +35,7 @@ public: static constexpr int32_t SC_ID_START = 1000; static constexpr int32_t SA_ID = 3506; - static constexpr double TEST_SIZE = 100.0; + static constexpr float TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIFF_COORDINATE = 200.0; static constexpr double TEST_DIMENSION = 100.0; @@ -55,11 +56,11 @@ public: static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr uint32_t TEST_TOKEN_ID = 1; - static void BuildLocationComponentJson(CJson* jsonComponent); - static void BuildSaveComponentJson(CJson* jsonComponent); - static void BuildPasteComponentJson(CJson* jsonComponent); - static void AddRectToJson(CJson* jsonComponent, const std::string key); - static void GenerateCommon(CJson* jsonComponent); + static void BuildLocationComponentJson(CJsonUnique& jsonComponent); + static void BuildSaveComponentJson(CJsonUnique& jsonComponent); + static void BuildPasteComponentJson(CJsonUnique& jsonComponent); + static void AddRectToJson(CJsonUnique& jsonComponent, const std::string key); + static void GenerateCommon(CJsonUnique& jsonComponent); }; } // namespace SecurityComponent } // namespace Security -- Gitee From 95f0605375f5044ad0a551d63de9a78326dedfb7 Mon Sep 17 00:00:00 2001 From: yujann Date: Mon, 26 Feb 2024 20:43:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=BC=96=E8=AF=91=E8=83=BD=E5=A4=9F?= =?UTF-8?q?=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I65450b535e6d0d4cfd9f010f0b6f3c2c9f8daed4 --- frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp | 1 - frameworks/enhance_adapter/test/BUILD.gn | 6 +++++- .../test/unittest/src/sec_comp_enhance_adapter_test.cpp | 3 ++- interfaces/inner_api/enhance_kits/BUILD.gn | 1 + interfaces/inner_api/enhance_kits/test/BUILD.gn | 6 ++++-- interfaces/inner_api/security_component/BUILD.gn | 2 ++ interfaces/inner_api/security_component/test/BUILD.gn | 1 + services/security_component_service/sa/test/BUILD.gn | 2 -- 8 files changed, 15 insertions(+), 7 deletions(-) diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp index 40052c4..f960ff4 100644 --- a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -15,7 +15,6 @@ #include "sec_comp_enhance_adapter.h" #include -#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" diff --git a/frameworks/enhance_adapter/test/BUILD.gn b/frameworks/enhance_adapter/test/BUILD.gn index 375300c..8ff36ad 100644 --- a/frameworks/enhance_adapter/test/BUILD.gn +++ b/frameworks/enhance_adapter/test/BUILD.gn @@ -27,7 +27,10 @@ ohos_unittest("sec_comp_enhance_adapter_test") { branch_protector_ret = "pac_ret" include_dirs = [ "unittest/src" ] - sources = [ "unittest/src/sec_comp_enhance_adapter_test.cpp" ] + sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", + "unittest/src/sec_comp_enhance_adapter_test.cpp", + ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -36,6 +39,7 @@ ohos_unittest("sec_comp_enhance_adapter_test") { external_deps = [ "c_utils:utils", + "cJSON:cjson", "hilog:libhilog", "ipc:ipc_single", ] diff --git a/frameworks/enhance_adapter/test/unittest/src/sec_comp_enhance_adapter_test.cpp b/frameworks/enhance_adapter/test/unittest/src/sec_comp_enhance_adapter_test.cpp index 73b957e..37b8e4d 100644 --- a/frameworks/enhance_adapter/test/unittest/src/sec_comp_enhance_adapter_test.cpp +++ b/frameworks/enhance_adapter/test/unittest/src/sec_comp_enhance_adapter_test.cpp @@ -15,6 +15,7 @@ #include "sec_comp_enhance_adapter_test.h" #include +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" #include "sec_comp_info.h" @@ -98,6 +99,6 @@ HWTEST_F(SecCompEnhanceAdapterTest, EnhanceAdapter001, TestSize.Level1) SecCompEnhanceAdapter::AddSecurityComponentProcess(0); SecCompEnhanceAdapter::isEnhanceSrvHandlerInit = false; std::shared_ptr compInfo; - const nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ASSERT_EQ(SC_OK, SecCompEnhanceAdapter::CheckComponentInfoEnhnace(0, compInfo, jsonComponent)); } diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn index 4c4d7e4..cef6ffd 100644 --- a/interfaces/inner_api/enhance_kits/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/BUILD.gn @@ -49,6 +49,7 @@ ohos_source_set("security_component_enhance_sdk_src_set") { external_deps = [ "c_utils:utils", + "cJSON:cjson", "hilog:libhilog", "ipc:ipc_single", ] diff --git a/interfaces/inner_api/enhance_kits/test/BUILD.gn b/interfaces/inner_api/enhance_kits/test/BUILD.gn index 4a34d4b..4a2daff 100644 --- a/interfaces/inner_api/enhance_kits/test/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -38,10 +38,12 @@ ohos_unittest("sec_comp_enhance_sdk_test") { configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] - deps = [ "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk" ] + deps = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", + "${sec_comp_root_dir}/frameworks:security_component_enhance_adapter_src_set", + ] external_deps = [ - "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_single", diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index 792a4c0..c0e0157 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -67,6 +67,8 @@ ohos_shared_library("libsecurity_component_sdk") { "samgr:samgr_proxy", ] + public_external_deps = [ "cJSON:cjson" ] + cflags_cc = [ "-DHILOG_ENABLE", "-fvisibility=hidden", diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index a7ec0dd..263c283 100644 --- a/interfaces/inner_api/security_component/test/BUILD.gn +++ b/interfaces/inner_api/security_component/test/BUILD.gn @@ -78,6 +78,7 @@ ohos_unittest("sec_comp_register_callback_test") { ] sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", "unittest/src/sec_comp_register_callback_test.cpp", "unittest/src/test_common.cpp", ] diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index e83fb68..72a7472 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -30,7 +30,6 @@ ohos_unittest("sec_comp_service_test") { ] sources = [ - "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", @@ -109,7 +108,6 @@ ohos_unittest("sec_comp_service_mock_test") { ] sources = [ - "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", -- Gitee