diff --git a/services/cloudfiledaemon.cfg b/services/cloudfiledaemon.cfg index 50ea8aa4ed046dc137c452f90abe90db26233963..ca6c19acdc6d9bc439892f0c5277d986001904a9 100644 --- a/services/cloudfiledaemon.cfg +++ b/services/cloudfiledaemon.cfg @@ -16,7 +16,8 @@ "ohos.permission.USE_CLOUD_DRIVE_SERVICE", "ohos.permission.READ_CLOUD_SYNC_CONFIG", "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS", - "ohos.permission.GET_BUNDLE_INFO" + "ohos.permission.GET_BUNDLE_INFO", + "ohos.permission.RUNNING_STATE_OBSERVER" ] }] } \ No newline at end of file diff --git a/services/cloudfiledaemon/BUILD.gn b/services/cloudfiledaemon/BUILD.gn index 4f87fe3915858c9a1abba5eee96e21c74649c924..b949a145253c0f0305ffc9471a905fc0d27a1473 100644 --- a/services/cloudfiledaemon/BUILD.gn +++ b/services/cloudfiledaemon/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2023-2024 Huawei Device Co., Ltd. +# Copyright (C) 2023-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 @@ -46,6 +46,8 @@ ohos_shared_library("cloudfiledaemon") { "src/cloud_disk/fuse_operations.cpp", "src/cloud_disk/account_status.cpp", "src/cloud_disk/account_status_listener.cpp", + "src/cloud_disk/io_message_listener.cpp", + "src/cloud_disk/appstate_observer.cpp", ] sources = [ @@ -98,6 +100,7 @@ ohos_shared_library("cloudfiledaemon") { "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", + "ability_runtime:app_manager" ] if (cloudsync_service_hicollie_enable) { diff --git a/services/cloudfiledaemon/include/cloud_disk/appstate_observer.h b/services/cloudfiledaemon/include/cloud_disk/appstate_observer.h new file mode 100644 index 0000000000000000000000000000000000000000..88f1d09b9dffcd2af051671a9963420948445012 --- /dev/null +++ b/services/cloudfiledaemon/include/cloud_disk/appstate_observer.h @@ -0,0 +1,47 @@ +/* + * 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 APPSTATE_OBSERVER_H +#define APPSTATE_OBSERVER_H + +#include "app_mgr_client.h" +#include "io_message_listener.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDisk { +class AppStateObserverManager { + public: + AppStateObserverManager() = default; + ~AppStateObserverManager() = default; + static AppStateObserverManager &GetInstance(); + void SubscribeAppState(const std::vector &bundleNameList); + void UnSubscribeAppState(); + + protected: + sptr appStateObserver_ = nullptr; +}; + +class AppStateObserver : public OHOS::AppExecFwk::ApplicationStateObserverStub { + public: + AppStateObserver() {}; + ~AppStateObserver() override = default; + + void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override; +}; +} // namespace CloudDisk +} // namespace FileManagement +} // namespace OHOS +#endif // APPSTATE_OBSERVER_H \ No newline at end of file diff --git a/services/cloudfiledaemon/include/cloud_disk/io_message_listener.h b/services/cloudfiledaemon/include/cloud_disk/io_message_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..655fe644aa1891991644c85dde2225236652e8f0 --- /dev/null +++ b/services/cloudfiledaemon/include/cloud_disk/io_message_listener.h @@ -0,0 +1,77 @@ +/* + * 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 CLOUD_FILE_DAEMON_IO_MESSAGE_STATUS_LISTENER_H +#define CLOUD_FILE_DAEMON_IO_MESSAGE_STATUS_LISTENER_H + +#include +#include +#include +#include +#include + +#include "application_state_observer_stub.h" +#include "common_event_subscriber.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDisk { + +struct IoData { + int64_t rchar = 0; + int64_t syscr = 0; + int64_t readBytes = 0; + int64_t syscopen = 0; + int64_t syscstat = 0; +}; + +struct IoDataToWrite { + std::string bundleName; + int64_t rcharDiff = 0; + int64_t syscrDiff = 0; + int64_t readBytesDiff = 0; + int64_t syscopenDiff = 0; + int64_t syscstatDiff = 0; + double result = 0; +}; + +class IoMessageManager { +private: + std::string currentBundleName = ""; + std::atomic isThreadRunning{false}; + std::thread ioThread; + std::mutex sleepMutex; + std::mutex cvMute; + std::condition_variable sleepCv; + std::map bundleTimeMap; + IoData currentData; + IoData preData; + IoDataToWrite dataToWrite; + AppExecFwk::AppStateData lastestAppStateData; + + bool ReadIoDataFromFile(const std::string &path); + void RecordDataToFile(const std::string &path); + bool IsFirstLineHeader(const std::string &path); + void RecordIoData(); + void ProcessIoData(const std::string &path); + +public: + static IoMessageManager &GetInstance(); + void OnReceiveEvent(const AppExecFwk::AppStateData &appStateData); +}; +} // namespace CloudDisk +} // namespace FileManagement +} // namespace OHOS +#endif // CLOUD_FILE_DAEMON_IO_MESSAGE_LISTENER_H \ No newline at end of file diff --git a/services/cloudfiledaemon/src/cloud_disk/appstate_observer.cpp b/services/cloudfiledaemon/src/cloud_disk/appstate_observer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5b0ac5d414eab9d54f501606aa694c07e4e2ee3 --- /dev/null +++ b/services/cloudfiledaemon/src/cloud_disk/appstate_observer.cpp @@ -0,0 +1,80 @@ +/* + * 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 "appstate_observer.h" +#include "dfs_error.h" +#include "singleton.h" +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement { +namespace CloudDisk { +using namespace std; + +AppStateObserverManager &AppStateObserverManager::GetInstance() +{ + static AppStateObserverManager instance; + return instance; +} + +void AppStateObserverManager::SubscribeAppState(const std::vector &bundleNameList) +{ + auto appMgrClient = DelayedSingleton::GetInstance(); + if (appMgrClient == nullptr) { + LOGE("appMgrClient_ is nullptr"); + return; + } + if (appStateObserver_ != nullptr) { + LOGE("appStateObserver has been registed"); + return; + } + appStateObserver_ = sptr(new (std::nothrow) AppStateObserver()); + if (appStateObserver_ == nullptr) { + LOGE("get appStateObserver failed"); + return; + } + + int32_t result = appMgrClient->RegisterApplicationStateObserver(appStateObserver_, bundleNameList); + if (result != E_OK) { + LOGE("RegistApplicationStateObserver failed result = %{public}d", result); + appStateObserver_ = nullptr; + } +} + +void AppStateObserverManager::UnSubscribeAppState() +{ + if (appStateObserver_ == nullptr) { + LOGE("appStateObserver is nullptr"); + return; + } + + auto appMgrClient = DelayedSingleton::GetInstance(); + if (appMgrClient == nullptr) { + LOGE("appMgrClient_ is nullptr"); + return; + } + int32_t result = appMgrClient->UnregisterApplicationStateObserver(appStateObserver_); + if (result != E_OK) { + LOGE("UnregisterApplicationStateObserver failed result = %{public}d", result); + } + appStateObserver_ = nullptr; +} + +void AppStateObserver::OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) +{ + IoMessageManager::GetInstance().OnReceiveEvent(appStateData); +} +} // namespace CloudDisk +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/services/cloudfiledaemon/src/cloud_disk/io_message_listener.cpp b/services/cloudfiledaemon/src/cloud_disk/io_message_listener.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cc66bdfe1c2563661d0ff21bc6789064b2b61a2 --- /dev/null +++ b/services/cloudfiledaemon/src/cloud_disk/io_message_listener.cpp @@ -0,0 +1,189 @@ +/* + * 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 "io_message_listener.h" + +#include "utils_log.h" + +using namespace std; +using namespace chrono; + +namespace OHOS { +namespace FileManagement { +namespace CloudDisk { + +const string DESK_BUNDLE_NAME = "com.ohos.sceneboard"; +const int32_t GET_FREQUENCY = 5; +const int32_t READ_THRESHOLD = 1000; +const int32_t OPEN_THRESHOLD = 1000; +const int32_t STAT_THRESHOLD = 1000; +const string IO_DATA_FILE_PATH = "/data/service/el1/public/cloudfile/rdb/io_message.csv"; +const int32_t TYPE_FRONT = 2; +const int32_t TYPE_BACKGROUND = 4; + +IoMessageManager &IoMessageManager::GetInstance() +{ + static IoMessageManager instance; + return instance; +} + +bool IoMessageManager::ReadIoDataFromFile(const std::string &path) +{ + ifstream inFile(path); + if (!inFile.is_open()) { + LOGE("Failed to open io, err: %{public}d", errno); + return false; + } + + string line; + while (getline(inFile, line)) { + size_t colon = line.find(':'); + if (colon == string::npos) continue; + + string key = line.substr(0, colon); + string value = line.substr(colon + 1); + + try { + if (key == "rchar") currentData.rchar = stol(value); + if (key == "syscr") currentData.syscr = stol(value); + if (key == "read_bytes") currentData.readBytes = stol(value); + if (key == "syscopen") currentData.syscopen = stol(value); + if (key == "syscstat") currentData.syscstat = stol(value); + } catch (const invalid_argument& e) { + LOGE("Invalid argument: %{public}s", e.what()); + currentData = preData; + return false; + } catch (const out_of_range& e) { + LOGE("Out of range: %{public}s", e.what()); + currentData = preData; + return false; + } + } + return true; +} + +bool IoMessageManager::IsFirstLineHeader(const string &path) +{ + ifstream inFile(path); + if (!inFile.is_open()) { + return false; + } + + string firstLine; + getline(inFile, firstLine); + return firstLine.find("time") != string::npos; +} + +void IoMessageManager::RecordDataToFile(const string &path) +{ + ofstream outFile(path, ios::app); + if (!outFile) { + LOGE("Failed to open io file to write, err: %{public}d", errno); + return; + } + + if (!IsFirstLineHeader(path)) { + outFile << "time, bundle_name, rchar_diff, syscr_diff, read_bytes_diff, syscopen_diff, syscstat_diff, result\n"; + } + + outFile << bundleTimeMap[dataToWrite.bundleName] << "," + << dataToWrite.bundleName << "," + << dataToWrite.rcharDiff << "," + << dataToWrite.syscrDiff << "," + << dataToWrite.readBytesDiff << "," + << dataToWrite.syscopenDiff << "," + << dataToWrite.syscstatDiff << "," + << dataToWrite.result << "\n"; + LOGI("Write io data success"); +} + +void IoMessageManager::ProcessIoData(const string &path) +{ + if (currentBundleName != lastestAppStateData.bundleName) { + bundleTimeMap[lastestAppStateData.bundleName] = 0; + } + + if (!ReadIoDataFromFile(path)) { + return; + } + + if (preData.rchar == 0) { + preData = currentData; + return; + } + + currentBundleName = lastestAppStateData.bundleName; + dataToWrite.bundleName = lastestAppStateData.bundleName; + dataToWrite.rcharDiff = currentData.rchar - preData.rchar; + dataToWrite.syscrDiff = currentData.syscr - preData.syscr; + dataToWrite.readBytesDiff = currentData.readBytes - preData.readBytes; + dataToWrite.syscopenDiff = currentData.syscopen - preData.syscopen; + dataToWrite.syscstatDiff = currentData.syscstat - preData.syscstat; + + /* Prevent division by zero */ + if (dataToWrite.rcharDiff + 1 != 0) { + dataToWrite.result = (dataToWrite.readBytesDiff) / (dataToWrite.rcharDiff + 1) * dataToWrite.syscrDiff; + } + + if (dataToWrite.result >= READ_THRESHOLD || + dataToWrite.syscopenDiff >= OPEN_THRESHOLD || + dataToWrite.syscstatDiff >= STAT_THRESHOLD) { + RecordDataToFile(IO_DATA_FILE_PATH); + } + + preData = currentData; + bundleTimeMap[lastestAppStateData.bundleName] += GET_FREQUENCY; +} + +void IoMessageManager::RecordIoData() +{ + while (isThreadRunning.load()) { + if (!lastestAppStateData.bundleName.empty()) { + string path = "/proc/" + to_string(lastestAppStateData.pid) + "/sys_count"; + ProcessIoData(path); + } + unique_lock lock(sleepMutex); + sleepCv.wait_for(lock, chrono::seconds(GET_FREQUENCY), [&] { + return !isThreadRunning.load(); + }); + } +} + +void IoMessageManager::OnReceiveEvent(const AppExecFwk::AppStateData &appStateData) +{ + if (appStateData.bundleName != DESK_BUNDLE_NAME) { + if (appStateData.state == TYPE_FRONT) { + lastestAppStateData = appStateData; + if (!ioThread.joinable()) { + isThreadRunning.store(true); + ioThread = thread(&IoMessageManager::RecordIoData, this); + } + return; + } + if (appStateData.state == TYPE_BACKGROUND) { + if (ioThread.joinable()) { + lock_guard lock(cvMute); + isThreadRunning.store(false); + sleepCv.notify_all(); + ioThread.join(); + ioThread = thread(); + currentData = {}; + preData = {}; + } + } + } +} +} // namespace CloudDisk +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/services/cloudfiledaemon/src/ipc/cloud_daemon.cpp b/services/cloudfiledaemon/src/ipc/cloud_daemon.cpp index 9ad6c858375eb6bf6f79e1793c2342848f5243e4..557fb9a2ba461d93a9f46ede98b8a20b14d566ce 100644 --- a/services/cloudfiledaemon/src/ipc/cloud_daemon.cpp +++ b/services/cloudfiledaemon/src/ipc/cloud_daemon.cpp @@ -22,6 +22,7 @@ #include #include +#include "appstate_observer.h" #include "cloud_file_fault_event.h" #include "concurrent_queue.h" #include "dfs_error.h" @@ -117,6 +118,12 @@ void CloudDaemon::OnStart() /* load cloud file ext plugin */ CloudFile::PluginLoader::GetInstance().LoadCloudKitPlugin(); ConcurrentQueue::GetInstance().Init(ffrt_qos_user_initiated, MAX_CONCURRENT_THREADS); + + std::thread listenThread([&] { + vector bundleNameList = {}; + CloudDisk::AppStateObserverManager::GetInstance().SubscribeAppState(bundleNameList); + }); + listenThread.detach(); LOGI("Start service successfully"); } @@ -163,6 +170,7 @@ void CloudDaemon::OnStop() state_ = ServiceRunningState::STATE_NOT_START; registerToService_ = false; ConcurrentQueue::GetInstance().Deinit(); + CloudDisk::AppStateObserverManager::GetInstance().UnSubscribeAppState(); LOGI("Stop finished successfully"); } diff --git a/test/unittests/cloud_disk/BUILD.gn b/test/unittests/cloud_disk/BUILD.gn index 21d8a1328312b034ef718995ba23119aea46b771..609647269dffabe8410e271ac8c1db9685de0578 100644 --- a/test/unittests/cloud_disk/BUILD.gn +++ b/test/unittests/cloud_disk/BUILD.gn @@ -1,4 +1,4 @@ -# 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 @@ -446,6 +446,123 @@ ohos_unittest("file_operations_helper_test") { use_exceptions = true } +ohos_unittest("io_message_listener_test") { + module_out_path = "dfs_service/distributed_filesystem" + + include_dirs = [ + "${distributedfile_path}/adapter/cloud_adapter_example/include", + "${distributedfile_path}/interfaces/inner_api/native/cloudsync_kit_inner", + "${distributedfile_path}/utils/log/include", + "${services_path}/clouddisk_database/include", + "${services_path}/cloudfiledaemon/include/cloud_disk/", + "${services_path}/cloudfiledaemon/src/cloud_disk/", + "mock", + ] + + sources = [ + "${services_path}/cloudfiledaemon/src/cloud_disk/io_message_listener.cpp", + "io_message_listener_test.cpp", + ] + + deps = [ + "${clouddisk_database_path}:clouddisk_database", + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${utils_path}:libdistributedfiledentry", + "${utils_path}:libdistributedfileutils", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:dataobs_manager", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ffrt:libffrt", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "init:libbegetutil", + "libfuse:libfuse", + "relational_store:native_rdb", + "ability_runtime:app_manager", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD004308", + "LOG_TAG=\"CLOUD_DAEMON_API\"", + ] + + if (cloudsync_service_hicollie_enable) { + external_deps += [ "hicollie:libhicollie" ] + defines += [ "HICOLLIE_ENABLE" ] + } + + use_exceptions = true +} + +ohos_unittest("appstate_observer_test") { + module_out_path = "dfs_service/distributed_filesystem" + + include_dirs = [ + "${distributedfile_path}/adapter/cloud_adapter_example/include", + "${distributedfile_path}/interfaces/inner_api/native/cloudsync_kit_inner", + "${distributedfile_path}/utils/log/include", + "${services_path}/clouddisk_database/include", + "${services_path}/cloudfiledaemon/include/cloud_disk/", + "${services_path}/cloudfiledaemon/src/cloud_disk/", + "mock", + ] + + sources = [ + "${services_path}/cloudfiledaemon/src/cloud_disk/appstate_observer.cpp", + "${services_path}/cloudfiledaemon/src/cloud_disk/io_message_listener.cpp", + "appstate_observer_test.cpp", + ] + + deps = [ + "${clouddisk_database_path}:clouddisk_database", + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${utils_path}:libdistributedfiledentry", + "${utils_path}:libdistributedfileutils", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:dataobs_manager", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "ffrt:libffrt", + "googletest:gmock_main", + "googletest:gtest_main", + "ipc:ipc_single", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "init:libbegetutil", + "libfuse:libfuse", + "relational_store:native_rdb", + "ability_runtime:app_manager", + ] + + defines = [ + "private=public", + "protected=public", + "LOG_DOMAIN=0xD004308", + "LOG_TAG=\"CLOUD_DAEMON_API\"", + ] + + if (cloudsync_service_hicollie_enable) { + external_deps += [ "hicollie:libhicollie" ] + defines += [ "HICOLLIE_ENABLE" ] + } + + use_exceptions = true +} + group("cloud_disk_test") { testonly = true @@ -457,5 +574,7 @@ group("cloud_disk_test") { ":file_operations_helper_test", ":file_operations_local_test", ":fuse_operations_test", + ":io_message_listener_test", + ":appstate_observer_test", ] } diff --git a/test/unittests/cloud_disk/appstate_observer_test.cpp b/test/unittests/cloud_disk/appstate_observer_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..099efa24600280e66ff17be58a98962cd115941d --- /dev/null +++ b/test/unittests/cloud_disk/appstate_observer_test.cpp @@ -0,0 +1,273 @@ +/* + * 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 "appstate_observer.h" + +#include +#include +#include +#include +#include + +#include "assistant.h" +#include "application_state_observer_stub.h" +#include "dfs_error.h" +#include "io_message_listener.h" +#include "singleton.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDisk::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class AppStateObserverTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr appStateObserverManager_ = nullptr; + static inline shared_ptr appStateObserver_ = nullptr; + static inline shared_ptr insMock = nullptr; +}; + +void AppStateObserverTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + appStateObserverManager_ = make_shared(); + appStateObserver_ = make_shared(); + insMock = make_shared(); + Assistant::ins = insMock; +} + +void AppStateObserverTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + appStateObserverManager_ = nullptr; + appStateObserver_ = nullptr; + Assistant::ins = nullptr; + insMock = nullptr; +} + +void AppStateObserverTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void AppStateObserverTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: OnForegroundApplicationChangedTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, OnForegroundApplicationChangedTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OnForegroundApplicationChangedTest001 Start"; + try { + auto appStateData_= make_shared(); + appStateData_->bundleName = "com.ohos.sceneboard"; + appStateObserver_->OnForegroundApplicationChanged(*appStateData_); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "OnForegroundApplicationChangedTest001 ERROR"; + } + GTEST_LOG_(INFO) << "OnForegroundApplicationChangedTest001 End"; +} + +/** + * @tc.name: GetInstanceTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, GetInstanceTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetInstanceTest001 Start"; + try { + shared_ptr appStateObserverManager1_ = make_shared(); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetInstanceTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetInstanceTest001 End"; +} + +/** + * @tc.name: SubscribeAppStateTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, SubscribeAppStateTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SubscribeAppStateTest001 Start"; + try { + vector bundleNameList = {"com.example"}; + OHOS::DelayedSingleton::instance_ = nullptr; + appStateObserverManager_->SubscribeAppState(bundleNameList); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "SubscribeAppStateTest001 ERROR"; + } + GTEST_LOG_(INFO) << "SubscribeAppStateTest001 End"; +} + +/** + * @tc.name: SubscribeAppStateTest002 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, SubscribeAppStateTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SubscribeAppStateTest002 Start"; + try { + vector bundleNameList = {"com.example"}; + OHOS::DelayedSingleton::instance_ = + make_shared(); + sptr appStateObserver1_ = + sptr(new (std::nothrow) AppStateObserver()); + appStateObserverManager_->appStateObserver_ = appStateObserver1_; + appStateObserverManager_->SubscribeAppState(bundleNameList); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "SubscribeAppStateTest002 ERROR"; + } + GTEST_LOG_(INFO) << "SubscribeAppStateTest002 End"; +} + +/** + * @tc.name: SubscribeAppStateTest003 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, SubscribeAppStateTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SubscribeAppStateTest003 Start"; + try { + vector bundleNameList = {"com.example"}; + OHOS::DelayedSingleton::instance_ = + make_shared(); + appStateObserverManager_->appStateObserver_ = nullptr; + appStateObserverManager_->SubscribeAppState(bundleNameList); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SubscribeAppStateTest003 ERROR"; + } + GTEST_LOG_(INFO) << "SubscribeAppStateTest003 End"; +} + +/** + * @tc.name: SubscribeAppStateTest004 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, SubscribeAppStateTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SubscribeAppStateTest004 Start"; + try { + vector bundleNameList = {""}; + OHOS::DelayedSingleton::instance_ = + make_shared(); + appStateObserverManager_->appStateObserver_ = nullptr; + appStateObserverManager_->SubscribeAppState(bundleNameList); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SubscribeAppStateTest004 ERROR"; + } + GTEST_LOG_(INFO) << "SubscribeAppStateTest004 End"; +} + +/** + * @tc.name: UnSubscribeAppStateTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, UnSubscribeAppStateTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest001 Start"; + try { + appStateObserverManager_->appStateObserver_ = nullptr; + appStateObserverManager_->UnSubscribeAppState(); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest001 ERROR"; + } + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest001 End"; +} + +/** + * @tc.name: UnSubscribeAppStateTest002 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, UnSubscribeAppStateTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest002 Start"; + try { + sptr appStateObserver1_ = + sptr(new (std::nothrow) AppStateObserver()); + appStateObserverManager_->appStateObserver_ = appStateObserver1_; + OHOS::DelayedSingleton::instance_ = nullptr; + appStateObserverManager_->UnSubscribeAppState(); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest002 ERROR"; + } + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest002 End"; +} + +/** + * @tc.name: UnSubscribeAppStateTest003 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(AppStateObserverTest, UnSubscribeAppStateTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest003 Start"; + try { + sptr appStateObserver1_ = + sptr(new (std::nothrow) AppStateObserver()); + appStateObserverManager_->appStateObserver_ = appStateObserver1_; + OHOS::DelayedSingleton::instance_ = + make_shared(); + appStateObserverManager_->UnSubscribeAppState(); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest003 ERROR"; + } + GTEST_LOG_(INFO) << "UnSubscribeAppStateTest003 End"; +} +} // namespace OHOS::FileManagement::CloudDisk::Test \ No newline at end of file diff --git a/test/unittests/cloud_disk/io_message_listener_test.cpp b/test/unittests/cloud_disk/io_message_listener_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89faf55bb45687eb4413d3406c7b6a06067c4dea --- /dev/null +++ b/test/unittests/cloud_disk/io_message_listener_test.cpp @@ -0,0 +1,1341 @@ +/* + * 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 "io_message_listener.h" + +#include +#include +#include +#include +#include + +#include "assistant.h" +#include "application_state_observer_stub.h" +#include "dfs_error.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDisk::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +const int64_t ZERO = 0; +const int64_t ONE = 1; +const int32_t ABOVE_DATA = 2000; +const int32_t THREAD_SLEEP_TIME = 100; +const int32_t FRONT_EVENT = 2; +const int32_t BACKGROUND_EVENT = 4; +const int32_t UNKNOWN_EVENT = 8; + +class IoMessageListenerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr ioMessageManager_ = nullptr; + static inline shared_ptr insMock = nullptr; +}; + +void IoMessageListenerTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; + ioMessageManager_ = make_shared(); + insMock = make_shared(); + Assistant::ins = insMock; +} + +void IoMessageListenerTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; + ioMessageManager_ = nullptr; + Assistant::ins = nullptr; + insMock = nullptr; +} + +void IoMessageListenerTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void IoMessageListenerTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: ReadIoDataFromFileTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest001 Start"; + try { + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest001 ERROR"; + } + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest001 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest002 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest002 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_TRUE(ret); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest002 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest002 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest003 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest003 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:a\n"; + write(fd, line1.c_str(), line1.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest003 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest003 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest004 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest004 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:a\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest004 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest004 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest005 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest005 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_TRUE(ret); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest005 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest005 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest006 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest006, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest006 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:a\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest006 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest006 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest007 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest007, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest007 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:a\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest007 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest007 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest008 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest008, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest008 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:a\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest008 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest008 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest009 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest009, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest009 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:99999999999999999999\n"; + write(fd, line1.c_str(), line1.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest009 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest009 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest010 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest010, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest010 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:99999999999999999999\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest010 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest010 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest011 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest011, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest011 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:99999999999999999999\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest011 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest011 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest012 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest012, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest012 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:99999999999999999999\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest012 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest012 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest013 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest013, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest013 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:99999999999999999999\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest013 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest013 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest014 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest014, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest014 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + write(fd, line1.c_str(), line1.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_TRUE(ret); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest014 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest014 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest015 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest015, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest015 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_TRUE(ret); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest015 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest015 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest016 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest016, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest016 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_TRUE(ret); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest016 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest016 End"; +} + +/** + * @tc.name: ReadIoDataFromFileTest017 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ReadIoDataFromFileTest017, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest017 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + close(fd); + bool ret = ioMessageManager_->ReadIoDataFromFile(path); + EXPECT_TRUE(ret); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest017 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ReadIoDataFromFileTest017 End"; +} + +/** + * @tc.name: IsFirstLineHeaderTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, IsFirstLineHeaderTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest001 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + bool ret = ioMessageManager_->IsFirstLineHeader(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest001 ERROR"; + } + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest001 End"; +} + +/** + * @tc.name: IsFirstLineHeaderTest002 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, IsFirstLineHeaderTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest002 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + close(fd); + bool ret = ioMessageManager_->IsFirstLineHeader(path); + EXPECT_FALSE(ret); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest002 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest002 End"; +} + +/** + * @tc.name: IsFirstLineHeaderTest003 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, IsFirstLineHeaderTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest003 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "time\n"; + write(fd, line1.c_str(), line1.size()); + close(fd); + bool ret = ioMessageManager_->IsFirstLineHeader(path); + EXPECT_TRUE(ret); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest003 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "IsFirstLineHeaderTest003 End"; +} + +/** + * @tc.name: RecordDataToFileTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, RecordDataToFileTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RecordDataToFileTest001 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->RecordDataToFile(path); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "RecordDataToFileTest001 ERROR"; + } + GTEST_LOG_(INFO) << "RecordDataToFileTest001 End"; +} + +/** + * @tc.name: RecordDataToFileTest002 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, RecordDataToFileTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RecordDataToFileTest002 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "time\n"; + write(fd, line1.c_str(), line1.size()); + close(fd); + ioMessageManager_->RecordDataToFile(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "RecordDataToFileTest002 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "RecordDataToFileTest002 End"; +} + +/** + * @tc.name: RecordDataToFileTest003 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, RecordDataToFileTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RecordDataToFileTest003 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "test\n"; + write(fd, line1.c_str(), line1.size()); + close(fd); + ioMessageManager_->RecordDataToFile(path); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "RecordDataToFileTest003 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "RecordDataToFileTest003 End"; +} + +/** + * @tc.name: ProcessIoDataTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest001 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = ""; + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ProcessIoDataTest001 ERROR"; + } + GTEST_LOG_(INFO) << "ProcessIoDataTest001 End"; +} + +/** + * @tc.name: ProcessIoDataTest002 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest002 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ProcessIoDataTest002 ERROR"; + } + GTEST_LOG_(INFO) << "ProcessIoDataTest002 End"; +} + +/** + * @tc.name: ProcessIoDataTest003 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest003 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:a\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProcessIoDataTest003 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest003 End"; +} + +/** + * @tc.name: ProcessIoDataTest004 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest004 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:99999999999999999999\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->ProcessIoData(path); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ProcessIoDataTest004 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest004 End"; +} + +/** + * @tc.name: ProcessIoDataTest005 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest005 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->preData.rchar = ONE; + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProcessIoDataTest005 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest005 End"; +} + +/** + * @tc.name: ProcessIoDataTest006 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest006, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest006 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->preData.rchar = ZERO; + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProcessIoDataTest006 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest006 End"; +} + +/** + * @tc.name: ProcessIoDataTest007 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest007, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest007 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:2\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->preData.rchar = ONE; + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProcessIoDataTest007 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest007 End"; +} + +/** + * @tc.name: ProcessIoDataTest008 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest008, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest008 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:0\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->preData.rchar = ONE; + ioMessageManager_->ProcessIoData(path); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ProcessIoDataTest008 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest008 End"; +} + +/** + * @tc.name: ProcessIoDataTest009 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest009, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest009 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:0\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:1\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->preData.rchar = ONE; + ioMessageManager_->dataToWrite.result = ABOVE_DATA; + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProcessIoDataTest009 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest009 End"; +} + +/** + * @tc.name: ProcessIoDataTest010 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest010, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest010 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:0\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:2000\n"; + string line5 = "syscstat:0\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->preData.rchar = ONE; + ioMessageManager_->dataToWrite.result = ZERO; + ioMessageManager_->preData.syscopen = ZERO; + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProcessIoDataTest010 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest010 End"; +} + +/** + * @tc.name: ProcessIoDataTest011 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest011, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest011 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:0\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:0\n"; + string line5 = "syscstat:2000\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->preData.rchar = ONE; + ioMessageManager_->dataToWrite.result = ZERO; + ioMessageManager_->preData.syscopen = ZERO; + ioMessageManager_->preData.syscstat = ZERO; + ioMessageManager_->ProcessIoData(path); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProcessIoDataTest011 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest011 End"; +} + +/** + * @tc.name: ProcessIoDataTest012 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest012, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest012 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:0\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:0\n"; + string line5 = "syscstat:0\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->preData.rchar = ZERO; + ioMessageManager_->dataToWrite.result = ZERO; + ioMessageManager_->preData.syscopen = ZERO; + ioMessageManager_->preData.syscstat = ZERO; + ioMessageManager_->ProcessIoData(path); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ProcessIoDataTest012 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest012 End"; +} + +/** + * @tc.name: ProcessIoDataTest013 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest013, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest013 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:1\n"; + string line2 = "syscr:1\n"; + string line3 = "read_bytes:1\n"; + string line4 = "syscopen:1\n"; + string line5 = "syscstat:a\n"; + write(fd, line1.c_str(), line1.size()); + write(fd, line2.c_str(), line2.size()); + write(fd, line3.c_str(), line3.size()); + write(fd, line4.c_str(), line4.size()); + write(fd, line5.c_str(), line5.size()); + close(fd); + ioMessageManager_->ProcessIoData(path); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ProcessIoDataTest013 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest013 End"; +} + +/** + * @tc.name: ProcessIoDataTest014 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, ProcessIoDataTest014, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProcessIoDataTest014 Start"; + string path = "/data/service/el1/public/cloudfile/rdb/1.txt"; + try { + ioMessageManager_->currentBundleName = ""; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + int fd = open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + string line1 = "rchar:99999999999999999999\n"; + write(fd, line1.c_str(), line1.size()); + close(fd); + ioMessageManager_->currentData.rchar = ZERO; + ioMessageManager_->preData.rchar = ONE; + ioMessageManager_->dataToWrite.result = ZERO; + ioMessageManager_->currentData.syscopen = ZERO; + ioMessageManager_->preData.syscopen = ZERO; + ioMessageManager_->currentData.syscstat = ZERO; + ioMessageManager_->preData.syscopen = ZERO; + ioMessageManager_->ProcessIoData(path); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "ProcessIoDataTest014 ERROR"; + } + unlink(path.c_str()); + GTEST_LOG_(INFO) << "ProcessIoDataTest014 End"; +} + +/** + * @tc.name: RecordIoDataTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, RecordIoDataTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RecordIoDataTest001 Start"; + try { + ioMessageManager_->RecordIoData(); + EXPECT_FALSE(false); + } catch (...) { + EXPECT_FALSE(true); + GTEST_LOG_(INFO) << "RecordIoDataTest001 ERROR"; + } + GTEST_LOG_(INFO) << "RecordIoDataTest001 End"; +} + +/** + * @tc.name: RecordIoDataTest002 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, RecordIoDataTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RecordIoDataTest002 Start"; + try { + ioMessageManager_->isThreadRunning = true; + std::thread recordThread([this]() { + this->ioMessageManager_->RecordIoData(); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_SLEEP_TIME)); + ioMessageManager_->isThreadRunning = false; + recordThread.join(); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "RecordIoDataTest002 ERROR"; + } + GTEST_LOG_(INFO) << "RecordIoDataTest002 End"; +} + +/** + * @tc.name: RecordIoDataTest003 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, RecordIoDataTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RecordIoDataTest003 Start"; + try { + ioMessageManager_->isThreadRunning = true; + ioMessageManager_->lastestAppStateData.bundleName = "test"; + std::thread recordThread([this]() { + this->ioMessageManager_->RecordIoData(); + }); + std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_SLEEP_TIME)); + ioMessageManager_->isThreadRunning = false; + recordThread.join(); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "RecordIoDataTest003 ERROR"; + } + GTEST_LOG_(INFO) << "RecordIoDataTest003 End"; +} + +/** + * @tc.name: GetInstanceTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, GetInstanceTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetInstanceTest001 Start"; + try { + shared_ptr ioMessageManager1_ = make_shared(); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetInstanceTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetInstanceTest001 End"; +} + +/** + * @tc.name: OnReceiveEventTest001 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, OnReceiveEventTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OnReceiveEventTest001 Start"; + try { + AppExecFwk::AppStateData appStateData; + appStateData.bundleName = "com.ohos.sceneboard"; + ioMessageManager_->OnReceiveEvent(appStateData); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "OnReceiveEventTest001 ERROR"; + } + GTEST_LOG_(INFO) << "OnReceiveEventTest001 End"; +} + +/** + * @tc.name: OnReceiveEventTest002 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, OnReceiveEventTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OnReceiveEventTest002 Start"; + try { + AppExecFwk::AppStateData appStateData; + appStateData.bundleName = ""; + appStateData.state = FRONT_EVENT; + ioMessageManager_->ioThread =std::thread(); + ioMessageManager_->OnReceiveEvent(appStateData); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "OnReceiveEventTest002 ERROR"; + } + GTEST_LOG_(INFO) << "OnReceiveEventTest002 End"; +} + +/** + * @tc.name: OnReceiveEventTest004 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, OnReceiveEventTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OnReceiveEventTest004 Start"; + AppExecFwk::AppStateData appStateData; + appStateData.bundleName = ""; + appStateData.state = BACKGROUND_EVENT; + try { + ioMessageManager_->OnReceiveEvent(appStateData); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "OnReceiveEventTest004 ERROR"; + } + GTEST_LOG_(INFO) << "OnReceiveEventTest004 End"; +} + +/** + * @tc.name: OnReceiveEventTest005 + * @tc.desc: Read IO data + * @tc.type: FUNC + * @tc.require: issuesI92WQP + */ +HWTEST_F(IoMessageListenerTest, OnReceiveEventTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OnReceiveEventTest005 Start"; + AppExecFwk::AppStateData appStateData; + appStateData.bundleName = ""; + appStateData.state = UNKNOWN_EVENT; + try { + ioMessageManager_->OnReceiveEvent(appStateData); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "OnReceiveEventTest005 ERROR"; + } + GTEST_LOG_(INFO) << "OnReceiveEventTest005 End"; +} + +} // namespace OHOS::FileManagement::CloudDisk::Test \ No newline at end of file diff --git a/test/unittests/cloud_disk/mock/singleton.h b/test/unittests/cloud_disk/mock/singleton.h new file mode 100644 index 0000000000000000000000000000000000000000..841a5367c8af837e4d3abd04a9be68f63c3b2703 --- /dev/null +++ b/test/unittests/cloud_disk/mock/singleton.h @@ -0,0 +1,36 @@ +/* + * 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 CLOUD_DISK_SINGLETON_TEST_H +#define CLOUD_DISK_SINGLETON_TEST_H + +#include + +namespace OHOS { +template +class DelayedSingleton { +public: + static std::shared_ptr GetInstance() + { + if (instance_ == nullptr) { + return nullptr; + } + return instance_; + } + static inline std::shared_ptr instance_{nullptr}; +}; +} // namespace OHOS + +#endif // CLOUD_DISK_SINGLETON_TEST_H \ No newline at end of file diff --git a/test/unittests/services_daemon/BUILD.gn b/test/unittests/services_daemon/BUILD.gn index 4163405a0e09a219f30363269c65c50729832e44..c6d50354795f0217fb37f7bd394d1190a78befab 100644 --- a/test/unittests/services_daemon/BUILD.gn +++ b/test/unittests/services_daemon/BUILD.gn @@ -178,6 +178,8 @@ ohos_unittest("cloud_daemon_test") { "${distributedfile_path}/services/cloudfiledaemon/src/cloud_disk/file_operations_helper.cpp", "${services_path}/cloudfiledaemon/src/cloud_disk/account_status.cpp", "${services_path}/cloudfiledaemon/src/cloud_disk/account_status_listener.cpp", + "${services_path}/cloudfiledaemon/src/cloud_disk/appstate_observer.cpp", + "${services_path}/cloudfiledaemon/src/cloud_disk/io_message_listener.cpp", ] sources += cloud_disk @@ -229,6 +231,7 @@ ohos_unittest("cloud_daemon_test") { "relational_store:native_rdb", "safwk:system_ability_fwk", "samgr:samgr_proxy", + "ability_runtime:app_manager", ] defines = [