diff --git a/bundle.json b/bundle.json index c321683c3307445b7ee59e9431ea2f97f61745b2..54a336a1e77d35a81db53c8a8417e5050d008bd3 100644 --- a/bundle.json +++ b/bundle.json @@ -91,7 +91,8 @@ "appspawn", "bounds_checking_function", "webview", - "user_auth_framework" + "user_auth_framework", + "cJSON" ], "third_party": [ "libuv", diff --git a/services/bundlemgr/BUILD.gn b/services/bundlemgr/BUILD.gn index 8c854da0868eda7a9a4111da9bf47876c629a338..eca1e8e948be076022715c01db2511136b942549 100644 --- a/services/bundlemgr/BUILD.gn +++ b/services/bundlemgr/BUILD.gn @@ -108,6 +108,7 @@ ohos_source_set("parser_common") { ] public_external_deps = [ + "cJSON:cjson", "json:nlohmann_json_static", "zlib:libz", ] @@ -204,6 +205,7 @@ ohos_source_set("bundle_parser") { ] public_external_deps = [ + "cJSON:cjson", "json:nlohmann_json_static", "zlib:libz", ] diff --git a/services/bundlemgr/include/bundle_backup/bundle_backup_mgr.h b/services/bundlemgr/include/bundle_backup/bundle_backup_mgr.h index ad9c0a5b5f2d85a6f0aaa0190e2772a8a5355b78..4415753ba1d4e2acccd2b3879c7b26c15737941a 100644 --- a/services/bundlemgr/include/bundle_backup/bundle_backup_mgr.h +++ b/services/bundlemgr/include/bundle_backup/bundle_backup_mgr.h @@ -16,8 +16,8 @@ #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_BACKUP_MGR_H #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_BACKUP_MGR_H +#include "cJSON.h" #include "iremote_stub.h" -#include "nlohmann/json.hpp" #include "singleton.h" namespace OHOS { diff --git a/services/bundlemgr/include/bundle_backup/bundle_backup_service.h b/services/bundlemgr/include/bundle_backup/bundle_backup_service.h index 934c0f0ec23c62a1784d2fb948a000a592867c86..680bab3fb8aa7457762e746b0da58038e438909c 100644 --- a/services/bundlemgr/include/bundle_backup/bundle_backup_service.h +++ b/services/bundlemgr/include/bundle_backup/bundle_backup_service.h @@ -18,7 +18,7 @@ #include "appexecfwk_errors.h" #include "bundle_data_mgr.h" -#include "nlohmann/json.hpp" +#include "cJSON.h" #include "shortcut_data_storage_interface.h" #include "singleton.h" @@ -30,8 +30,8 @@ public: BundleBackupService(); ~BundleBackupService(); - ErrCode OnBackup(nlohmann::json &jsonObject); - ErrCode OnRestore(nlohmann::json &jsonObject); + ErrCode OnBackup(cJSON*& jsonObject); + ErrCode OnRestore(cJSON* jsonObject); private: std::shared_ptr shortcutStorage_; diff --git a/services/bundlemgr/include/bundle_data_mgr.h b/services/bundlemgr/include/bundle_data_mgr.h index af96eb3739afa417d5e68d0fa52cc9a3c29076ad..ea3bfe9ddce232df241f803ca97b8b02a5758898 100644 --- a/services/bundlemgr/include/bundle_data_mgr.h +++ b/services/bundlemgr/include/bundle_data_mgr.h @@ -1162,7 +1162,7 @@ public: ErrCode GetAllShortcutInfoForSelf(std::vector &shortcutInfos); bool GreatOrEqualTargetAPIVersion(const int32_t platformVersion, const int32_t minorVersion, const int32_t patchVersion); - void CheckIfShortcutBundleExist(nlohmann::json &jsonResult); + void CheckIfShortcutBundleExist(cJSON* jsonResult); ErrCode IsSystemApp(const std::string &bundleName, bool &isSystemApp); private: diff --git a/services/bundlemgr/include/shortcut_data_storage_interface.h b/services/bundlemgr/include/shortcut_data_storage_interface.h index 3b3d0afb4477b99c199705781946d671c375ab97..ba01b5b171a85fdc897ac36ed9639e106c0d6d45 100644 --- a/services/bundlemgr/include/shortcut_data_storage_interface.h +++ b/services/bundlemgr/include/shortcut_data_storage_interface.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 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 @@ -16,6 +16,7 @@ #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHORTCUT_DATA_STORAGE_INTERFACE_H #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHORTCUT_DATA_STORAGE_INTERFACE_H +#include "cJSON.h" #include "json_serializer.h" #include "shortcut_info.h" @@ -30,8 +31,8 @@ public: virtual void GetAllDesktopShortcutInfo(int32_t userId, std::vector &shortcutInfos) = 0; virtual bool DeleteDesktopShortcutInfo(const std::string &bundleName) = 0; virtual bool DeleteDesktopShortcutInfo(const std::string &bundleName, int32_t userId, int32_t appIndex) = 0; - virtual bool GetAllTableDataToJson(nlohmann::json &jsonResult) = 0; - virtual bool UpdateAllShortcuts(nlohmann::json &jsonResult) = 0; + virtual bool GetAllTableDataToJson(cJSON*& jsonResult) = 0; + virtual bool UpdateAllShortcuts(cJSON* jsonResult) = 0; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/include/shortcut_data_storage_rdb.h b/services/bundlemgr/include/shortcut_data_storage_rdb.h index a7d7f530a4f09b6d77c68f75fae0db6e4f00bd55..c04b1aece4520ce39cf7dcf2b076aa1804c34811 100644 --- a/services/bundlemgr/include/shortcut_data_storage_rdb.h +++ b/services/bundlemgr/include/shortcut_data_storage_rdb.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 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 @@ -33,8 +33,8 @@ public: void GetAllDesktopShortcutInfo(int32_t userId, std::vector &shortcutInfos); bool DeleteDesktopShortcutInfo(const std::string &bundleName); bool DeleteDesktopShortcutInfo(const std::string &bundleName, int32_t userId, int32_t appIndex); - bool GetAllTableDataToJson(nlohmann::json &jsonResult); - bool UpdateAllShortcuts(nlohmann::json &jsonResult); + bool GetAllTableDataToJson(cJSON*& jsonResult); + bool UpdateAllShortcuts(cJSON* jsonResult); private: bool ShortcutIdVerification(const ShortcutInfo &shortcutInfo, int32_t userId); diff --git a/services/bundlemgr/src/bundle_backup/bundle_backup_mgr.cpp b/services/bundlemgr/src/bundle_backup/bundle_backup_mgr.cpp index 7f89fe0f86e60fb412f06a40f2c9e9b9d0dcac6b..1ba25eebe8c6cdd7c2077e46d1843c546ca89413 100644 --- a/services/bundlemgr/src/bundle_backup/bundle_backup_mgr.cpp +++ b/services/bundlemgr/src/bundle_backup/bundle_backup_mgr.cpp @@ -33,7 +33,11 @@ BundleBackupMgr::~BundleBackupMgr() {} ErrCode BundleBackupMgr::OnBackup(MessageParcel& data, MessageParcel& reply) { - nlohmann::json backupJson = nlohmann::json::array(); + cJSON* backupJson = cJSON_CreateArray(); + if (backupJson == nullptr) { + APP_LOGE("Failed to create JSON array"); + return ERR_APPEXECFWK_NULL_PTR; + } std::shared_ptr service = DelayedSingleton::GetInstance(); if (service == nullptr) { APP_LOGE("Get BundleBackupService failed"); @@ -41,24 +45,35 @@ ErrCode BundleBackupMgr::OnBackup(MessageParcel& data, MessageParcel& reply) } auto ret = service->OnBackup(backupJson); if (ret != ERR_OK) { + cJSON_Delete(backupJson); return ret; } - ret = SaveToFile(backupJson.dump()); + char* jsonStr = cJSON_PrintUnformatted(backupJson); + if (jsonStr == nullptr) { + APP_LOGE("Failed to print JSON to string"); + cJSON_Delete(backupJson); + return ERR_APPEXECFWK_NULL_PTR; + } + ret = SaveToFile(jsonStr); + cJSON_free(jsonStr); if (ret != ERR_OK) { APP_LOGE("Save backup config failed"); + cJSON_Delete(backupJson); return ret; } - int32_t fd = open(BACKUP_FILE_PATH, O_RDONLY); if (fd < 0) { APP_LOGE("Open backup file failed"); + cJSON_Delete(backupJson); return ERR_APPEXECFWK_BACKUP_FILE_IO_ERROR; } if (!reply.WriteFileDescriptor(fd)) { APP_LOGE("Write file descriptor failed"); close(fd); + cJSON_Delete(backupJson); return ERR_APPEXECFWK_PARCEL_ERROR; } + cJSON_Delete(backupJson); return ERR_OK; } @@ -76,13 +91,13 @@ ErrCode BundleBackupMgr::OnRestore(MessageParcel& data, MessageParcel& reply) return ret; } (void)remove(BACKUP_FILE_PATH); - if (config.empty() || !nlohmann::json::accept(config)) { - APP_LOGE("Invalid JSON format"); + if (config.empty()) { + APP_LOGE("Empty config data"); return ERR_APPEXECFWK_BACKUP_INVALID_JSON_STRUCTURE; } - auto json = nlohmann::json::parse(config, nullptr, false); - if (json.is_null()) { - APP_LOGE("Invalid JSON structure"); + cJSON* json = cJSON_Parse(config.c_str()); + if (json == nullptr) { + APP_LOGE("Invalid JSON format"); return ERR_APPEXECFWK_BACKUP_INVALID_JSON_STRUCTURE; } std::shared_ptr service = DelayedSingleton::GetInstance(); @@ -94,6 +109,7 @@ ErrCode BundleBackupMgr::OnRestore(MessageParcel& data, MessageParcel& reply) if (ret != ERR_OK) { return ret; } + cJSON_Delete(json); return ERR_OK; } diff --git a/services/bundlemgr/src/bundle_backup/bundle_backup_service.cpp b/services/bundlemgr/src/bundle_backup/bundle_backup_service.cpp index bf03bf52dfd77becd75d2091d160aa40d2866a32..0e242e826d02237753c78bdf67f03d8dfcc089d1 100644 --- a/services/bundlemgr/src/bundle_backup/bundle_backup_service.cpp +++ b/services/bundlemgr/src/bundle_backup/bundle_backup_service.cpp @@ -30,7 +30,7 @@ BundleBackupService::BundleBackupService() BundleBackupService::~BundleBackupService() {} -ErrCode BundleBackupService::OnBackup(nlohmann::json &jsonObject) +ErrCode BundleBackupService::OnBackup(cJSON*& jsonObject) { if (!shortcutStorage_->GetAllTableDataToJson(jsonObject)) { APP_LOGE("Failed to get shortcuts from storage"); @@ -39,7 +39,7 @@ ErrCode BundleBackupService::OnBackup(nlohmann::json &jsonObject) return ERR_OK; } -ErrCode BundleBackupService::OnRestore(nlohmann::json &jsonObject) +ErrCode BundleBackupService::OnRestore(cJSON* jsonObject) { if (dataMgr_ == nullptr) { return ERR_APPEXECFWK_NULL_PTR; diff --git a/services/bundlemgr/src/bundle_data_mgr.cpp b/services/bundlemgr/src/bundle_data_mgr.cpp index 85895804f0619bbf74c2a3fe54129615e50342dd..725fb5cce183dac957d779e9bf4c654303ca784b 100644 --- a/services/bundlemgr/src/bundle_data_mgr.cpp +++ b/services/bundlemgr/src/bundle_data_mgr.cpp @@ -38,6 +38,7 @@ #include "bundle_parser.h" #include "bundle_permission_mgr.h" #include "bundle_status_callback_death_recipient.h" +#include "cJSON.h" #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP #include "default_app_mgr.h" #endif @@ -11091,53 +11092,61 @@ std::string BundleDataMgr::GenerateUuidByKey(const std::string &key) const return OHOS::Security::Verify::GenerateUuidByKey(message); } -void BundleDataMgr::CheckIfShortcutBundleExist(nlohmann::json &jsonResult) +void BundleDataMgr::CheckIfShortcutBundleExist(cJSON* jsonResult) { - if (!jsonResult.is_array()) { + if (jsonResult == nullptr || !cJSON_IsArray(jsonResult)) { APP_LOGE("Invalid JSON format: expected array"); return; } - for (auto it = jsonResult.begin(); it != jsonResult.end();) { - if (!it->contains(BUNDLE_NAME) || !it->at(BUNDLE_NAME).is_string()) { - it = jsonResult.erase(it); + int arraySize = cJSON_GetArraySize(jsonResult); + for (int i = arraySize - 1; i >= 0; i--) { + cJSON* item = cJSON_GetArrayItem(jsonResult, i); + if (item == nullptr || !cJSON_IsObject(item)) { + cJSON_Delete(cJSON_DetachItemFromArray(jsonResult, i)); continue; } - if (!it->contains(APP_INDEX) || !it->at(APP_INDEX).is_number()) { - it = jsonResult.erase(it); + cJSON* bundleNameItem = cJSON_GetObjectItemCaseSensitive(item, BUNDLE_NAME.c_str()); + if (bundleNameItem == nullptr || !cJSON_IsString(bundleNameItem)) { + cJSON_Delete(cJSON_DetachItemFromArray(jsonResult, i)); continue; } - if (!it->contains(USER_ID) || !it->at(USER_ID).is_number()) { - it = jsonResult.erase(it); + cJSON* appIndexItem = cJSON_GetObjectItemCaseSensitive(item, APP_INDEX.c_str()); + if (appIndexItem == nullptr || !cJSON_IsNumber(appIndexItem)) { + cJSON_Delete(cJSON_DetachItemFromArray(jsonResult, i)); continue; } - std::string bundleName = (*it)[BUNDLE_NAME].get(); - int32_t appIndex = (*it)[APP_INDEX].get(); - int32_t userId = (*it)[USER_ID].get(); + cJSON* userIdItem = cJSON_GetObjectItemCaseSensitive(item, USER_ID.c_str()); + if (userIdItem == nullptr || !cJSON_IsNumber(userIdItem)) { + cJSON_Delete(cJSON_DetachItemFromArray(jsonResult, i)); + continue; + } + std::string bundleName = bundleNameItem->valuestring; + int32_t appIndex = appIndexItem->valueint; + int32_t userId = userIdItem->valueint; { std::shared_lock lock(bundleInfoMutex_); auto iter = bundleInfos_.find(bundleName); if (iter == bundleInfos_.end()) { - it = jsonResult.erase(it); + cJSON_Delete(cJSON_DetachItemFromArray(jsonResult, i)); continue; } if (!HasUserId(userId)) { - it = jsonResult.erase(it); + cJSON_Delete(cJSON_DetachItemFromArray(jsonResult, i)); continue; } InnerBundleUserInfo innerBundleUserInfo; if (!iter->second.GetInnerBundleUserInfo(userId, innerBundleUserInfo)) { - it = jsonResult.erase(it); + cJSON_Delete(cJSON_DetachItemFromArray(jsonResult, i)); continue; } if (appIndex != 0) { auto cloneIter = innerBundleUserInfo.cloneInfos.find(std::to_string(appIndex)); if (cloneIter == innerBundleUserInfo.cloneInfos.end()) { - it = jsonResult.erase(it); + cJSON_Delete(cJSON_DetachItemFromArray(jsonResult, i)); continue; } } } - ++it; } } } // namespace AppExecFwk diff --git a/services/bundlemgr/src/shortcut_data_storage_rdb.cpp b/services/bundlemgr/src/shortcut_data_storage_rdb.cpp index 0fbd4af20c78b214d0bded9ace41f746f395ea17..f0c5b297cb019fdb7adb1ab380aaf6dd562ec66f 100644 --- a/services/bundlemgr/src/shortcut_data_storage_rdb.cpp +++ b/services/bundlemgr/src/shortcut_data_storage_rdb.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 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 @@ -16,6 +16,7 @@ #include "shortcut_data_storage_rdb.h" #include "app_log_wrapper.h" +#include "cJSON.h" #include "json_serializer.h" #include "scope_guard.h" @@ -232,7 +233,7 @@ void ShortcutDataStorageRdb::GetDesktopShortcutInfosByDefaultUserId(std::vector< } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK); } -bool ShortcutDataStorageRdb::GetAllTableDataToJson(nlohmann::json &jsonResult) +bool ShortcutDataStorageRdb::GetAllTableDataToJson(cJSON*& jsonResult) { if (rdbDataManager_ == nullptr) { APP_LOGE("rdbDataManager is null"); @@ -251,42 +252,46 @@ bool ShortcutDataStorageRdb::GetAllTableDataToJson(nlohmann::json &jsonResult) return false; } do { - nlohmann::json rowData; + cJSON* rowData = cJSON_CreateObject(); + if (rowData == nullptr) { + APP_LOGE("cJSON_CreateObject failed"); + return false; + } std::string bundleName; if (absSharedResultSet->GetString(BUNDLE_NAME_INDEX, bundleName) == NativeRdb::E_OK) { - rowData[BUNDLE_NAME] = bundleName; + cJSON_AddStringToObject(rowData, BUNDLE_NAME.c_str(), bundleName.c_str()); } std::string shortcutId; if (absSharedResultSet->GetString(SHORTCUT_ID_INDEX, shortcutId) == NativeRdb::E_OK) { - rowData[SHORTCUT_ID] = shortcutId; + cJSON_AddStringToObject(rowData, SHORTCUT_ID.c_str(), shortcutId.c_str()); } int32_t userId = 0; if (absSharedResultSet->GetInt(USER_ID_INDEX, userId) == NativeRdb::E_OK) { - rowData[USER_ID] = userId; + cJSON_AddNumberToObject(rowData, USER_ID.c_str(), userId); } int32_t appIndex = 0; if (absSharedResultSet->GetInt(APP_INDEX_INDEX, appIndex) == NativeRdb::E_OK) { - rowData[APP_INDEX] = appIndex; + cJSON_AddNumberToObject(rowData, APP_INDEX.c_str(), appIndex); } std::string shortcutInfoJson; if (absSharedResultSet->GetString(SHORTCUT_INFO_INDEX, shortcutInfoJson) == NativeRdb::E_OK) { - auto jsonObj = nlohmann::json::parse(shortcutInfoJson, nullptr, false); - if (!jsonObj.is_discarded()) { - rowData[SHORTCUT_INFO] = jsonObj; + cJSON* shortcutInfoObj = cJSON_Parse(shortcutInfoJson.c_str()); + if (shortcutInfoObj != nullptr) { + cJSON_AddItemToObject(rowData, SHORTCUT_INFO.c_str(), shortcutInfoObj); } } - jsonResult.emplace_back(rowData); + cJSON_AddItemToArray(jsonResult, rowData); } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK); return true; } -bool ShortcutDataStorageRdb::UpdateAllShortcuts(nlohmann::json &jsonResult) +bool ShortcutDataStorageRdb::UpdateAllShortcuts(cJSON* jsonResult) { - if (!jsonResult.is_array()) { + if (jsonResult == nullptr || !cJSON_IsArray(jsonResult)) { APP_LOGE("Invalid JSON format: expected array"); return false; } @@ -295,13 +300,43 @@ bool ShortcutDataStorageRdb::UpdateAllShortcuts(nlohmann::json &jsonResult) return false; } bool finalResult = true; - for (auto& item : jsonResult) { + int arraySize = cJSON_GetArraySize(jsonResult); + for (int i = 0; i < arraySize; i++) { + cJSON* item = cJSON_GetArrayItem(jsonResult, i); + if (item == nullptr || !cJSON_IsObject(item)) { + APP_LOGE("invalid json item at index %{public}d", i); + finalResult = false; + continue; + } NativeRdb::AbsRdbPredicates absRdbPredicates(SHORTCUT_RDB_TABLE_NAME); - absRdbPredicates.EqualTo(BUNDLE_NAME, item[BUNDLE_NAME].get()); - absRdbPredicates.EqualTo(SHORTCUT_ID, item[SHORTCUT_ID].get()); - absRdbPredicates.EqualTo(APP_INDEX, item[APP_INDEX].get()); - absRdbPredicates.EqualTo(USER_ID, item[USER_ID].get()); - + cJSON* bundleNameItem = cJSON_GetObjectItemCaseSensitive(item, BUNDLE_NAME.c_str()); + if (bundleNameItem == nullptr || !cJSON_IsString(bundleNameItem)) { + APP_LOGE("missing or invalid BUNDLE_NAME at index %{public}d", i); + finalResult = false; + continue; + } + absRdbPredicates.EqualTo(BUNDLE_NAME, bundleNameItem->valuestring); + cJSON* shortcutIdItem = cJSON_GetObjectItemCaseSensitive(item, SHORTCUT_ID.c_str()); + if (shortcutIdItem == nullptr || !cJSON_IsString(shortcutIdItem)) { + APP_LOGE("missing or invalid SHORTCUT_ID at index %{public}d", i); + finalResult = false; + continue; + } + absRdbPredicates.EqualTo(SHORTCUT_ID, shortcutIdItem->valuestring); + cJSON* appIndexItem = cJSON_GetObjectItemCaseSensitive(item, APP_INDEX.c_str()); + if (appIndexItem == nullptr || !cJSON_IsNumber(appIndexItem)) { + APP_LOGE("missing or invalid APP_INDEX at index %{public}d", i); + finalResult = false; + continue; + } + absRdbPredicates.EqualTo(APP_INDEX, appIndexItem->valueint); + cJSON* userIdItem = cJSON_GetObjectItemCaseSensitive(item, USER_ID.c_str()); + if (userIdItem == nullptr || !cJSON_IsNumber(userIdItem)) { + APP_LOGE("Missing or invalid USER_ID at index %{public}d", i); + finalResult = false; + continue; + } + absRdbPredicates.EqualTo(USER_ID, userIdItem->valueint); auto resultSet = rdbDataManager_->QueryData(absRdbPredicates); if (resultSet == nullptr) { APP_LOGE("resultSet is null."); @@ -309,11 +344,18 @@ bool ShortcutDataStorageRdb::UpdateAllShortcuts(nlohmann::json &jsonResult) } ScopeGuard guard([resultSet] { resultSet->Close(); }); NativeRdb::ValuesBucket bucket; - bucket.PutString(BUNDLE_NAME, item[BUNDLE_NAME]); - bucket.PutString(SHORTCUT_ID, item[SHORTCUT_ID]); - bucket.PutInt(USER_ID, item[USER_ID]); - bucket.PutInt(APP_INDEX, item[APP_INDEX]); - bucket.PutString(SHORTCUT_INFO, item[SHORTCUT_INFO].dump()); + bucket.PutString(BUNDLE_NAME, bundleNameItem->valuestring); + bucket.PutString(SHORTCUT_ID, shortcutIdItem->valuestring); + bucket.PutInt(USER_ID, userIdItem->valueint); + bucket.PutInt(APP_INDEX, appIndexItem->valueint); + cJSON* shortcutInfoItem = cJSON_GetObjectItemCaseSensitive(item, SHORTCUT_INFO.c_str()); + if (shortcutInfoItem != nullptr) { + char* jsonStr = cJSON_PrintUnformatted(shortcutInfoItem); + if (jsonStr != nullptr) { + bucket.PutString(SHORTCUT_INFO, jsonStr); + cJSON_free(jsonStr); + } + } if (!rdbDataManager_->UpdateOrInsertData(bucket, absRdbPredicates)) { finalResult = false; } diff --git a/services/bundlemgr/test/unittest/bms_data_mgr_test/bms_data_mgr_test.cpp b/services/bundlemgr/test/unittest/bms_data_mgr_test/bms_data_mgr_test.cpp index 962f07a90af399916b57b49a5008f18c1ebe28fa..d2dbfd65830c80a7fae7c1a38cb3f5d567718798 100644 --- a/services/bundlemgr/test/unittest/bms_data_mgr_test/bms_data_mgr_test.cpp +++ b/services/bundlemgr/test/unittest/bms_data_mgr_test/bms_data_mgr_test.cpp @@ -28,6 +28,7 @@ #include "bundle_data_storage_interface.h" #include "bundle_data_mgr.h" #include "bundle_mgr_service.h" +#include "cJSON.h" #include "int_wrapper.h" #include "json_constants.h" #include "json_serializer.h" @@ -5817,7 +5818,8 @@ HWTEST_F(BmsDataMgrTest, OnExtension_0010, Function | SmallTest | Level1) bool ret = shortcutDataStorageRdb->AddDesktopShortcutInfo(shortcutInfo, USERID, isIdIllegal); EXPECT_TRUE(ret); - nlohmann::json backupJson = nlohmann::json::array(); + cJSON* backupJson = cJSON_CreateArray(); + ASSERT_NE(backupJson, nullptr); ret = shortcutDataStorageRdb->GetAllTableDataToJson(backupJson); EXPECT_TRUE(ret); @@ -5833,6 +5835,7 @@ HWTEST_F(BmsDataMgrTest, OnExtension_0010, Function | SmallTest | Level1) ret = shortcutDataStorageRdb->DeleteDesktopShortcutInfo(shortcutInfo, USERID); EXPECT_TRUE(ret); + cJSON_Delete(backupJson); } /** @@ -5844,7 +5847,8 @@ HWTEST_F(BmsDataMgrTest, OnExtension_0020, Function | MediumTest | Level1) { std::shared_ptr shortcutDataStorageRdb = std::make_shared(); ASSERT_NE(shortcutDataStorageRdb, nullptr); - nlohmann::json backupJson; + cJSON* backupJson = cJSON_CreateObject(); + ASSERT_NE(backupJson, nullptr); NativeRdb::AbsRdbPredicates absRdbPredicates("shortcut_info"); shortcutDataStorageRdb->rdbDataManager_->DeleteData(absRdbPredicates); std::shared_ptr bundleBackupService = DelayedSingleton::GetInstance(); @@ -5854,6 +5858,7 @@ HWTEST_F(BmsDataMgrTest, OnExtension_0020, Function | MediumTest | Level1) EXPECT_EQ(shortcutDataStorageRdb->GetAllTableDataToJson(backupJson), false); shortcutDataStorageRdb->rdbDataManager_ = nullptr; EXPECT_EQ(shortcutDataStorageRdb->GetAllTableDataToJson(backupJson), false); + cJSON_Delete(backupJson); } /** @@ -5868,12 +5873,16 @@ HWTEST_F(BmsDataMgrTest, OnExtension_0030, Function | MediumTest | Level1) std::shared_ptr bundleBackupService = DelayedSingleton::GetInstance(); ASSERT_NE(bundleBackupService, nullptr); bundleBackupService->dataMgr_ = std::make_shared(); - nlohmann::json backupJson; + cJSON* backupJson = cJSON_CreateObject(); + ASSERT_NE(backupJson, nullptr); auto ret = bundleBackupService->OnRestore(backupJson); EXPECT_EQ(ret, ERR_APPEXECFWK_DB_UPDATE_ERROR); shortcutDataStorageRdb->rdbDataManager_ = nullptr; - backupJson = nlohmann::json::array(); - EXPECT_EQ(shortcutDataStorageRdb->UpdateAllShortcuts(backupJson), false); + cJSON* jsonArray = cJSON_CreateArray(); + ASSERT_NE(jsonArray, nullptr); + EXPECT_EQ(shortcutDataStorageRdb->UpdateAllShortcuts(jsonArray), false); + cJSON_Delete(backupJson); + cJSON_Delete(jsonArray); } /** @@ -6122,9 +6131,9 @@ HWTEST_F(BmsDataMgrTest, GetAllShortcutInfoForSelf_0040, Function | MediumTest | HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0010, Function | MediumTest | Level1) { BundleDataMgr bundleDataMgr; - nlohmann::json backupJson; + cJSON* backupJson = nullptr; bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_FALSE(backupJson.is_object()); + EXPECT_FALSE(cJSON_IsObject(backupJson)); } /** @@ -6135,10 +6144,17 @@ HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0010, Function | MediumTest HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0020, Function | MediumTest | Level1) { BundleDataMgr bundleDataMgr; - nlohmann::json backupJson; - backupJson.push_back({{"BUNDLE_NAME", "com.invalid.bundle"}, {"APP_INDEX", 0}, {"USER_ID", 100}}); + cJSON* backupJson = cJSON_CreateArray(); + EXPECT_NE(backupJson, nullptr); + cJSON* item = cJSON_CreateObject(); + EXPECT_NE(item, nullptr); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.invalid.bundle"); + cJSON_AddNumberToObject(item, "APP_INDEX", 0); + cJSON_AddNumberToObject(item, "USER_ID", 100); + cJSON_AddItemToArray(backupJson, item); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); } /** @@ -6149,14 +6165,21 @@ HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0020, Function | MediumTest HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0030, Function | MediumTest | Level1) { BundleDataMgr bundleDataMgr; - nlohmann::json backupJson; + cJSON* backupJson = cJSON_CreateArray(); + EXPECT_NE(backupJson, nullptr); + cJSON* item = cJSON_CreateObject(); + EXPECT_NE(item, nullptr); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.valid.bundle"); + cJSON_AddNumberToObject(item, "APP_INDEX", 0); + cJSON_AddNumberToObject(item, "USER_ID", 100); + cJSON_AddItemToArray(backupJson, item); std::string bundleName = "com.valid.bundle"; - backupJson.push_back({{"BUNDLE_NAME", "com.valid.bundle"}, {"APP_INDEX", 0}, {"USER_ID", 100}}); InnerBundleInfo innerBundleInfo; bundleDataMgr.bundleInfos_.emplace(bundleName, innerBundleInfo); bundleDataMgr.bundleIdMap_.emplace(1, bundleName); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); } /** @@ -6167,15 +6190,22 @@ HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0030, Function | MediumTest HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0040, Function | MediumTest | Level1) { BundleDataMgr bundleDataMgr; - nlohmann::json backupJson; std::string bundleName = "com.valid.bundle"; - backupJson.push_back({{"BUNDLE_NAME", "com.valid.bundle"}, {"APP_INDEX", 0}, {"USER_ID", 100}}); + cJSON* backupJson = cJSON_CreateArray(); + EXPECT_NE(backupJson, nullptr); + cJSON* item = cJSON_CreateObject(); + EXPECT_NE(item, nullptr); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.valid.bundle"); + cJSON_AddNumberToObject(item, "APP_INDEX", 0); + cJSON_AddNumberToObject(item, "USER_ID", 100); + cJSON_AddItemToArray(backupJson, item); InnerBundleInfo innerBundleInfo; bundleDataMgr.bundleInfos_.emplace(bundleName, innerBundleInfo); bundleDataMgr.bundleIdMap_.emplace(1, bundleName); bundleDataMgr.multiUserIdsSet_.insert(100); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); } /** @@ -6186,18 +6216,26 @@ HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0040, Function | MediumTest HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0050, Function | MediumTest | Level1) { BundleDataMgr bundleDataMgr; - nlohmann::json backupJson; std::string bundleName = "com.valid.bundle"; - backupJson.push_back({{"BUNDLE_NAME", "com.valid.bundle"}, {"APP_INDEX", 0}, {"USER_ID", -5}}); + cJSON* backupJson = cJSON_CreateArray(); + EXPECT_NE(backupJson, nullptr); + cJSON* item = cJSON_CreateObject(); + EXPECT_NE(item, nullptr); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.valid.bundle"); + cJSON_AddNumberToObject(item, "APP_INDEX", 0); + cJSON_AddNumberToObject(item, "USER_ID", -5); + + cJSON_AddItemToArray(backupJson, item); InnerBundleInfo innerBundleInfo; bundleDataMgr.bundleInfos_.emplace(bundleName, innerBundleInfo); bundleDataMgr.bundleIdMap_.emplace(1, bundleName); bundleDataMgr.multiUserIdsSet_.insert(-5); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 1); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 1); std::shared_ptr shortcutDataStorageRdb = std::make_shared(); bool result = shortcutDataStorageRdb->DeleteDesktopShortcutInfo(bundleName); EXPECT_TRUE(result); + cJSON_Delete(backupJson); } /** @@ -6208,9 +6246,15 @@ HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0050, Function | MediumTest HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0060, Function | MediumTest | Level1) { BundleDataMgr bundleDataMgr; - nlohmann::json backupJson; std::string bundleName = "com.valid.bundle"; - backupJson.push_back({{"BUNDLE_NAME", "com.valid.bundle"}, {"APP_INDEX", 1}, {"USER_ID", 100}}); + cJSON* backupJson = cJSON_CreateArray(); + EXPECT_NE(backupJson, nullptr); + cJSON* item = cJSON_CreateObject(); + EXPECT_NE(item, nullptr); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.valid.bundle"); + cJSON_AddNumberToObject(item, "APP_INDEX", 1); + cJSON_AddNumberToObject(item, "USER_ID", 100); + cJSON_AddItemToArray(backupJson, item); InnerBundleInfo innerBundleInfo; std::map innerBundleUserInfos; InnerBundleUserInfo info; @@ -6223,7 +6267,8 @@ HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0060, Function | MediumTest bundleDataMgr.bundleIdMap_.emplace(1, bundleName); bundleDataMgr.multiUserIdsSet_.insert(100); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); } /** @@ -6234,35 +6279,65 @@ HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0060, Function | MediumTest HWTEST_F(BmsDataMgrTest, CheckIfShortcutBundleExist_0130, Function | MediumTest | Level1) { BundleDataMgr bundleDataMgr; - nlohmann::json backupJson; - backupJson.push_back({{"APP_INDEX", 0}, {"USER_ID", 100}}); + cJSON* backupJson = nullptr; + backupJson = cJSON_CreateArray(); + EXPECT_NE(backupJson, nullptr); + cJSON* item = cJSON_CreateObject(); + EXPECT_NE(item, nullptr); + cJSON_AddNumberToObject(item, "APP_INDEX", 0); + cJSON_AddNumberToObject(item, "USER_ID", 100); + cJSON_AddItemToArray(backupJson, item); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); - - backupJson.clear(); - backupJson.push_back({{"BUNDLE_NAME", "com.valid.bundle"}, {"USER_ID", 100}}); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); + + backupJson = cJSON_CreateArray(); + item = cJSON_CreateObject(); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.valid.bundle"); + cJSON_AddNumberToObject(item, "USER_ID", 100); + cJSON_AddItemToArray(backupJson, item); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); - - backupJson.clear(); - backupJson.push_back({{"BUNDLE_NAME", "com.valid.bundle"}, {"APP_INDEX", 0}}); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); + + backupJson = cJSON_CreateArray(); + item = cJSON_CreateObject(); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.valid.bundle"); + cJSON_AddNumberToObject(item, "APP_INDEX", 0); + cJSON_AddItemToArray(backupJson, item); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); - - backupJson.clear(); - backupJson.push_back({{"BUNDLE_NAME", 1}, {"APP_INDEX", 0}, {"USER_ID", 100}}); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); + + backupJson = cJSON_CreateArray(); + item = cJSON_CreateObject(); + cJSON_AddNumberToObject(item, "BUNDLE_NAME", 1); + cJSON_AddNumberToObject(item, "APP_INDEX", 0); + cJSON_AddNumberToObject(item, "USER_ID", 100); + cJSON_AddItemToArray(backupJson, item); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); - - backupJson.clear(); - backupJson.push_back({{"BUNDLE_NAME", "com.valid.bundle"}, {"APP_INDEX", "appIndex"}, {"USER_ID", 100}}); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); + + backupJson = cJSON_CreateArray(); + item = cJSON_CreateObject(); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.valid.bundle"); + cJSON_AddStringToObject(item, "APP_INDEX", "appIndex"); + cJSON_AddNumberToObject(item, "USER_ID", 100); + cJSON_AddItemToArray(backupJson, item); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); - - backupJson.clear(); - backupJson.push_back({{"BUNDLE_NAME", "com.valid.bundle"}, {"APP_INDEX", 0}, {"USER_ID", "userId"}}); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); + + backupJson = cJSON_CreateArray(); + item = cJSON_CreateObject(); + cJSON_AddStringToObject(item, "BUNDLE_NAME", "com.valid.bundle"); + cJSON_AddNumberToObject(item, "APP_INDEX", 0); + cJSON_AddStringToObject(item, "USER_ID", "userId"); + cJSON_AddItemToArray(backupJson, item); bundleDataMgr.CheckIfShortcutBundleExist(backupJson); - EXPECT_EQ(backupJson.size(), 0); + EXPECT_EQ(cJSON_GetArraySize(backupJson), 0); + cJSON_Delete(backupJson); } /**