diff --git a/foundations/ability/utils/include/updateservice_json_utils.h b/foundations/ability/utils/include/updateservice_json_utils.h index d9bb0eb07d7666fd565663c0a9b6aa5270ba8a50..d29fef712de7f4ec2e5262b6a602b8531c472239 100644 --- a/foundations/ability/utils/include/updateservice_json_utils.h +++ b/foundations/ability/utils/include/updateservice_json_utils.h @@ -43,26 +43,24 @@ public: } cJSON *item = cJSON_GetObjectItemCaseSensitive(jsonObject, key.c_str()); - if (!item) { + if (item == nullptr) { return CAST_INT(JsonParseError::MISSING_PROP); } - if (!CheckBaseType(item, value)) { + if (!CheckTypeAndAsign(item, value)) { return CAST_INT(JsonParseError::TYPE_ERROR); } - GetValue(item, value); return CAST_INT(JsonParseError::ERR_OK); } static cJSON *ParseAndGetJsonObject(const std::string &jsonStr) { cJSON *object = cJSON_Parse(jsonStr.c_str()); - if (!object) { + if (object == nullptr) { return nullptr; } - if (!cJSON_IsObject(object)) - { + if (!cJSON_IsObject(object)) { cJSON_Delete(object); return nullptr; } @@ -70,93 +68,98 @@ public: return object; } -private: - - // 检查基本类型 - template static bool CheckBaseType(cJSON *jsonObject, T &value) + static std::shared_ptr ParseJson(const std::string &jsonStr) { - if (!jsonObject) { - return false; + cJSON *root = cJSON_Parse(jsonStr.c_str()); + if (root == nullptr) { + return nullptr; } - if constexpr (std::is_same_v) { - return cJSON_IsString(jsonObject); - } else if constexpr (std::is_same_v || std::is_same_v || - std::is_same_v) { - return cJSON_IsNumber(jsonObject); - } else if constexpr (std::is_same_v || std::is_same_v) { - return cJSON_IsNumber(jsonObject) && jsonObject->valuedouble >=0; - } else if constexpr (std::is_same_v) { - return (jsonObject->type == cJSON_True || jsonObject->type == cJSON_False); - } else { - return false; - } + return std::shared_ptr(root, [](cJSON *r) { cJSON_Delete(r); }); } - template - static bool CheckArrayType(cJSON *jsonObject, std::vector &value) +private: + + static bool IsInteger(double d) { - return cJSON_IsArray(jsonObject); + return std::floor(d) == d; } - static void GetValueVecString(const cJSON *jsonArray, std::vector &value) + // 判断int32_t, int64_t, uint32_t, uint64_t 是否合法整数 + template static bool CheckInteger(cJSON *jsonObj, T &value) { - if (!jsonArray || !cJSON_IsArray(jsonArray)) { - return; + if (!cJSON_IsNumber(jsonObj)) { + return false; + } + + double objValue = jsonObj->valuedouble; + if (!IsInteger(objValue)) { + return false; } - const cJSON *element = nullptr; - cJSON_ArrayForEach(element, jsonArray) { - if (cJSON_IsString(element) && element->valuestring != nullptr) { - value.push_back(element->valuestring); + if (std::is_same_v) { + if (objValue < std::numeric_limits::min() || objValue > std::numeric_limits::max()) { + return false; } } - } - static void GetValueVecInt(const cJSON *jsonArray, std::vector &value) - { - if (!jsonArray || !cJSON_IsArray(jsonArray)) { - return; + if (std::is_same_v) { + if (objValue < std::numeric_limits::min() || objValue > std::numeric_limits::max()) { + return false; + } } - const cJSON *element = nullptr; - cJSON_ArrayForEach(element, jsonArray) { - if (cJSON_IsNumber(element)) { - value.push_back(element->valueint); + if (std::is_same_v) { + if (objValue < 0 || objValue > std::numeric_limits::max()) { + return false; } } + + if (std::is_same_v) { + if (objValue < 0 || objValue > std::numeric_limits::max()) { + return false; + } + } + value = objValue; + return true; } - template - static void GetValue(cJSON *item, T &value) + // 检查基本类型 + template static bool CheckTypeAndAsign(cJSON *jsonObject, T &value) { - if (!item) { - return; + if constexpr (std::is_same_v) { + if (cJSON_IsString(jsonObject)) { + value = jsonObject->valuestring; + return true; + } + return false; } - if constexpr (std::is_integral_v && !std::is_same_v) - { - if (cJSON_IsNumber(item)) { - (sizeof(T) <= sizeof(int)) ? value = static_cast(item->valueint) : - value = static_cast(item->valuedouble); + if constexpr (std::is_same_v) { + if (jsonObject->type != cJSON_True && jsonObject->type != cJSON_False) { + return false; } - } else if constexpr (std::is_floating_point_v) { - if (cJSON_IsNumber(item)) { - value = static_cast(item->valuedouble); + value = cJSON_IsTrue(jsonObject) ? true : false; + return true; + } + + if constexpr (std::is_same_v || std::is_same_v || + std::is_same_v || std::is_same_v) { + return CheckInteger(jsonObject, value); + } + + if constexpr (std::is_same_v) { + if (!cJSON_IsNumber(jsonObject)) { + return false; } - } else if constexpr (std::is_same_v) { - value = cJSON_IsTrue(item) ? true : false; - } else if constexpr (std::is_same_v) { - if (cJSON_IsString(item)) { - value = item->valuestring; + double dbValue = jsonObject->valuedouble; + if (!std::isfinite(dbValue)) { + return false; } - } else if constexpr (std::is_same_v>) { - GetValueVecInt(item, value); - } else if constexpr (std::is_same_v>) { - GetValueVecString(item, value); - } else { - return; + value = dbValue; + return true; } + return false; } }; } // namespace OHOS::UpdateService diff --git a/foundations/model/include/error_message.h b/foundations/model/include/error_message.h index e4e735f589f922d93bc307a5e9191e35ff0957c4..fc093562069fce76e7afe6586f4157211b831daf 100644 --- a/foundations/model/include/error_message.h +++ b/foundations/model/include/error_message.h @@ -19,10 +19,10 @@ #include #include -#include "updateservice_json_utils.h" #include "json_builder.h" #include "parcel.h" +#include "update_log.h" namespace OHOS::UpdateService { struct ErrorMessage : public Parcelable { diff --git a/services/core/ability/adapter/src/config_parse.cpp b/services/core/ability/adapter/src/config_parse.cpp index 28d4629dfd1a340735a4c5c9269e833a9d2f7485..fbad28e1e5b1ff1a1511a2a2ac01c47d29dc9c61 100644 --- a/services/core/ability/adapter/src/config_parse.cpp +++ b/services/core/ability/adapter/src/config_parse.cpp @@ -63,16 +63,15 @@ void ConfigParse::LoadConfigInfo() std::string rawJson(streambuffer.str()); readFile.close(); - cJSON *root = cJSON_Parse(rawJson.c_str()); - if (!root) { + auto root = UpdateServiceJsonUtils::ParseJson(rawJson.c_str()); + if (root == nullptr) { ENGINE_LOGE("json Create error!"); return; } - UpdateServiceJsonUtils::GetValueAndSetTo(root, "abInstallTimeout", configInfo_.abInstallTimeout); - UpdateServiceJsonUtils::GetValueAndSetTo(root, "streamInstallTimeout", configInfo_.streamInstallTimeout); - UpdateServiceJsonUtils::GetValueAndSetTo(root, "moduleLibPath", configInfo_.moduleLibPath); - cJSON_Delete(root); + UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "abInstallTimeout", configInfo_.abInstallTimeout); + UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "streamInstallTimeout", configInfo_.streamInstallTimeout); + UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "moduleLibPath", configInfo_.moduleLibPath); } } // namespace UpdateService } // namespace OHOS diff --git a/services/core/ability/model/include/device_info.h b/services/core/ability/model/include/device_info.h index eabf71ef873d1a10a8ad46fe67b67be22b10db3a..8345e47d2b5bea436342d68bf19fbac311e9e52e 100644 --- a/services/core/ability/model/include/device_info.h +++ b/services/core/ability/model/include/device_info.h @@ -27,19 +27,18 @@ public: std::string udid; std::string deviceId; - friend void ToJson(cJSON *jsonObject, const DeviceInfo &deviceInfo, bool isPrint) + friend void ToJson(cJSON *jsonObject, bool isPrint) { - if (jsonObject == nullptr) - { + if (jsonObject == nullptr) { return; } if (isPrint) { - cJSON_AddStringToObject(jsonObject, "udid", AnonymousUtils::AnonymousString(deviceInfo.udid).c_str()); - cJSON_AddStringToObject(jsonObject, "deviceId", AnonymousUtils::AnonymousString(deviceInfo.udid).c_str()); + cJSON_AddStringToObject(jsonObject, "udid", AnonymousUtils::AnonymousString(udid.c_str())); + cJSON_AddStringToObject(jsonObject, "deviceId", AnonymousUtils::AnonymousString(deviceId.c_str())); } else { - cJSON_AddStringToObject(jsonObject, "udid", deviceInfo.udid.c_str()); - cJSON_AddStringToObject(jsonObject, "deviceId", deviceInfo.deviceId.c_str()); + cJSON_AddStringToObject(jsonObject, "udid", udid.c_str()); + cJSON_AddStringToObject(jsonObject, "deviceId", deviceId.c_str()); } } }; diff --git a/services/core/ability/utils/include/sha256_utils.h b/services/core/ability/utils/include/sha256_utils.h index 60f8df064eb92f0b6d017a2f5306b4c2f40fe74e..97612df593b257565c21f4c0081137d3a5116c6d 100644 --- a/services/core/ability/utils/include/sha256_utils.h +++ b/services/core/ability/utils/include/sha256_utils.h @@ -35,6 +35,7 @@ private: static bool Sha256Calculate(const unsigned char *input, size_t len, char *componentId, unsigned int componentIdLen); static bool TransDigestToSha256Result(char *sha256Result, uint32_t componentIdLen, const unsigned char *digest); + static void FreeBuffer(char *buffer, std::ifstream &file); }; } // namespace UpdateService } // namespace OHOS diff --git a/services/core/ability/utils/src/sha256_utils.cpp b/services/core/ability/utils/src/sha256_utils.cpp index 89ba60d621330f34bdaa703abcd9bd4bc78c2d7f..7fe206c9f67167b04aed8881b453fbcd862beedc 100644 --- a/services/core/ability/utils/src/sha256_utils.cpp +++ b/services/core/ability/utils/src/sha256_utils.cpp @@ -25,6 +25,7 @@ namespace OHOS { namespace UpdateService { +constexpr int OPENSSL_SUCCESS = 1; constexpr unsigned int SHA256_STRING_LEN = 65; constexpr unsigned int SHA256_LENGTH = 32; constexpr unsigned int MAX_BUFFER_LENGTH = 1024; @@ -59,6 +60,17 @@ bool Sha256Utils::CheckFileSha256String(const std::string &fileName, const std:: return true; } +void Sha256Utils::FreeBuffer(char *buffer, std::ifstream &file) +{ + if (buffer != nullptr) { + free(buffer); + } + + if (file.is_open()) { + file.close(); + } +} + bool Sha256Utils::GetDigestFromFile(const char *fileName, unsigned char digest[]) { char realPath[PATH_MAX] = {}; @@ -76,18 +88,33 @@ bool Sha256Utils::GetDigestFromFile(const char *fileName, unsigned char digest[] char *buffer = (char *)malloc(MAX_BUFFER_LENGTH); /* buffer len 1024 */ if (buffer == nullptr) { ENGINE_LOGI("failed to allocate memory"); + file.close(); return false; } SHA256_CTX sha256; - SHA256_Init(&sha256); + int32_t startRet = SHA256_Init(&sha256); + if (startRet != OPENSSL_SUCCESS) { + ENGINE_LOGE("SHA256_init_ret failed, startRet = %{public}d", startRet); + FreeBuffer(buffer, file); + return false; + } while (!file.eof()) { file.read(buffer, MAX_BUFFER_LENGTH); - SHA256_Update(&sha256, buffer, file.gcount()); + int32_t updateRet = SHA256_Update(&sha256, buffer, file.gcount()); + if (updateRet != OPENSSL_SUCCESS) { + ENGINE_LOGE("SHA256_update_ret failed, updateRet = %{public}d", updateRet); + FreeBuffer(buffer, file); + return false; + } + } + int32_t finishRet = SHA256_Final(digest, &sha256); + if (finishRet != OPENSSL_SUCCESS) { + ENGINE_LOGE("SHA256_finish_ret failed, finishRet = %{public}d", finishRet); + FreeBuffer(buffer, file); + return false; } - SHA256_Final(digest, &sha256); - file.close(); - free(buffer); + FreeBuffer(buffer, file); return true; } @@ -109,19 +136,19 @@ bool Sha256Utils::Sha256Calculate(const unsigned char *input, size_t len, char * return false; } int startRet = SHA256_Init(&ctx); - if (startRet != 1) { + if (startRet != OPENSSL_SUCCESS) { ENGINE_LOGE("SHA256_Init failed, startRet = %{public}d", startRet); return false; } int updateRet = SHA256_Update(&ctx, input, len); - if (updateRet != 1) { + if (updateRet != OPENSSL_SUCCESS) { ENGINE_LOGE("SHA256_Update failed, updateRet = %{public}d", updateRet); return false; } int finishRet = SHA256_Final(digest, &ctx); - if (finishRet != 1) { + if (finishRet != OPENSSL_SUCCESS) { ENGINE_LOGE("SHA256_Final failed, finishRet = %{public}d", finishRet); return false; } diff --git a/services/firmware/check/include/firmware_icheck.h b/services/firmware/check/include/firmware_icheck.h index 0cadca3c63476604635172bea84cacf95b5d4ab8..8e57ef8fa16d79adf871b6afaac66e3a980d966f 100644 --- a/services/firmware/check/include/firmware_icheck.h +++ b/services/firmware/check/include/firmware_icheck.h @@ -106,11 +106,11 @@ public: if (response.status != static_cast(HttpConstant::SUCCESS) || response.content.empty()) { checkStatus = CheckStatus::CHECK_FAIL; } else { - cJSON *root = cJSON_Parse(response.content.c_str()); - if (!root) { + auto root = UpdateServiceJsonUtils::ParseJson(response.content.c_str()); + if (root == nullptr) { FIRMWARE_LONG_LOGI("FirmwareCheck response: %{public}s", response.content.c_str()); + return; } - cJSON_Delete(root); FirmwareCheckAnalyzeUtils().DoAnalyze(response.content, checkResultList_, duration_, checkAndAuthInfo_); checkStatus = CheckStatus::CHECK_SUCCESS; } diff --git a/services/firmware/common/include/firmware_common.h b/services/firmware/common/include/firmware_common.h index afd8a1f6263443dee379decf2b9d263c8227ddd7..3ac697f4bff4538c17570e032d3b9a933b9ca184 100644 --- a/services/firmware/common/include/firmware_common.h +++ b/services/firmware/common/include/firmware_common.h @@ -19,7 +19,6 @@ #include #include "anonymous_utils.h" -#include "cJSON.h" #include "constant.h" #include "firmware_component.h" #include "firmware_log.h" diff --git a/services/firmware/utils/src/firmware_check_analyze_utils.cpp b/services/firmware/utils/src/firmware_check_analyze_utils.cpp index 7f0fc84534e773ef1c8d741737feb62ad9d2a29d..67f611219a0206078fcdb34b0944fb3e11359426 100644 --- a/services/firmware/utils/src/firmware_check_analyze_utils.cpp +++ b/services/firmware/utils/src/firmware_check_analyze_utils.cpp @@ -22,13 +22,13 @@ #include #include "constant.h" -#include "updateservice_json_utils.h" #include "file_utils.h" #include "firmware_combine_version_utils.h" #include "firmware_constant.h" #include "firmware_log.h" #include "firmware_preferences_utils.h" #include "string_utils.h" +#include "updateservice_json_utils.h" namespace OHOS { namespace UpdateService { @@ -37,37 +37,35 @@ void FirmwareCheckAnalyzeUtils::DoAnalyze(const std::string &rawJson, std::vecto { BlCheckResponse response; int32_t ret = CAST_INT(JsonParseError::ERR_OK); - cJSON *root = UpdateServiceJsonUtils::ParseAndGetJsonObject(rawJson); - if (!root) { + auto root = UpdateServiceJsonUtils::ParseJson(rawJson); + if (root == nullptr) { FIRMWARE_LOGE("fail to parse out a json object"); return; } int32_t status = CAST_INT(CheckResultStatus::STATUS_SYSTEM_ERROR); - UpdateServiceJsonUtils::GetValueAndSetTo(root, "searchStatus", status); + UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "searchStatus", status); checkAndAuthInfo.responseStatus = std::to_string(status); if (!IsLegalStatus(status)) { FIRMWARE_LOGI("not found new version!"); - cJSON_Delete(root); return; } if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) { - ret += AnalyzeBlVersionCheckResults(root, response); - ret += AnalyzeComponents(root); + ret += AnalyzeBlVersionCheckResults(root.get(), response); + ret += AnalyzeComponents(root.get()); } // 解析的都是必须字段,全部解析正常,才能给component赋值 if (ret == CAST_INT(JsonParseError::ERR_OK)) { components = components_; } - cJSON_Delete(root); } int32_t FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults(cJSON *root, BlCheckResponse &response) { cJSON *itemCheckResults = cJSON_GetObjectItemCaseSensitive(root, "checkResults"); - if (!itemCheckResults) { + if (itemCheckResults == nullptr) { FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults no key checkResults"); return CAST_INT(JsonParseError::MISSING_PROP); } @@ -78,10 +76,9 @@ int32_t FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults(cJSON *root, BlC int32_t ret = CAST_INT(JsonParseError::ERR_OK); int32_t status = CAST_INT(CheckResultStatus::STATUS_SYSTEM_ERROR); UpdateServiceJsonUtils::GetValueAndSetTo(root, "searchStatus", status); - if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) - { - for (int i = 0; i < cJSON_GetArraySize(itemCheckResults); i++) - { + if (status == CAST_INT(CheckResultStatus::STATUS_NEW_VERSION_AVAILABLE)) { + int32_t arraySize = cJSON_GetArraySize(itemCheckResults); + for (int i = 0; i < arraySize; i++) { auto item = cJSON_GetArrayItem(itemCheckResults, i); BlVersionCheckResult checkResult; ret += UpdateServiceJsonUtils::GetValueAndSetTo(item, "descriptPackageId", checkResult.descriptPackageId); @@ -99,7 +96,6 @@ int32_t FirmwareCheckAnalyzeUtils::AnalyzeBlVersionCheckResults(cJSON *root, BlC ret += UpdateServiceJsonUtils::GetValueAndSetTo(item, "versionName", component.displayVersionNumber); ret += UpdateServiceJsonUtils::GetValueAndSetTo(item, "versionName", component.versionNumber); checkResult.targetBlComponents.push_back(component); - UpdateServiceJsonUtils::GetValueAndSetTo(item, "blVersionInfo", checkResult.blVersionInfo); response.blVersionCheckResults.push_back(checkResult); Version version; version.versionId = "1"; @@ -115,7 +111,7 @@ int32_t FirmwareCheckAnalyzeUtils::AnalyzeComponents(cJSON *root) { // 检查 "checkResults" 是否存在 cJSON *itemCheckResults = cJSON_GetObjectItemCaseSensitive(root, "checkResults"); - if (!itemCheckResults) { + if (itemCheckResults == nullptr) { FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeComponents no key checkResults"); return CAST_INT(JsonParseError::MISSING_PROP); } @@ -130,7 +126,7 @@ int32_t FirmwareCheckAnalyzeUtils::AnalyzeComponents(cJSON *root) // 检查 "descriptInfo" 是否存在 cJSON *itemDescriptInfo = cJSON_GetObjectItemCaseSensitive(root, "descriptInfo"); - if (!itemDescriptInfo) { + if (itemDescriptInfo == nullptr) { FIRMWARE_LOGE("FirmwareCheckAnalyzeUtils::AnalyzeComponents no key descriptInfo"); return CAST_INT(JsonParseError::MISSING_PROP); } @@ -146,12 +142,8 @@ int32_t FirmwareCheckAnalyzeUtils::ProcessCheckResults(cJSON *checkResults) int32_t ret = CAST_INT(JsonParseError::ERR_OK); std::string componentId; - if (!checkResults) { - FIRMWARE_LOGE("AnalyzeComponents no key checkResults"); - return CAST_INT(JsonParseError::MISSING_PROP); - } - - for (int i = 0; i < cJSON_GetArraySize(checkResults); i++) { + int32_t arraySize = cJSON_GetArraySize(checkResults); + for (int i = 0; i < arraySize; i++) { cJSON *itemResult = cJSON_GetArrayItem(checkResults, i); FirmwareComponent component; @@ -190,12 +182,8 @@ int32_t FirmwareCheckAnalyzeUtils::ProcessDescriptInfo(cJSON *descriptInfo) int32_t ret = CAST_INT(JsonParseError::ERR_OK); std::string componentId = components_.empty() ? "" : components_.back().descriptPackageId; - if (!descriptInfo) { - FIRMWARE_LOGE("ProcessDescriptInfo no descriptInfo"); - return CAST_INT(JsonParseError::MISSING_PROP); - } - - for (int i = 0; i < cJSON_GetArraySize(descriptInfo); i++) { + int32_t arraySize = cJSON_GetArraySize(descriptInfo); + for (int i = 0; i < arraySize; i++) { cJSON *itemInfo = cJSON_GetArrayItem(descriptInfo, i); int32_t descriptInfoType; std::string descContent; diff --git a/services/startup/manage/src/schedule_config.cpp b/services/startup/manage/src/schedule_config.cpp index d25948cc5e26c1753be8fb3f3f2b594386c19e44..f59c495a37a35f50fbecdc478b39be2d28f55aeb 100644 --- a/services/startup/manage/src/schedule_config.cpp +++ b/services/startup/manage/src/schedule_config.cpp @@ -15,9 +15,10 @@ #include "schedule_config.h" +#include "constant.h" #include "file_utils.h" #include "update_log.h" -#include "constant.h" +#include "updateservice_json_utils.h" namespace OHOS { namespace UpdateService { @@ -27,16 +28,21 @@ uint64_t ScheduleConfig::idleCheckInterval_ = Startup::IDLE_CHECK_INTERVAL; void ScheduleConfig::InitConfig() { ENGINE_LOGI("InitConfig"); - cJSON* root = cJSON_Parse(FileUtils::ReadDataFromFile(Constant::DUPDATE_ENGINE_CONFIG_PATH).c_str()); - if (!root) { + + std::string dataString = FileUtils::ReadDataFromFile(Constant::DUPDATE_ENGINE_CONFIG_PATH); + if (dataString.empty()) { + ENGINE_LOGI("file content is null"); + return; + } + auto root = UpdateServiceJsonUtils::ParseJson(dataString); + if (root == nullptr) { ENGINE_LOGI("InitConfig load fail"); return; } - pullupInterval_ = ParseConfig(root, Startup::PULLUP_INTERVAL_CONFIG, Startup::PULLUP_INTERVAL); - idleCheckInterval_ = ParseConfig(root, Startup::IDLE_CHECK_INTERVAL_CONFIG, Startup::IDLE_CHECK_INTERVAL); + pullupInterval_ = ParseConfig(root.get(), Startup::PULLUP_INTERVAL_CONFIG, Startup::PULLUP_INTERVAL); + idleCheckInterval_ = ParseConfig(root.get(), Startup::IDLE_CHECK_INTERVAL_CONFIG, Startup::IDLE_CHECK_INTERVAL); ENGINE_LOGI("InitConfig pullupInterval: %{public}s, idleCheckInterval: %{public}s", std::to_string(pullupInterval_).c_str(), std::to_string(idleCheckInterval_).c_str()); - cJSON_Delete(root); } uint64_t ScheduleConfig::GetPullupInterval() diff --git a/test/unittest/json_utils_test/BUILD.gn b/test/unittest/json_utils_test/BUILD.gn index 862261a269c9dbb25a15d87e4ec4512e90f8715b..3b0e6fd6211e503fae2ab0406772f5a4b5372d86 100644 --- a/test/unittest/json_utils_test/BUILD.gn +++ b/test/unittest/json_utils_test/BUILD.gn @@ -36,6 +36,11 @@ ohos_unittest("UpdateServiceJsonUtilsTest") { "googletest:gtest_main", "hilog:libhilog", ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] deps = [] part_name = updateengine_part_name } diff --git a/test/unittest/json_utils_test/updateservice_json_utils_test.cpp b/test/unittest/json_utils_test/updateservice_json_utils_test.cpp index 63ed0a3ca561f75e6071c6d97d50688c6ef4d109..086daf56d336028d463a146c37412e2b6b130bf7 100644 --- a/test/unittest/json_utils_test/updateservice_json_utils_test.cpp +++ b/test/unittest/json_utils_test/updateservice_json_utils_test.cpp @@ -55,70 +55,656 @@ HWTEST_F(UpdateServiceJsonUtilsTest, ParseAndGetJsonObjectFalse, TestSize.Level1 } /** -* @tc.name: GetValueAndSetToInt -* @tc.desc: Test json string, get int +* @tc.name: ParseJsonTrue +* @tc.desc: Test json string is true * @tc.type: FUNC */ -HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt, TestSize.Level1) +HWTEST_F(UpdateServiceJsonUtilsTest, ParseJsonTrue, TestSize.Level1) { - std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; - int ageNumber = 0; - cJSON *root = cJSON_Parse(jsonStr.c_str()); + std::string jsonStr = "{\"name\":\"Alice\", \"age\":25}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); EXPECT_NE(nullptr, root); - int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root, "age", ageNumber); - EXPECT_EQ(ageNumber, 54); - EXPECT_EQ(0, ret); - cJSON_Delete(root); } /** -* @tc.name: GetValueAndSetToUint64 -* @tc.desc: Test json string, get uint64 +* @tc.name: ParseJsonFalse +* @tc.desc: Test json string is true * @tc.type: FUNC */ -HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUint64, TestSize.Level1) +HWTEST_F(UpdateServiceJsonUtilsTest, ParseJsonFalse, TestSize.Level1) { - std::string jsonStr = "{\"time\": 123456789, \"file\": \"a.txt\"}"; - uint64_t timeNumber = 0; - cJSON *root = cJSON_Parse(jsonStr.c_str()); - EXPECT_NE(nullptr, root); - int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root, "time", timeNumber); - EXPECT_EQ(timeNumber, 123456789); - EXPECT_EQ(0, ret); - cJSON_Delete(root); + std::string jsonStr = "{\"name\":\"Alice\", \"age\":25, xxx}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + EXPECT_EQ(nullptr, root); +} + +/** +* @tc.name: GetValueAndSetToNullptr +* @tc.desc: Test json string, root is nullptr +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToNullptr, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\", xxx}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + int32_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::MISSING_PROP), ret); +} + +/** +* @tc.name: GetValueAndSetToKeyNullptr +* @tc.desc: Test json string, key is nullptr +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToKeyNullptr, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + int32_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::MISSING_PROP), ret); } /** -* @tc.name: GetValueAndSetToString -* @tc.desc: Test json string, get string +* @tc.name: GetValueAndSetToKeyNotFound +* @tc.desc: Test json string, key not found * @tc.type: FUNC */ -HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToString, TestSize.Level1) +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToKeyNotFound, TestSize.Level1) { std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + int32_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "book", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::MISSING_PROP), ret); +} + +/** +* @tc.name: GetValueAndSetToStringTypeErr +* @tc.desc: Test json string, get string type error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToStringTypeErr, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"file\": 1234}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + std::string fileName; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "file", fileName); + EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); +} + +/** +* @tc.name: GetValueAndSetToStringOk +* @tc.desc: Test json string, get string ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToStringOk, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + std::string fileName; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "file", fileName); + EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); + EXPECT_EQ("a.txt", fileName); +} + +/** +* @tc.name: GetValueAndSetToBoolTypeErr +* @tc.desc: Test json string, get Bool type error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToBoolTypeErr, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"boolFlag\": 123}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + bool boolFlag; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "boolFlag", boolFlag); + EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); +} + +/** +* @tc.name: GetValueAndSetToBoolTypeOk +* @tc.desc: Test json string, get Bool type ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToBoolTypeOk, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"boolFlag\": false}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + bool boolFlag; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "boolFlag", boolFlag); + EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); + EXPECT_EQ(false, boolFlag); +} + +/** +* @tc.name: GetValueAndSetToInt32TypeError +* @tc.desc: Test json string, int32_t type error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt32TypeError, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54.22, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + int32_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); +} + +/** +* @tc.name: GetValueAndSetToInt32Ok +* @tc.desc: Test json string, get int32_t ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt32Ok, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + int32_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); + EXPECT_EQ(54, ageNumber); +} + +/** +* @tc.name: GetValueAndSetToInt64TypeError +* @tc.desc: Test json string, int64_t type error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt64TypeError, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 541212121.44, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + int64_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); +} + +/** +* @tc.name: GetValueAndSetToInt64Ok +* @tc.desc: Test json string, get int64_t ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToInt64Ok, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54121212123, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + int64_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); + EXPECT_EQ(54121212123, ageNumber); +} + +/** +* @tc.name: GetValueAndSetToUInt32TypeError +* @tc.desc: Test json string, uint32_t type error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUInt32TypeError, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + uint32_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); +} + +/** +* @tc.name: GetValueAndSetToUInt32Ok +* @tc.desc: Test json string, get uint32_t ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUInt32Ok, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 12, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + uint32_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); + EXPECT_EQ(12, ageNumber); +} + +/** +* @tc.name: GetValueAndSetToUInt64TypeError +* @tc.desc: Test json string, uint64_t type error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUInt64TypeError, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": -100, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + uint64_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); +} + +/** +* @tc.name: GetValueAndSetToUInt64Ok +* @tc.desc: Test json string, get uint64_t ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToUInt64Ok, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 1212121211255, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + uint64_t ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); + EXPECT_EQ(1212121211255, ageNumber); +} + +/** +* @tc.name: GetValueAndSetToDoubleTypeError +* @tc.desc: Test json string, Double type error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToDoubleTypeError, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": \"NAN\", \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + double ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::TYPE_ERROR), ret); +} + +/** +* @tc.name: GetValueAndSetToDoubleOk +* @tc.desc: Test json string, get double ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToDoubleOk, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 1212121211255.99, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + double ageNumber; + int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root.get(), "age", ageNumber); + EXPECT_EQ(CAST_INT(JsonParseError::ERR_OK), ret); + EXPECT_EQ(1212121211255.99, ageNumber); +} + +/** +* @tc.name: IsIntegerTrue +* @tc.desc: check isInteger true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, IsIntegerTrue, TestSize.Level1) +{ + double d = 100; + bool bl = UpdateServiceJsonUtils::IsInteger(d); + EXPECT_EQ(true, bl); +} + +/** +* @tc.name: IsIntegerFalse +* @tc.desc: check isInteger false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, IsIntegerFalse, TestSize.Level1) +{ + double d = 100.123; + bool bl = UpdateServiceJsonUtils::IsInteger(d); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckIntegerInt32True +* @tc.desc: test json string, check int32 true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerInt32True, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + int32_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); + EXPECT_EQ(true, bl); +} + +/** +* @tc.name: CheckIntegerInt32False +* @tc.desc: test json string, check int32 false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerInt32False, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123.11, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + int32_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckIntegerInt64True +* @tc.desc: test json string, check int64 true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerInt64True, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123454545, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + int64_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); + EXPECT_EQ(true, bl); +} + +/** +* @tc.name: CheckIntegerInt64False +* @tc.desc: test json string, check int64 false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerInt64False, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123.11111, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + int64_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckIntegerUInt32True +* @tc.desc: test json string, check uint32 true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerUInt32True, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + uint32_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); + EXPECT_EQ(true, bl); +} + +/** +* @tc.name: CheckIntegerUInt32False +* @tc.desc: test json string, check uint32 false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerUInt32False, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + uint32_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); + EXPECT_EQ(false, bl); +} + + +/** +* @tc.name: CheckIntegerUInt64True +* @tc.desc: test json string, check uint64 true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerUInt64True, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + uint64_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); + EXPECT_EQ(true, bl); +} + +/** +* @tc.name: CheckIntegerUInt64False +* @tc.desc: test json string, check uint64 false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckIntegerUInt64False, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + uint64_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckInteger(item, ageNumber); + EXPECT_EQ(false, bl); +} + +//CheckTypeAndAsign +/** +* @tc.name: CheckTypeAndAsignStringError +* @tc.desc: test json string, check String is error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignStringError, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": -111, \"file\": 1234}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "file"); + ASSERT_NE(nullptr, item); std::string fileName; - cJSON *root = cJSON_Parse(jsonStr.c_str()); - EXPECT_NE(nullptr, root); - int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root, "file", fileName); - EXPECT_EQ(fileName, "a.txt"); - EXPECT_EQ(0, ret); - cJSON_Delete(root); + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, fileName); + EXPECT_EQ(false, bl); } /** -* @tc.name: GetValueAndSetToBool -* @tc.desc: Test json string, get bool +* @tc.name: CheckTypeAndAsignStringOk +* @tc.desc: test json string, check String is ok * @tc.type: FUNC */ -HWTEST_F(UpdateServiceJsonUtilsTest, GetValueAndSetToBool, TestSize.Level1) +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignStringOk, TestSize.Level1) { - std::string jsonStr = "{\"exist\": true, \"file\": \"a.txt\"}"; - bool existYN = false; - cJSON *root = cJSON_Parse(jsonStr.c_str()); - EXPECT_NE(nullptr, root); - int ret = UpdateServiceJsonUtils::GetValueAndSetTo(root, "exist", existYN); - EXPECT_EQ(existYN, true); - EXPECT_EQ(0, ret); - cJSON_Delete(root); + std::string jsonStr = "{\"age\": -111, \"file\": \"1234.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "file"); + ASSERT_NE(nullptr, item); + std::string fileName; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, fileName); + EXPECT_EQ(true, bl); + EXPECT_EQ("1234.txt", fileName); +} + +/** +* @tc.name: CheckTypeAndAsignBoolError +* @tc.desc: test json string, check bool is error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignBoolError, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"boolFlag\": 123}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "boolFlag"); + ASSERT_NE(nullptr, item); + bool boolFlag; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, boolFlag); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckTypeAndAsignBoolOk +* @tc.desc: test json string, check bool is ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignBoolOk, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 54, \"boolFlag\": true}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "boolFlag"); + ASSERT_NE(nullptr, item); + bool boolFlag = false; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, boolFlag); + EXPECT_EQ(true, bl); + EXPECT_EQ(true, bl); +} + +/** +* @tc.name: CheckTypeAndAsignInt32True +* @tc.desc: test json string, check int32 true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignInt32True, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + int32_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(true, bl); + EXPECT_EQ(123, ageNumber); +} + +/** +* @tc.name: CheckTypeAndAsignInt32False +* @tc.desc: test json string, check int32 false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignInt32False, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123.11, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + int32_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckTypeAndAsignInt64True +* @tc.desc: test json string, check int64 true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignInt64True, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123454545, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + int64_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(true, bl); + EXPECT_EQ(123454545, ageNumber); +} + +/** +* @tc.name: CheckTypeAndAsignInt64False +* @tc.desc: test json string, check int64 false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignInt64False, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123.11111, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + int64_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckTypeAndAsignUInt32True +* @tc.desc: test json string, check uint32 true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignUInt32True, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 123, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + uint32_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(true, bl); + EXPECT_EQ(123, ageNumber); +} + +/** +* @tc.name: CheckTypeAndAsignUInt32False +* @tc.desc: test json string, check uint32 false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignUInt32False, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + uint32_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckTypeAndAsignUInt64True +* @tc.desc: test json string, check uint64 true +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignUInt64True, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 12312121, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + uint64_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(true, bl); + EXPECT_EQ(12312121, ageNumber); +} + +/** +* @tc.name: CheckTypeAndAsignUInt64False +* @tc.desc: test json string, check uint64 false +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignUInt64False, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": -111, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + uint64_t ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckTypeAndAsignDoubleFalse +* @tc.desc: Test json string, Double type error +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignDoubleFalse, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": \"NAN\", \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + double ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(false, bl); +} + +/** +* @tc.name: CheckTypeAndAsignDoubleTrue +* @tc.desc: Test json string, get double ok +* @tc.type: FUNC +*/ +HWTEST_F(UpdateServiceJsonUtilsTest, CheckTypeAndAsignDoubleTrue, TestSize.Level1) +{ + std::string jsonStr = "{\"age\": 1212121211255.99, \"file\": \"a.txt\"}"; + auto root = UpdateServiceJsonUtils::ParseJson(jsonStr); + cJSON *item = cJSON_GetObjectItemCaseSensitive(root.get(), "age"); + ASSERT_NE(nullptr, item); + double ageNumber; + bool bl = UpdateServiceJsonUtils::CheckTypeAndAsign(item, ageNumber); + EXPECT_EQ(true, bl); + EXPECT_EQ(1212121211255.99, ageNumber); } } // namespace OHOS::UpdateService \ No newline at end of file