diff --git a/tests/mock/module_external/include/datashare_helper_mock.h b/tests/mock/module_external/include/datashare_helper_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..4db3d3b5913397e3f0af7d12815aa4bd3443641d --- /dev/null +++ b/tests/mock/module_external/include/datashare_helper_mock.h @@ -0,0 +1,149 @@ +/* + * 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 OHOS_FILEMGMT_BACKUP_DATASHARE_HELPER_MOCK_H +#define OHOS_FILEMGMT_BACKUP_DATASHARE_HELPER_MOCK_H + +#include +#include "gmock/gmock.h" +#include "datashare_helper.h" +#include "datashare_result_set_mock.h" + +namespace OHOS { +using namespace DataShare; +namespace FileManagement::Backup { +class IDataShareHelper { +public: + IDataShareHelper() = default; + virtual ~IDataShareHelper() = default; + virtual std::shared_ptr Creator(const sptr &token, + const string &strUri, const string &extUri, const int waitTime, bool isSystem) = 0; + virtual std::shared_ptr Query(Uri &uri, const DataSharePredicates &predicates, + std::vector &columns, DatashareBusinessError *businessError) = 0; +public: + static inline std::shared_ptr idsh = nullptr; +}; + +class DataShareHelperImpl : public DataShareHelper { +public: + MOCK_METHOD(std::shared_ptr, Query, (Uri &uri, + const DataShare::DataSharePredicates& predicates, std::vector &columns, + DatashareBusinessError *businessError), (override)); + + MOCK_METHOD(bool, Release, (), (override)); + + MOCK_METHOD(std::vector, GetFileTypes, (Uri &uri, const string &mimeTypeFilter), (override)); + + MOCK_METHOD(int, OpenFile, (Uri &uri, const std::string &mode), (override)); + + MOCK_METHOD(int, OpenRawFile, (Uri &uri, const std::string &mode), (override)); + + MOCK_METHOD(int, Insert, (Uri &uri, const DataShareValuesBucket &value), (override)); + + MOCK_METHOD(int, InsertExt, (Uri &uri, const DataShareValuesBucket &value, std::string &result), (override)); + + MOCK_METHOD(int, Update, (Uri &uri, const DataSharePredicates &predicates, + const DataShareValuesBucket &value), (override)); + + MOCK_METHOD(int, BatchUpdate, (const UpdateOperations &operations, + std::vector &results), (override)); + + MOCK_METHOD(int, Delete, (Uri &uri, const DataSharePredicates &predicates), (override)); + + MOCK_METHOD(string, GetType, (Uri &uri), (override)); + + MOCK_METHOD(int, BatchInsert, (Uri &uri, const std::vector &values), (override)); + + MOCK_METHOD(int, ExecuteBatch, (const std::vector &statements, + ExecResultSet &result), (override)); + + MOCK_METHOD(int, RegisterObserver, (const Uri &uri, + const sptr &dataObserver), (override)); + + MOCK_METHOD(int, UnregisterObserver, (const Uri &uri, + const sptr &dataObserver), (override)); + + MOCK_METHOD(void, NotifyChange, (const Uri &uri), (override)); + + MOCK_METHOD(int, RegisterObserverExtProvider, (const Uri &uri, std::shared_ptr dataObserver, + bool isDescendants), (override)); + + MOCK_METHOD(int, UnregisterObserverExtProvider, (const Uri &uri, + std::shared_ptr dataObserver), (override)); + + MOCK_METHOD(void, NotifyChangeExtProvider, (const DataShareObserver::ChangeInfo &changeInfo), (override)); + + MOCK_METHOD(Uri, NormalizeUri, (Uri &uri), (override)); + + MOCK_METHOD(Uri, DenormalizeUri, (Uri &uri), (override)); + + MOCK_METHOD(int, AddQueryTemplate, (const std::string &uri, int64_t subscriberId, Template &tpl), (override)); + + MOCK_METHOD(int, DelQueryTemplate, (const std::string &uri, int64_t subscriberId), (override)); + + MOCK_METHOD(std::vector, Publish, (const Data &data, const std::string &bundleName), (override)); + + MOCK_METHOD(Data, GetPublishedData, (const std::string &bundleName, int &resultCode), (override)); + + MOCK_METHOD(std::vector, SubscribeRdbData, (const std::vector &uris, + const TemplateId &templateId, + const std::function &callback), (override)); + + MOCK_METHOD(std::vector, UnsubscribeRdbData, (const std::vector &uris, + const TemplateId &templateId), (override)); + + MOCK_METHOD(std::vector, EnableRdbSubs, (const std::vector &uris, + const TemplateId &templateId), (override)); + + MOCK_METHOD(std::vector, DisableRdbSubs, (const std::vector &uris, + const TemplateId &templateId), (override)); + + MOCK_METHOD(std::vector, SubscribePublishedData, + (const std::vector &uris, int64_t subscriberId, + const std::function &callback), (override)); + + MOCK_METHOD(std::vector, UnsubscribePublishedData, (const std::vector &uris, + int64_t subscriberId), (override)); + + MOCK_METHOD(std::vector, EnablePubSubs, + (const std::vector &uris, int64_t subscriberId), (override)); + + MOCK_METHOD(std::vector, DisablePubSubs, + (const std::vector &uris, int64_t subscriberId), (override)); + + MOCK_METHOD((std::pair), InsertEx, + (Uri &uri, const DataShareValuesBucket &value), (override)); + + MOCK_METHOD((std::pair), UpdateEx, (Uri &uri, const DataSharePredicates &predicates, + const DataShareValuesBucket &value), (override)); + + MOCK_METHOD((std::pair), DeleteEx, + (Uri &uri, const DataSharePredicates &predicates), (override)); + + MOCK_METHOD(int32_t, UserDefineFunc, (MessageParcel &data, MessageParcel &reply, + MessageOption &option), (override)); +}; + +class DataShareHelperMock : public IDataShareHelper { +public: + MOCK_METHOD(std::shared_ptr, Creator, (const sptr &token, + const string &strUri, const string &extUri, const int waitTime, bool isSystem)); + + MOCK_METHOD(std::shared_ptr, Query, (Uri &uri, const DataSharePredicates &predicates, + std::vector &colums, DatashareBusinessError *businessError)); +}; +} +} // namespace OHOS::FileManagement::Backup +#endif diff --git a/tests/mock/module_external/include/datashare_result_set_mock.h b/tests/mock/module_external/include/datashare_result_set_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..93c306ef12b46ed0dcc66f4396419288c85e61a6 --- /dev/null +++ b/tests/mock/module_external/include/datashare_result_set_mock.h @@ -0,0 +1,50 @@ +/* + * 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 OHOS_FILEMGMT_BACKUP_DATASHARE_RESULT_SET_MOCK_H +#define OHOS_FILEMGMT_BACKUP_DATASHARE_RESULT_SET_MOCK_H + +#include +#include "gmock/gmock.h" +#include "datashare_result_set.h" + +namespace OHOS { +using namespace DataShare; +namespace FileManagement::Backup { + +class IDataShareResultSet { +public: + IDataShareResultSet() = default; + virtual ~IDataShareResultSet() = default; + virtual int GetRowCount(int &count) = 0; + virtual int GoToNextRow() = 0; + virtual int GetColumnIndex(const std::string &columnName, int &columnIndex) = 0; + virtual int GetInt(int columnIndex, int &value) = 0; + virtual int GetLong(int columnIndex, int64_t &value) = 0; +public: + static inline std::shared_ptr idsrs = nullptr; +}; + +class DataShareResultSetMock : public IDataShareResultSet { +public: + MOCK_METHOD(int, GetRowCount, (int &count), (override)); + MOCK_METHOD(int, GoToNextRow, ()); + MOCK_METHOD(int, GetColumnIndex, (const std::string &columnName, int &columnIndex)); + MOCK_METHOD(int, GetInt, (int columnIndex, int &value), (override)); + MOCK_METHOD(int, GetLong, (int columnIndex, int64_t &value), (override)); +}; +} +} +#endif diff --git a/tests/mock/module_external/src/datashare_helper_mock.cpp b/tests/mock/module_external/src/datashare_helper_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92bc1e4a87161707c6c8f288548de64e8211ba55 --- /dev/null +++ b/tests/mock/module_external/src/datashare_helper_mock.cpp @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#include "datashare_helper.h" +#include "datashare_helper_mock.h" + +namespace OHOS { +namespace DataShare { +using namespace std; +using namespace FileManagement::Backup; + +std::shared_ptr DataShareHelper::Creator(const sptr &token, + const std::string &strUri, const std::string &extUri, const int waitTime, bool isSystem) +{ + GTEST_LOG_(INFO) << "DataShareHelperMock Creator is OK"; + return IDataShareHelper::idsh->Creator(token, strUri, extUri, waitTime, isSystem); +} + +std::shared_ptr Query(Uri &uri, const DataSharePredicates &predicates, + std::vector &colums, DatashareBusinessError *businessError) +{ + GTEST_LOG_(INFO) << "DataShareResultSet Query is OK"; + return IDataShareHelper::idsh->Query(uri, predicates, colums, businessError); +} +} +} \ No newline at end of file diff --git a/tests/mock/module_external/src/datashare_result_set_mock.cpp b/tests/mock/module_external/src/datashare_result_set_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b01978b6cf763057c62412b5fdc3dd05a9241396 --- /dev/null +++ b/tests/mock/module_external/src/datashare_result_set_mock.cpp @@ -0,0 +1,54 @@ +/* + * 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. + */ + +#include "datashare_result_set.h" +#include "datashare_result_set_mock.h" + +namespace OHOS { +namespace DataShare { +using namespace std; +using namespace FileManagement::Backup; + +int GetRowCount(int &count) +{ + GTEST_LOG_(INFO) << "DataShareResultSet GetRowCount is OK"; + return IDataShareResultSet::idsrs->GetRowCount(count); +} + +int GoToNextRow() +{ + GTEST_LOG_(INFO) << "DataShareResultSet GoToNextRow is OK"; + return IDataShareResultSet::idsrs->GoToNextRow(); +} + +int GetColumnIndex(const std::string &columnName, int &columnIndex) +{ + GTEST_LOG_(INFO) << "DataShareResultSet GetColumnIndex is OK"; + return IDataShareResultSet::idsrs->GetColumnIndex(columnName, columnIndex); +} + +int GetInt(int columnIndex, int &value) +{ + GTEST_LOG_(INFO) << "DataShareResultSet GetInt is OK"; + return IDataShareResultSet::idsrs->GetInt(columnIndex, value); +} + +int GetLong(int columnIndex, int64_t &value) +{ + GTEST_LOG_(INFO) << "DataShareResultSet GetLong is OK"; + return IDataShareResultSet::idsrs->GetLong(columnIndex, value); +} +} +} \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_external/BUILD.gn b/tests/unittests/backup_sa/module_external/BUILD.gn index f0f63b4d4ccd47830d0f5c50b8c6d90a4cf82022..d1ddd21eb2a0338acb8d936197a2385342276f15 100644 --- a/tests/unittests/backup_sa/module_external/BUILD.gn +++ b/tests/unittests/backup_sa/module_external/BUILD.gn @@ -72,11 +72,14 @@ ohos_unittest("storage_manager_service_test") { "${path_backup}/utils/include", "${path_backup}/utils/include/b_hilog", "${path_backup_mock}/utils_mock/include", + "${path_backup_mock}/module_external/include", ] sources = [ "${path_backup_mock}/utils_mock/src/b_jsonutil_mock.cpp", "${path_backup_mock}/utils_mock/src/b_sa_utils_mock.cpp", + "${path_backup_mock}/module_external/src/datashare_helper_mock.cpp", + "${path_backup_mock}/module_external/src/datashare_result_set_mock.cpp", "storage_manager_service_test.cpp", ] @@ -90,7 +93,7 @@ ohos_unittest("storage_manager_service_test") { defines = [ "LOG_TAG=\"app_file_service\"", - "LOG_DOMAIN=0xD200000", + "LOG_DOMAIN=0xD004303", "private = public", "protected = public", ] diff --git a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp index 5cf4c353e66e598a9319530254515de17cde72ef..876197ec56995f5ba976f2e06304b86625494668 100644 --- a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp +++ b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp @@ -22,28 +22,111 @@ #include #include "file_uri.h" #include "sandbox_helper.h" +#include "b_jsonutil_mock.h" +#include "b_sa_utils_mock.h" +#include "if_system_ability_manager.h" +#include "iremote_stub.h" +#include "iservice_registry.h" +#include "module_external/bms_adapter.h" #include "module_external/storage_manager_service.h" #include "module_external/storage_manager_service.cpp" +#include "datashare_helper_mock.h" +#include "datashare_result_set_mock.h" +#include "directory_ex.h" namespace OHOS { const std::string CAMERA_BUNDLENAME = "file"; +const std::string MMS_BUNDLENAME = "com.ohos.mms"; using namespace std; using namespace testing; using namespace FileManagement::Backup; namespace fs = std::filesystem; + +class SystemAbilityManagerMock : public ISystemAbilityManager { +public: + MOCK_METHOD((sptr), AsObject, ()); + MOCK_METHOD((std::vector), ListSystemAbilities, (unsigned int)); + MOCK_METHOD((sptr), GetSystemAbility, (int32_t)); + MOCK_METHOD((sptr), CheckSystemAbility, (int32_t)); + MOCK_METHOD(int32_t, RemoveSystemAbility, (int32_t)); + MOCK_METHOD(int32_t, SubscribeSystemAbility, (int32_t, (const sptr&))); + MOCK_METHOD(int32_t, UnSubscribeSystemAbility, (int32_t, (const sptr&))); + MOCK_METHOD((sptr), GetSystemAbility, (int32_t, const std::string&)); + MOCK_METHOD(sptr, CheckSystemAbility, (int32_t, const std::string&)); + MOCK_METHOD(int32_t, AddOnDemandSystemAbilityInfo, (int32_t, const std::u16string&)); + MOCK_METHOD((sptr), CheckSystemAbility, (int32_t, bool&)); + MOCK_METHOD(int32_t, AddSystemAbility, (int32_t, (const sptr&), const SAExtraProp&)); + MOCK_METHOD(int32_t, AddSystemProcess, (const std::u16string&, (const sptr&))); + MOCK_METHOD((sptr), LoadSystemAbility, (int32_t, int32_t)); + MOCK_METHOD(int32_t, LoadSystemAbility, (int32_t, (const sptr&))); + MOCK_METHOD(int32_t, LoadSystemAbility, (int32_t, const std::string&, (const sptr&))); + MOCK_METHOD(int32_t, UnloadSystemAbility, (int32_t)); + MOCK_METHOD(int32_t, CancelUnloadSystemAbility, (int32_t)); + MOCK_METHOD(int32_t, UnloadAllIdleSystemAbility, ()); + MOCK_METHOD(int32_t, GetSystemProcessInfo, (int32_t, SystemProcessInfo&)); + MOCK_METHOD(int32_t, GetRunningSystemProcess, (std::list&)); + MOCK_METHOD(int32_t, SubscribeSystemProcess, (const sptr&)); + MOCK_METHOD(int32_t, SendStrategy, (int32_t, (std::vector&), int32_t, std::string&)); + MOCK_METHOD(int32_t, UnSubscribeSystemProcess, (const sptr&)); + MOCK_METHOD(int32_t, GetOnDemandReasonExtraData, (int64_t, MessageParcel&)); + MOCK_METHOD(int32_t, GetOnDemandPolicy, (int32_t, OnDemandPolicyType, (std::vector&))); + MOCK_METHOD(int32_t, UpdateOnDemandPolicy, (int32_t, OnDemandPolicyType, + (const std::vector&))); + MOCK_METHOD(int32_t, GetOnDemandSystemAbilityIds, (std::vector&)); + MOCK_METHOD(int32_t, GetExtensionSaIds, (const std::string&, std::vector&)); + MOCK_METHOD(int32_t, GetExtensionRunningSaList, (const std::string&, (std::vector>&))); + MOCK_METHOD(int32_t, GetRunningSaExtensionInfoList, (const std::string&, (std::vector&))); + MOCK_METHOD(int32_t, GetCommonEventExtraDataIdlist, (int32_t, (std::vector&), const std::string&)); +}; + +class BundleMgrMock : public IRemoteStub { +public: + MOCK_METHOD((sptr), AsObject, ()); + MOCK_METHOD(bool, GetBundleInfos, (const AppExecFwk::BundleFlag, (std::vector&), int32_t)); + MOCK_METHOD(ErrCode, GetCloneBundleInfo, (const std::string&, int32_t, int32_t, AppExecFwk::BundleInfo&, int32_t)); +}; + class StorageManagerServiceTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp() override; void TearDown() override; + + static inline sptr bms = nullptr; + static inline sptr sam = nullptr; + static inline shared_ptr dsh = nullptr; + static inline shared_ptr dsrs = nullptr; + static inline shared_ptr dhelper = nullptr; + static inline shared_ptr dset = nullptr; }; -void StorageManagerServiceTest::SetUpTestCase(void) {} -void StorageManagerServiceTest::TearDownTestCase(void) {} +void StorageManagerServiceTest::SetUpTestCase(void) +{ + bms = sptr(new BundleMgrMock()); + sam = sptr(new SystemAbilityManagerMock()); + dsh = make_shared(); + DataShareHelperMock::idsh = dsh; + dsrs = make_shared(); + DataShareResultSetMock::idsrs = dsrs; + dhelper = make_shared(); + dset = std::make_shared(); +} + +void StorageManagerServiceTest::TearDownTestCase(void) +{ + bms = nullptr; + sam = nullptr; +} + void StorageManagerServiceTest::SetUp(void) {} void StorageManagerServiceTest::TearDown(void) {} +sptr SystemAbilityManagerClient::GetSystemAbilityManager() +{ + return StorageManagerServiceTest::sam; +} + /** * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsForIncrease_001 * @tc.desc: check the GetBundleStatsForIncrease function @@ -92,7 +175,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsFo testing::ext::TestSize.Level1) { uint32_t userId = 100; - std::string bundleName = "testBundle"; + std::string bundleName = MMS_BUNDLENAME; int64_t lastBackupTime = 123456789; std::vector pkgFileSizes; std::vector incPkgFileSizes; @@ -113,7 +196,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsFo HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_001, testing::ext::TestSize.Level1) { - std::string bundleName = ""; + std::string bundleName = MMS_BUNDLENAME; int64_t lastBackupTime = 123456789; uint32_t userId = 100; auto result = StorageManagerService::GetInstance().ReadIncludesExcludesPath(bundleName, lastBackupTime, userId); @@ -130,7 +213,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcl HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_002, testing::ext::TestSize.Level1) { - std::string bundleName = "testBundle"; + std::string bundleName = MMS_BUNDLENAME; int64_t lastBackupTime = 123456789; uint32_t userId = 100; // Assuming the file does not exist or cannot be opened @@ -148,7 +231,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcl HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_001, testing::ext::TestSize.Level1) { - std::string bundleName = CAMERA_BUNDLENAME; + std::string bundleName = MMS_BUNDLENAME; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::vector includes = {"data/storage/el1/base/" + DEFAULT_PATH_WITH_WILDCARD}; @@ -167,7 +250,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeF HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_002, testing::ext::TestSize.Level1) { - std::string bundleName = CAMERA_BUNDLENAME; + std::string bundleName = MMS_BUNDLENAME; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::vector includes = {NORMAL_SAND_PREFIX}; @@ -186,7 +269,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeF HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_003, testing::ext::TestSize.Level1) { - std::string bundleName = "testBundle"; + std::string bundleName = MMS_BUNDLENAME; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::vector includes = {}; @@ -206,7 +289,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRe testing::ext::TestSize.Level1) { uint32_t userId = 100; - std::string bundleName = "com.example.app"; + std::string bundleName = MMS_BUNDLENAME; std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/path/to/file"; std::vector realPaths; std::map pathMap; @@ -250,7 +333,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForInc testing::ext::TestSize.Level1) { ofstream closedStatFile; - std::string bundleName = "testBundle"; + std::string bundleName = MMS_BUNDLENAME; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::map pathMap; @@ -270,7 +353,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForInc testing::ext::TestSize.Level1) { ofstream statFile; - std::string bundleName = "testBundle"; + std::string bundleName = MMS_BUNDLENAME; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::map pathMap; @@ -311,17 +394,6 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForInc } catch (const fs::filesystem_error& e) { std::cerr << "Filesystem error: " << e.what() << '\n'; } - - statFile.open("/system/etc/NOTICE.TXT", ios::out | ios::trunc); - EXPECT_FALSE(!statFile.is_open()) << "file can not open"; - std::string bundleName = "testBundle"; - BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, - .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; - std::map pathMap; - std::map excludesMap; - auto result = StorageManagerService::GetInstance().CheckIfDirForIncludes("/data/service", paras, pathMap, - statFile, excludesMap); - EXPECT_EQ(result, std::make_tuple(true, true)); } /** @@ -363,14 +435,19 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ExcludeFilter_00 HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_001, testing::ext::TestSize.Level1) { - StorageManager::StorageStats storageStats; - std::string type = "MEDIA_TYPE"; int32_t userId = 100; + string basePath = "/data/storage/el2/" + to_string(userId); + string path = basePath + "/base"; + EXPECT_TRUE(OHOS::ForceCreateDirectory(path)); + + StorageManager::StorageStats storageStats; + std::string type = MEDIA_TYPE; + EXPECT_CALL(*sam, GetSystemAbility(_)).WillRepeatedly(Return(bms)); + EXPECT_CALL(*dsh, Creator(_, _, _, _, _)).WillRepeatedly(Return(dhelper)); + EXPECT_CALL(*dhelper, Query(_, _, _, _)).WillOnce(Return(dset)); + EXPECT_CALL(*dsrs, GetRowCount(_)).WillOnce(DoAll(SetArgReferee<0>(100), Return(0))); int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); - EXPECT_EQ(result, E_ERR); - EXPECT_EQ(storageStats.video_, 0); - EXPECT_EQ(storageStats.image_, 0); - EXPECT_EQ(storageStats.file_, 0); + EXPECT_EQ(result, E_GETROWCOUNT); } /** @@ -383,13 +460,10 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageSt testing::ext::TestSize.Level1) { StorageManager::StorageStats storageStats; - std::string type = "FILE_TYPE"; + std::string type = FILE_TYPE; int32_t userId = 100; int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); - EXPECT_EQ(result, E_ERR); - EXPECT_EQ(storageStats.video_, 0); - EXPECT_EQ(storageStats.image_, 0); - EXPECT_EQ(storageStats.file_, 0); + EXPECT_EQ(result, 0); } /** @@ -402,13 +476,51 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageSt testing::ext::TestSize.Level1) { StorageManager::StorageStats storageStats; - std::string type = "OTHER_TYPE"; + std::string type = "other"; int32_t userId = 100; int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); EXPECT_EQ(result, E_ERR); - EXPECT_EQ(storageStats.video_, 0); - EXPECT_EQ(storageStats.image_, 0); - EXPECT_EQ(storageStats.file_, 0); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetMediaStorageStats_001 + * @tc.number: GetMediaStorageStats_001 + * @tc.desc: 测试返回E_MEDIALIBRARY_ERROR + */ +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetMediaStorageStats_001, + testing::ext::TestSize.Level1) { + StorageManager::StorageStats storageStats; + + EXPECT_CALL(*sam, GetSystemAbility(_)).WillOnce(Return(nullptr)).WillRepeatedly(Return(bms)); + int32_t result = StorageManagerService::GetInstance().GetMediaStorageStats(storageStats); + EXPECT_EQ(result, E_REMOTE_IS_NULLPTR); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetMediaStorageStats_002 + * @tc.number: GetMediaStorageStats_002 + * @tc.desc: 测试返回E_MEDIALIBRARY_ERROR + */ +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetMediaStorageStats_002, + testing::ext::TestSize.Level1) { + StorageManager::StorageStats storageStats; + EXPECT_CALL(*dsh, Creator(_, _, _, _, _)).WillRepeatedly(Return(nullptr)); + int32_t result = StorageManagerService::GetInstance().GetMediaStorageStats(storageStats); + EXPECT_EQ(result, E_MEDIALIBRARY_ERROR); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetMediaStorageStats_002 + * @tc.number: GetMediaStorageStats_002 + * @tc.desc: 测试返回E_QUERY + */ +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetMediaStorageStats_003, + testing::ext::TestSize.Level1) { + StorageManager::StorageStats storageStats; + EXPECT_CALL(*dsh, Creator(_, _, _, _, _)).WillRepeatedly(Return(dhelper)); + EXPECT_CALL(*dhelper, Query(_, _, _, _)).WillOnce(Return(nullptr)); + int32_t result = StorageManagerService::GetInstance().GetMediaStorageStats(storageStats); + EXPECT_EQ(result, E_QUERY); } /** @@ -438,7 +550,8 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetMediaTypeAndS { auto resultSet = std::make_shared(); StorageManager::StorageStats storageStats; - StorageManagerService::GetInstance().GetMediaTypeAndSize(nullptr, storageStats); + EXPECT_CALL(*dsrs, GoToNextRow()).WillRepeatedly(Return(true)); + StorageManagerService::GetInstance().GetMediaTypeAndSize(resultSet, storageStats); EXPECT_EQ(storageStats.image_, 0); EXPECT_EQ(storageStats.audio_, 0); EXPECT_EQ(storageStats.video_, 0); @@ -493,4 +606,98 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_PathSortFunc_001 std::string path2 = "BBB"; EXPECT_TRUE(PathSortFunc(path1, path2)); } + +/** + * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsTest_001 + * @tc.number: GetBundleStatsTest_001 + * @tc.desc: 测试 GetBundleInfosForLocalCapabilities 接口 + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStats_001, testing::ext::TestSize.Level1) +{ + string bundleName = MMS_BUNDLENAME; + StorageManager::BundleStats bundleStats; + + bool result = StorageManagerService::GetInstance().GetBundleStats(bundleName, bundleStats); + EXPECT_TRUE(result); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_DeduplicationPath_001 + * @tc.number: DeduplicationPathTest_001 + * @tc.desc: 测试configPath为空时,不执行任何操作 + */ +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DeduplicationPath_001, + testing::ext::TestSize.Level1) +{ + std::vector configPaths; + StorageManagerService::GetInstance().DeduplicationPath(configPaths); + EXPECT_TRUE(true); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_DeduplicationPath_002 + * @tc.number: DeduplicationPathTest_002 + * @tc.desc: 测试configPath不为空时,执行去重 + */ +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DeduplicationPath_002, + testing::ext::TestSize.Level1) +{ + std::vector configPaths = {"path1", "path2", "path3"}; + StorageManagerService::GetInstance().DeduplicationPath(configPaths); + EXPECT_TRUE(true); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_ScanExtensionPath_001 + * @tc.number: ScanExtensionPathTest_001 + * @tc.desc: ScanExtensionPath 有效路径正确扫描 + */ +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ScanExtensionPath_001, + testing::ext::TestSize.Level1) +{ + std::string bundleName = MMS_BUNDLENAME; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 123456789, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::vector includes = {"/path/to/include"}; + std::vector excludes = {"/path/to/exclude"}; + std::map pathMap; + std::ofstream statFile("statfile.txt"); + + StorageManagerService::GetInstance().ScanExtensionPath(paras, includes, excludes, pathMap, statFile); + EXPECT_TRUE(pathMap.empty()); + EXPECT_TRUE(statFile.good()); + + statFile.close(); + remove("statfile.txt"); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_AddOuterDirIntoFileStat_001 + * @tc.number: AddOuterDirIntoFileStat_001 + * @tc.desc: AddOuterDirIntoFileStat 调用时正常返回 + */ +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_AddOuterDirIntoFileStat_001, + testing::ext::TestSize.Level1) +{ + std::string bundleName = MMS_BUNDLENAME; + std::string dir = "/data/app/el1/100/base/" + bundleName + "/.backup"; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::string sandboxDir = "/path/to/sandboxDir"; + std::ofstream statFile("statfile.txt"); + std::map excludesMap; + + StorageManagerService::GetInstance().AddOuterDirIntoFileStat(dir, paras, sandboxDir, statFile, excludesMap); + EXPECT_TRUE(true); + + dir = ""; + StorageManagerService::GetInstance().AddOuterDirIntoFileStat(dir, paras, sandboxDir, statFile, excludesMap); + EXPECT_TRUE(true); + + statFile.close(); + remove("statfile.txt"); +} } \ No newline at end of file