diff --git a/bundle.json b/bundle.json index c3244e1b71c7ab7ae6883ee501a365e92b919faa..452d97c8b7a5b84f7f868a11ee7f51d20d5d93b5 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 94f7ec0c4342518497debb11d4021ef12c60e4f7..9dbd3501115f174befe397b4c282c5c0ad9f2737 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 2e9637235a461b484c91b1992f9e5e426715b7d9..bfb347c39b1500d0ec5032cf4840e61693ea6387 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; + 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 nlohmann::json& 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 2e4c3219bde7ecc3c42e56b8ad6151b15c78ee5a..f960ff42e9d8df1212912e4e6cf54b657daf7e6b 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, CJsonUnique& jsonComponent) { if (!isEnhanceSrvHandlerInit) { InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); diff --git a/frameworks/enhance_adapter/test/BUILD.gn b/frameworks/enhance_adapter/test/BUILD.gn index 375300c5fdd7fced38c55cb113a2c6acefd8e05d..8ff36adf78f4945b743608fcd4b04a3041dc7426 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 73b957ebfecc55c883fb2c99d851b6aede5bf2c0..37b8e4d79eadca06f21e8d07799ac6beab7129b1 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/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index 2b6754d1fef108e3e61ba2f2fa78893c54eaf919..faccb762f94c083f2a56ceb733df2fc339db4e35 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)) { 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)) { 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)) { 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(CJsonUnique& 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(CJsonUnique& 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(CJsonUnique& 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(CJsonUnique& 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(CJsonUnique& 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(CJsonUnique& 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)) { 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_)) { 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_)) { 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(CJsonUnique& 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_)) { + 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_)) { + 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_)) { + 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; + auto 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(CJsonUnique& 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_)) { + 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_)) { + 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)) { + 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,182 @@ bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag) return true; } + +bool SecCompBase::AddRect(CJsonUnique& json, const std::string jsonTag, const SecCompRect rectObj) const +{ + 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_); + return false; + } + 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_); + return false; + } + 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_); + return false; + } + 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_); + return false; + } + if (!AddObjToJson(json, jsonTag, childJson)) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_RECT.c_str()); + return false; + } + return true; +} + +bool SecCompBase::AddPadding(CJsonUnique& json) const +{ + 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)) { + 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)) { + 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)) { + 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(CJsonUnique& json) const +{ + auto jsonPadding = CreateJson(); + if (!AddPadding(jsonPadding)) { + return false; + } + + 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_); + return false; + } + 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_); + return false; + } + 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_); + return false; + } + 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()); + return false; + } + if (!AddObjToJson(json, JsonTagConstants::JSON_SIZE_TAG, childJson)) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_SIZE_TAG.c_str()); + return false; + } + return true; +} + +bool SecCompBase::AddColors(CJsonUnique& json) const +{ + 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); + return false; + } + 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); + return false; + } + 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); + return false; + } + if (!AddObjToJson(json, JsonTagConstants::JSON_COLORS_TAG, childJson)) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_COLORS_TAG.c_str()); + return false; + } + return true; +} + +bool SecCompBase::AddBorders(CJsonUnique& json) const +{ + 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_); + return false; + } + if (!AddObjToJson(json, JsonTagConstants::JSON_BORDER_TAG, childJson)) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_BORDER_TAG.c_str()); + return false; + } + return true; +} + +bool SecCompBase::AddParent(CJsonUnique& json) const +{ + 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_); + return false; + } + if (!AddObjToJson(json, JsonTagConstants::JSON_PARENT_TAG, childJson)) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_PARENT_TAG.c_str()); + return false; + } + return true; +} + +bool SecCompBase::AddStyle(CJsonUnique& json) const +{ + 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_); + return false; + } + 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_); + return false; + } + 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_); + return false; + } + if (!AddObjToJson(json, JsonTagConstants::JSON_STYLE_TAG, childJson)) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_STYLE_TAG.c_str()); + return false; + } + return true; +} } // namespace base } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/enhance_kits/BUILD.gn b/interfaces/inner_api/enhance_kits/BUILD.gn index 4c4d7e467ade81f1247ba2eb417593e29db698dc..cef6ffd1805a7091ca09dec01dfb9a60cd2ef2ac 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 a82de68f1d37e86e4aa5bff1762bd57deb3667ba..4a2daff644e82ee3217896440f410082f73c060e 100644 --- a/interfaces/inner_api/enhance_kits/test/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -25,14 +25,23 @@ 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" ] - 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 = [ "c_utils:utils", 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 d17f3e02cef4fb28794f110572f49e6d3fa7e6b7..fd051e5729fcef21df004a6f6f4dc59651522564 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" @@ -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/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index 73f98c540eec78bcd454f1ae9aa2827d162db390..c0e01570c048a5224781a4f888736e4cf040971d 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", ] } @@ -68,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/include/cjson_utils.h b/interfaces/inner_api/security_component/include/cjson_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..d4aaf480dbde0c60be298f367276a37c7096128a --- /dev/null +++ b/interfaces/inner_api/security_component/include/cjson_utils.h @@ -0,0 +1,87 @@ +/* + * 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 +#include "cJSON.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +typedef cJSON CJson; +typedef std::unique_ptr> CJsonUnique; + +/* 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); + +/* + * 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* 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); + +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); + +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 +#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 9788b0ca81b4215306ee74772c1abb2bbd5cfb52..e22b5e42d07d1ab9476241e773d9225b00a8cc98 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(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 0e43c8f8fd20dfd4f8068102be74e30a4fbcd519..7f0a490fe45149bb58d4762fbdd794d943a6f190 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(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 e1f45e6901f93f77fe93251701c19d3f810799f4..dfcb61abb25c7825ad0d40927bcdb681655e5906 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(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 30d15fe8fe50cbcfa701a1b0a62960ba68dd3fd4..8affc319731ca2a714632f03d3e2bb9e98495f1d 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(CJsonUnique& jsonSrc); + void ToJson(CJsonUnique& 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(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 new file mode 100644 index 0000000000000000000000000000000000000000..8c12eaa21742cb65a59d8c6d2f4c292e11fa9107 --- /dev/null +++ b/interfaces/inner_api/security_component/src/cjson_utils.cpp @@ -0,0 +1,421 @@ +/* + * 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 +#include + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +#define RECURSE_FLAG_TRUE 1 +} + +CJsonUnique CreateJsonFromString(const std::string jsonStr) +{ + if (jsonStr.empty()) { + return nullptr; + } + CJsonUnique aPtr(cJSON_Parse(jsonStr.c_str()), FreeJson); + return aPtr; +} + +CJsonUnique CreateJson(void) +{ + CJsonUnique aPtr(cJSON_CreateObject(), FreeJson); + return aPtr; +} + +CJsonUnique CreateJsonArray(void) +{ + CJsonUnique aPtr(cJSON_CreateArray(), FreeJson); + return aPtr; +} + +void FreeJson(CJson* jsonObj) +{ + cJSON_Delete(jsonObj); + jsonObj = nullptr; +} + +std::string PackJsonToString(const CJson* jsonObj) +{ + char* ptr = cJSON_PrintUnformatted(jsonObj); + if (ptr == nullptr) { + return std::string(); + } + std::string ret = std::string(ptr); + FreeJsonString(ptr); + return ret; +} + +std::string PackJsonToString(CJsonUnique& jsonObj) +{ + return PackJsonToString(jsonObj.get()); +} + +void FreeJsonString(char* jsonStr) +{ + if (jsonStr != nullptr) { + cJSON_free(jsonStr); + } +} + +CJson* GetObjFromJson(const CJson* jsonObj, const std::string key) +{ + if (key.empty()) { + return nullptr; + } + + CJson* objValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objValue != nullptr && cJSON_IsObject(objValue)) { + return objValue; + } + 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 (key.empty()) { + return nullptr; + } + + CJson* objValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objValue != nullptr && cJSON_IsArray(objValue)) { + return objValue; + } + 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 (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(); +} + +std::string GetStringFromJson(CJsonUnique& jsonObj, const std::string key) +{ + 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 true; + } + return false; +} + +bool GetIntFromJson(CJsonUnique& jsonObj, const std::string key, int32_t& value) +{ + return GetIntFromJson(jsonObj.get(), key, value); +} + +bool GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value) +{ + if (key.empty()) { + return false; + } + + 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 true; + } + return false; +} + +bool GetUnsignedIntFromJson(CJsonUnique& jsonObj, const std::string key, uint32_t& value) +{ + return GetUnsignedIntFromJson(jsonObj.get(), key, value); +} + +bool GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value) +{ + 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 true; + } + return false; +} + +bool GetBoolFromJson(CJsonUnique& jsonObj, const std::string key, bool& value) +{ + return GetBoolFromJson(jsonObj.get(), key, value); +} + +bool GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value) +{ + if (key.empty()) { + return false; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { + value = cJSON_GetNumberValue(jsonObjTmp); + return true; + } + return false; +} + +bool GetDoubleFromJson(CJsonUnique& jsonObj, const std::string key, double& value) +{ + 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 false; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (!cJSON_AddItemToObject(jsonObj, key.c_str(), tmpObj)) { + cJSON_Delete(tmpObj); + return false; + } + } else { + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmpObj)) { + cJSON_Delete(tmpObj); + return false; + } + } + return true; +} + +bool AddObjToJson(CJsonUnique& jsonObj, const std::string key, CJsonUnique& childObj) +{ + return AddObjToJson(jsonObj.get(), key, childObj.get()); +} + +bool AddObjToArray(CJson* jsonArr, CJson* item) +{ + if (item == nullptr) { + return false; + } + + if (!cJSON_IsArray(jsonArr)) { + return false; + } + + CJson* tmpObj = cJSON_Duplicate(item, RECURSE_FLAG_TRUE); + if (tmpObj == nullptr) { + return false; + } + + bool ret = cJSON_AddItemToArray(jsonArr, tmpObj); + return ret; +} + +bool AddObjToArray(CJsonUnique& jsonArr, CJsonUnique& item) +{ + return AddObjToArray(jsonArr.get(), item.get()); +} + +bool AddStringToJson(CJson* jsonObj, const std::string key, const std::string value) +{ + 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 false; + } + } else { + CJson* tmp = cJSON_CreateString(value.c_str()); + if (tmp == nullptr) { + return false; + } + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { + cJSON_Delete(tmp); + return false; + } + } + + return true; +} + +bool AddStringToJson(CJsonUnique& jsonObj, const std::string key, const std::string value) +{ + return AddStringToJson(jsonObj.get(), key, value); +} + +bool AddBoolToJson(CJson* jsonObj, const std::string key, const bool value) +{ + 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 false; + } + } else { + CJson* tmp = cJSON_CreateBool(value); + if (tmp == nullptr) { + return false; + } + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { + cJSON_Delete(tmp); + return false; + } + } + + return true; +} + +bool AddBoolToJson(CJsonUnique& jsonObj, const std::string key, const bool value) +{ + 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 false; + } + } else { + CJson* tmp = cJSON_CreateNumber(value); + if (tmp == nullptr) { + return false; + } + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { + cJSON_Delete(tmp); + return false; + } + } + + return true; +} + +bool AddIntToJson(CJsonUnique& jsonObj, const std::string key, const int value) +{ + return AddIntToJson(jsonObj.get(), key, value); +} + +bool AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value) +{ + 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 false; + } + } else { + CJson* tmp = cJSON_CreateNumber(tmpValue); + if (tmp == nullptr) { + return false; + } + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { + cJSON_Delete(tmp); + return false; + } + } + return true; +} + +bool AddUnsignedIntToJson(CJsonUnique& jsonObj, const std::string key, const uint32_t value) +{ + 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 false; + } + } else { + CJson* tmp = cJSON_CreateNumber(value); + if (tmp == nullptr) { + return false; + } + if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp)) { + cJSON_Delete(tmp); + return false; + } + } + + return true; +} + +bool AddDoubleToJson(CJsonUnique& jsonObj, const std::string key, const double value) +{ + return AddDoubleToJson(jsonObj.get(), key, value); +} +} // 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 45479996fa73ae3d2f71ac247d9761a08fe13a58..263c283e97e89f418b3f120f9c35c7233d1c349f 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", @@ -76,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/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 335bb9845ef5f030b4e13cab179f744f335fe963..eae922ee21a97cebf4476aa95572333b38bc7fef 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,7 +52,7 @@ void LocationButtonTest::TearDown() */ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; @@ -66,7 +67,7 @@ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); LocationButton comp; ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -79,15 +80,13 @@ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); - - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = WRONG_TYPE; + ASSERT_TRUE(AddStringToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = 0; + ASSERT_TRUE(AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SecCompType::UNKNOWN_SC_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -99,30 +98,33 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_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_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_RECT, 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; + auto rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_RECT); + ASSERT_NE(nullptr, rectJson); + ASSERT_TRUE(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_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)); - rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE; - 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)); - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE; - 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)); } @@ -134,30 +136,33 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_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_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT, 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; + auto rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); + ASSERT_NE(nullptr, rectJson); + ASSERT_TRUE(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_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)); - rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE; - 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)); - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE; - 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)); } @@ -169,21 +174,22 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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; + auto rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, rectJson); + ASSERT_TRUE(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_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)); - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = TestCommon::TEST_SIZE; - sizeJson[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)); } @@ -195,18 +201,21 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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; + 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_TRUE(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_TRUE(AddDoubleToJson(paddingJson, + JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION)); + ASSERT_TRUE(AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -218,18 +227,21 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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; + 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_TRUE(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_TRUE(AddDoubleToJson(paddingJson, + JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION)); + ASSERT_TRUE(AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); } @@ -241,20 +253,20 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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 }, - }; + 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); - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, - }; + 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)); } @@ -266,21 +278,24 @@ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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; + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_TRUE(AddStringToJson(styleJson, JsonTagConstants::JSON_BG_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -292,21 +307,24 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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; + auto colorJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); + ASSERT_NE(nullptr, colorJson); + ASSERT_TRUE(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_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + TestCommon::TEST_COLOR_RED)); + ASSERT_TRUE(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_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + TestCommon::TEST_COLOR_BLUE)); + ASSERT_TRUE(AddStringToJson(colorJson, JsonTagConstants::JSON_BG_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -318,34 +336,45 @@ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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; + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -357,12 +386,14 @@ HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) */ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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()); } /** @@ -373,7 +404,7 @@ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; @@ -391,7 +422,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) LocationButton button1; LocationButton button2; - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -421,7 +452,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); LocationButton comp1; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -488,7 +519,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton003, TestSize.Level1) { LocationButton button1; SaveButton button2; - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(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 d79349ddb51e352852008fe32485db266f0a9f43..21d9003bca7371aa9aa5e4f504041f619e23bb04 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,34 +51,44 @@ void PasteButtonTest::TearDown() */ HWTEST_F(PasteButtonTest, IsParamValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(PasteDesc::PASTE))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(PasteDesc::PASTE))); + ASSERT_TRUE(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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -92,7 +103,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) PasteButton button1; PasteButton button2; - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -122,7 +133,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) */ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); PasteButton comp1; TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -157,7 +168,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton003, TestSize.Level1) { PasteButton button1; SaveButton button2; - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(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 6c710884bed298b7f7b49fd0b2e02fdc9d0dda24..d99e8d1e870fff319f1b2861ea61017d6e06978e 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,34 +51,44 @@ void SaveButtonTest::TearDown() */ HWTEST_F(SaveButtonTest, IsParamValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_TRUE(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_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD))); + ASSERT_TRUE(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_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_TRUE(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_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_TRUE(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_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD))); + ASSERT_TRUE(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_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_TRUE(AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); } @@ -92,7 +103,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) SaveButton button1; SaveButton button2; - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -122,7 +133,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) */ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); SaveButton comp1; TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -157,7 +168,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton003, TestSize.Level1) { SaveButton button1; PasteButton button2; - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(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 4c82ffc13a6121d092f9fff5b9818f6fa2a19715..22b09bcf26f680a0091cbbab0383c113cb762c09 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,11 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) comp.rect_.width_ = TestCommon::TEST_COORDINATE; comp.rect_.height_ = TestCommon::TEST_COORDINATE; - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); comp.ToJson(jsonRes); int32_t scId = -1; - std::string jsonStr = jsonRes.dump(); + std::string jsonStr = PackJsonToString(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 +185,9 @@ HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) */ HWTEST_F(SecCompKitTest, RegisterWithoutCallback001, TestSize.Level1) { - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(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 aff741385aa74011d835d2312cd13e3e7c6560bb..2dcbfa9f97f012e157616cb7c7bbf8e502464d59 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,10 @@ void SecCompRegisterCallbackTest::TearDown() */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) { - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -148,9 +150,10 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = -1; @@ -175,9 +178,10 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level1) { - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -195,9 +199,10 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -230,9 +235,10 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -263,9 +269,10 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1) { - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; setuid(100); EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -280,9 +287,10 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -317,9 +325,10 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, Test HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -354,9 +363,10 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, Test */ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) { - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -386,9 +396,10 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, Test HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -423,9 +434,10 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -463,9 +475,10 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -498,9 +511,10 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -531,9 +545,9 @@ HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.L */ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1) { - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); int32_t scId; ASSERT_EQ(SC_OK, RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); ASSERT_NE(-1, scId); @@ -549,11 +563,11 @@ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level */ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); - int32_t scId; + std::string saveInfo = PackJsonToString(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 e4342c4e92e3a14b05ed49d21bce17cab6948058..fee62732af39da49328469513e93bb71c382b875 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,101 @@ * 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(CJsonUnique& 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 }, - }; + auto 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); +} - 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(CJsonUnique& 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; + auto 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); } -void TestCommon::BuildSaveComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::BuildPasteComponentInfo(CJsonUnique& 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 } - }; + auto 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); +} - 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(CJsonUnique& jsonComponent, const std::string key) +{ + 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); } -void TestCommon::BuildPasteComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::GenerateCommon(CJsonUnique& 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); + + 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); + + 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); - 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 }, - }; + 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); - 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 } - }; + auto borderTagChild = CreateJson(); + AddDoubleToJson(borderTagChild, JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, 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; + auto parentTagChild = CreateJson(); + AddBoolToJson(parentTagChild, JsonTagConstants::JSON_PARENT_EFFECT_TAG, false); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, 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 b42b47ae448fcf9801ad66b04632270e5637fb4d..d36adb5ad2677224b62cb68a537aeceba34b85c5 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" @@ -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(CJsonUnique& jsonComponent); + static void BuildSaveComponentInfo(CJsonUnique& jsonComponent); + static void BuildPasteComponentInfo(CJsonUnique& jsonComponent); +private: + 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/BUILD.gn b/services/security_component_service/sa/BUILD.gn index 7ea81ab5e4d5d4512ac461c90daee44d05aee120..19498a0be618802255f51a754ba8350d01cf952e 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 6840b51f976d4db7980ac562070074d294e36aaf..0cad93182047d7de701b0f5931c498c74e973e3a 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)) { 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)) { 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(CJsonUnique& 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,8 +166,8 @@ void FirstUseDialog::LoadFirstUseRecord(void) return; } - nlohmann::json jsonRes = nlohmann::json::parse(content, nullptr, false); - if (jsonRes.is_discarded()) { + auto jsonRes = CreateJsonFromString(content); + if (jsonRes == nullptr) { SC_LOG_ERROR(LABEL, "cfg info format is invalid"); return; } @@ -193,26 +192,19 @@ void FirstUseDialog::SaveFirstUseRecord(void) } } - nlohmann::json jsonRes; + auto 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)) { + return; } - - jsonRes[FIRST_USE_RECORD_TAG] = recordsJson; } - WriteCfgContent(jsonRes.dump()); + std::string jsonStr = PackJsonToString(jsonRes); + if (jsonStr.empty()) { + SC_LOG_ERROR(LABEL, "json: PackJsonToString return empty."); + return; + } + WriteCfgContent(jsonStr); } void FirstUseDialog::StartDialogAbility(SecCompType type, sptr callerToken) @@ -294,6 +286,37 @@ void FirstUseDialog::Init(std::shared_ptr secHandler) secHandler_ = secHandler; LoadFirstUseRecord(); } + +bool FirstUseDialog::GenerateRecordsJson(CJsonUnique& jsonRes) +{ + auto 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; + } + auto recordJson = CreateJson(); + if (!AddUnsignedIntToJson(recordJson, TOKEN_ID_TAG, id)) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", TOKEN_ID_TAG.c_str()); + return false; + } + if (!AddUnsignedIntToJson(recordJson, COMP_TYPE_TAG, iter->second)) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", COMP_TYPE_TAG.c_str()); + return false; + } + if (!AddObjToArray(recordsJson, recordJson)) { + SC_LOG_ERROR(LABEL, "json: add array recordsJson error."); + return false; + } + } + if (!AddObjToJson(jsonRes, FIRST_USE_RECORD_TAG, recordsJson)) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", FIRST_USE_RECORD_TAG.c_str()); + return false; + } + 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 753500d9b971b24d46fdfdc6b832221050e8db40..e29e0f4957cbf1546039fb06a3ddb6bcdaf06a1f 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(CJsonUnique& jsonRes); void LoadFirstUseRecord(void); void SaveFirstUseRecord(void); void StartDialogAbility(SecCompType type, sptr callerToken); void SendSaveEventHandler(void); + bool GenerateRecordsJson(CJsonUnique& 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 4e4f68e126a5cf1f573fecdcbe837090c4f58a0a..08c7f6b0aa600724b85938de9b2849f968222c50 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 nlohmann::json& 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 7949f4e40592b67d2893462653615809b7307d0a..aed79007faab3f9f59d724fe2e253a642a90d63e 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(CJsonUnique& 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, 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 9da222fea282e91fdc464172a64b6119c6381f86..27563c5c476c254c0bc7b262b68c4a718a9fffc6 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) + CJsonUnique& 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, CJsonUnique& 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) + CJsonUnique& 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, + CJsonUnique& jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& clickInfo, sptr callerToken) { if (malicious_.IsInMaliciousAppList(caller.pid, caller.uid)) { @@ -493,10 +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 ++) { - nlohmann::json json; + 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()) + ", " + json.dump() + "\n"); + ", isGrant:" + std::to_string(compIter->IsGrant()) + ", " + jsonStr + "\n"); } } } 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 fdf83d0542c71b3c9fb434dfbe814de2b6d2f0c5..64b01ee4c7ae18f2a4e568dd16aa1f5aa1158c70 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, CJsonUnique& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId); - int32_t UpdateSecurityComponent(int32_t scId, const nlohmann::json& 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 nlohmann::json& 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); @@ -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); + 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 ad22a292269c92d91afa36132b0b1c394ea08efa..0ba21f8b109f0e54ddec4545f24cab24af5197de 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,12 @@ */ #include "sec_comp_service.h" +#include #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 +169,14 @@ bool SecCompService::GetCallerInfo(SecCompCallerInfo& caller) } int32_t SecCompService::ParseParams(const std::string& componentInfo, - SecCompCallerInfo& caller, nlohmann::json& jsonRes) + SecCompCallerInfo& caller, CJsonUnique& 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,8 +197,8 @@ 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); return SC_SERVICE_ERROR_VALUE_INVALID; @@ -210,11 +212,12 @@ int32_t SecCompService::RegisterSecurityComponent(SecCompType type, int32_t SecCompService::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) { SecCompCallerInfo caller; - nlohmann::json jsonRes; + CJsonUnique jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { return SC_SERVICE_ERROR_VALUE_INVALID; } - return SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonRes, caller); + int32_t ret = SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonRes, caller); + return ret; } int32_t SecCompService::UnregisterSecurityComponent(int32_t scId) @@ -232,7 +235,7 @@ int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, { StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentClick"); SecCompCallerInfo caller; - nlohmann::json jsonRes; + CJsonUnique jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { FinishTrace(HITRACE_TAG_ACCESS_CONTROL); return SC_SERVICE_ERROR_VALUE_INVALID; 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 c7c548e685847bfa5646c7a9cdbc702f97384729..6b181505a949dd42d88ac136f48027db9ad68c49 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, CJsonUnique& 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 09bc579eca8f5c7360db5927e4fe6bf0c4fc4b63..72a74729e4426e8173579f44a226cf8f76bfc26a 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -27,7 +27,6 @@ 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 = [ @@ -77,6 +76,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,7 +105,6 @@ 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 = [ @@ -145,6 +144,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 594d42a8953650b57a2e6566d2eea4095b2626ff..f67b7dbe91422630d51a60caee902fc7cdcb5ffa 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, 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 0a20e63d4aae20c2d13ddecf69942cdf05378e08..62a820946815c835ed6a7ae835513098900407a7 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,7 +171,7 @@ HWTEST_F(SecCompEntityTest, CheckClickInfo002, TestSize.Level1) */ HWTEST_F(SecCompEntityTest, CompareComponentBasicInfo001, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* other = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); bool isRectCheck = true; 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 a14a3e162bb670aa676cd99986f6879beb963a83..2d35a2a32d78ddb1d690795b0ed547b9ebecc947 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,7 +85,7 @@ void SecCompInfoHelperTest::TearDown() */ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_NE(nullptr, comp); @@ -99,7 +100,7 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent)); @@ -115,13 +116,13 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent003, 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_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); } @@ -225,15 +226,16 @@ 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_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()); } @@ -246,24 +248,30 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, 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]; - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + ASSERT_TRUE(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_TRUE(AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_TRUE(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_TRUE(AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_TRUE(AddDoubleToJson(sizeJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -276,19 +284,25 @@ 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_TRUE(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_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -301,19 +315,22 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_TRUE(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_TRUE(AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_TRUE(AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -326,18 +343,22 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_TRUE(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_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + ASSERT_TRUE(AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_INVALID)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -350,16 +371,18 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, 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_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, NO_TEXT }, - { JsonTagConstants::JSON_ICON_TAG, NO_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; + 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_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -372,23 +395,29 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_TRUE(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_TRUE(AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + 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 = 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_TRUE(AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::NO_BG_TYPE))); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -401,25 +430,37 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_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_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(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_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + static_cast(ServiceTestCommon::TEST_DIMENSION))); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -432,26 +473,37 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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_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_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(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_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_TRUE(AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + static_cast(ServiceTestCommon::TEST_DIMENSION))); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -465,21 +517,28 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent015, 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_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 } - }; + + auto styleTagChild = CreateJson(); + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_TRUE(AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); + + auto colorsTagChild = CreateJson(); + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_BLACK)); + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); } @@ -492,16 +551,21 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + auto 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 } - }; + + auto colorsTagChild = CreateJson(); + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_BLACK)); + ASSERT_TRUE(AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_WHITE)); + ASSERT_TRUE(AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); } @@ -514,7 +578,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->text_ = UNKNOWN_TEXT; @@ -536,7 +600,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->type_ = SecCompType::UNKNOWN_SC_TYPE; @@ -554,7 +618,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -583,7 +647,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid005, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, 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 25f5d7f3392aba6897e780b5a2e14c6ead8308b5..d76cff7b45e53cd0c185234f24e841dc82e0405a 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; + auto jsonInvalid = CreateJson(); EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId)); SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); @@ -320,7 +321,7 @@ 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; + auto jsonValid = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); @@ -338,7 +339,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; + auto jsonValid = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); SecCompCallerInfo caller = { @@ -357,7 +358,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity)); // wrong json - nlohmann::json jsonInvalid; + auto jsonInvalid = CreateJson(); LocationButton buttonInvalid = BuildInvalidLocationComponent(); buttonInvalid.ToJson(jsonInvalid); EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, @@ -399,7 +400,7 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve .pid = ServiceTestCommon::TEST_PID_1 }; SecCompClickEvent clickInfo; - nlohmann::json jsonVaild; + auto jsonVaild = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); ASSERT_NE(SC_OK, @@ -446,19 +447,24 @@ HWTEST_F(SecCompManagerTest, CheckClickSecurityComponentInfo001, TestSize.Level1 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity)); SecCompClickEvent clickInfo; - nlohmann::json jsonVaild; + auto jsonVaild = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); - 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)); - jsonVaild[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_TRUE(AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + + auto rectObj = GetObjFromJson(jsonVaild, JsonTagConstants::JSON_RECT); + ASSERT_NE(nullptr, rectObj); + 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)); - jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_COORDINATE - 1; + 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)); } @@ -510,7 +516,7 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList002, TestSize.Level1 */ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonValid; + auto jsonValid = CreateJson(); SecCompCallerInfo caller = { .tokenId = ServiceTestCommon::TEST_TOKEN_ID, .pid = ServiceTestCommon::TEST_PID_1 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 c66b058f229ba0d52d5b452fe74857abe85bc89a..e507e3d7cb9698730a0cec206316e255aca94417 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; + auto 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)); @@ -125,9 +125,9 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + auto 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 = { @@ -160,9 +160,9 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + auto jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(0)); AppExecFwk::AppStateData stateData = { @@ -196,9 +196,9 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + auto 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 = { @@ -250,9 +250,9 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + auto 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 = { 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 08978d1bffde7ef3d0cd29aaff4f6d91eb0a514e..20b8d857e3a495f379785797a4cfff96c9051c10 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,9 +249,9 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId)); - nlohmann::json jsonRes; + auto 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)); @@ -357,7 +358,7 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level1) setuid(0); const std::string componentInfo; - nlohmann::json jsonRes; + 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); 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 d2358ac531cb82045f166e6599ee00d4658561a1..9df4a92c36af56ec8d16d01ee8f86caf6e90d33a 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,101 @@ * 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(CJsonUnique& 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 }, - }; + auto 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); +} - 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(CJsonUnique& 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; + auto 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); } -void ServiceTestCommon::BuildSaveComponentJson(nlohmann::json& jsonComponent) +void ServiceTestCommon::BuildPasteComponentJson(CJsonUnique& 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 } - }; + auto 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); +} - 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(CJsonUnique& jsonComponent, const std::string key) +{ + 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); } -void ServiceTestCommon::BuildPasteComponentJson(nlohmann::json& jsonComponent) +void ServiceTestCommon::GenerateCommon(CJsonUnique& 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); + + 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); + + 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); - 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 }, - }; + 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); - 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 } - }; + auto borderTagChild = CreateJson(); + AddDoubleToJson(borderTagChild, JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, 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; + auto parentTagChild = CreateJson(); + AddBoolToJson(parentTagChild, JsonTagConstants::JSON_PARENT_EFFECT_TAG, false); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, 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 64989b23ffd1504663b02b4b5919f37ba0eb14b1..2a7a5f7f41a59bbdd38a5493400f1f65e7afe5bf 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" @@ -55,9 +56,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(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 diff --git a/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn b/test/fuzztest/security_component/getenhanceremoteobjectInner_fuzzer/BUILD.gn index fa31e3cfedcb96cc8ef2f7f55c0138c48f4110f2..5ee78a2b6bf446a10ac707c53ba72e9721973f66 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 11747663d2589d1f7a0b0dbb35fb8b0fd94f2c6d..c3a34a862d980821878a54f8d1563bf10f5b21e6 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 8adc654e80da557e4ad488a8c99321be9a39229c..0bba9782a2e95a9901c216a1546c20de877b9aa7 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 bfafdf29f64eaa2154105826b2ca86c0dff342bf..deb899366e99b962d6fe1ba53b2ecf32cfef3ddc 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 a46e82bdb41122922f3cb83c294cb984dc72a6ca..dad031f2fa5a81051d595cf39d4c5b605e1ab0e2 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 3eaaaf9fb227b7c165bb257b940360e13d8ab9f5..78795564388d8828b30e5790762b60bf6bb41443 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",