diff --git a/tests/mock/cJson/include/cJsonMock.h b/tests/mock/cJson/include/cJsonMock.h index 5f39b5bf1a5b73641166f35579485123cf72f5b6..30903f4218cad00c306282f924510086ec39850e 100644 --- a/tests/mock/cJson/include/cJsonMock.h +++ b/tests/mock/cJson/include/cJsonMock.h @@ -18,6 +18,12 @@ #include #include +CJSON_PUBLIC(cJSON *) CJSONParse(const char *value); +CJSON_PUBLIC(cJSON *) CJSONGetObjectItem(const cJSON *const object, const char *const string); +CJSON_PUBLIC(void) CJSONDelete(cJSON *item); +CJSON_PUBLIC(cJSON_bool) CJSONIsArray(const cJSON * const item); +CJSON_PUBLIC(void) CJSONFree(void *object); + namespace OHOS::FileManagement::Backup { class CJson { public: diff --git a/tests/mock/cJson/include/cjson_func_define.h b/tests/mock/cJson/include/cjson_func_define.h new file mode 100644 index 0000000000000000000000000000000000000000..4cc3a4f806d0988f549f8b3f0f5c59be49e02647 --- /dev/null +++ b/tests/mock/cJson/include/cjson_func_define.h @@ -0,0 +1,25 @@ +/* +* Copyright (c) 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_CJSON_FUNC_DEFINE_H +#define FILEMANAGEMENT_APP_FILE_SERVICE_CJSON_FUNC_DEFINE_H + +#define cJSON_Parse CJSONParse +#define cJSON_GetObjectItem CJSONGetObjectItem +#define cJSON_Delete CJSONDelete +#define cJSON_IsArray CJSONIsArray +#define cJSON_free CJSONFree + +#endif // FILEMANAGEMENT_APP_FILE_SERVICE_CJSON_FUNC_DEFINE_H \ No newline at end of file diff --git a/tests/mock/cJson/include/cjson_func_undef.h b/tests/mock/cJson/include/cjson_func_undef.h new file mode 100644 index 0000000000000000000000000000000000000000..f546373dc03d061c6682106122d4687280d8e009 --- /dev/null +++ b/tests/mock/cJson/include/cjson_func_undef.h @@ -0,0 +1,25 @@ +/* +* Copyright (c) 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 +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef FILEMANAGEMENT_APP_FILE_SERVICE_CJSON_FUNC_UNDEF_H +#define FILEMANAGEMENT_APP_FILE_SERVICE_CJSON_FUNC_UNDEF_H + +#define CJSONParse cJSON_Parse +#define CJSONGetObjectItem cJSON_GetObjectItem +#define CJSONDelete cJSON_Delete +#define CJSONIsArray cJSON_IsArray +#define CJSONFree cJSON_free + +#endif // FILEMANAGEMENT_APP_FILE_SERVICE_CJSON_FUNC_UNDEF_H \ No newline at end of file diff --git a/tests/mock/cJson/src/cJsonMock.cpp b/tests/mock/cJson/src/cJsonMock.cpp index 04e1985b634fc36f0739436d04fa96eee39364a4..a24468ed4886f0a9aa4a185c048b2c72a536254a 100644 --- a/tests/mock/cJson/src/cJsonMock.cpp +++ b/tests/mock/cJson/src/cJsonMock.cpp @@ -16,150 +16,62 @@ #include "cJsonMock.h" -using namespace OHOS::FileManagement::Backup; - -static int CaseInsensitiveStrcmp(const unsigned char *string1, const unsigned char *string2) -{ - if ((string1 == NULL) || (string2 == NULL)) { - return 1; - } - - if (string1 == string2) { - return 0; - } - - for (; tolower(*string1) == tolower(*string2); (void)string1++, string2++) { - if (*string1 == '\0') { - return 0; - } - } - - return tolower(*string1) - tolower(*string2); -} - -static cJSON *GetObjectItem(const cJSON * const object, const char * const name, const cJSON_bool caseSensitive) -{ - cJSON *current_element = NULL; - - if ((object == NULL) || (name == NULL)) { - return NULL; - } - - current_element = object->child; - if (caseSensitive) { - while ((current_element != NULL) && (current_element->string != NULL) && - (strcmp(name, current_element->string) != 0)) { - current_element = current_element->next; - } - } else { - while ((current_element != NULL) && - (CaseInsensitiveStrcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) { - current_element = current_element->next; - } - } - - if ((current_element == NULL) || (current_element->string == NULL)) { - return NULL; - } - - return current_element; -} - -CJSON_PUBLIC(void) CJSONDelete(cJSON *item) -{ - cJSON *next = NULL; - while (item != NULL) - { - next = item->next; - if (!(item->type & cJSON_IsReference) && (item->child != NULL)) - { - CJSONDelete(item->child); - } - if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) - { - free(item->valuestring); - item->valuestring = NULL; - } - if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) - { - free(item->string); - item->string = NULL; - } - if (next == item) - { - break; - } - free(item); - item = next; - } -} - CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) { - return CJson::cJsonPtr->cJSON_CreateArray(); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_CreateArray(); } CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) { - return CJson::cJsonPtr->cJSON_CreateObject(); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_CreateObject(); } CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) { - return CJson::cJsonPtr->cJSON_Print(item); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_Print(item); } -CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +CJSON_PUBLIC(cJSON *) CJSONParse(const char *value) { - return (CJson::cJsonPtr == nullptr) ? cJSON_ParseWithOpts(value, 0, 0) : CJson::cJsonPtr->cJSON_Parse(value); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_Parse(value); } -CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *const object, const char *const string) +CJSON_PUBLIC(cJSON *) CJSONGetObjectItem(const cJSON *const object, const char *const string) { - return (CJson::cJsonPtr == nullptr) ? GetObjectItem(object, string, false) : - CJson::cJsonPtr->cJSON_GetObjectItem(object, string); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_GetObjectItem(object, string); } -CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) +CJSON_PUBLIC(void) CJSONDelete(cJSON *item) { - return (CJson::cJsonPtr == nullptr) ? CJSONDelete(item) : CJson::cJsonPtr->cJSON_Delete(item); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_Delete(item); } CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) { - return CJson::cJsonPtr->cJSON_AddItemToObject(object, string, item); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_AddItemToObject(object, string, item); } CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) { - return CJson::cJsonPtr->cJSON_GetArraySize(array); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_GetArraySize(array); } CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) { - return CJson::cJsonPtr->cJSON_AddItemToArray(array, item); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_AddItemToArray(array, item); } CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) { - return CJson::cJsonPtr->cJSON_AddStringToObject(object, name, string); -} - -static CJSON_PUBLIC(cJSON_bool) IsArray(const cJSON * const item) -{ - if (item == NULL) { - return false; - } - - return (item->type & 0xFF) == cJSON_Array; + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_AddStringToObject(object, name, string); } -CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) +CJSON_PUBLIC(cJSON_bool) CJSONIsArray(const cJSON * const item) { - return (CJson::cJsonPtr == nullptr) ? IsArray(item) : CJson::cJsonPtr->cJSON_IsArray(item); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_IsArray(item); } -CJSON_PUBLIC(void) cJSON_free(void *object) +CJSON_PUBLIC(void) CJSONFree(void *object) { - return (CJson::cJsonPtr == nullptr) ? free(object) : CJson::cJsonPtr->cJSON_free(object); + return OHOS::FileManagement::Backup::CJson::cJsonPtr->cJSON_free(object); } \ No newline at end of file diff --git a/tests/unittests/backup_utils/BUILD.gn b/tests/unittests/backup_utils/BUILD.gn index 9a1d706712a71784e2854eec6f81ed50c535521c..36b24da70a756dafc2ae272444d6a7b539364731 100644 --- a/tests/unittests/backup_utils/BUILD.gn +++ b/tests/unittests/backup_utils/BUILD.gn @@ -274,13 +274,13 @@ ohos_unittest("b_json_other_test") { sources = [ "${path_backup}/tests/mock/cJson/src/cJsonMock.cpp", "${path_backup}/tests/unittests/backup_utils/b_json/b_json_service_disposal_config_other_test.cpp", - "${path_backup}/utils/src/b_json/b_json_service_disposal_config.cpp", ] sources += backup_mock_parameter_src include_dirs = [ "${path_backup}/utils", "${path_backup}/tests/mock/cJson/include", + "${path_backup}/utils/src/b_json", ] include_dirs += backup_mock_parameter_include_dirs @@ -315,13 +315,13 @@ ohos_unittest("b_jsonutil_other_test") { sources = [ "${path_backup}/tests/mock/cJson/src/cJsonMock.cpp", "${path_backup}/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_other_test.cpp", - "${path_backup}/utils/src/b_jsonutil/b_jsonutil.cpp", ] sources += backup_mock_parameter_src include_dirs = [ "${path_backup}/utils", "${path_backup}/tests/mock/cJson/include", + "${path_backup}/utils/src/b_jsonutil", ] include_dirs += backup_mock_parameter_include_dirs @@ -355,13 +355,13 @@ ohos_unittest("b_json_clear_data_test") { sources = [ "${path_backup}/tests/mock/cJson/src/cJsonMock.cpp", "${path_backup}/tests/unittests/backup_utils/b_json/b_json_clear_data_config_test.cpp", - "${path_backup}/utils/src/b_json/b_json_clear_data_config.cpp", ] sources += backup_mock_parameter_src include_dirs = [ "${path_backup}/utils", "${path_backup}/tests/mock/cJson/include", + "${path_backup}/utils/src/b_json", ] include_dirs += backup_mock_parameter_include_dirs diff --git a/tests/unittests/backup_utils/b_json/b_json_clear_data_config_test.cpp b/tests/unittests/backup_utils/b_json/b_json_clear_data_config_test.cpp index 357ff2ada20267731f825c56470a28ea47b62131..ba8b58e44f7a1a8629f7d53825555aee9382f3b7 100644 --- a/tests/unittests/backup_utils/b_json/b_json_clear_data_config_test.cpp +++ b/tests/unittests/backup_utils/b_json/b_json_clear_data_config_test.cpp @@ -29,13 +29,16 @@ #include "b_json/b_json_clear_data_config.h" #include "cJsonMock.h" +#include "cjson_func_define.h" +#include "b_json_clear_data_config.cpp" +#include "cjson_func_undef.h" + namespace OHOS::FileManagement::Backup { using namespace std; using namespace testing; namespace { const string PATH = "/data/service/el2/100/backup/"; -const string CONFIG_NAME = "ClearDataConfig.json"; } // namespace class BJsonClearDataConfigTest : public testing::Test { diff --git a/tests/unittests/backup_utils/b_json/b_json_service_disposal_config_other_test.cpp b/tests/unittests/backup_utils/b_json/b_json_service_disposal_config_other_test.cpp index 1a7050b37b2a133826e8248ab985132c587f10d5..a6d544617d367ab2edf37b2d0e837639e31406a3 100644 --- a/tests/unittests/backup_utils/b_json/b_json_service_disposal_config_other_test.cpp +++ b/tests/unittests/backup_utils/b_json/b_json_service_disposal_config_other_test.cpp @@ -29,13 +29,16 @@ #include "b_json/b_json_service_disposal_config.h" #include "cJsonMock.h" +#include "cjson_func_define.h" +#include "b_json_service_disposal_config.cpp" +#include "cjson_func_undef.h" + namespace OHOS::FileManagement::Backup { using namespace std; using namespace testing; namespace { const string PATH = "/data/service/el2/100/backup/"; -const string CONFIG_NAME = "RestoreDisposalConfig.json"; } // namespace class BJsonServiceDisposalConfigTest : public testing::Test { diff --git a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_other_test.cpp b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_other_test.cpp index 7cac718a48ff6cb6906b81f71fa1a60ce22325aa..f3812e08eee52198510388b6d6910e603d0d824c 100644 --- a/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_other_test.cpp +++ b/tests/unittests/backup_utils/b_jsonutil/b_jsonutil_other_test.cpp @@ -27,12 +27,13 @@ #include "b_process/b_process.h" #include "cJsonMock.h" +#include "cjson_func_define.h" +#include "b_jsonutil.cpp" +#include "cjson_func_undef.h" + namespace OHOS::FileManagement::Backup { using namespace std; using namespace testing; -namespace { - const static int BUNDLE_INDEX_DEFAULT_VAL = 0; -} class BJsonUtilTest : public testing::Test { public: