diff --git a/bundle.json b/bundle.json index 17bb881167ecce98a9d5d940fc10e718630a75f7..2c716f99ee0be3d159070dbb0b884773bcc21dc5 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 e60bc1b0b1d2e2efb3b16e2e68f525e36480492d..a5918da436adb5037e3e79c2fa269c1bbf5ebdbc 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -36,10 +36,10 @@ ohos_shared_library("libsecurity_component_framework") { "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", @@ -56,6 +56,9 @@ ohos_shared_library("libsecurity_component_framework") { "hilog:libhilog", ] + public_external_deps = [ + "cJSON:cjson" + ] cflags_cc = [ "-DHILOG_ENABLE" ] } @@ -64,7 +67,6 @@ config("libsecurity_component_enhance_adapter_config") { "common/include", "enhance_adapter/include", "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", - "//third_party/json/include", ] } @@ -82,7 +84,6 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { "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" ] @@ -96,6 +97,10 @@ ohos_shared_library("libsecurity_component_enhance_adapter") { "ipc:ipc_single", ] + public_external_deps = [ + "cJSON:cjson" + ] + cflags_cc = [ "-DHILOG_ENABLE" ] if (current_cpu == "arm64") { diff --git a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h index 65ff6baa05751f1504223f27254f05bfd8dd266a..96341b803394f423560083b1cc7840b123232156 100644 --- a/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h +++ b/frameworks/enhance_adapter/include/sec_comp_enhance_adapter.h @@ -16,8 +16,8 @@ #define SECURITY_COMPONENT_ENHANCE_ADAPTER_H #include +#include "cjson_utils.h" #include "iremote_object.h" -#include "nlohmann/json.hpp" #include "sec_comp_base.h" #include "sec_comp_info.h" @@ -55,7 +55,7 @@ public: // send component info to enhance service for checking its validity virtual int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, - const nlohmann::json& jsonComponent) = 0; + const CJson* jsonComponent) = 0; // get RemoteObject of enhance service to connect it virtual sptr GetEnhanceRemoteObject() = 0; @@ -68,6 +68,9 @@ public: // notify process died virtual void NotifyProcessDied(int32_t pid) = 0; + + // notify process registered + virtual void AddSecurityComponentProcess(int32_t pid) = 0; }; // for client @@ -94,7 +97,7 @@ public: static int32_t EnableInputEnhance(); static int32_t DisableInputEnhance(); static int32_t CheckComponentInfoEnhnace(int32_t pid, std::shared_ptr& compInfo, - const nlohmann::json& jsonComponent); + const CJson* jsonComponent); static sptr GetEnhanceRemoteObject(); static void StartEnhanceService(); static void ExistEnhanceService(); @@ -105,6 +108,8 @@ public: static void RegisterScIdEnhance(int32_t scId); static void UnregisterScIdEnhance(int32_t scId); + static void AddSecurityComponentProcess(int32_t pid); + static SecCompInputEnhanceInterface* inputHandler; static bool isEnhanceInputHandlerInit; diff --git a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp index 8478062bdcef3ea705ae41fde95caec73640fa56..24bd0dcd994460173e0655358481b501ca0b7ef3 100644 --- a/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp +++ b/frameworks/enhance_adapter/src/sec_comp_enhance_adapter.cpp @@ -105,6 +105,16 @@ int32_t SecCompEnhanceAdapter::CheckExtraInfo(const SecCompClickEvent& clickInfo return SC_ENHANCE_ERROR_NOT_EXIST_ENHANCE; } +void SecCompEnhanceAdapter::AddSecurityComponentProcess(int32_t pid) +{ + if (!isEnhanceSrvHandlerInit) { + InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); + } + if (srvHandler != nullptr) { + srvHandler->AddSecurityComponentProcess(pid); + } +} + bool SecCompEnhanceAdapter::EnhanceDataPreprocess(std::string& componentInfo) { if (!isEnhanceClientHandlerInit) { @@ -208,7 +218,7 @@ void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid) } int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid, - std::shared_ptr& compInfo, const nlohmann::json& jsonComponent) + std::shared_ptr& compInfo, const CJson* jsonComponent) { if (!isEnhanceSrvHandlerInit) { InitEnhanceHandler(SEC_COMP_ENHANCE_SRV_INTERFACE); diff --git a/frameworks/security_component/src/sec_comp_base.cpp b/frameworks/security_component/src/sec_comp_base.cpp index 2b6754d1fef108e3e61ba2f2fa78893c54eaf919..7fd12fc582f3c3812799aa7f7257508da27388d3 100644 --- a/frameworks/security_component/src/sec_comp_base.cpp +++ b/frameworks/security_component/src/sec_comp_base.cpp @@ -14,6 +14,8 @@ */ #include "sec_comp_base.h" +#include +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" @@ -57,47 +59,49 @@ const std::string JsonTagConstants::JSON_ICON_TAG = "icon"; const std::string JsonTagConstants::JSON_BG_TAG = "bg"; const std::string JsonTagConstants::JSON_WINDOW_ID = "windowId"; -bool SecCompBase::ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res) +bool SecCompBase::ParseDimension(const CJson* json, const std::string& tag, DimensionT& res) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_number_float()) { + double searchRes; + if (GetDoubleFromJson(json, tag, searchRes) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - - res = json.at(tag).get(); + res = searchRes; return true; } -bool SecCompBase::ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res) +bool SecCompBase::ParseColor(const CJson* json, const std::string& tag, SecCompColor& res) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_number()) { + uint32_t searchRes; + if (GetUnsignedIntFromJson(json, tag, searchRes) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - res.value = json.at(tag).get(); + res.value = searchRes; return true; } -bool SecCompBase::ParseBool(const nlohmann::json& json, const std::string& tag, bool& res) +bool SecCompBase::ParseBool(const CJson* json, const std::string& tag, bool& res) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_boolean()) { + bool searchRes; + if (GetBoolFromJson(json, tag, searchRes) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - - res = json.at(tag).get(); + + res = searchRes; return true; } -bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res) +bool SecCompBase::ParsePadding(const CJson* json, const std::string& tag, PaddingSize& res) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonPadding = GetObjFromJson(json, tag); + if (jsonPadding == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonPadding = json.at(tag); if (!ParseDimension(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, res.top)) { return false; } @@ -113,13 +117,14 @@ bool SecCompBase::ParsePadding(const nlohmann::json& json, const std::string& ta return true; } -bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseColors(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonColors = GetObjFromJson(json, tag); + if (jsonColors == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonColors = json.at(tag); + if (!ParseColor(jsonColors, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_)) { return false; } @@ -132,24 +137,25 @@ bool SecCompBase::ParseColors(const nlohmann::json& json, const std::string& tag return true; } -bool SecCompBase::ParseBorders(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseBorders(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonBorder = GetObjFromJson(json, tag); + if (jsonBorder == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonBorder = json.at(tag); + return ParseDimension(jsonBorder, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_); } -bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseSize(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonSize = GetObjFromJson(json, tag); + if (jsonSize == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonSize = json.at(tag); if (!ParseDimension(jsonSize, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_)) { return false; } @@ -169,24 +175,25 @@ bool SecCompBase::ParseSize(const nlohmann::json& json, const std::string& tag) return true; } -bool SecCompBase::ParseParent(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseParent(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonParent = GetObjFromJson(json, tag); + if (jsonParent == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonParent = json.at(tag); + return ParseBool(jsonParent, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_); } -bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect) +bool SecCompBase::ParseRect(const CJson* json, const std::string& tag, SecCompRect& rect) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + auto jsonSize = GetObjFromJson(json, tag); + if (jsonSize == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonSize = json.at(tag); if (!ParseDimension(jsonSize, JsonTagConstants::JSON_RECT_X, rect.x_)) { return false; } @@ -206,15 +213,16 @@ bool SecCompBase::ParseRect(const nlohmann::json& json, const std::string& tag, return true; } -bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) +bool SecCompBase::FromJson(const CJson* jsonSrc) { - SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonSrc.dump().c_str()); - if ((jsonSrc.find(JsonTagConstants::JSON_SC_TYPE) == jsonSrc.end()) || - !jsonSrc.at(JsonTagConstants::JSON_SC_TYPE).is_number()) { + std::string jsonStr = PackJsonToString(jsonSrc); + SC_LOG_DEBUG(LABEL, "Button info %{public}s.", jsonStr.c_str()); + + int32_t value; + if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_SC_TYPE, value) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_SC_TYPE.c_str()); return false; } - int32_t value = jsonSrc.at(JsonTagConstants::JSON_SC_TYPE).get(); if ((value <= static_cast(SecCompType::UNKNOWN_SC_TYPE)) || (value >= static_cast(SecCompType::MAX_SC_TYPE))) { SC_LOG_ERROR(LABEL, "scType value is invalid."); @@ -222,12 +230,10 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) } type_ = static_cast(value); - if ((jsonSrc.find(JsonTagConstants::JSON_NODE_ID) == jsonSrc.end()) || - !jsonSrc.at(JsonTagConstants::JSON_NODE_ID).is_number()) { + if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_NODE_ID, nodeId_) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_NODE_ID.c_str()); return false; } - nodeId_ = jsonSrc.at(JsonTagConstants::JSON_NODE_ID).get(); if (!ParseRect(jsonSrc, JsonTagConstants::JSON_RECT, rect_)) { return false; @@ -250,72 +256,60 @@ bool SecCompBase::FromJson(const nlohmann::json& jsonSrc) if (!ParseStyle(jsonSrc, JsonTagConstants::JSON_STYLE_TAG)) { return false; } - - if ((jsonSrc.find(JsonTagConstants::JSON_WINDOW_ID) == jsonSrc.end()) || - !jsonSrc.at(JsonTagConstants::JSON_WINDOW_ID).is_number()) { + if (GetIntFromJson(jsonSrc, JsonTagConstants::JSON_WINDOW_ID, windowId_) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", JsonTagConstants::JSON_WINDOW_ID.c_str()); return false; } - windowId_ = jsonSrc.at(JsonTagConstants::JSON_WINDOW_ID).get(); return true; } -void SecCompBase::ToJson(nlohmann::json& jsonRes) const +void SecCompBase::ToJson(CJson* jsonRes) const { - jsonRes[JsonTagConstants::JSON_SC_TYPE] = type_; - jsonRes[JsonTagConstants::JSON_NODE_ID] = nodeId_; - jsonRes[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, rect_.x_}, - {JsonTagConstants::JSON_RECT_Y, rect_.y_}, - {JsonTagConstants::JSON_RECT_WIDTH, rect_.width_}, - {JsonTagConstants::JSON_RECT_HEIGHT, rect_.height_} - }; - jsonRes[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, windowRect_.x_}, - {JsonTagConstants::JSON_RECT_Y, windowRect_.y_}, - {JsonTagConstants::JSON_RECT_WIDTH, windowRect_.width_}, - {JsonTagConstants::JSON_RECT_HEIGHT, windowRect_.height_} - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, padding_.top }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding_.right }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding_.bottom }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, padding_.left }, - }; - - jsonRes[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_ }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_ }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_ }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonRes[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value }, - { JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value } - }; - - jsonRes[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_ }, - }; - jsonRes[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_ }, - }; - - jsonRes[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, text_ }, - { JsonTagConstants::JSON_ICON_TAG, icon_ }, - { JsonTagConstants::JSON_BG_TAG, bg_ }, - }; - jsonRes[JsonTagConstants::JSON_WINDOW_ID] = windowId_; + if (AddIntToJson(jsonRes, JsonTagConstants::JSON_SC_TYPE, type_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_SC_TYPE.c_str(), type_); + return; + } + if (AddIntToJson(jsonRes, JsonTagConstants::JSON_NODE_ID, nodeId_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_NODE_ID.c_str(), nodeId_); + return; + } + if (!AddRect(jsonRes, JsonTagConstants::JSON_RECT, rect_)) { + return; + } + if (!AddRect(jsonRes, JsonTagConstants::JSON_WINDOW_RECT, windowRect_)) { + return; + } + + if (!AddSize(jsonRes)) { + return; + } + if (!AddColors(jsonRes)) { + return; + } + if (!AddBorders(jsonRes)) { + return; + } + if (!AddParent(jsonRes)) { + return; + } + if (!AddStyle(jsonRes)) { + return; + } + if (AddIntToJson(jsonRes, JsonTagConstants::JSON_WINDOW_ID, windowId_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_WINDOW_ID.c_str(), windowId_); + return; + } } std::string SecCompBase::ToJsonStr() const { - nlohmann::json json; + CJson* json = CreateJson(); ToJson(json); - return json.dump(); + std::string jsonStr = PackJsonToString(json); + return jsonStr; } bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const @@ -343,27 +337,38 @@ bool SecCompBase::CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck return (leftValue == rightValue); } -bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag) +bool SecCompBase::ParseStyle(const CJson* json, const std::string& tag) { - if ((json.find(tag) == json.end()) || !json.at(tag).is_object()) { + CJson* jsonStyle = GetObjFromJson(json, tag); + if (jsonStyle == nullptr) { SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); return false; } - auto jsonStyle = json.at(tag); - if (!(jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).is_number() && - jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).is_number() && - jsonStyle.at(JsonTagConstants::JSON_BG_TAG).is_number())) { - SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", tag.c_str()); + + if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_TEXT_TAG, text_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", + tag.c_str(), JsonTagConstants::JSON_TEXT_TAG.c_str()); return false; } - text_ = jsonStyle.at(JsonTagConstants::JSON_TEXT_TAG).get(); - icon_ = jsonStyle.at(JsonTagConstants::JSON_ICON_TAG).get(); + if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_ICON_TAG, icon_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", + tag.c_str(), JsonTagConstants::JSON_ICON_TAG.c_str()); + return false; + } + if (!IsTextIconTypeValid()) { SC_LOG_ERROR(LABEL, "text or icon is invalid."); return false; } - bg_ = static_cast(jsonStyle.at(JsonTagConstants::JSON_BG_TAG).get()); + int32_t bg; + if (GetIntFromJson(jsonStyle, JsonTagConstants::JSON_BG_TAG, bg) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s.%{public}s tag invalid.", + tag.c_str(), JsonTagConstants::JSON_BG_TAG.c_str()); + return false; + } + + bg_ = static_cast(bg); if ((bg_ <= SecCompBackground::UNKNOWN_BG) || (bg_ >= SecCompBackground::MAX_BG_TYPE)) { SC_LOG_ERROR(LABEL, "bg is invalid."); return false; @@ -371,6 +376,217 @@ bool SecCompBase::ParseStyle(const nlohmann::json& json, const std::string& tag) return true; } + +bool SecCompBase::AddRect(CJson* json, const std::string jsonTag, const SecCompRect rectObj) const +{ + CJson* childJson = CreateJson(); + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_X, rectObj.x_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_RECT_X.c_str(), rectObj.x_); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_Y, rectObj.y_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_RECT_Y.c_str(), rectObj.y_); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_WIDTH, rectObj.width_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_RECT_WIDTH.c_str(), rectObj.width_); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_RECT_HEIGHT, rectObj.height_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_RECT_HEIGHT.c_str(), rectObj.height_); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, jsonTag, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_RECT.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddPadding(CJson* json) const +{ + if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_TOP_TAG, padding_.top) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_PADDING_TOP_TAG.c_str(), padding_.top); + return false; + } + if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_RIGHT_TAG, padding_.right) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_PADDING_RIGHT_TAG.c_str(), padding_.right); + return false; + } + if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, padding_.bottom) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_PADDING_BOTTOM_TAG.c_str(), padding_.bottom); + return false; + } + if (AddDoubleToJson(json, JsonTagConstants::JSON_PADDING_LEFT_TAG, padding_.left) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_PADDING_LEFT_TAG.c_str(), padding_.left); + return false; + } + return true; +} + +bool SecCompBase::AddSize(CJson* json) const +{ + CJson* jsonPadding = CreateJson(); + if (!AddPadding(jsonPadding)) { + FreeJson(jsonPadding); + return false; + } + + CJson* childJson = CreateJson(); + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_FONT_SIZE_TAG, fontSize_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_FONT_SIZE_TAG.c_str(), fontSize_); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_ICON_SIZE_TAG, iconSize_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_ICON_SIZE_TAG.c_str(), iconSize_); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, textIconSpace_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG.c_str(), textIconSpace_); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + if (AddObjToJson(childJson, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_PADDING_SIZE_TAG.c_str()); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_SIZE_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_SIZE_TAG.c_str()); + FreeJson(jsonPadding); + FreeJson(childJson); + return false; + } + FreeJson(jsonPadding); + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddColors(CJson* json) const +{ + CJson* childJson = CreateJson(); + if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_FONT_COLOR_TAG, fontColor_.value) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", + JsonTagConstants::JSON_FONT_COLOR_TAG.c_str(), fontColor_.value); + FreeJson(childJson); + return false; + } + if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_ICON_COLOR_TAG, iconColor_.value) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", + JsonTagConstants::JSON_ICON_COLOR_TAG.c_str(), iconColor_.value); + FreeJson(childJson); + return false; + } + if (AddUnsignedIntToJson(childJson, JsonTagConstants::JSON_BG_COLOR_TAG, bgColor_.value) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}u val invalid.", + JsonTagConstants::JSON_BG_COLOR_TAG.c_str(), bgColor_.value); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_COLORS_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_COLORS_TAG.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddBorders(CJson* json) const +{ + CJson* childJson = CreateJson(); + if (AddDoubleToJson(childJson, JsonTagConstants::JSON_BORDER_WIDTH_TAG, borderWidth_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}lf val invalid.", + JsonTagConstants::JSON_BORDER_WIDTH_TAG.c_str(), borderWidth_); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_BORDER_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_BORDER_TAG.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddParent(CJson* json) const +{ + CJson* childJson = CreateJson(); + if (AddBoolToJson(childJson, JsonTagConstants::JSON_PARENT_EFFECT_TAG, parentEffect_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_PARENT_EFFECT_TAG.c_str(), parentEffect_); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_PARENT_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_PARENT_TAG.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} + +bool SecCompBase::AddStyle(CJson* json) const +{ + CJson* childJson = CreateJson(); + if (AddIntToJson(childJson, JsonTagConstants::JSON_TEXT_TAG, text_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_TEXT_TAG.c_str(), text_); + FreeJson(childJson); + return false; + } + if (AddIntToJson(childJson, JsonTagConstants::JSON_ICON_TAG, icon_) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_ICON_TAG.c_str(), icon_); + FreeJson(childJson); + return false; + } + if (AddIntToJson(childJson, JsonTagConstants::JSON_BG_TAG, static_cast(bg_)) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag %{public}d val invalid.", + JsonTagConstants::JSON_BG_TAG.c_str(), bg_); + FreeJson(childJson); + return false; + } + if (AddObjToJson(json, JsonTagConstants::JSON_STYLE_TAG, childJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add %{public}s tag invalid.", + JsonTagConstants::JSON_STYLE_TAG.c_str()); + FreeJson(childJson); + return false; + } + FreeJson(childJson); + return true; +} } // namespace base } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/enhance_kits/test/BUILD.gn b/interfaces/inner_api/enhance_kits/test/BUILD.gn index 4f849a59e0bd7b50962bda5d4c7f06012817b86b..99e1750956e5b7b814c3ce552bcada1d34892563 100644 --- a/interfaces/inner_api/enhance_kits/test/BUILD.gn +++ b/interfaces/inner_api/enhance_kits/test/BUILD.gn @@ -25,9 +25,15 @@ ohos_unittest("sec_comp_enhance_sdk_test") { debug = false } branch_protector_ret = "pac_ret" - include_dirs = [ "unittest/src" ] + include_dirs = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", + "unittest/src", + ] - sources = [ "unittest/src/sec_comp_enhance_test.cpp" ] + sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", + "unittest/src/sec_comp_enhance_test.cpp", + ] configs = [ "${sec_comp_root_dir}/config:coverage_flags" ] cflags_cc = [ "-DHILOG_ENABLE" ] @@ -38,6 +44,7 @@ ohos_unittest("sec_comp_enhance_sdk_test") { ] external_deps = [ + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "ipc:ipc_single", diff --git a/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp b/interfaces/inner_api/enhance_kits/test/unittest/src/sec_comp_enhance_test.cpp index 9ac2300810c186dc8a2af2ba4891d4db3646b9a3..d635917a74d17709350f4605e3d78320a9dd4b78 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" @@ -170,10 +170,11 @@ HWTEST_F(SecCompEnhanceTest, InitEnhanceHandler001, TestSize.Level1) SecCompEnhanceAdapter::NotifyProcessDied(scId); SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; std::shared_ptr compInfo; - const nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); SecCompEnhanceAdapter::CheckComponentInfoEnhnace(scId, compInfo, jsonComponent); SecCompEnhanceAdapter::isEnhanceInputHandlerInit = false; SecCompEnhanceAdapter::GetEnhanceRemoteObject(); + FreeJson(jsonComponent); } /** @@ -211,7 +212,8 @@ HWTEST_F(SecCompEnhanceTest, InitEnhanceHandler002, TestSize.Level1) SecCompEnhanceAdapter::ExistEnhanceService(); SecCompEnhanceAdapter::NotifyProcessDied(scId); std::shared_ptr compInfo; - const nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ASSERT_EQ(SC_OK, SecCompEnhanceAdapter::CheckComponentInfoEnhnace(scId, compInfo, jsonComponent)); ASSERT_EQ(nullptr, SecCompEnhanceAdapter::GetEnhanceRemoteObject()); + FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/BUILD.gn b/interfaces/inner_api/security_component/BUILD.gn index 41cef615cfa6ecc15796a53ef715313ff70e9ef3..e90eaa0428523b9f115525019449d5ba94a759b1 100644 --- a/interfaces/inner_api/security_component/BUILD.gn +++ b/interfaces/inner_api/security_component/BUILD.gn @@ -20,7 +20,6 @@ config("sec_comp_config") { include_dirs = [ "include", "${sec_comp_root_dir}/frameworks/common/include", - "//third_party/json/include", ] } diff --git a/interfaces/inner_api/security_component/include/cjson_utils.h b/interfaces/inner_api/security_component/include/cjson_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..dcdbd6940fd0b28372b21dbc633477405bd1bd29 --- /dev/null +++ b/interfaces/inner_api/security_component/include/cjson_utils.h @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JSON_UTILS_H +#define JSON_UTILS_H + +#include +#include +#include +#include +#include "cJSON.h" + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +typedef cJSON CJson; +enum { + CJSON_SUCCESS = 0, + CJSON_ERR_INVALID_PARAM = -1, + CJSON_ERR_BAD_ALLOC = -2, + CJSON_ERR_NULL_PTR = -3, + CJSON_ERR_JSON_GET = -4, + CJSON_ERR_JSON_ADD = -5, + CJSON_ERR_JSON_REPLACE = -6, + CJSON_ERR_JSON_DUPLICATE = -7, +}; + +/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJson* CreateJsonFromString(const std::string jsonStr); +/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJson* CreateJson(void); +/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ +CJson* CreateJsonArray(void); +void FreeJson(CJson*& jsonObj); + +/* NO Need to call FreeJsonString to free the returned pointer when it's no longer in use. */ +std::string PackJsonToString(const CJson* jsonObj); +void FreeJsonString(char* jsonStr); + +/* + * Can't release the returned pointer, otherwise, an exception may occur. + * It refers to the parent object(param--jsonObj)'s memory. + * It will be recycled along with jsonObj when jsonObj is released. + */ +CJson* GetObjFromJson(const CJson* jsonObj, const std::string key); +CJson* GetArrayFromJson(const CJson* jsonObj, const std::string key); + +/* +* Return a copy of string in jsonObj in std::string +*/ +std::string GetStringFromJson(const CJson* jsonObj, const std::string key); + +int32_t GetIntFromJson(const CJson* jsonObj, const std::string key, int32_t& value); +int32_t GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value); +int32_t GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value); +int32_t GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value); + +int32_t AddObjToJson(CJson* jsonObj, const std::string key, const CJson* childObj); +int32_t AddObjToArray(CJson* jsonArr, CJson* item); +int32_t AddStringToJson(CJson* jsonObj, const std::string key, const std::string value); +int32_t AddBoolToJson(CJson* jsonObj, const std::string key, const bool value); +int32_t AddIntToJson(CJson* jsonObj, const std::string key, const int value); +int32_t AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value); +int32_t AddDoubleToJson(CJson* jsonObj, const std::string key, const double value); +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS +#endif \ No newline at end of file diff --git a/interfaces/inner_api/security_component/include/i_sec_comp_service.h b/interfaces/inner_api/security_component/include/i_sec_comp_service.h index 8df9269e9c09bbad526817c5238e7719da7fe8d4..cd76c2e7aade5c3fe45c89d48888180d50d631f9 100644 --- a/interfaces/inner_api/security_component/include/i_sec_comp_service.h +++ b/interfaces/inner_api/security_component/include/i_sec_comp_service.h @@ -38,6 +38,7 @@ public: const SecCompClickEvent& clickInfo, sptr callerToken) = 0; virtual bool VerifySavePermission(AccessToken::AccessTokenID tokenId) = 0; virtual sptr GetEnhanceRemoteObject() = 0; + virtual int32_t PreRegisterSecCompProcess() = 0; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/location_button.h b/interfaces/inner_api/security_component/include/location_button.h index a98772f85b4de1c0b9b945d64a4299572bb59e2a..95fdde149b366681bc3e55a87668a65425416c9e 100644 --- a/interfaces/inner_api/security_component/include/location_button.h +++ b/interfaces/inner_api/security_component/include/location_button.h @@ -16,7 +16,7 @@ #define I_SECURITY_COMPONENT_LOCATION_BUTTON_H #include -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_base.h" #include "sec_comp_info.h" @@ -50,7 +50,7 @@ public: virtual bool IsCorrespondenceType() override; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const nlohmann::json& json, const std::string& tag); + bool ParseStyle(const CJson* json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/paste_button.h b/interfaces/inner_api/security_component/include/paste_button.h index 11b243f17d72b46e1e18c479c7501c9610bed656..d26bedd7bf76e455911f960cda4ca91e53be963f 100644 --- a/interfaces/inner_api/security_component/include/paste_button.h +++ b/interfaces/inner_api/security_component/include/paste_button.h @@ -16,7 +16,7 @@ #define SECURITY_COMPONENT_PASTE_BUTTON_H #include -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_base.h" namespace OHOS { @@ -40,7 +40,7 @@ public: virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const nlohmann::json& json, const std::string& tag); + bool ParseStyle(const CJson* json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/save_button.h b/interfaces/inner_api/security_component/include/save_button.h index 126b69075a38b6f257ee7bc829364f930967244e..071fae7d2d701c9d33f80132baadc9c9d4b2a790 100644 --- a/interfaces/inner_api/security_component/include/save_button.h +++ b/interfaces/inner_api/security_component/include/save_button.h @@ -16,7 +16,7 @@ #define I_SECURITY_COMPONENT_SAVE_BUTTON_H #include -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_base.h" #include "sec_comp_info.h" @@ -47,7 +47,7 @@ public: virtual bool IsCorrespondenceType() override; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const override; private: - bool ParseStyle(const nlohmann::json& json, const std::string& tag); + bool ParseStyle(const CJson* json, const std::string& tag); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_base.h b/interfaces/inner_api/security_component/include/sec_comp_base.h index db539bd46832445e3b3d57642c344099277a0cfd..be6591c057eacc6ddd2ae22509661eebbf9c0f04 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 SecCompBase { public: SecCompBase() = default; virtual ~SecCompBase() = default; - bool FromJson(const nlohmann::json& jsonSrc); - void ToJson(nlohmann::json& jsonRes) const; + bool FromJson(const CJson* jsonSrc); + void ToJson(CJson* jsonRes) const; std::string ToJsonStr(void) const; virtual bool CompareComponentBasicInfo(SecCompBase *other, bool isRectCheck) const; void SetValid(bool valid) @@ -124,16 +124,23 @@ protected: virtual bool IsTextIconTypeValid() = 0; virtual bool IsCorrespondenceType() = 0; private: - bool ParseDimension(const nlohmann::json& json, const std::string& tag, DimensionT& res); - bool ParseColor(const nlohmann::json& json, const std::string& tag, SecCompColor& res); - bool ParseBool(const nlohmann::json& json, const std::string& tag, bool& res); - bool ParsePadding(const nlohmann::json& json, const std::string& tag, PaddingSize& res); - bool ParseColors(const nlohmann::json& json, const std::string& tag); - bool ParseBorders(const nlohmann::json& json, const std::string& tag); - bool ParseSize(const nlohmann::json& json, const std::string& tag); - bool ParseParent(const nlohmann::json& json, const std::string& tag); - bool ParseRect(const nlohmann::json& json, const std::string& tag, SecCompRect& rect); - bool ParseStyle(const nlohmann::json& json, const std::string& tag); + bool ParseDimension(const CJson* json, const std::string& tag, DimensionT& res); + bool ParseColor(const CJson* json, const std::string& tag, SecCompColor& res); + bool ParseBool(const CJson* json, const std::string& tag, bool& res); + bool ParsePadding(const CJson* json, const std::string& tag, PaddingSize& res); + bool ParseColors(const CJson* json, const std::string& tag); + bool ParseBorders(const CJson* json, const std::string& tag); + bool ParseSize(const CJson* json, const std::string& tag); + bool ParseParent(const CJson* json, const std::string& tag); + bool ParseRect(const CJson* json, const std::string& tag, SecCompRect& rect); + bool ParseStyle(const CJson* json, const std::string& tag); + bool AddRect(CJson* json, const std::string jsonTag, const SecCompRect rectObj) const; + bool AddPadding(CJson* json) const; + bool AddSize(CJson* json) const; + bool AddColors(CJson* json) const; + bool AddBorders(CJson* json) const; + bool AddParent(CJson* json) const; + bool AddStyle(CJson* json) const; }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_client.h b/interfaces/inner_api/security_component/include/sec_comp_client.h index 719489f71cef6ac8a276cb36af8de827d6670ee9..fca78d5f19d7cb1a76685082a34f9eb193a57961 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_client.h +++ b/interfaces/inner_api/security_component/include/sec_comp_client.h @@ -37,6 +37,7 @@ public: const std::string& componentInfo, const SecCompClickEvent& clickInfo, sptr callerToken); bool VerifySavePermission(AccessToken::AccessTokenID tokenId); sptr GetEnhanceRemoteObject(bool doLoadSa); + int32_t PreRegisterSecCompProcess(); void FinishStartSASuccess(const sptr& remoteObject); void FinishStartSAFail(); diff --git a/interfaces/inner_api/security_component/include/sec_comp_kit.h b/interfaces/inner_api/security_component/include/sec_comp_kit.h index 0f0ee946efd5b15a644845d67ca1ab49ce2deb35..93c7e531c08d711b562178dce43bbb7b26ac0484 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_kit.h +++ b/interfaces/inner_api/security_component/include/sec_comp_kit.h @@ -33,6 +33,7 @@ public: std::string& componentInfo, const SecCompClickEvent& clickInfo, sptr callerToken = nullptr); static bool VerifySavePermission(AccessToken::AccessTokenID tokenId); static sptr GetEnhanceRemoteObject(bool isLoad); + static int32_t PreRegisterSecCompProcess(); }; } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/include/sec_comp_proxy.h b/interfaces/inner_api/security_component/include/sec_comp_proxy.h index 9b5b4e213cdd6c25da866290fd7a67929ecff4d5..f63496527e122afc07812dc82ff10d68142d67c6 100644 --- a/interfaces/inner_api/security_component/include/sec_comp_proxy.h +++ b/interfaces/inner_api/security_component/include/sec_comp_proxy.h @@ -33,6 +33,7 @@ public: const std::string& componentInfo, const SecCompClickEvent& clickInfo, sptr callerToken) override; bool VerifySavePermission(AccessToken::AccessTokenID tokenId) override; sptr GetEnhanceRemoteObject() override; + int32_t PreRegisterSecCompProcess() override; private: static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h b/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h index 968ad969b21152ca7d7887c3eba2b5d833adc4cc..f87389d69364c8f057a61828ab053fc5c9376655 100644 --- a/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h +++ b/interfaces/inner_api/security_component/include/security_component_service_ipc_interface_code.h @@ -27,6 +27,7 @@ enum SecurityComponentServiceInterfaceCode { REPORT_SECURITY_COMPONENT_CLICK_EVENT = 3, GET_SECURITY_COMPONENT_ENHANCE_OBJECT = 4, VERIFY_TEMP_SAVE_PERMISSION = 5, + PRE_REGISTER_PROCESS = 6, }; enum EnhanceInterfaceCode { @@ -41,4 +42,3 @@ enum EnhanceCallbackCheckCode { } // namespace Security } // namespace OHOS #endif // SECURITY_COMPONENT_IPC_INTERFACE_CODE_H - 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..26ae6983f5eb24eb979875313092b3856c61057d --- /dev/null +++ b/interfaces/inner_api/security_component/src/cjson_utils.cpp @@ -0,0 +1,344 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "cjson_utils.h" +#include +#include + +namespace OHOS { +namespace Security { +namespace SecurityComponent { +namespace { +#define RECURSE_FLAG_TRUE 1 +} + +CJson* CreateJsonFromString(const std::string jsonStr) +{ + if (jsonStr.empty()) { + return nullptr; + } + return cJSON_Parse(jsonStr.c_str()); +} + +CJson* CreateJson(void) +{ + return cJSON_CreateObject(); +} + +CJson* CreateJsonArray(void) +{ + return cJSON_CreateArray(); +} + +void FreeJson(CJson*& jsonObj) +{ + cJSON_Delete(jsonObj); + jsonObj = nullptr; +} + +std::string PackJsonToString(const CJson* jsonObj) +{ + if (jsonObj == nullptr) { + return std::string(); + } + char* ptr = cJSON_PrintUnformatted(jsonObj); + if (ptr == nullptr) { + return std::string(); + } + std::string ret = std::string(ptr); + FreeJsonString(ptr); + return ret; +} + +void FreeJsonString(char* jsonStr) +{ + if (jsonStr != nullptr) { + cJSON_free(jsonStr); + } +} + +CJson* GetObjFromJson(const CJson* jsonObj, const std::string key) +{ + if (jsonObj == nullptr || key.empty()) { + return nullptr; + } + + CJson* objValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objValue != nullptr && cJSON_IsObject(objValue)) { + return objValue; + } + return nullptr; +} + +CJson* GetArrayFromJson(const CJson* jsonObj, const std::string key) +{ + if (jsonObj == nullptr || key.empty()) { + return nullptr; + } + + CJson* objValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objValue != nullptr && cJSON_IsArray(objValue)) { + return objValue; + } + return nullptr; +} + +std::string GetStringFromJson(const CJson* jsonObj, const std::string key) +{ + if (jsonObj == nullptr || key.empty()) { + return std::string(); + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsString(jsonObjTmp)) { + char* getStrPtr = cJSON_GetStringValue(jsonObjTmp); + return std::string(getStrPtr); + } + return std::string(); +} + +int32_t GetIntFromJson(const CJson* jsonObj, const std::string key, int32_t& value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { + value = (int)cJSON_GetNumberValue(jsonObjTmp); + return CJSON_SUCCESS; + } + return CJSON_ERR_JSON_GET; +} + +int32_t GetUnsignedIntFromJson(const CJson* jsonObj, const std::string key, uint32_t& value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { + double realValue = cJSON_GetNumberValue(jsonObjTmp); + if (realValue < 0) { + int32_t tmpValue = static_cast(realValue); + value = static_cast(tmpValue); + } else { + value = static_cast(realValue); + } + return CJSON_SUCCESS; + } + return CJSON_ERR_JSON_GET; +} + +int32_t GetBoolFromJson(const CJson* jsonObj, const std::string key, bool& value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsBool(jsonObjTmp)) { + value = cJSON_IsTrue(jsonObjTmp) ? true : false; + return CJSON_SUCCESS; + } + return CJSON_ERR_JSON_GET; +} + +int32_t GetDoubleFromJson(const CJson* jsonObj, const std::string key, double& value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* jsonObjTmp = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (jsonObjTmp != nullptr && cJSON_IsNumber(jsonObjTmp)) { + value = cJSON_GetNumberValue(jsonObjTmp); + return CJSON_SUCCESS; + } + return CJSON_ERR_JSON_GET; +} + +int32_t AddObjToJson(CJson* jsonObj, const std::string key, const CJson* childObj) +{ + if (jsonObj == nullptr || key.empty() || childObj == nullptr) { + return CJSON_ERR_NULL_PTR; + } + + CJson* tmpObj = cJSON_Duplicate(childObj, RECURSE_FLAG_TRUE); + if (tmpObj == nullptr) { + return CJSON_ERR_JSON_DUPLICATE; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddItemToObject(jsonObj, key.c_str(), tmpObj) == false) { + cJSON_Delete(tmpObj); + return CJSON_ERR_JSON_ADD; + } + } else { + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmpObj) == false) { + cJSON_Delete(tmpObj); + return CJSON_ERR_JSON_REPLACE; + } + } + return CJSON_SUCCESS; +} + +int32_t AddObjToArray(CJson* jsonArr, CJson* item) +{ + if (jsonArr == nullptr || item == nullptr) { + return CJSON_ERR_NULL_PTR; + } + + if (cJSON_IsArray(jsonArr) == false) { + return CJSON_ERR_INVALID_PARAM; + } + + bool ret = cJSON_AddItemToArray(jsonArr, item); + if (ret == false) { + return CJSON_ERR_JSON_ADD; + } + + return CJSON_SUCCESS; +} + +int32_t AddStringToJson(CJson* jsonObj, const std::string key, const std::string value) +{ + if (jsonObj == nullptr || key.empty() || value.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddStringToObject(jsonObj, key.c_str(), value.c_str()) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateString(value.c_str()); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + + return CJSON_SUCCESS; +} + +int32_t AddBoolToJson(CJson* jsonObj, const std::string key, const bool value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddBoolToObject(jsonObj, key.c_str(), value) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateBool(value); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + + return CJSON_SUCCESS; +} + +int32_t AddIntToJson(CJson* jsonObj, const std::string key, const int value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddNumberToObject(jsonObj, key.c_str(), value) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateNumber(value); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + + return CJSON_SUCCESS; +} + +int32_t AddUnsignedIntToJson(CJson* jsonObj, const std::string key, const uint32_t value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + double tmpValue = static_cast(value); + if (objInJson == nullptr) { + if (cJSON_AddNumberToObject(jsonObj, key.c_str(), tmpValue) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateNumber(tmpValue); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + return CJSON_SUCCESS; +} + +int32_t AddDoubleToJson(CJson* jsonObj, const std::string key, const double value) +{ + if (jsonObj == nullptr || key.empty()) { + return CJSON_ERR_NULL_PTR; + } + + CJson* objInJson = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str()); + if (objInJson == nullptr) { + if (cJSON_AddNumberToObject(jsonObj, key.c_str(), value) == nullptr) { + return CJSON_ERR_JSON_GET; + } + } else { + CJson* tmp = cJSON_CreateNumber(value); + if (tmp == nullptr) { + return CJSON_ERR_BAD_ALLOC; + } + if (cJSON_ReplaceItemInObjectCaseSensitive(jsonObj, key.c_str(), tmp) == false) { + cJSON_Delete(tmp); + return CJSON_ERR_JSON_REPLACE; + } + } + + return CJSON_SUCCESS; +} +} // namespace SecurityComponent +} // namespace Security +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/security_component/src/sec_comp_client.cpp b/interfaces/inner_api/security_component/src/sec_comp_client.cpp index 27545fa62817064ee75dbfdb0bdb85eb0717aafa..7ce91dc5df616938da8c1ff2b0b0fcb38db67209 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_client.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_client.cpp @@ -113,6 +113,17 @@ sptr SecCompClient::GetEnhanceRemoteObject(bool doLoadSa) return proxy->GetEnhanceRemoteObject(); } +int32_t SecCompClient::PreRegisterSecCompProcess() +{ + auto proxy = GetProxy(true); + if (proxy == nullptr) { + SC_LOG_ERROR(LABEL, "Proxy is null"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + + return proxy->PreRegisterSecCompProcess(); +} + bool SecCompClient::StartLoadSecCompSa() { { diff --git a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp index 04562ca83b0b3ba8dac313eddc20b70e00e1fe5e..a8314fb6208d32422b3f8f6707ae60cdc1f74c64 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_kit.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_kit.cpp @@ -133,6 +133,11 @@ sptr SecCompKit::GetEnhanceRemoteObject(bool isLoad) { return SecCompClient::GetInstance().GetEnhanceRemoteObject(isLoad); } + +int32_t SecCompKit::PreRegisterSecCompProcess() +{ + return SecCompClient::GetInstance().PreRegisterSecCompProcess(); +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp index 3b567699306305f6c512537844a6f1118edbc262..6182d7a7e49474ff142c42d44425deaa108f1314 100644 --- a/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp +++ b/interfaces/inner_api/security_component/src/sec_comp_proxy.cpp @@ -269,6 +269,36 @@ sptr SecCompProxy::GetEnhanceRemoteObject() } return callback; } + +int32_t SecCompProxy::PreRegisterSecCompProcess() +{ + MessageParcel data; + if (!data.WriteInterfaceToken(SecCompProxy::GetDescriptor())) { + SC_LOG_ERROR(LABEL, "PreRegister write descriptor fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + sptr remote = Remote(); + if (remote == nullptr) { + SC_LOG_ERROR(LABEL, "PreRegister remote service is null"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + int32_t requestResult = remote->SendRequest( + static_cast(SecurityComponentServiceInterfaceCode::PRE_REGISTER_PROCESS), + data, reply, option); + if (requestResult != SC_OK) { + SC_LOG_ERROR(LABEL, "PreRegister request fail, result: %{public}d", requestResult); + return requestResult; + } + int32_t res; + if (!reply.ReadInt32(res)) { + SC_LOG_ERROR(LABEL, "PreRegister read int32 fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return res; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/interfaces/inner_api/security_component/test/BUILD.gn b/interfaces/inner_api/security_component/test/BUILD.gn index c7363f68162da50a786e61ddf8a2742eaff12bc5..e3d4cff01ca71cb8eab806389def70291727fa59 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", ] @@ -42,12 +43,14 @@ ohos_unittest("sec_comp_sdk_test") { } deps = [ + "${sec_comp_root_dir}/frameworks:libsecurity_component_enhance_adapter", "${sec_comp_root_dir}/frameworks:libsecurity_component_framework", "${sec_comp_root_dir}/interfaces/inner_api/security_component:libsecurity_component_sdk", ] external_deps = [ "access_token:libaccesstoken_sdk", + "cJSON:cjson", "c_utils:utils", "hilog:libhilog", "hisysevent:libhisysevent", diff --git a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp index 335bb9845ef5f030b4e13cab179f744f335fe963..0b079ef7d02ed8fa4bb1afb57ef5557bf9f33c3c 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/location_button_test.cpp @@ -15,6 +15,7 @@ #include "location_button_test.h" #include +#include "cjson_utils.h" #include "sec_comp_log.h" #include "sec_comp_err.h" #include "test_common.h" @@ -51,11 +52,12 @@ void LocationButtonTest::TearDown() */ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -66,9 +68,10 @@ HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -79,16 +82,15 @@ HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton comp; ASSERT_TRUE(comp.FromJson(jsonComponent)); - - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = 0; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SecCompType::UNKNOWN_SC_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -99,31 +101,36 @@ HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); - jsonComponent[JsonTagConstants::JSON_RECT] = wrongJson; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + CJson* wrongJson = CreateJsonFromString("{"); + ASSERT_EQ(nullptr, wrongJson); + wrongJson = CreateJson(); // add a empty json obj instead + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_RECT, wrongJson)); + FreeJson(wrongJson); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& rectJson = jsonComponent[JsonTagConstants::JSON_RECT]; - rectJson[JsonTagConstants::JSON_RECT_X] = WRONG_TYPE; + CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_RECT); + ASSERT_NE(nullptr, rectJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_X, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_X] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_Y] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_Y, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_HEIGHT] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -134,31 +141,36 @@ HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false); - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = wrongJson; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + CJson* wrongJson = CreateJsonFromString("{"); + ASSERT_EQ(nullptr, wrongJson); + wrongJson = CreateJson(); // add a empty json obj instead + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT, wrongJson)); + FreeJson(wrongJson); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& rectJson = jsonComponent[JsonTagConstants::JSON_WINDOW_RECT]; - rectJson[JsonTagConstants::JSON_RECT_X] = WRONG_TYPE; + CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); + ASSERT_NE(nullptr, rectJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_X, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_X] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_Y] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_Y, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE; - rectJson[JsonTagConstants::JSON_RECT_HEIGHT] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_RECT_HEIGHT, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -169,22 +181,24 @@ HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = WRONG_TYPE; + CJson* rectJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, rectJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_FONT_SIZE_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = TestCommon::TEST_SIZE; - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_ICON_SIZE_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = TestCommon::TEST_SIZE; - sizeJson[JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectJson, JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(rectJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -195,19 +209,23 @@ HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = WRONG_TYPE; + CJson* sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + CJson* paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, paddingJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = TestCommon::TEST_DIMENSION; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, + JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -218,19 +236,23 @@ HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = WRONG_TYPE; + CJson* sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + CJson* paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, paddingJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = TestCommon::TEST_DIMENSION; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, + JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, WRONG_TYPE)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -241,21 +263,24 @@ HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp.FromJson(jsonComponent)); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, WRONG_TYPE }, - }; + CJson* borderTag = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(borderTag, JsonTagConstants::JSON_BORDER_WIDTH_TAG, WRONG_TYPE)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTag)); + FreeJson(borderTag); ASSERT_FALSE(comp.FromJson(jsonComponent)); TestCommon::BuildLocationComponentInfo(jsonComponent); - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE }, - }; + CJson* parentTag = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(parentTag, JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTag)); ASSERT_FALSE(comp.FromJson(jsonComponent)); + FreeJson(parentTag); + FreeJson(jsonComponent); } /** @@ -266,22 +291,26 @@ HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = WRONG_TYPE; + CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION; - styleJson[JsonTagConstants::JSON_ICON_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(styleJson, JsonTagConstants::JSON_BG_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -292,22 +321,26 @@ HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& colorJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG]; - colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = WRONG_TYPE; + CJson* colorJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); + ASSERT_NE(nullptr, colorJson); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = TestCommon::TEST_COLOR_RED; - colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + TestCommon::TEST_COLOR_RED)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); - colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = TestCommon::TEST_COLOR_BLUE; - colorJson[JsonTagConstants::JSON_BG_COLOR_TAG] = WRONG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + TestCommon::TEST_COLOR_BLUE)); + ASSERT_EQ(CJSON_SUCCESS, AddStringToJson(colorJson, JsonTagConstants::JSON_BG_COLOR_TAG, WRONG_TYPE)); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -318,35 +351,47 @@ HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1) */ HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = UNKNOWN_TEXT; + CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION; - styleJson[JsonTagConstants::JSON_ICON_TAG] = UNKNOWN_ICON; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::UNKNOWN_BG; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::CIRCLE; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::MAX_LABEL_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION; - styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::MAX_ICON_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::MAX_BG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -357,12 +402,15 @@ HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1) */ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - ASSERT_EQ(jsonComponent.dump(), button.ToJsonStr()); + std::string jsonDump = PackJsonToString(jsonComponent); + ASSERT_FALSE(jsonDump.empty()); + ASSERT_EQ(jsonDump, button.ToJsonStr()); + FreeJson(jsonComponent); } /** @@ -373,11 +421,12 @@ HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); LocationButton button; ASSERT_FALSE(button.CompareComponentBasicInfo(nullptr, true)); + FreeJson(jsonComponent); } /** @@ -391,7 +440,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) LocationButton button1; LocationButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -411,6 +460,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } /** @@ -421,7 +471,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1) */ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); LocationButton comp1; TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -476,6 +526,7 @@ HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1) comp1.iconColor_.value = TestCommon::TEST_COLOR_BLUE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); + FreeJson(jsonComponent); } /** @@ -488,9 +539,10 @@ HWTEST_F(LocationButtonTest, CompareLocationButton003, TestSize.Level1) { LocationButton button1; SaveButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, false)); + FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp index d79349ddb51e352852008fe32485db266f0a9f43..bc34c99a42153aa8e4420486d1dd0d2fcb4737dd 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/paste_button_test.cpp @@ -15,6 +15,7 @@ #include "paste_button_test.h" #include +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" #include "test_common.h" @@ -50,35 +51,46 @@ void PasteButtonTest::TearDown() */ HWTEST_F(PasteButtonTest, IsParamValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); PasteButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = UNKNOWN_TEXT; + CJson* styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = PasteDesc::PASTE; - styleJson[JsonTagConstants::JSON_ICON_TAG] = UNKNOWN_ICON; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(PasteDesc::PASTE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = PasteIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::UNKNOWN_BG; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::CIRCLE; - styleJson[JsonTagConstants::JSON_TEXT_TAG] = PasteDesc::MAX_LABEL_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_TEXT_TAG] = PasteDesc::PASTE; - styleJson[JsonTagConstants::JSON_ICON_TAG] = PasteIcon::MAX_ICON_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(PasteDesc::PASTE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(PasteIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - styleJson[JsonTagConstants::JSON_ICON_TAG] = PasteIcon::LINE_ICON; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::MAX_BG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -92,7 +104,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) PasteButton button1; PasteButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -112,6 +124,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } /** @@ -122,7 +135,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton001, TestSize.Level1) */ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); PasteButton comp1; TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -145,6 +158,7 @@ HWTEST_F(PasteButtonTest, ComparePasteButton002, TestSize.Level1) comp1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); + FreeJson(jsonComponent); } /** @@ -157,9 +171,10 @@ HWTEST_F(PasteButtonTest, ComparePasteButton003, TestSize.Level1) { PasteButton button1; SaveButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildPasteComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp index 6c710884bed298b7f7b49fd0b2e02fdc9d0dda24..bdd5df233799a906becfeaba7ee1d2981bd477fd 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/save_button_test.cpp @@ -15,6 +15,7 @@ #include "save_button_test.h" #include +#include "cjson_utils.h" #include "sec_comp_log.h" #include "sec_comp_err.h" #include "test_common.h" @@ -50,35 +51,46 @@ void SaveButtonTest::TearDown() */ HWTEST_F(SaveButtonTest, IsParamValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); SaveButton button; ASSERT_TRUE(button.FromJson(jsonComponent)); - auto& stylesJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - stylesJson[JsonTagConstants::JSON_TEXT_TAG] = UNKNOWN_TEXT; + CJson* stylesJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, stylesJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, UNKNOWN_TEXT)); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_TEXT_TAG] = SaveDesc::DOWNLOAD; - stylesJson[JsonTagConstants::JSON_ICON_TAG] = UNKNOWN_ICON; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, UNKNOWN_ICON)); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_ICON_TAG] = SaveIcon::LINE_ICON; - stylesJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::UNKNOWN_BG; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::UNKNOWN_BG))); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::CIRCLE; - stylesJson[JsonTagConstants::JSON_TEXT_TAG] = SaveDesc::MAX_LABEL_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::MAX_LABEL_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_TEXT_TAG] = SaveDesc::DOWNLOAD; - stylesJson[JsonTagConstants::JSON_ICON_TAG] = SaveIcon::MAX_ICON_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::MAX_ICON_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); - stylesJson[JsonTagConstants::JSON_ICON_TAG] = SaveIcon::LINE_ICON; - stylesJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::MAX_BG_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(stylesJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::MAX_BG_TYPE))); ASSERT_FALSE(button.FromJson(jsonComponent)); + FreeJson(jsonComponent); } /** @@ -92,7 +104,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) SaveButton button1; SaveButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); @@ -112,6 +124,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) button1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } /** @@ -122,7 +135,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton001, TestSize.Level1) */ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); SaveButton comp1; TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(comp1.FromJson(jsonComponent)); @@ -145,6 +158,7 @@ HWTEST_F(SaveButtonTest, CompareSaveButton002, TestSize.Level1) comp1.bg_ = SecCompBackground::CIRCLE; ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true)); + FreeJson(jsonComponent); } /** @@ -157,9 +171,10 @@ HWTEST_F(SaveButtonTest, CompareSaveButton003, TestSize.Level1) { SaveButton button1; PasteButton button2; - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonComponent); ASSERT_TRUE(button1.FromJson(jsonComponent)); ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true)); + FreeJson(jsonComponent); } diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp index 4c82ffc13a6121d092f9fff5b9818f6fa2a19715..53cd8ea831ae96009c3fd8f6c8ed7c4b6cea79c7 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_kit_test.cpp @@ -14,6 +14,7 @@ */ #include "sec_comp_kit_test.h" +#include "cjson_utils.h" #include "location_button.h" #define private public #include "sec_comp_caller_authorization.h" @@ -109,10 +110,12 @@ HWTEST_F(SecCompKitTest, ExceptCall001, TestSize.Level1) comp.rect_.width_ = TestCommon::TEST_COORDINATE; comp.rect_.height_ = TestCommon::TEST_COORDINATE; - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); comp.ToJson(jsonRes); int32_t scId = -1; - std::string jsonStr = jsonRes.dump(); + std::string jsonStr = PackJsonToString(jsonRes); + FreeJson(jsonRes); + ASSERT_NE(SC_OK, SecCompKit::RegisterSecurityComponent(LOCATION_COMPONENT, jsonStr, scId)); ASSERT_EQ(-1, scId); ASSERT_NE(SC_OK, SecCompKit::UpdateSecurityComponent(scId, jsonStr)); @@ -183,9 +186,10 @@ HWTEST_F(SecCompKitTest, TestCallerCheck002, TestSize.Level1) */ HWTEST_F(SecCompKitTest, RegisterWithoutCallback001, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); int32_t scId; #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE diff --git a/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp b/interfaces/inner_api/security_component/test/unittest/src/sec_comp_register_callback_test.cpp index 6782c7f9533435c022f394397402f3ba73cc006e..88a5ba8e029536399a78e9c4083f6606ab9556ea 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" #define private public @@ -104,9 +105,10 @@ void SecCompRegisterCallbackTest::TearDown() */ HWTEST_F(SecCompRegisterCallbackTest, RegisterWithoutPreprocess001, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); SecCompEnhanceAdapter::InitEnhanceHandler(SEC_COMP_ENHANCE_CLIENT_INTERFACE); int32_t scId; @@ -130,9 +132,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterWithoutPreprocess001, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -151,9 +155,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent001, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = -1; @@ -178,9 +184,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent002, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -198,11 +206,12 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent003, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); - int32_t scId; + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent clickInfo = { @@ -233,11 +242,12 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent004, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); - int32_t scId; + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent clickInfo = { @@ -266,9 +276,11 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent005, TestSize.Lev */ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; setuid(100); EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); @@ -283,11 +295,12 @@ HWTEST_F(SecCompRegisterCallbackTest, RegisterSecurityComponent006, TestSize.Lev HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); - int32_t scId; + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent clickInfo = { @@ -320,9 +333,11 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent001, Test HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -357,9 +372,11 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent002, Test */ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -389,9 +406,11 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportSecurityComponentClickEvent003, Test HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildLocationComponentInfo(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + g_probe.mockComponentInfo = locationInfo; g_probe.mockRes = 0; @@ -426,9 +445,11 @@ HWTEST_F(SecCompRegisterCallbackTest, ReportClickWithoutHmac001, TestSize.Level1 HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); @@ -466,11 +487,12 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission001, TestSize.Level1) HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); - int32_t scId; + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent clickInfo = { @@ -501,11 +523,12 @@ HWTEST_F(SecCompRegisterCallbackTest, VerifySavePermission002, TestSize.Level1) HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.Level1) { system("param set sec.comp.enhance 1"); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); - int32_t scId; + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; EXPECT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); uint8_t data[TestCommon::MAX_HMAC_SIZE] = { 0 }; struct SecCompClickEvent clickInfo = { @@ -537,9 +560,10 @@ HWTEST_F(SecCompRegisterCallbackTest, UnregisterSecurityComponent001, TestSize.L */ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); int32_t scId; ASSERT_EQ(SC_OK, SecCompKit::RegisterSecurityComponent(SAVE_COMPONENT, saveInfo, scId)); ASSERT_NE(-1, scId); @@ -555,11 +579,12 @@ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent001, TestSize.Level */ HWTEST_F(SecCompRegisterCallbackTest, UpdateSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); TestCommon::BuildSaveComponentInfo(jsonRes); - std::string saveInfo = jsonRes.dump(); - int32_t scId; + std::string saveInfo = PackJsonToString(jsonRes); + FreeJson(jsonRes); + int32_t scId; ASSERT_EQ(0, SetSelfTokenID(TestCommon::HAP_TOKEN_ID)); ASSERT_EQ(SC_OK, SecCompKit::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..0d0ba8df215bdd4518622f0f3b93e7f81c27784b 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.cpp @@ -13,158 +13,110 @@ * limitations under the License. */ #include "test_common.h" +#include "cjson_utils.h" namespace OHOS { namespace Security { namespace SecurityComponent { -void TestCommon::BuildLocationComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::BuildLocationComponentInfo(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); +} - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } - }; +void TestCommon::BuildSaveComponentInfo(CJson* jsonComponent) +{ + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SAVE_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); } -void TestCommon::BuildSaveComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::BuildPasteComponentInfo(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, PASTE_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } - }; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); +} - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; +void TestCommon::AddRectToJson(CJson* jsonComponent, const std::string key) +{ + CJson* rectChild = CreateJson(); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE); + AddObjToJson(jsonComponent, key, rectChild); + FreeJson(rectChild); } -void TestCommon::BuildPasteComponentInfo(nlohmann::json& jsonComponent) +void TestCommon::GenerateCommon(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, TestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, TestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_NODE_ID, 0); + AddRectToJson(jsonComponent, JsonTagConstants::JSON_RECT); + AddRectToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); + + CJson* jsonPadding = CreateJson(); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, TestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_RIGHT_TAG, TestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, TestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_LEFT_TAG, TestCommon::TEST_DIMENSION); + + CJson* sizeTagChild = CreateJson(); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE); + AddObjToJson(sizeTagChild, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG, sizeTagChild); + FreeJson(jsonPadding); + FreeJson(sizeTagChild); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, TestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; + CJson* colorsTagChild = CreateJson(); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild); + FreeJson(colorsTagChild); - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, TestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, TestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, TestCommon::TEST_COLOR_YELLOW } - }; + CJson* borderTagChild = CreateJson(); + AddDoubleToJson(borderTagChild, JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTagChild); + FreeJson(borderTagChild); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, TestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, - { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; + CJson* parentTagChild = CreateJson(); + AddBoolToJson(parentTagChild, JsonTagConstants::JSON_PARENT_EFFECT_TAG, false); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTagChild); + FreeJson(parentTagChild); } } // namespace SecurityComponent } // namespace Security diff --git a/interfaces/inner_api/security_component/test/unittest/src/test_common.h b/interfaces/inner_api/security_component/test/unittest/src/test_common.h index b42b47ae448fcf9801ad66b04632270e5637fb4d..f890828bcb9270a9884bda829ee3698e089f5a6a 100644 --- a/interfaces/inner_api/security_component/test/unittest/src/test_common.h +++ b/interfaces/inner_api/security_component/test/unittest/src/test_common.h @@ -14,6 +14,9 @@ */ #ifndef TEST_COMMON_H #define TEST_COMMON_H +#include + +#include "cjson_utils.h" #include "location_button.h" #include "paste_button.h" #include "save_button.h" @@ -23,7 +26,7 @@ namespace Security { namespace SecurityComponent { class TestCommon { public: - static constexpr float TEST_SIZE = 100.0; + static constexpr double TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIMENSION = 100.0; static constexpr uint32_t TEST_COLOR = 0xffffffff; @@ -36,9 +39,12 @@ public: static constexpr uint32_t MAX_HMAC_SIZE = 64; static constexpr size_t MAX_CALLER_SIZE = 10; - static void BuildLocationComponentInfo(nlohmann::json& jsonComponent); - static void BuildSaveComponentInfo(nlohmann::json& jsonComponent); - static void BuildPasteComponentInfo(nlohmann::json& jsonComponent); + static void BuildLocationComponentInfo(CJson* jsonComponent); + static void BuildSaveComponentInfo(CJson* jsonComponent); + static void BuildPasteComponentInfo(CJson* jsonComponent); +private: + static void AddRectToJson(CJson* jsonComponent, const std::string key); + static void GenerateCommon(CJson* jsonComponent); }; } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/BUILD.gn b/services/security_component_service/sa/BUILD.gn index a906600037d5a44d858490535991d2b5faa0080c..1a14a816f0a4095330005f93194d035540a1cc19 100644 --- a/services/security_component_service/sa/BUILD.gn +++ b/services/security_component_service/sa/BUILD.gn @@ -84,6 +84,7 @@ ohos_shared_library("security_component_service") { "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..aa098aae6bc701463a9e27c0f0233cdab0089207 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.cpp +++ b/services/security_component_service/sa/sa_main/first_use_dialog.cpp @@ -14,13 +14,16 @@ */ #include "first_use_dialog.h" +#include #include #include +#include #include #include #include #include "ability_manager_client.h" #include "accesstoken_kit.h" +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" #include "want_params_wrapper.h" @@ -111,43 +114,39 @@ void FirstUseDialog::WriteCfgContent(const std::string& content) out.close(); } -bool FirstUseDialog::ParseRecord(nlohmann::json& jsonRes, - AccessToken::AccessTokenID& id, uint64_t& type) +bool FirstUseDialog::ParseRecord(CJson* jsonRes, + AccessToken::AccessTokenID& id, uint32_t& type) { - if (jsonRes.find(TOKEN_ID_TAG) == jsonRes.end() || - !jsonRes.at(TOKEN_ID_TAG).is_number()) { + if (GetUnsignedIntFromJson(jsonRes, TOKEN_ID_TAG, id) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "parse TokenId failed."); return false; } - id = jsonRes.at(TOKEN_ID_TAG).get(); if (id == AccessToken::INVALID_TOKENID) { SC_LOG_ERROR(LABEL, "TokenId is not invalid."); return false; } - if (jsonRes.find(COMP_TYPE_TAG) == jsonRes.end() || - !jsonRes.at(COMP_TYPE_TAG).is_number()) { + if (GetUnsignedIntFromJson(jsonRes, COMP_TYPE_TAG, type) != CJSON_SUCCESS) { SC_LOG_ERROR(LABEL, "parse CompType failed."); return false; } - type = jsonRes.at(COMP_TYPE_TAG).get(); return true; } -void FirstUseDialog::ParseRecords(nlohmann::json& jsonRes) +void FirstUseDialog::ParseRecords(CJson* jsonRes) { std::unique_lock lock(useMapMutex_); - if (jsonRes.find(FIRST_USE_RECORD_TAG) == jsonRes.end() || - !jsonRes.at(FIRST_USE_RECORD_TAG).is_array()) { + CJson* recordListJson = GetArrayFromJson(jsonRes, FIRST_USE_RECORD_TAG); + if (recordListJson == nullptr) { SC_LOG_ERROR(LABEL, "parse tag failed."); return; } - nlohmann::json recordListJson = jsonRes.at(FIRST_USE_RECORD_TAG); - for (auto& recordJson : recordListJson) { + CJson* iter = nullptr; + cJSON_ArrayForEach(iter, recordListJson) { AccessToken::AccessTokenID id; - uint64_t type; - if (!ParseRecord(recordJson, id, type)) { + uint32_t type; + if (!ParseRecord(iter, id, type)) { SC_LOG_ERROR(LABEL, "parse record failed."); return; } @@ -167,13 +166,14 @@ void FirstUseDialog::LoadFirstUseRecord(void) return; } - nlohmann::json jsonRes = nlohmann::json::parse(content, nullptr, false); - if (jsonRes.is_discarded()) { + CJson* jsonRes = CreateJsonFromString(content); + if (jsonRes == nullptr) { SC_LOG_ERROR(LABEL, "cfg info format is invalid"); return; } ParseRecords(jsonRes); + FreeJson(jsonRes); } void FirstUseDialog::SaveFirstUseRecord(void) @@ -193,26 +193,21 @@ void FirstUseDialog::SaveFirstUseRecord(void) } } - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); { std::unique_lock lock(useMapMutex_); - nlohmann::json recordsJson; - for (auto iter = firstUseMap_.begin(); iter != firstUseMap_.end(); ++iter) { - AccessToken::AccessTokenID id = iter->first; - AccessToken::HapTokenInfo info; - if (AccessToken::AccessTokenKit::GetHapTokenInfo(id, info) != AccessToken::RET_SUCCESS) { - SC_LOG_INFO(LABEL, "token id %{public}d is not exist, do not update it.", id); - continue; - } - nlohmann::json recordJson; - recordJson[TOKEN_ID_TAG] = id; - recordJson[COMP_TYPE_TAG] = iter->second; - recordsJson.emplace_back(recordJson); + if (!GenerateRecordsJson(jsonRes)) { + FreeJson(jsonRes); + return; } - - jsonRes[FIRST_USE_RECORD_TAG] = recordsJson; } - WriteCfgContent(jsonRes.dump()); + std::string jsonStr = PackJsonToString(jsonRes); + FreeJson(jsonRes); + if (jsonStr.empty()) { + SC_LOG_ERROR(LABEL, "json: PackJsonToString return empty."); + return; + } + WriteCfgContent(jsonStr); } void FirstUseDialog::StartDialogAbility(SecCompType type, sptr callerToken) @@ -294,6 +289,45 @@ void FirstUseDialog::Init(std::shared_ptr secHandler) secHandler_ = secHandler; LoadFirstUseRecord(); } + +bool FirstUseDialog::GenerateRecordsJson(CJson* jsonRes) +{ + CJson* recordsJson = CreateJsonArray(); + for (auto iter = firstUseMap_.begin(); iter != firstUseMap_.end(); ++iter) { + AccessToken::AccessTokenID id = iter->first; + AccessToken::HapTokenInfo info; + if (AccessToken::AccessTokenKit::GetHapTokenInfo(id, info) != AccessToken::RET_SUCCESS) { + SC_LOG_INFO(LABEL, "token id %{public}d is not exist, do not update it.", id); + continue; + } + CJson* recordJson = CreateJson(); + if (AddUnsignedIntToJson(recordJson, TOKEN_ID_TAG, id) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", TOKEN_ID_TAG.c_str()); + FreeJson(recordJson); + FreeJson(recordsJson); + return false; + } + if (AddUnsignedIntToJson(recordJson, COMP_TYPE_TAG, iter->second) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", COMP_TYPE_TAG.c_str()); + FreeJson(recordJson); + FreeJson(recordsJson); + return false; + } + if (AddObjToArray(recordsJson, recordJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: add array recordsJson error."); + FreeJson(recordJson); + FreeJson(recordsJson); + return false; + } + } + if (AddObjToJson(jsonRes, FIRST_USE_RECORD_TAG, recordsJson) != CJSON_SUCCESS) { + SC_LOG_ERROR(LABEL, "json: %{public}s tag invalid.", FIRST_USE_RECORD_TAG.c_str()); + FreeJson(recordsJson); + return false; + } + FreeJson(recordsJson); + return true; +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS diff --git a/services/security_component_service/sa/sa_main/first_use_dialog.h b/services/security_component_service/sa/sa_main/first_use_dialog.h index 753500d9b971b24d46fdfdc6b832221050e8db40..c62cd7843d030b5a5102c0e64cef0ec9aabeef32 100644 --- a/services/security_component_service/sa/sa_main/first_use_dialog.h +++ b/services/security_component_service/sa/sa_main/first_use_dialog.h @@ -21,7 +21,7 @@ #include #include "access_token.h" #include "iremote_object.h" -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_info.h" #include "sec_event_handler.h" @@ -42,16 +42,17 @@ private: bool IsCfgFileValid(void); bool ReadCfgContent(std::string& content); void WriteCfgContent(const std::string& content); - bool ParseRecord(nlohmann::json& jsonRes, - AccessToken::AccessTokenID& id, uint64_t& type); - void ParseRecords(nlohmann::json& jsonRes); + bool ParseRecord(CJson* jsonRes, + AccessToken::AccessTokenID& id, uint32_t& type); + void ParseRecords(CJson* jsonRes); void LoadFirstUseRecord(void); void SaveFirstUseRecord(void); void StartDialogAbility(SecCompType type, sptr callerToken); void SendSaveEventHandler(void); + bool GenerateRecordsJson(CJson* jsonRes); std::mutex useMapMutex_; - std::unordered_map firstUseMap_; + std::unordered_map firstUseMap_; std::shared_ptr secHandler_; }; } // namespace SecurityComponentEnhance diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 4e4f68e126a5cf1f573fecdcbe837090c4f58a0a..b9cec8470627f5e5fc8871e4e899f09f84f3e62e 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -47,7 +47,7 @@ void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, float scale) comp->rect_.x_, comp->rect_.y_, comp->rect_.width_, comp->rect_.height_); } -SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const nlohmann::json& jsonComponent) +SecCompBase* SecCompInfoHelper::ParseComponent(SecCompType type, const CJson* jsonComponent) { SecCompBase* comp = nullptr; switch (type) { diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h index ba8006bef2f39f92488189e67fdcb9705e83aec3..8f4200bac7388ce66a8de926d883d2900b36884d 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.h +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.h @@ -16,7 +16,7 @@ #define I_SECURITY_COMPONENT_INFO_HELPER_H #include "accesstoken_kit.h" -#include "nlohmann/json.hpp" +#include "cjson_utils.h" #include "sec_comp_base.h" #include "sec_comp_info.h" #include "sec_comp_perm_manager.h" @@ -25,7 +25,7 @@ namespace OHOS { namespace Security { namespace SecurityComponent { template -T* ConstructComponent(const nlohmann::json& jsonComponent) +T* ConstructComponent(const CJson* jsonComponent) { T *componentPtr = new (std::nothrow)T(); if ((componentPtr != nullptr) && !componentPtr->FromJson(jsonComponent)) { @@ -37,7 +37,7 @@ T* ConstructComponent(const nlohmann::json& jsonComponent) class SecCompInfoHelper { public: - static SecCompBase* ParseComponent(SecCompType type, const nlohmann::json& jsonComponent); + static SecCompBase* ParseComponent(SecCompType type, const CJson* jsonComponent); static int32_t GrantTempPermission(AccessToken::AccessTokenID tokenId, const std::shared_ptr& componentInfo); static bool CheckComponentValid(SecCompBase* comp); diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.cpp b/services/security_component_service/sa/sa_main/sec_comp_manager.cpp index f4bd825387476f25964c1c26634db2e2e2c0abb3..b7e12615de91b49a4029165e565911f06e966b81 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" @@ -155,7 +157,7 @@ SecCompEntity* SecCompManager::GetSecurityComponentFromList(int32_t pid, int32_t bool SecCompManager::IsForegroundCompExist() { return std::any_of(componentMap_.begin(), componentMap_.end(), [](const auto & iter) { - return (iter.second.isForeground) && (iter.second.compList.size() > 0); + return iter.second.isForeground; }); } @@ -285,8 +287,31 @@ void SecCompManager::SendCheckInfoEnhanceSysEvent(int32_t scId, } } +int32_t SecCompManager::AddSecurityComponentProcess(const SecCompCallerInfo& caller) +{ + DelayExitTask::GetInstance().Stop(); + { + OHOS::Utils::UniqueWriteGuard lk(this->componentInfoLock_); + if (isSaExit_) { + SC_LOG_ERROR(LABEL, "SA is exiting, retry..."); + return SC_SERVICE_ERROR_SERVICE_NOT_EXIST; + } + + auto iter = componentMap_.find(caller.pid); + if (iter != componentMap_.end()) { + ProcessCompInfos newProcess; + newProcess.isForeground = true; + newProcess.tokenId = caller.tokenId; + componentMap_[caller.pid] = newProcess; + } + SecCompEnhanceAdapter::EnableInputEnhance(); + } + SecCompEnhanceAdapter::AddSecurityComponentProcess(caller.pid); + return SC_OK; +} + int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) + const CJson* jsonComponent, const SecCompCallerInfo& caller, int32_t& scId) { DelayExitTask::GetInstance().Stop(); SC_LOG_DEBUG(LABEL, "PID: %{public}d, register security component", caller.pid); @@ -326,7 +351,7 @@ int32_t SecCompManager::RegisterSecurityComponent(SecCompType type, return ret; } -int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, +int32_t SecCompManager::UpdateSecurityComponent(int32_t scId, const CJson* jsonComponent, const SecCompCallerInfo& caller) { SC_LOG_DEBUG(LABEL, "PID: %{public}d, update security component", caller.pid); @@ -376,7 +401,7 @@ int32_t SecCompManager::UnregisterSecurityComponent(int32_t scId, const SecCompC } int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller) + const CJson* jsonComponent, const SecCompCallerInfo& caller) { SC_LOG_DEBUG(LABEL, "PID: %{public}d, Check security component", caller.pid); SecCompBase* report = SecCompInfoHelper::ParseComponent(sc->GetType(), jsonComponent); @@ -411,7 +436,7 @@ int32_t SecCompManager::CheckClickSecurityComponentInfo(SecCompEntity* sc, int32 } int32_t SecCompManager::ReportSecurityComponentClickEvent(int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller, + const CJson* jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& clickInfo, sptr callerToken) { if (malicious_.IsInMaliciousAppList(caller.pid, caller.uid)) { @@ -462,10 +487,12 @@ void SecCompManager::DumpSecComp(std::string& dumpStr) for (auto iter = componentMap_.begin(); iter != componentMap_.end(); ++iter) { dumpStr.append("pid:" + std::to_string(iter->first) + "\n"); for (auto compIter = iter->second.compList.begin(); compIter != iter->second.compList.end(); compIter ++) { - nlohmann::json json; + CJson* json = CreateJson(); compIter->GetComponentInfo()->ToJson(json); + std::string jsonStr = PackJsonToString(json); dumpStr.append(" scId:" + std::to_string(compIter->GetScId()) + - ", isGrant:" + std::to_string(compIter->IsGrant()) + ", " + json.dump() + "\n"); + ", isGrant:" + std::to_string(compIter->IsGrant()) + ", " + jsonStr + "\n"); + FreeJson(json); } } } diff --git a/services/security_component_service/sa/sa_main/sec_comp_manager.h b/services/security_component_service/sa/sa_main/sec_comp_manager.h index d6a79fa37ccd5af56de1c75cd4e56391ac03c31e..b2611d8ca60c0d1dbce9064500f8fd7a1dfd32c7 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_manager.h +++ b/services/security_component_service/sa/sa_main/sec_comp_manager.h @@ -22,6 +22,7 @@ #include #include "accesstoken_kit.h" #include "app_state_observer.h" +#include "cjson_utils.h" #include "first_use_dialog.h" #include "nocopyable.h" #include "rwlock.h" @@ -51,12 +52,12 @@ public: static SecCompManager& GetInstance(); virtual ~SecCompManager() = default; - int32_t RegisterSecurityComponent(SecCompType type, const nlohmann::json& jsonComponent, + int32_t RegisterSecurityComponent(SecCompType type, const CJson* jsonComponent, const SecCompCallerInfo& caller, int32_t& scId); - int32_t UpdateSecurityComponent(int32_t scId, const nlohmann::json& jsonComponent, + int32_t UpdateSecurityComponent(int32_t scId, const CJson* jsonComponent, const SecCompCallerInfo& caller); int32_t UnregisterSecurityComponent(int32_t scId, const SecCompCallerInfo& caller); - int32_t ReportSecurityComponentClickEvent(int32_t scId, const nlohmann::json& jsonComponent, + int32_t ReportSecurityComponentClickEvent(int32_t scId, const CJson* jsonComponent, const SecCompCallerInfo& caller, const SecCompClickEvent& clickInfo, sptr callerToken); void NotifyProcessForeground(int32_t pid); void NotifyProcessBackground(int32_t pid); @@ -65,6 +66,7 @@ public: bool Initialize(); void ExitSaProcess(); void ExitWhenAppMgrDied(); + int32_t AddSecurityComponentProcess(const SecCompCallerInfo& caller); private: SecCompManager(); @@ -74,7 +76,7 @@ private: int32_t DeleteSecurityComponentFromList(int32_t pid, int32_t scId); SecCompEntity* GetSecurityComponentFromList(int32_t pid, int32_t scId); int32_t CheckClickSecurityComponentInfo(SecCompEntity* sc, int32_t scId, - const nlohmann::json& jsonComponent, const SecCompCallerInfo& caller); + const CJson* jsonComponent, const SecCompCallerInfo& caller); void SendCheckInfoEnhanceSysEvent(int32_t scId, SecCompType type, const std::string& scene, int32_t res); int32_t CreateScId(); diff --git a/services/security_component_service/sa/sa_main/sec_comp_service.cpp b/services/security_component_service/sa/sa_main/sec_comp_service.cpp index 19451bd5171ad277a927cf138f125cd579e9bd67..7ea4ceb6acce105ecc968fa2d279690fb58a16b3 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_service.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_service.cpp @@ -14,9 +14,11 @@ */ #include "sec_comp_service.h" +#include #include #include "app_mgr_death_recipient.h" +#include "cjson_utils.h" #include "hisysevent.h" #include "hitrace_meter.h" #include "ipc_skeleton.h" @@ -84,8 +86,6 @@ void SecCompService::OnStop() SC_LOG_INFO(LABEL, "Stop service"); state_ = ServiceRunningState::STATE_NOT_START; UnregisterAppStateObserver(); - iAppMgr_ = nullptr; - appStateObserver_ = nullptr; } bool SecCompService::RegisterAppStateObserver() @@ -168,15 +168,14 @@ bool SecCompService::GetCallerInfo(SecCompCallerInfo& caller) } int32_t SecCompService::ParseParams(const std::string& componentInfo, - SecCompCallerInfo& caller, nlohmann::json& jsonRes) + SecCompCallerInfo& caller, CJson*& jsonRes) { if (!GetCallerInfo(caller)) { SC_LOG_ERROR(LABEL, "Check caller failed"); return SC_SERVICE_ERROR_VALUE_INVALID; } - - jsonRes = nlohmann::json::parse(componentInfo, nullptr, false); - if (jsonRes.is_discarded()) { + jsonRes = CreateJsonFromString(componentInfo); + if (jsonRes == nullptr) { SC_LOG_ERROR(LABEL, "component info invalid %{public}s", componentInfo.c_str()); return SC_SERVICE_ERROR_VALUE_INVALID; } @@ -188,25 +187,30 @@ int32_t SecCompService::RegisterSecurityComponent(SecCompType type, { StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentRegister"); SecCompCallerInfo caller; - nlohmann::json jsonRes; + CJson* jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } int32_t res = SecCompManager::GetInstance().RegisterSecurityComponent(type, jsonRes, caller, scId); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + FreeJson(jsonRes); return res; } int32_t SecCompService::UpdateSecurityComponent(int32_t scId, const std::string& componentInfo) { SecCompCallerInfo caller; - nlohmann::json jsonRes; + CJson* jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { + FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } - return SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonRes, caller); + int32_t ret = SecCompManager::GetInstance().UpdateSecurityComponent(scId, jsonRes, caller); + FreeJson(jsonRes); + return ret; } int32_t SecCompService::UnregisterSecurityComponent(int32_t scId) @@ -225,17 +229,29 @@ int32_t SecCompService::ReportSecurityComponentClickEvent(int32_t scId, { StartTrace(HITRACE_TAG_ACCESS_CONTROL, "SecurityComponentClick"); SecCompCallerInfo caller; - nlohmann::json jsonRes; + CJson* jsonRes = nullptr; if (ParseParams(componentInfo, caller, jsonRes) != SC_OK) { FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + FreeJson(jsonRes); return SC_SERVICE_ERROR_VALUE_INVALID; } int32_t res = SecCompManager::GetInstance().ReportSecurityComponentClickEvent(scId, jsonRes, caller, clickInfo, callerToken); FinishTrace(HITRACE_TAG_ACCESS_CONTROL); + FreeJson(jsonRes); return res; } +int32_t SecCompService::PreRegisterSecCompProcess() +{ + SecCompCallerInfo caller; + if (!GetCallerInfo(caller)) { + SC_LOG_ERROR(LABEL, "Check caller failed"); + return SC_SERVICE_ERROR_VALUE_INVALID; + } + return SecCompManager::GetInstance().AddSecurityComponentProcess(caller); +} + bool SecCompService::VerifySavePermission(AccessToken::AccessTokenID tokenId) { return SecCompPermManager::GetInstance().VerifySavePermission(tokenId); 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 f9b43de3771938159d1c114e4319f2c5af15dccb..a91377c540d2bb8d6c99a5b1cb9d3b1a0f5c3f6c 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" @@ -47,11 +47,12 @@ public: const std::string& componentInfo, const SecCompClickEvent& clickInfo, sptr callerToken) override; bool VerifySavePermission(AccessToken::AccessTokenID tokenId) override; sptr GetEnhanceRemoteObject() override; + int32_t PreRegisterSecCompProcess() override; int Dump(int fd, const std::vector& args) override; private: - int32_t ParseParams(const std::string& componentInfo, SecCompCallerInfo& caller, nlohmann::json& jsonRes); + int32_t ParseParams(const std::string& componentInfo, SecCompCallerInfo& caller, CJson*& jsonRes); bool Initialize() const; bool RegisterAppStateObserver(); void UnregisterAppStateObserver(); diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp index 85a36b49108efcd700eb57c49ad3e103b41d0007..417e512199979f6403408d53c7ef0352b87e117e 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.cpp @@ -205,6 +205,16 @@ int32_t SecCompStub::GetEnhanceRemoteObjectInner(MessageParcel& data, MessagePar return SC_OK; } +int32_t SecCompStub::PreRegisterSecCompProcessInner(MessageParcel& data, MessageParcel& reply) +{ + int32_t res = this->PreRegisterSecCompProcess(); + if (!reply.WriteInt32(res)) { + SC_LOG_ERROR(LABEL, "preRegister write result fail"); + return SC_SERVICE_ERROR_PARCEL_OPERATE_FAIL; + } + return SC_OK; +} + bool SecCompStub::IsMediaLibraryCalling() { int32_t uid = IPCSkeleton::GetCallingUid(); @@ -236,6 +246,9 @@ SecCompStub::SecCompStub() requestFuncMap_[static_cast( SecurityComponentServiceInterfaceCode::GET_SECURITY_COMPONENT_ENHANCE_OBJECT)] = &SecCompStub::GetEnhanceRemoteObjectInner; + requestFuncMap_[static_cast( + SecurityComponentServiceInterfaceCode::PRE_REGISTER_PROCESS)] = + &SecCompStub::PreRegisterSecCompProcessInner; } SecCompStub::~SecCompStub() diff --git a/services/security_component_service/sa/sa_main/sec_comp_stub.h b/services/security_component_service/sa/sa_main/sec_comp_stub.h index 8b4262d9b994278072a41d68faf57a64d773bfba..57ce8b47fd3a86ac2de173b4645525dfd4796491 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_stub.h +++ b/services/security_component_service/sa/sa_main/sec_comp_stub.h @@ -37,6 +37,7 @@ private: int32_t ReportSecurityComponentClickEventInner(MessageParcel& data, MessageParcel& reply); int32_t VerifySavePermissionInner(MessageParcel& data, MessageParcel& reply); int32_t GetEnhanceRemoteObjectInner(MessageParcel& data, MessageParcel& reply); + int32_t PreRegisterSecCompProcessInner(MessageParcel& data, MessageParcel& reply); bool IsMediaLibraryCalling(); using RequestFuncType = int32_t (SecCompStub::*)(MessageParcel& data, MessageParcel& reply); diff --git a/services/security_component_service/sa/test/BUILD.gn b/services/security_component_service/sa/test/BUILD.gn index d4cf894f041e516774dfcd0a036be03813e1a5c7..4c42191bc6b9fcab3fd054bffb2eeb3a9d95d1a6 100644 --- a/services/security_component_service/sa/test/BUILD.gn +++ b/services/security_component_service/sa/test/BUILD.gn @@ -27,10 +27,10 @@ ohos_unittest("sec_comp_service_test") { "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/include", - "//third_party/json/include", ] sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", @@ -77,6 +77,7 @@ ohos_unittest("sec_comp_service_test") { "ability_runtime:ability_manager", "ability_runtime:runtime", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "eventhandler:libeventhandler", "graphic_2d:librender_service_client", @@ -105,10 +106,10 @@ ohos_unittest("sec_comp_service_mock_test") { "${sec_comp_root_dir}/interfaces/inner_api/security_component/include", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main", "${sec_comp_root_dir}/services/security_component_service/sa/test/mock/include", - "//third_party/json/include", ] sources = [ + "${sec_comp_root_dir}/interfaces/inner_api/security_component/src/cjson_utils.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_mgr_death_recipient.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/app_state_observer.cpp", "${sec_comp_root_dir}/services/security_component_service/sa/sa_main/delay_exit_task.cpp", @@ -145,6 +146,7 @@ ohos_unittest("sec_comp_service_mock_test") { "ability_runtime:ability_manager", "ability_runtime:runtime", "access_token:libtoken_setproc", + "cJSON:cjson", "c_utils:utils", "eventhandler:libeventhandler", "graphic_2d:librender_service_client", diff --git a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp index d6259e3c60a1aa944f0fc59a320bbec6d1574d2f..17f75611ce4159398a4e7c28bddda307d4d0074e 100644 --- a/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp +++ b/services/security_component_service/sa/test/mock/src/sec_comp_enhance_adapter.cpp @@ -14,6 +14,7 @@ */ #include "sec_comp_enhance_adapter.h" +#include "cjson_utils.h" #include "sec_comp_err.h" #include "sec_comp_log.h" @@ -94,7 +95,7 @@ void SecCompEnhanceAdapter::NotifyProcessDied(int32_t pid) } int32_t SecCompEnhanceAdapter::CheckComponentInfoEnhnace(int32_t pid, - std::shared_ptr& compInfo, const nlohmann::json& jsonComponent) + std::shared_ptr& compInfo, const CJson* jsonComponent) { SC_LOG_DEBUG(LABEL, "CheckComponentInfoEnhnace success"); return SC_OK; @@ -105,6 +106,11 @@ sptr SecCompEnhanceAdapter::GetEnhanceRemoteObject() SC_LOG_DEBUG(LABEL, "GetEnhanceRemoteObject success"); return nullptr; } + +void SecCompEnhanceAdapter::AddSecurityComponentProcess(int32_t pid) +{ + SC_LOG_DEBUG(LABEL, "AddSecurityComponentProcess success"); +} } // namespace SecurityComponent } // namespace Security } // namespace OHOS 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 7e50288928611f11d88f8727e8a319060744dd21..7367d3c791230ccf4afcdb4848f6fe406fccfe9e 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 @@ -175,11 +175,12 @@ HWTEST_F(SecCompEntityTest, CheckClickInfo002, TestSize.Level1) */ HWTEST_F(SecCompEntityTest, CompareComponentBasicInfo001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* other = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); bool isRectCheck = true; ASSERT_FALSE(entity_->CompareComponentBasicInfo(other, isRectCheck)); + FreeJson(jsonComponent); } /** diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index a14a3e162bb670aa676cd99986f6879beb963a83..97dddb75e68e62d65e8891b5265cdf344559eb3f 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -15,6 +15,7 @@ #include "sec_comp_info_helper_test.h" +#include "cjson_utils.h" #include "display.h" #include "display_info.h" #include "display_manager.h" @@ -84,11 +85,12 @@ void SecCompInfoHelperTest::TearDown() */ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_NE(nullptr, comp); ASSERT_TRUE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -99,12 +101,13 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent)); ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent)); + FreeJson(jsonComponent); } /** @@ -115,15 +118,16 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, UNKNOWN_SC_TYPE)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_EQ(nullptr, comp); + FreeJson(jsonComponent); } static SecCompRect GetDefaultRect(void) @@ -225,17 +229,20 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, true }, - }; + auto childObj = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddBoolToJson(childObj, JsonTagConstants::JSON_PARENT_EFFECT_TAG, true)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, childObj)); + FreeJson(childObj); + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -246,26 +253,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = ServiceTestCommon::TEST_DIMENSION; - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_FONT_SIZE_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = ServiceTestCommon::TEST_DIMENSION; - sizeJson[JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_ICON_SIZE_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(sizeJson, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -276,21 +290,28 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) { - nlohmann::json jsonComponent; + auto jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + ASSERT_NE(nullptr, paddingJson); + + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = ServiceTestCommon::TEST_DIMENSION; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -301,21 +322,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& sizesJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingsJson = sizesJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingsJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + auto paddingsJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - paddingsJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = ServiceTestCommon::TEST_DIMENSION; - paddingsJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + ServiceTestCommon::TEST_DIMENSION)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingsJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + ServiceTestCommon::TEST_INVALID_DIMENSION)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -326,20 +351,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& colorJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG]; - colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_INVALID; + auto colorJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); + ASSERT_NE(nullptr, colorJson); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_INVALID)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_RED; - colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_INVALID; + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_INVALID)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -350,18 +380,22 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, NO_TEXT }, - { JsonTagConstants::JSON_ICON_TAG, NO_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; + CJson* styleTagChild = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, NO_TEXT)); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, NO_ICON)); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); + FreeJson(styleTagChild); + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -372,25 +406,32 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& colorsJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG]; - colorsJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_YELLOW; + auto colorsJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG); + ASSERT_NE(nullptr, colorsJson); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - colorsJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_RED; - colorsJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_YELLOW; + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsJson, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE; + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::NO_BG_TYPE))); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -401,27 +442,40 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE; - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = MIN_PADDING_WITHOUT_BG; + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::NO_BG_TYPE))); + + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, paddingJson); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = ServiceTestCommon::TEST_DIMENSION; - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = MIN_PADDING_WITHOUT_BG; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + static_cast(ServiceTestCommon::TEST_DIMENSION))); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -432,28 +486,40 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG]; - styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE; - - auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG]; - auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG]; - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = MIN_PADDING_WITHOUT_BG; + auto styleJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG); + ASSERT_NE(nullptr, styleJson); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleJson, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::NO_BG_TYPE))); + + auto sizeJson = GetObjFromJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG); + ASSERT_NE(nullptr, sizeJson); + auto paddingJson = GetObjFromJson(sizeJson, JsonTagConstants::JSON_PADDING_SIZE_TAG); + ASSERT_NE(nullptr, paddingJson); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + MIN_PADDING_WITHOUT_BG)); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); - paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG; - paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = ServiceTestCommon::TEST_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_TOP_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_RIGHT_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, + MIN_PADDING_WITHOUT_BG)); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(paddingJson, JsonTagConstants::JSON_PADDING_LEFT_TAG, + static_cast(ServiceTestCommon::TEST_DIMENSION))); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } @@ -465,23 +531,33 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); - - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE } - }; - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } - }; + + CJson* styleTagChild = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON))); + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE))); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild)); + FreeJson(styleTagChild); + + CJson* colorsTagChild = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_BLACK)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_YELLOW)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); + FreeJson(colorsTagChild); comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_FALSE(comp->GetValid()); + FreeJson(jsonComponent); } /** @@ -492,18 +568,25 @@ HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(comp->GetValid()); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_WHITE } - }; + + CJson* colorsTagChild = CreateJson(); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_RED)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_BLACK)); + ASSERT_EQ(CJSON_SUCCESS, AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, + ServiceTestCommon::TEST_COLOR_WHITE)); + ASSERT_EQ(CJSON_SUCCESS, AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild)); + FreeJson(colorsTagChild); + comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } /** @@ -514,7 +597,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->text_ = UNKNOWN_TEXT; @@ -526,6 +609,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) comp->text_ = UNKNOWN_TEXT; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } /** @@ -536,7 +620,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); comp->type_ = SecCompType::UNKNOWN_SC_TYPE; @@ -544,6 +628,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) comp->type_ = SecCompType::MAX_SC_TYPE; ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } /** @@ -554,7 +639,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); @@ -573,6 +658,7 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) list.emplace_back(otherWin); Rosen::WindowManager::GetInstance().list_ = list; ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } /** @@ -583,10 +669,11 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1) */ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid005, TestSize.Level1) { - nlohmann::json jsonComponent; + CJson* jsonComponent = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonComponent); SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent); Rosen::WindowManager::GetInstance().result_ = static_cast(-1); ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp)); + FreeJson(jsonComponent); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp index 8207e3678d278b10daf0334719f74ea7d75098cc..c7c87ae8282bb7520b3a60e58c0ca25cc7105176 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_manager_test.cpp @@ -15,6 +15,7 @@ #include "sec_comp_manager_test.h" +#include "cjson_utils.h" #include "sec_comp_log.h" #define private public #include "sec_comp_manager.h" @@ -310,7 +311,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) }; int32_t scId; SecCompManager::GetInstance().malicious_.maliciousAppList_.insert(ServiceTestCommon::TEST_PID_1); - nlohmann::json jsonInvalid; + CJson* jsonInvalid = CreateJson(); EXPECT_EQ(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonInvalid, caller, scId)); SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); @@ -320,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; + CJson* jsonValid = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); @@ -333,6 +334,8 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) SecCompManager::GetInstance().RegisterSecurityComponent(LOCATION_COMPONENT, jsonValid, caller, scId)); #endif SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); + FreeJson(jsonValid); + FreeJson(jsonInvalid); } /** @@ -344,7 +347,7 @@ HWTEST_F(SecCompManagerTest, RegisterSecurityComponent001, TestSize.Level1) HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) { SecCompManager::GetInstance().malicious_.maliciousAppList_.insert(ServiceTestCommon::TEST_PID_1); - nlohmann::json jsonValid; + CJson* jsonValid = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonValid); SecCompCallerInfo caller = { @@ -363,7 +366,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity)); // wrong json - nlohmann::json jsonInvalid; + CJson* jsonInvalid = CreateJson(); LocationButton buttonInvalid = BuildInvalidLocationComponent(); buttonInvalid.ToJson(jsonInvalid); EXPECT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, @@ -378,6 +381,8 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent001, TestSize.Level1) SecCompManager::GetInstance().UpdateSecurityComponent(ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller)); #endif SecCompManager::GetInstance().malicious_.maliciousAppList_.clear(); + FreeJson(jsonValid); + FreeJson(jsonInvalid); } /** @@ -410,11 +415,12 @@ HWTEST_F(SecCompManagerTest, ReportSecurityComponentClickEvent001, TestSize.Leve .pid = ServiceTestCommon::TEST_PID_1 }; SecCompClickEvent clickInfo; - nlohmann::json jsonVaild; + CJson* jsonVaild = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); ASSERT_NE(SC_OK, SecCompManager::GetInstance().ReportSecurityComponentClickEvent(1, jsonVaild, caller, clickInfo, nullptr)); + FreeJson(jsonVaild); } /** @@ -457,21 +463,27 @@ HWTEST_F(SecCompManagerTest, CheckClickSecurityComponentInfo001, TestSize.Level1 SecCompManager::GetInstance().AddSecurityComponentToList(ServiceTestCommon::TEST_PID_1, 0, entity)); SecCompClickEvent clickInfo; - nlohmann::json jsonVaild; + CJson* jsonVaild = CreateJson(); LocationButton buttonValid = BuildValidLocationComponent(); buttonValid.ToJson(jsonVaild); - jsonVaild[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, UNKNOWN_SC_TYPE)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); - jsonVaild[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_INVALID_DIMENSION; + ASSERT_EQ(CJSON_SUCCESS, AddIntToJson(jsonVaild, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT)); + + auto rectObj = GetObjFromJson(jsonVaild, JsonTagConstants::JSON_RECT); + ASSERT_NE(nullptr, rectObj); + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectObj, JsonTagConstants::JSON_RECT_X, + ServiceTestCommon::TEST_INVALID_DIMENSION)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); - jsonVaild[JsonTagConstants::JSON_RECT][JsonTagConstants::JSON_RECT_X] = ServiceTestCommon::TEST_COORDINATE - 1; + ASSERT_EQ(CJSON_SUCCESS, AddDoubleToJson(rectObj, JsonTagConstants::JSON_RECT_X, + ServiceTestCommon::TEST_COORDINATE - 1)); ASSERT_EQ(SC_SERVICE_ERROR_COMPONENT_INFO_INVALID, SecCompManager::GetInstance().ReportSecurityComponentClickEvent( ServiceTestCommon::TEST_SC_ID_1, jsonVaild, caller, clickInfo, nullptr)); + FreeJson(jsonVaild); } /** @@ -521,7 +533,7 @@ HWTEST_F(SecCompManagerTest, DeleteSecurityComponentFromList002, TestSize.Level1 */ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent002, TestSize.Level1) { - nlohmann::json jsonValid; + CJson* jsonValid = CreateJson(); SecCompCallerInfo caller = { .tokenId = ServiceTestCommon::TEST_TOKEN_ID, .pid = ServiceTestCommon::TEST_PID_1 @@ -534,6 +546,7 @@ HWTEST_F(SecCompManagerTest, UpdateSecurityComponent002, TestSize.Level1) buttonValid.ToJson(jsonValid); ASSERT_NE(SC_ENHANCE_ERROR_IN_MALICIOUS_LIST, SecCompManager::GetInstance().UpdateSecurityComponent( ServiceTestCommon::TEST_SC_ID_1, jsonValid, caller)); + FreeJson(jsonValid); } /** @@ -570,14 +583,14 @@ HWTEST_F(SecCompManagerTest, ExitSaProcess001, TestSize.Level1) std::shared_ptr saClient = std::make_shared(); ASSERT_NE(nullptr, saClient); SystemAbilityManagerClient::clientInstance = saClient.get(); - SystemAbilityManagerProxy proxy(nullptr); - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + sptr proxy = new SystemAbilityManagerProxy(nullptr); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); instance->ExitSaProcess(); EXPECT_TRUE(instance->isSaExit_); instance->isSaExit_ = false; - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); - EXPECT_CALL(proxy, UnloadSystemAbility(testing::_)).WillOnce(testing::Return(-1)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); + EXPECT_CALL(*proxy, UnloadSystemAbility(testing::_)).WillOnce(testing::Return(-1)); instance->ExitSaProcess(); EXPECT_TRUE(instance->isSaExit_); } @@ -608,17 +621,17 @@ HWTEST_F(SecCompManagerTest, ExitWhenAppMgrDied001, TestSize.Level1) instance->ExitWhenAppMgrDied(); EXPECT_TRUE(instance->isSaExit_); - SystemAbilityManagerProxy proxy(nullptr); - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + sptr proxy = new SystemAbilityManagerProxy(nullptr); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); instance->ExitWhenAppMgrDied(); EXPECT_TRUE(instance->isSaExit_); - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); instance->ExitWhenAppMgrDied(); EXPECT_TRUE(instance->isSaExit_); - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); - EXPECT_CALL(proxy, UnloadSystemAbility(testing::_)).WillOnce(testing::Return(-1)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); + EXPECT_CALL(*proxy, UnloadSystemAbility(testing::_)).WillOnce(testing::Return(-1)); instance->ExitWhenAppMgrDied(); EXPECT_TRUE(instance->isSaExit_); } 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..c67c16d1bf6692feefc9a433f97530a500232247 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_service_mock_test.cpp @@ -84,9 +84,9 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(SAVE_COMPONENT, "", scId)); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); // parse component json fail ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); @@ -112,6 +112,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent001, TestSize.Level1) EXPECT_EQ(SC_OK, secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); + FreeJson(jsonRes); } /** @@ -125,9 +126,9 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { @@ -147,6 +148,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent002, TestSize.Level1) secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); + FreeJson(jsonRes); } /** @@ -160,9 +162,9 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(0)); AppExecFwk::AppStateData stateData = { @@ -183,6 +185,7 @@ HWTEST_F(SecCompServiceMockTest, RegisterSecurityComponent003, TestSize.Level1) secCompService_->ReportSecurityComponentClickEvent(scId, saveInfo, touch, nullptr)); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); SecCompPermManager::GetInstance().applySaveCountMap_.clear(); + FreeJson(jsonRes); } /** @@ -196,9 +199,9 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildSaveComponentJson(jsonRes); - std::string saveInfo = jsonRes.dump(); + std::string saveInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { @@ -236,6 +239,7 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent001, TestSize. ASSERT_TRUE(secCompService_->VerifySavePermission(ServiceTestCommon::HAP_TOKEN_ID)); sleep(2); EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); + FreeJson(jsonRes); } /** @@ -250,9 +254,9 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. int32_t scId; secCompService_->state_ = ServiceRunningState::STATE_RUNNING; secCompService_->Initialize(); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); AppExecFwk::AppStateData stateData = { @@ -289,4 +293,5 @@ HWTEST_F(SecCompServiceMockTest, ReportSecurityComponentClickEvent002, TestSize. ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.LOCATION"), 0); ASSERT_NE(AccessTokenKit::VerifyAccessToken(ServiceTestCommon::HAP_TOKEN_ID, "ohos.permission.APPROXIMATELY_LOCATION"), 0); + FreeJson(jsonRes); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_service_test.cpp index 8f3bc96f7d5373a280303abe850cf0aa68a00004..035f88e04a50b8195f9c2cf8f0221cc9b45db1b3 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 @@ -93,7 +93,6 @@ HWTEST_F(SecCompServiceTest, OnStart001, TestSize.Level1) ASSERT_EQ(ServiceRunningState::STATE_RUNNING, secCompService_->state_); secCompService_->OnStop(); - ASSERT_EQ(nullptr, secCompService_->appStateObserver_); } /** @@ -114,15 +113,15 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) // GetSystemAbility get app mgr failed secCompService_->appStateObserver_ = nullptr; - SystemAbilityManagerProxy proxy(nullptr); - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); + sptr proxy = new SystemAbilityManagerProxy(nullptr); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); EXPECT_FALSE(secCompService_->RegisterAppStateObserver()); // RegisterApplicationStateObserver failed secCompService_->appStateObserver_ = nullptr; - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); - MockIRemoteObject object; - EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); + sptr object = new MockIRemoteObject(); + EXPECT_CALL(*proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(object)); sptr appProxy = new (std::nothrow) MockAppMgrProxy(nullptr); MockAppMgrProxy::g_MockAppMgrProxy = appProxy; EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, @@ -131,8 +130,8 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) // GetForegroundApplications failed secCompService_->appStateObserver_ = nullptr; - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); - EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); + EXPECT_CALL(*proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(object)); EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(0)); EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, @@ -142,8 +141,8 @@ HWTEST_F(SecCompServiceTest, RegisterAppStateObserver001, TestSize.Level1) // get one foreground app secCompService_->appStateObserver_ = nullptr; - EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(&proxy)); - EXPECT_CALL(proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(&object)); + EXPECT_CALL(*saClient, GetSystemAbilityManager()).WillOnce(testing::Return(proxy)); + EXPECT_CALL(*proxy, GetSystemAbility(testing::_)).WillOnce(testing::Return(object)); EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, RegisterApplicationStateObserver(testing::_, testing::_)).WillOnce(testing::Return(0)); EXPECT_CALL(*MockAppMgrProxy::g_MockAppMgrProxy, GetForegroundApplications(testing::_)) @@ -249,9 +248,9 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve EXPECT_EQ(SC_SERVICE_ERROR_VALUE_INVALID, secCompService_->RegisterSecurityComponent(LOCATION_COMPONENT, "", scId)); - nlohmann::json jsonRes; + CJson* jsonRes = CreateJson(); ServiceTestCommon::BuildLocationComponentJson(jsonRes); - std::string locationInfo = jsonRes.dump(); + std::string locationInfo = PackJsonToString(jsonRes); // parse component json fail ASSERT_EQ(0, SetSelfTokenID(ServiceTestCommon::HAP_TOKEN_ID)); @@ -292,6 +291,7 @@ HWTEST_F(SecCompServiceTest, ReportSecurityComponentClickEvent001, TestSize.Leve EXPECT_EQ(SC_OK, secCompService_->UnregisterSecurityComponent(scId)); #endif setuid(uid); + FreeJson(jsonRes); } /** @@ -372,7 +372,7 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level1) setuid(0); const std::string componentInfo; - nlohmann::json jsonRes; + CJson* jsonRes = nullptr; int32_t scId = 0; EXPECT_EQ(secCompService_->ParseParams(componentInfo, caller, jsonRes), SC_SERVICE_ERROR_VALUE_INVALID); EXPECT_NE(secCompService_->UnregisterSecurityComponent(scId), SC_SERVICE_ERROR_VALUE_INVALID); @@ -381,4 +381,5 @@ HWTEST_F(SecCompServiceTest, GetCallerInfo002, TestSize.Level1) EXPECT_EQ(secCompService_->ReportSecurityComponentClickEvent(scId, componentInfo, touchInfo, nullptr), SC_SERVICE_ERROR_VALUE_INVALID); secCompService_->GetEnhanceRemoteObject(); + FreeJson(jsonRes); } diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h index 27d6f0ee6babb9dc8d740f7b0345b5188b2c70c1..438c1f56d72b1fdbe6e811641eddc92d58733c19 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_stub_test.h @@ -52,12 +52,17 @@ public: bool VerifySavePermission(AccessToken::AccessTokenID tokenId) override { return true; - } + }; sptr GetEnhanceRemoteObject() override { return nullptr; - } + }; + + int32_t PreRegisterSecCompProcess() override + { + return 0; + }; }; class SecCompStubTest : public testing::Test { 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..b315ead26cdf1d0ad62cb8e772dccd0e32d88df5 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.cpp +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.cpp @@ -13,158 +13,110 @@ * limitations under the License. */ #include "service_test_common.h" +#include "cjson_utils.h" namespace OHOS { namespace Security { namespace SecurityComponent { -void ServiceTestCommon::BuildLocationComponentJson(nlohmann::json& jsonComponent) +void ServiceTestCommon::BuildLocationComponentJson(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - { JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - { JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, LOCATION_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(LocationDesc::SELECT_LOCATION)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(LocationIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); +} - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } - }; +void ServiceTestCommon::BuildSaveComponentJson(CJson* jsonComponent) +{ + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, SAVE_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION }, - { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); } -void ServiceTestCommon::BuildSaveComponentJson(nlohmann::json& jsonComponent) +void ServiceTestCommon::BuildPasteComponentJson(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = SAVE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_SC_TYPE, PASTE_COMPONENT); + GenerateCommon(jsonComponent); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; - - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } - }; + CJson* styleTagChild = CreateJson(); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_TEXT_TAG, + static_cast(SaveDesc::DOWNLOAD)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_ICON_TAG, + static_cast(SaveIcon::LINE_ICON)); + AddIntToJson(styleTagChild, JsonTagConstants::JSON_BG_TAG, + static_cast(SecCompBackground::CIRCLE)); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_STYLE_TAG, styleTagChild); + AddIntToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_ID, 0); + FreeJson(styleTagChild); +} - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, SaveDesc::DOWNLOAD }, - { JsonTagConstants::JSON_ICON_TAG, SaveIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; +void ServiceTestCommon::AddRectToJson(CJson* jsonComponent, const std::string key) +{ + CJson* rectChild = CreateJson(); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE); + AddDoubleToJson(rectChild, JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE); + AddObjToJson(jsonComponent, key, rectChild); + FreeJson(rectChild); } -void ServiceTestCommon::BuildPasteComponentJson(nlohmann::json& jsonComponent) +void ServiceTestCommon::GenerateCommon(CJson* jsonComponent) { - jsonComponent[JsonTagConstants::JSON_SC_TYPE] = PASTE_COMPONENT; - jsonComponent[JsonTagConstants::JSON_NODE_ID] = 0; - jsonComponent[JsonTagConstants::JSON_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = nlohmann::json { - {JsonTagConstants::JSON_RECT_X, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_Y, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_WIDTH, ServiceTestCommon::TEST_COORDINATE }, - {JsonTagConstants::JSON_RECT_HEIGHT, ServiceTestCommon::TEST_COORDINATE } - }; - nlohmann::json jsonPadding = nlohmann::json { - { JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION }, - { JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION }, - }; + AddIntToJson(jsonComponent, JsonTagConstants::JSON_NODE_ID, 0); + AddRectToJson(jsonComponent, JsonTagConstants::JSON_RECT); + AddRectToJson(jsonComponent, JsonTagConstants::JSON_WINDOW_RECT); + + CJson* jsonPadding = CreateJson(); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_TOP_TAG, ServiceTestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_RIGHT_TAG, ServiceTestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_BOTTOM_TAG, ServiceTestCommon::TEST_DIMENSION); + AddDoubleToJson(jsonPadding, JsonTagConstants::JSON_PADDING_LEFT_TAG, ServiceTestCommon::TEST_DIMENSION); + + CJson* sizeTagChild = CreateJson(); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE); + AddDoubleToJson(sizeTagChild, JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE); + AddObjToJson(sizeTagChild, JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_SIZE_TAG, sizeTagChild); + FreeJson(jsonPadding); + FreeJson(sizeTagChild); - jsonComponent[JsonTagConstants::JSON_SIZE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_ICON_SIZE_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG, ServiceTestCommon::TEST_SIZE }, - { JsonTagConstants::JSON_PADDING_SIZE_TAG, jsonPadding }, - }; + CJson* colorsTagChild = CreateJson(); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE); + AddUnsignedIntToJson(colorsTagChild, JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_COLORS_TAG, colorsTagChild); + FreeJson(colorsTagChild); - jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json { - { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED }, - { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLUE }, - { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW } - }; + CJson* borderTagChild = CreateJson(); + AddDoubleToJson(borderTagChild, JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_BORDER_TAG, borderTagChild); + FreeJson(borderTagChild); - jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json { - { JsonTagConstants::JSON_BORDER_WIDTH_TAG, ServiceTestCommon::TEST_SIZE }, - }; - jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json { - { JsonTagConstants::JSON_PARENT_EFFECT_TAG, false }, - }; - jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json { - { JsonTagConstants::JSON_TEXT_TAG, PasteDesc::PASTE }, - { JsonTagConstants::JSON_ICON_TAG, PasteIcon::LINE_ICON }, - { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }, - }; - jsonComponent[JsonTagConstants::JSON_WINDOW_ID] = 0; + CJson* parentTagChild = CreateJson(); + AddBoolToJson(parentTagChild, JsonTagConstants::JSON_PARENT_EFFECT_TAG, false); + AddObjToJson(jsonComponent, JsonTagConstants::JSON_PARENT_TAG, parentTagChild); + FreeJson(parentTagChild); } } // namespace SecurityComponent } // namespace Security diff --git a/services/security_component_service/sa/test/unittest/src/service_test_common.h b/services/security_component_service/sa/test/unittest/src/service_test_common.h index 64989b23ffd1504663b02b4b5919f37ba0eb14b1..d2635b2a03d0de60bbbc80ce508c97a582a2a140 100644 --- a/services/security_component_service/sa/test/unittest/src/service_test_common.h +++ b/services/security_component_service/sa/test/unittest/src/service_test_common.h @@ -34,7 +34,7 @@ public: static constexpr int32_t SC_ID_START = 1000; static constexpr int32_t SA_ID = 3506; - static constexpr float TEST_SIZE = 100.0; + static constexpr double TEST_SIZE = 100.0; static constexpr double TEST_COORDINATE = 100.0; static constexpr double TEST_DIFF_COORDINATE = 200.0; static constexpr double TEST_DIMENSION = 100.0; @@ -55,9 +55,11 @@ public: static constexpr uint32_t HAP_TOKEN_ID = 537715419; static constexpr uint32_t TEST_TOKEN_ID = 1; - static void BuildLocationComponentJson(nlohmann::json& jsonComponent); - static void BuildSaveComponentJson(nlohmann::json& jsonComponent); - static void BuildPasteComponentJson(nlohmann::json& jsonComponent); + static void BuildLocationComponentJson(CJson* jsonComponent); + static void BuildSaveComponentJson(CJson* jsonComponent); + static void BuildPasteComponentJson(CJson* jsonComponent); + static void AddRectToJson(CJson* jsonComponent, const std::string key); + static void GenerateCommon(CJson* jsonComponent); }; } // namespace SecurityComponent } // namespace Security