diff --git a/tests/mock/cJson/include/cJsonMock.h b/tests/mock/cJson/include/cJsonMock.h deleted file mode 100644 index 6d47050cd8f046f0a327eae5b9be8bfc7719545b..0000000000000000000000000000000000000000 --- a/tests/mock/cJson/include/cJsonMock.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef OHOS_FILEMGMT_BACKUP_CJSON_MOCK_H -#define OHOS_FILEMGMT_BACKUP_CJSON_MOCK_H - -#include -#include - -namespace OHOS::FileManagement::Backup { -class CJson { -public: - CJson() = default; - virtual ~CJson() = default; - -public: - virtual cJSON *cJSON_CreateArray() = 0; - virtual cJSON *cJSON_CreateObject() = 0; - virtual char *cJSON_Print(const cJSON *item) = 0; - virtual cJSON *cJSON_Parse(const char *value) = 0; - virtual cJSON *cJSON_GetObjectItem(const cJSON *const object, const char *const string) = 0; - virtual void cJSON_Delete(cJSON *item) = 0; - virtual cJSON_bool cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) = 0; - virtual int cJSON_GetArraySize(const cJSON *array) = 0; - virtual cJSON_bool cJSON_AddItemToArray(cJSON *array, cJSON *item); - virtual cJSON *cJSON_AddStringToObject(cJSON *const object, const char *const name, const char *const string); - -public: - static inline std::shared_ptr cJsonPtr = nullptr; -}; - -class CJsonMock : public CJson { -public: - MOCK_METHOD3(cJSON_AddItemToObject, cJSON_bool(cJSON *object, const char *string, cJSON *item)); - MOCK_METHOD1(cJSON_Delete, void(cJSON *item)); - MOCK_METHOD0(cJSON_CreateObject, cJSON *()); - MOCK_METHOD0(cJSON_CreateArray, cJSON *()); - MOCK_METHOD1(cJSON_Print, char *(const cJSON *item)); - MOCK_METHOD1(cJSON_Parse, cJSON *(const char *value)); - MOCK_METHOD2(cJSON_GetObjectItem, cJSON *(const cJSON *const object, const char *const string)); - MOCK_METHOD1(cJSON_GetArraySize, int(const cJSON *array)); - MOCK_METHOD2(cJSON_AddItemToArray, cJSON_bool(cJSON *array, cJSON *item)); - MOCK_METHOD3(cJSON_AddStringToObject, - cJSON *(cJSON *const object, const char *const name, const char *const string)); -}; -} // namespace OHOS::FileManagement::Backup -#endif \ No newline at end of file diff --git a/tests/mock/cJson/src/cJsonMock.cpp b/tests/mock/cJson/src/cJsonMock.cpp deleted file mode 100644 index 8aa2427dd9bb1a7b0be9618bc5f35dbd5c78662e..0000000000000000000000000000000000000000 --- a/tests/mock/cJson/src/cJsonMock.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include - -#include "cJsonMock.h" - -using namespace OHOS::FileManagement::Backup; - -CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) -{ - return CJson::cJsonPtr->cJSON_CreateArray(); -} - -CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) -{ - return CJson::cJsonPtr->cJSON_CreateObject(); -} - -CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) -{ - return CJson::cJsonPtr->cJSON_Print(item); -} - -CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) -{ - return CJson::cJsonPtr->cJSON_Parse(value); -} - -CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *const object, const char *const string) -{ - return CJson::cJsonPtr->cJSON_GetObjectItem(object, string); -} - -CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) -{ - return 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); -} - -CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) -{ - return CJson::cJsonPtr->cJSON_GetArraySize(array); -} - -CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) -{ - return 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); -} \ No newline at end of file diff --git a/tests/unittests/backup_utils/BUILD.gn b/tests/unittests/backup_utils/BUILD.gn index 40476eb1bc07c723cb250aff488ce0299725b90a..f2fff48e6d09360d7af063ef012db5879072508e 100644 --- a/tests/unittests/backup_utils/BUILD.gn +++ b/tests/unittests/backup_utils/BUILD.gn @@ -244,54 +244,12 @@ ohos_unittest("b_process_test") { use_exceptions = true } -ohos_unittest("b_json_other_test") { - module_out_path = path_module_out_tests - - 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_base}/include", - "${path_backup}/tests/mock/cJson/include", - ] - include_dirs += backup_mock_parameter_include_dirs - - deps = [ - "${path_backup}/interfaces/innerkits/native:sandbox_helper_native", - "${path_backup}/tests/utils:backup_test_utils", - "${path_backup}/utils/:backup_utils", - "${path_googletest}:gmock_main", - "${path_jsoncpp}:jsoncpp", - ] - - external_deps = [ - "cJSON:cjson", - "c_utils:utils", - "hilog:libhilog", - ] - - defines = [ - "LOG_TAG=\"app_file_service\"", - "LOG_DOMAIN=0xD004303", - "private = public", - "protected = public", - ] - - use_exceptions = true -} - group("backup_test") { testonly = true deps = [ ":b_error_test", ":b_file_test", - ":b_json_other_test", ":b_json_test", ":b_process_test", ":b_tarball_cmdline_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 deleted file mode 100644 index 10b967f402bcf627d73d1615ff9d1a1737018769..0000000000000000000000000000000000000000 --- a/tests/unittests/backup_utils/b_json/b_json_service_disposal_config_other_test.cpp +++ /dev/null @@ -1,228 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "json/value.h" -#include - -#include "b_error/b_error.h" -#include "b_error/b_excep_utils.h" -#include "b_json/b_json_service_disposal_config.h" -#include "cJsonMock.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 { -public: - // 所有测试用例执行之前执行 - static void SetUpTestCase(void); - // 所有测试用例执行之后执行 - static void TearDownTestCase(void); - // 每次测试用例执行之前执行 - void SetUp(); - // 每次测试用例执行之后执行 - void TearDown(); - - static inline shared_ptr cJsonMock = nullptr; -}; - -void BJsonServiceDisposalConfigTest::TearDown() {} - -void BJsonServiceDisposalConfigTest::SetUp() -{ - string filePath = PATH + CONFIG_NAME; - bool result = remove(filePath.c_str()); - if (result) { - GTEST_LOG_(INFO) << "delete file success."; - } else { - GTEST_LOG_(INFO) << "delete file failed."; - } -} - -void BJsonServiceDisposalConfigTest::SetUpTestCase() -{ - cJsonMock = make_shared(); - CJson::cJsonPtr = cJsonMock; -} - -void BJsonServiceDisposalConfigTest::TearDownTestCase() -{ - CJson::cJsonPtr = nullptr; - cJsonMock = nullptr; -} - -/* * - * @tc.number: SUB_Disposal_Config_Test_0102 - * @tc.name: SUB_Disposal_Config_Test_0102 - * @tc.desc: 测试print返回空指针情况 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: IAAMIK - */ -HWTEST_F(BJsonServiceDisposalConfigTest, SUB_Disposal_Config_Test_0102, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin SUB_Disposal_Config_Test_0102"; - try { - int cjson = 0; - EXPECT_CALL(*cJsonMock, cJSON_CreateObject()).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_CreateArray()).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_AddItemToObject(_, _, _)).WillOnce(Return(true)); - EXPECT_CALL(*cJsonMock, cJSON_Print(_)).WillOnce(Return(nullptr)); - EXPECT_CALL(*cJsonMock, cJSON_Delete(_)).WillOnce(Return()); - BJsonDisposalConfig config; - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction."; - } - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end SUB_Disposal_Config_Test_0102"; -} - -/* * - * @tc.number: SUB_Disposal_Config_Test_0103 - * @tc.name: SUB_Disposal_Config_Test_0103 - * @tc.desc: 测试配置文件存在的情况 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: IAAMIK - */ -HWTEST_F(BJsonServiceDisposalConfigTest, SUB_Disposal_Config_Test_0103, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin SUB_Disposal_Config_Test_0103"; - try { - EXPECT_CALL(*cJsonMock, cJSON_CreateObject()).WillOnce(Return(nullptr)); - BJsonDisposalConfig config; - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction."; - } - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end SUB_Disposal_Config_Test_0103"; -} - -/* * - * @tc.number: SUB_Disposal_Config_Test_0104 - * @tc.name: SUB_Disposal_Config_Test_0104 - * @tc.desc: 测试creatArray返回空指针情况 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: IAAMIK - */ -HWTEST_F(BJsonServiceDisposalConfigTest, SUB_Disposal_Config_Test_0104, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin SUB_Disposal_Config_Test_0104"; - try { - int cjson = 0; - EXPECT_CALL(*cJsonMock, cJSON_CreateObject()).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_CreateArray()).WillOnce(Return(nullptr)); - EXPECT_CALL(*cJsonMock, cJSON_Delete(_)).WillOnce(Return()); - BJsonDisposalConfig config; - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction."; - } - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end SUB_Disposal_Config_Test_0104"; -} - -/* * - * @tc.number: SUB_Disposal_Config_Test_0202 - * @tc.name: Disposal_Config_Test_0202 - * @tc.desc: 测试修改config文件时print返回空指针 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: IAAMIK - */ -HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0202, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0202"; - try { - string filePath = PATH + CONFIG_NAME; - int cjson = 0; - string str = "test"; - EXPECT_CALL(*cJsonMock, cJSON_CreateObject()).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_CreateArray()).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_AddItemToObject(_, _, _)).WillOnce(Return(true)); - EXPECT_CALL(*cJsonMock, cJSON_Print(_)).WillOnce(Return(nullptr)); - EXPECT_CALL(*cJsonMock, cJSON_Delete(_)).WillOnce(Return()); - BJsonDisposalConfig config; - EXPECT_EQ(access(filePath.c_str(), F_OK), 0); - const string bundleName = "test1"; - EXPECT_CALL(*cJsonMock, cJSON_Parse(_)).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_GetObjectItem(_, _)).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_CreateObject()).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_AddStringToObject(_, _, _)).WillOnce(Return(nullptr)); - EXPECT_CALL(*cJsonMock, cJSON_AddItemToArray(_, _)).WillOnce(Return(true)); - EXPECT_CALL(*cJsonMock, cJSON_Print(_)).WillOnce(Return(nullptr)); - EXPECT_CALL(*cJsonMock, cJSON_Delete(_)).WillOnce(Return()); - bool retAdd = config.AppendIntoDisposalConfigFile(bundleName); - EXPECT_FALSE(retAdd); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction."; - } - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0202"; -} - -/* * - * @tc.number: SUB_Disposal_Config_Test_0203 - * @tc.name: Disposal_Config_Test_0203 - * @tc.desc: 测试修改config文件时parse返回空指针的情况 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: IAAMIK - */ -HWTEST_F(BJsonServiceDisposalConfigTest, Disposal_Config_Test_0203, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-begin Disposal_Config_Test_0203"; - try { - string filePath = PATH + CONFIG_NAME; - int cjson = 0; - string str = "test"; - EXPECT_CALL(*cJsonMock, cJSON_CreateObject()).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_CreateArray()).WillOnce(Return(reinterpret_cast(&cjson))); - EXPECT_CALL(*cJsonMock, cJSON_AddItemToObject(_, _, _)).WillOnce(Return(true)); - EXPECT_CALL(*cJsonMock, cJSON_Print(_)).WillOnce(Return(nullptr)); - EXPECT_CALL(*cJsonMock, cJSON_Delete(_)).WillOnce(Return()); - BJsonDisposalConfig config; - EXPECT_EQ(access(filePath.c_str(), F_OK), 0); - const string bundleName = "test1"; - EXPECT_CALL(*cJsonMock, cJSON_Parse(_)).WillOnce(Return(nullptr)); - EXPECT_CALL(*cJsonMock, cJSON_Delete(_)).WillOnce(Return()); - bool retAdd = config.AppendIntoDisposalConfigFile(bundleName); - EXPECT_FALSE(retAdd); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-an exception occurred by construction."; - } - GTEST_LOG_(INFO) << "BJsonServiceDisposalConfigTest-end Disposal_Config_Test_0203"; -} -} // namespace OHOS::FileManagement::Backup \ No newline at end of file