diff --git a/interfaces/inner_api/appexecfwk_base/include/json_util.h b/interfaces/inner_api/appexecfwk_base/include/json_util.h index d49f5237446ec2acb8ab71d7c996134d9f206dde..0603e9ba7eb733d426db56960dc3f6421f029259 100644 --- a/interfaces/inner_api/appexecfwk_base/include/json_util.h +++ b/interfaces/inner_api/appexecfwk_base/include/json_util.h @@ -48,7 +48,8 @@ public: static void GetBoolValueIfFindKey(const nlohmann::json &jsonObject, const nlohmann::detail::iter_impl &end, const std::string &key, bool &data, bool isNecessary, int32_t &parseResult); - static bool CheckMapValueType(JsonType valueType, const nlohmann::json &value); + static bool CheckArrayValueType(const nlohmann::json &value, ArrayType arrayType); + static bool CheckMapValueType(const nlohmann::json &value, JsonType valueType, ArrayType arrayType); }; template @@ -211,10 +212,12 @@ void GetBigStringIfFindKey(const nlohmann::json &jsonObject, } } +// valueType: the type of map value +// arrayType: when valueType is ARRAY, the type of array item template void GetMapValueIfFindKey(const nlohmann::json &jsonObject, const nlohmann::detail::iter_impl &end, const std::string &key, dataType &data, - bool isNecessary, int32_t &parseResult, JsonType valueType) + bool isNecessary, int32_t &parseResult, JsonType valueType, ArrayType arrayType) { if (parseResult != ERR_OK) { return; @@ -226,7 +229,7 @@ void GetMapValueIfFindKey(const nlohmann::json &jsonObject, return; } for (const auto& [mapKey, mapValue] : jsonObject.at(key).items()) { - if (!BMSJsonUtil::CheckMapValueType(valueType, mapValue)) { + if (!BMSJsonUtil::CheckMapValueType(mapValue, valueType, arrayType)) { APP_LOGE("type error key:%{public}s", mapKey.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; return; diff --git a/interfaces/inner_api/appexecfwk_base/src/bms_json_util.cpp b/interfaces/inner_api/appexecfwk_base/src/bms_json_util.cpp index 30e6be2401c614e1f71157116c5d19a5f7e40301..465e16730172aa122fc52fe191d44f6bf15a2be7 100644 --- a/interfaces/inner_api/appexecfwk_base/src/bms_json_util.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/bms_json_util.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "form_info_base.h" #include "json_util.h" namespace OHOS { @@ -62,7 +63,38 @@ void BMSJsonUtil::GetBoolValueIfFindKey(const nlohmann::json &jsonObject, } } -bool BMSJsonUtil::CheckMapValueType(JsonType valueType, const nlohmann::json &value) +bool BMSJsonUtil::CheckArrayValueType(const nlohmann::json &value, ArrayType arrayType) +{ + if (!value.is_array()) { + return false; + } + switch (arrayType) { + case ArrayType::NUMBER: + if (value.empty()) { + return true; + } + for (const auto &item : value) { + if (!item.is_number()) { + return false; + } + } + return true; + case ArrayType::STRING: + if (value.empty()) { + return true; + } + for (const auto &item : value) { + if (!item.is_string()) { + return false; + } + } + return true; + default: + return false; + } +} + +bool BMSJsonUtil::CheckMapValueType(const nlohmann::json &value, JsonType valueType, ArrayType arrayType) { switch (valueType) { case JsonType::BOOLEAN: @@ -71,6 +103,8 @@ bool BMSJsonUtil::CheckMapValueType(JsonType valueType, const nlohmann::json &va return value.is_number(); case JsonType::STRING: return value.is_string(); + case JsonType::ARRAY: + return CheckArrayValueType(value, arrayType); default: return false; } diff --git a/interfaces/inner_api/appexecfwk_base/src/free_install/target_ability_info.cpp b/interfaces/inner_api/appexecfwk_base/src/free_install/target_ability_info.cpp index c4f02487019f17cbca9bc48070d8d0924f296a86..7bb3795850472d0c7ec2da16a4c5c8a86c2567fe 100644 --- a/interfaces/inner_api/appexecfwk_base/src/free_install/target_ability_info.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/free_install/target_ability_info.cpp @@ -55,13 +55,13 @@ void from_json(const nlohmann::json &jsonObject, TargetExtSetting &targetExtSett { const auto &jsonObjectEnd = jsonObject.end(); int32_t parseResult = ERR_OK; - GetValueIfFindKey>(jsonObject, + GetMapValueIfFindKey>(jsonObject, jsonObjectEnd, JSON_KEY_EXTINFO, targetExtSetting.extValues, - JsonType::OBJECT, false, parseResult, + JsonType::STRING, ArrayType::NOT_ARRAY); if (parseResult != ERR_OK) { LOG_E(BMS_TAG_DEFAULT, "read module targetExtSetting from jsonObject error: %{public}d", parseResult); diff --git a/interfaces/inner_api/appexecfwk_base/src/hap_module_info.cpp b/interfaces/inner_api/appexecfwk_base/src/hap_module_info.cpp index d4a0241b2478cef6f9f00a6e32dd07edd9b70ad1..6b991909ff171ae4a4e6426627b7c28130e0fde7 100644 --- a/interfaces/inner_api/appexecfwk_base/src/hap_module_info.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/hap_module_info.cpp @@ -401,13 +401,13 @@ void from_json(const nlohmann::json &jsonObject, RouterItem &routerItem) routerItem.moduleName, false, parseResult); - GetValueIfFindKey>(jsonObject, + GetMapValueIfFindKey>(jsonObject, jsonObjectEnd, ROUTER_ITEM_KEY_DATA, routerItem.data, - JsonType::OBJECT, false, parseResult, + JsonType::STRING, ArrayType::NOT_ARRAY); if (parseResult != ERR_OK) { APP_LOGE("read RouterItem jsonObject error : %{public}d", parseResult); @@ -1023,14 +1023,14 @@ void from_json(const nlohmann::json &jsonObject, HapModuleInfo &hapModuleInfo) false, parseResult, ArrayType::STRING); - GetValueIfFindKey>>(jsonObject, + GetMapValueIfFindKey>>(jsonObject, jsonObjectEnd, HAP_MODULE_INFO_REQUIRED_DEVICE_FEATURES, hapModuleInfo.requiredDeviceFeatures, - JsonType::OBJECT, false, parseResult, - ArrayType::NOT_ARRAY); + JsonType::ARRAY, + ArrayType::STRING); GetValueIfFindKey>(jsonObject, jsonObjectEnd, HAP_MODULE_INFO_ABILITY_INFOS, @@ -1125,13 +1125,13 @@ void from_json(const nlohmann::json &jsonObject, HapModuleInfo &hapModuleInfo) hapModuleInfo.isStageBasedModel, false, parseResult); - GetValueIfFindKey>(jsonObject, + GetMapValueIfFindKey>(jsonObject, jsonObjectEnd, HAP_MODULE_INFO_IS_REMOVABLE, hapModuleInfo.isRemovable, - JsonType::OBJECT, false, parseResult, + JsonType::BOOLEAN, ArrayType::NOT_ARRAY); GetValueIfFindKey(jsonObject, jsonObjectEnd, diff --git a/interfaces/inner_api/appexecfwk_base/src/shortcut_info.cpp b/interfaces/inner_api/appexecfwk_base/src/shortcut_info.cpp index dba662dba9d04b86edbfcaa746b941d0315eef39..04921f650e49ca82cd2b7a989a69b181c0e60ffe 100644 --- a/interfaces/inner_api/appexecfwk_base/src/shortcut_info.cpp +++ b/interfaces/inner_api/appexecfwk_base/src/shortcut_info.cpp @@ -198,7 +198,8 @@ void from_json(const nlohmann::json &jsonObject, ShortcutIntent &shortcutIntent) shortcutIntent.parameters, false, parseResult, - JsonType::STRING); + JsonType::STRING, + ArrayType::NOT_ARRAY); if (parseResult != ERR_OK) { APP_LOGE("read shortcutIntent jsonObject error : %{public}d", parseResult); } @@ -349,7 +350,8 @@ void from_json(const nlohmann::json &jsonObject, ShortcutWant &shortcutWant) shortcutWant.parameters, false, parseResult, - JsonType::STRING); + JsonType::STRING, + ArrayType::NOT_ARRAY); if (parseResult != ERR_OK) { APP_LOGE("read shortcutWant module.json error : %{public}d", parseResult); } diff --git a/interfaces/inner_api/test/unittest/bundle_info_test/bundle_info_test.cpp b/interfaces/inner_api/test/unittest/bundle_info_test/bundle_info_test.cpp index 3370f0e9477368bf2d5b1d8b72239c1d2f00e0f8..0ab2bbc084e9cd7be792940b721606aa9565a35f 100644 --- a/interfaces/inner_api/test/unittest/bundle_info_test/bundle_info_test.cpp +++ b/interfaces/inner_api/test/unittest/bundle_info_test/bundle_info_test.cpp @@ -22,6 +22,7 @@ #include "string_ex.h" #include #include +#include using namespace testing::ext; @@ -85,32 +86,32 @@ HWTEST_F(BundleInfoTest, Json_Util_Test_0100, Function | SmallTest | Level0) std::map parameters; int32_t parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_STR, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::NUMBER); + "parameters", parameters, false, parseResult, JsonType::NUMBER, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_STR, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::STRING); + "parameters", parameters, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_OK); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_STR, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::BOOLEAN); + "parameters", parameters, false, parseResult, JsonType::BOOLEAN, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; GetMapValueIfFindKey>(JSON_DATA_STR, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::STRING); + "parameters", parameters, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_STR, jsonObjectEnd, - "notfound", parameters, true, parseResult, JsonType::STRING); + "notfound", parameters, true, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_STR, jsonObjectEnd, - "notfound", parameters, false, parseResult, JsonType::STRING); + "notfound", parameters, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_OK); } @@ -135,32 +136,32 @@ HWTEST_F(BundleInfoTest, Json_Util_Test_0200, Function | SmallTest | Level0) std::map parameters; int32_t parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_INT, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::NUMBER); + "parameters", parameters, false, parseResult, JsonType::NUMBER, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_OK); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_INT, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::STRING); + "parameters", parameters, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_INT, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::BOOLEAN); + "parameters", parameters, false, parseResult, JsonType::BOOLEAN, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; GetMapValueIfFindKey>(JSON_DATA_INT, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::NUMBER); + "parameters", parameters, false, parseResult, JsonType::NUMBER, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_INT, jsonObjectEnd, - "notfound", parameters, true, parseResult, JsonType::NUMBER); + "notfound", parameters, true, parseResult, JsonType::NUMBER, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_INT, jsonObjectEnd, - "notfound", parameters, false, parseResult, JsonType::NUMBER); + "notfound", parameters, false, parseResult, JsonType::NUMBER, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_OK); } @@ -185,41 +186,257 @@ HWTEST_F(BundleInfoTest, Json_Util_Test_0300, Function | SmallTest | Level0) std::map parameters; int32_t parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_BOOL, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::NUMBER); + "parameters", parameters, false, parseResult, JsonType::NUMBER, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_BOOL, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::STRING); + "parameters", parameters, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_BOOL, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::BOOLEAN); + "parameters", parameters, false, parseResult, JsonType::BOOLEAN, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_OK); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; GetMapValueIfFindKey>(JSON_DATA_BOOL, jsonObjectEnd, - "parameters", parameters, false, parseResult, JsonType::BOOLEAN); + "parameters", parameters, false, parseResult, JsonType::BOOLEAN, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_BOOL, jsonObjectEnd, - "notfound", parameters, true, parseResult, JsonType::BOOLEAN); + "notfound", parameters, true, parseResult, JsonType::BOOLEAN, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP); parseResult = ERR_OK; GetMapValueIfFindKey>(JSON_DATA_BOOL, jsonObjectEnd, - "notfound", parameters, false, parseResult, JsonType::BOOLEAN); + "notfound", parameters, false, parseResult, JsonType::BOOLEAN, ArrayType::NOT_ARRAY); EXPECT_EQ(parseResult, ERR_OK); } /** * @tc.number: Json_Util_Test_0400 * @tc.name: Json_Util_Test_0400 - * @tc.desc: test CheckMapValueType + * @tc.desc: test GetMapValueIfFindKey */ HWTEST_F(BundleInfoTest, Json_Util_Test_0400, Function | SmallTest | Level0) +{ + const nlohmann::json JSON_DATA = R"( + { + "parameters": { + "shortCutKey": [1, 2, 3], + "flag": [1, 2, 3], + "uri": [] + } + } + )"_json; + + const auto &jsonObjectEnd = JSON_DATA.end(); + std::map> parameters; + int32_t parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::NUMBER); + EXPECT_EQ(parseResult, ERR_OK); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::STRING); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::OBJECT); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); +} + +/** + * @tc.number: Json_Util_Test_0500 + * @tc.name: Json_Util_Test_0500 + * @tc.desc: test GetMapValueIfFindKey + */ +HWTEST_F(BundleInfoTest, Json_Util_Test_0500, Function | SmallTest | Level0) +{ + const nlohmann::json JSON_DATA = R"( + { + "parameters": { + "shortCutKey": ["ok", "err"], + "flag": ["ok", "err"], + "uri": [] + } + } + )"_json; + + const auto &jsonObjectEnd = JSON_DATA.end(); + std::map> parameters; + int32_t parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::NUMBER); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::STRING); + EXPECT_EQ(parseResult, ERR_OK); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::OBJECT); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); +} + +/** + * @tc.number: Json_Util_Test_0600 + * @tc.name: Json_Util_Test_0600 + * @tc.desc: test GetMapValueIfFindKey + */ +HWTEST_F(BundleInfoTest, Json_Util_Test_0600, Function | SmallTest | Level0) +{ + const nlohmann::json JSON_DATA = R"( + { + "parameters": { + "shortCutKey": [], + "flag": [], + "uri": [] + } + } + )"_json; + + const auto &jsonObjectEnd = JSON_DATA.end(); + std::map> parameters; + int32_t parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::NUMBER); + EXPECT_EQ(parseResult, ERR_OK); + + std::map> parameters2; + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters2, false, parseResult, JsonType::ARRAY, ArrayType::STRING); + EXPECT_EQ(parseResult, ERR_OK); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::OBJECT); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); +} + +/** + * @tc.number: Json_Util_Test_0700 + * @tc.name: Json_Util_Test_0700 + * @tc.desc: test GetMapValueIfFindKey + */ +HWTEST_F(BundleInfoTest, Json_Util_Test_0700, Function | SmallTest | Level0) +{ + const nlohmann::json JSON_DATA = R"( + { + "parameters": { + "shortCutKey": [1, 2, 3], + "flag": 1, + "uri": "test" + } + } + )"_json; + + const auto &jsonObjectEnd = JSON_DATA.end(); + std::map> parameters; + int32_t parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parameters, false, parseResult, JsonType::ARRAY, ArrayType::NUMBER); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + std::map> parametersStr; + parseResult = ERR_OK; + GetMapValueIfFindKey>>(JSON_DATA, jsonObjectEnd, + "parameters", parametersStr, false, parseResult, JsonType::ARRAY, ArrayType::STRING); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + const nlohmann::json JSON_DATA2 = R"( + { + "parameters": { + "flag": 1, + "uri": "test", + "shortCutKey": [1, 2, 3] + } + } + )"_json; + + const auto &jsonObjectEnd2 = JSON_DATA2.end(); + std::map parameters2; + parseResult = ERR_OK; + GetMapValueIfFindKey>(JSON_DATA2, jsonObjectEnd2, + "parameters", parameters2, false, parseResult, JsonType::NUMBER, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + const nlohmann::json JSON_DATA3 = R"( + { + "parameters": { + "uri": "test", + "flag": 1, + "shortCutKey": [1, 2, 3] + } + } + )"_json; + const auto &jsonObjectEnd3 = JSON_DATA3.end(); + std::map parameters3; + parseResult = ERR_OK; + GetMapValueIfFindKey>(JSON_DATA3, jsonObjectEnd3, + "parameters", parameters3, false, parseResult, JsonType::STRING, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); + + const nlohmann::json JSON_DATA4 = R"( + { + "parameters": { + "flag": true, + "uri": 1, + "shortCutKey": [1, 2, 3] + } + } + )"_json; + const auto &jsonObjectEnd4 = JSON_DATA4.end(); + std::map parameters4; + parseResult = ERR_OK; + GetMapValueIfFindKey>(JSON_DATA4, jsonObjectEnd4, + "parameters", parameters4, false, parseResult, JsonType::BOOLEAN, ArrayType::NOT_ARRAY); + EXPECT_EQ(parseResult, ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR); +} + +/** + * @tc.number: CheckMapValueType_0100 + * @tc.name: CheckMapValueType_0100 + * @tc.desc: test CheckMapValueType + */ +HWTEST_F(BundleInfoTest, CheckMapValueType_0100, Function | SmallTest | Level0) { const nlohmann::json JSON_DATA = R"( { @@ -229,24 +446,61 @@ HWTEST_F(BundleInfoTest, Json_Util_Test_0400, Function | SmallTest | Level0) "stringValue": "test" } )"_json; - EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JsonType::BOOLEAN, JSON_DATA.at("boolValue"))); - EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JsonType::BOOLEAN, JSON_DATA.at("boolValueF"))); - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::BOOLEAN, JSON_DATA.at("numberValue"))); - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::BOOLEAN, JSON_DATA.at("stringValue"))); - - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::NUMBER, JSON_DATA.at("boolValue"))); - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::NUMBER, JSON_DATA.at("boolValueF"))); - EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JsonType::NUMBER, JSON_DATA.at("numberValue"))); - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::NUMBER, JSON_DATA.at("stringValue"))); - - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::STRING, JSON_DATA.at("boolValue"))); - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::STRING, JSON_DATA.at("boolValueF"))); - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::STRING, JSON_DATA.at("numberValue"))); - EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JsonType::STRING, JSON_DATA.at("stringValue"))); - - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::ARRAY, JSON_DATA.at("boolValue"))); - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::NULLABLE, JSON_DATA.at("boolValueF"))); - EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JsonType::OBJECT, JSON_DATA.at("numberValue"))); + EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValue"), JsonType::BOOLEAN, ArrayType::NOT_ARRAY)); + EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValueF"), JsonType::BOOLEAN, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("numberValue"), JsonType::BOOLEAN, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("stringValue"), JsonType::BOOLEAN, ArrayType::NOT_ARRAY)); + + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValue"), JsonType::NUMBER, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValueF"), JsonType::NUMBER, ArrayType::NOT_ARRAY)); + EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("numberValue"), JsonType::NUMBER, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("stringValue"), JsonType::NUMBER, ArrayType::NOT_ARRAY)); + + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValue"), JsonType::STRING, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValueF"), JsonType::STRING, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("numberValue"), JsonType::STRING, ArrayType::NOT_ARRAY)); + EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("stringValue"), JsonType::STRING, ArrayType::NOT_ARRAY)); + + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValue"), JsonType::ARRAY, ArrayType::NUMBER)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValueF"), JsonType::ARRAY, ArrayType::OBJECT)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("numberValue"), JsonType::ARRAY, ArrayType::STRING)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("stringValue"), JsonType::ARRAY, ArrayType::NOT_ARRAY)); +} + +/** + * @tc.number: CheckMapValueType_0200 + * @tc.name: CheckMapValueType_0200 + * @tc.desc: test CheckMapValueType + */ +HWTEST_F(BundleInfoTest, CheckMapValueType_0200, Function | SmallTest | Level0) +{ + const nlohmann::json JSON_DATA = R"( + { + "boolValue": [true, false], + "numberValue": [1, 2, 3], + "stringValue": ["test", "test2"], + "emptyArray": [] + } + )"_json; + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValue"), JsonType::ARRAY, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("numberValue"), JsonType::ARRAY, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("stringValue"), JsonType::ARRAY, ArrayType::NOT_ARRAY)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("emptyArray"), JsonType::ARRAY, ArrayType::NOT_ARRAY)); + + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValue"), JsonType::ARRAY, ArrayType::NUMBER)); + EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("numberValue"), JsonType::ARRAY, ArrayType::NUMBER)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("stringValue"), JsonType::ARRAY, ArrayType::NUMBER)); + EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("emptyArray"), JsonType::ARRAY, ArrayType::NUMBER)); + + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValue"), JsonType::ARRAY, ArrayType::STRING)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("numberValue"), JsonType::ARRAY, ArrayType::STRING)); + EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("stringValue"), JsonType::ARRAY, ArrayType::STRING)); + EXPECT_TRUE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("emptyArray"), JsonType::ARRAY, ArrayType::STRING)); + + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("boolValue"), JsonType::ARRAY, ArrayType::OBJECT)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("numberValue"), JsonType::ARRAY, ArrayType::OBJECT)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("stringValue"), JsonType::ARRAY, ArrayType::OBJECT)); + EXPECT_FALSE(BMSJsonUtil::CheckMapValueType(JSON_DATA.at("emptyArray"), JsonType::ARRAY, ArrayType::OBJECT)); } } // AppExecFwk } // OHOS diff --git a/services/bundlemgr/src/inner_bundle_info.cpp b/services/bundlemgr/src/inner_bundle_info.cpp index 9f5713b3ffec1ada446b807c15b714c54247d665..f192779102b39e0101d2968872b03f47771f59ea 100644 --- a/services/bundlemgr/src/inner_bundle_info.cpp +++ b/services/bundlemgr/src/inner_bundle_info.cpp @@ -719,13 +719,13 @@ void from_json(const nlohmann::json &jsonObject, InnerModuleInfo &info) info.installationFree, false, parseResult); - GetValueIfFindKey>(jsonObject, + GetMapValueIfFindKey>(jsonObject, jsonObjectEnd, MODULE_IS_REMOVABLE, info.isRemovable, - JsonType::OBJECT, false, parseResult, + JsonType::BOOLEAN, ArrayType::NOT_ARRAY); GetValueIfFindKey(jsonObject, jsonObjectEnd, @@ -779,14 +779,14 @@ void from_json(const nlohmann::json &jsonObject, InnerModuleInfo &info) false, parseResult, ArrayType::STRING); - GetValueIfFindKey>>(jsonObject, + GetMapValueIfFindKey>>(jsonObject, jsonObjectEnd, MODULE_REQUIRED_DEVICE_FEATURES, info.requiredDeviceFeatures, - JsonType::OBJECT, false, parseResult, - ArrayType::NOT_ARRAY); + JsonType::ARRAY, + ArrayType::STRING); BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, MODULE_VIRTUAL_MACHINE, diff --git a/services/bundlemgr/src/module_profile.cpp b/services/bundlemgr/src/module_profile.cpp index 3ea3d29a0988e9854c74388a1f29d96855ade436..a06b1585a9232cfa9e9652ea3818fa32907914e8 100644 --- a/services/bundlemgr/src/module_profile.cpp +++ b/services/bundlemgr/src/module_profile.cpp @@ -1435,14 +1435,14 @@ void from_json(const nlohmann::json &jsonObject, Module &module) true, g_parseResult, ArrayType::STRING); - GetValueIfFindKey>>(jsonObject, + GetMapValueIfFindKey>>(jsonObject, jsonObjectEnd, MODULE_REQUIRED_DEVICE_FEATURES, module.requiredDeviceFeatures, - JsonType::OBJECT, false, g_parseResult, - ArrayType::NOT_ARRAY); + JsonType::ARRAY, + ArrayType::STRING); BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, MODULE_DELIVERY_WITH_INSTALL,