1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "dfs_error.h"
20 #include "fuse_assistant.h"
21 #include "fuse_manager/fuse_manager.h"
22 #include "ipc/cloud_daemon.h"
23 #include "iremote_object.h"
24 #include "setting_data_helper.h"
25 #include "system_ability_definition.h"
26 #include "system_mock.h"
27 #include "utils_log.h"
28
29 namespace OHOS::FileManagement::CloudFile::Test {
30 using namespace testing;
31 using namespace testing::ext;
32 using namespace system;
33 constexpr int32_t USER_ID = 100;
34 constexpr int32_t DEV_FD = 10;
35 const string REGION_PARAMETER = "const.global.region";
36 const string VERSION_TYPE_PARAMETER = "const.logsystem.versiontype";
37
38 class CloudDaemonTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 std::shared_ptr<CloudDaemon> cloudDaemon_;
45 static inline shared_ptr<FuseAssistantMock> insMock_ = nullptr;
46 static inline shared_ptr<SystemMock> systemMock = nullptr;
47 };
48
SetUpTestCase(void)49 void CloudDaemonTest::SetUpTestCase(void)
50 {
51 GTEST_LOG_(INFO) << "SetUpTestCase";
52 }
53
TearDownTestCase(void)54 void CloudDaemonTest::TearDownTestCase(void)
55 {
56 GTEST_LOG_(INFO) << "TearDownTestCase";
57 }
58
SetUp(void)59 void CloudDaemonTest::SetUp(void)
60 {
61 int32_t saID = FILEMANAGEMENT_CLOUD_DAEMON_SERVICE_SA_ID;
62 bool runOnCreate = true;
63 cloudDaemon_ = std::make_shared<CloudDaemon>(saID, runOnCreate);
64 insMock_ = make_shared<FuseAssistantMock>();
65 FuseAssistantMock::ins = insMock_;
66
67 systemMock = make_shared<SystemMock>();
68 ISystem::system_ = systemMock;
69 GTEST_LOG_(INFO) << "SetUp";
70 }
71
TearDown(void)72 void CloudDaemonTest::TearDown(void)
73 {
74 FuseAssistantMock::ins = nullptr;
75 insMock_ = nullptr;
76
77 systemMock = nullptr;
78 ISystem::system_ = nullptr;
79 GTEST_LOG_(INFO) << "TearDown";
80 }
81
82 /**
83 * @tc.name: OnAddSystemAbilityTest001
84 * @tc.desc: Verify the OnStart function
85 * @tc.type: FUNC
86 * @tc.require: issuesIB538J
87 */
88 HWTEST_F(CloudDaemonTest, OnAddSystemAbility001, TestSize.Level1)
89 {
90 GTEST_LOG_(INFO) << "OnAddSystemAbility001 start";
91 try {
92 const int32_t systemAbilityId = 100;
93 const std::string deviceId = "device_test";
94 cloudDaemon_->OnAddSystemAbility(systemAbilityId, deviceId);
95 } catch (...) {
96 EXPECT_TRUE(false);
97 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest001 failed";
98 }
99 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest001 end";
100 }
101
102 /**
103 * @tc.name: OnAddSystemAbilityTest002
104 * @tc.desc: Verify the OnAddSystemAbility function
105 * @tc.type: FUNC
106 */
107 HWTEST_F(CloudDaemonTest, OnAddSystemAbilityTest002, TestSize.Level1)
108 {
109 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest002 start";
110 try {
111 const int32_t systemAbilityId = COMMON_EVENT_SERVICE_ID;
112 const std::string deviceId = "device_test";
113 SettingDataHelper::GetInstance().isBundleInited_ = true;
114 cloudDaemon_->OnAddSystemAbility(systemAbilityId, deviceId);
115 } catch (...) {
116 EXPECT_TRUE(false);
117 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest002 failed";
118 }
119 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest002 end";
120 }
121
122 /**
123 * @tc.name: OnAddSystemAbilityTest003
124 * @tc.desc: Verify the OnAddSystemAbility function
125 * @tc.type: FUNC
126 */
127 HWTEST_F(CloudDaemonTest, OnAddSystemAbilityTest003, TestSize.Level1)
128 {
129 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest003 start";
130 try {
131 const int32_t systemAbilityId = DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID;
132 const std::string deviceId = "device_test";
133 SettingDataHelper::GetInstance().isBundleInited_ = true;
134 cloudDaemon_->OnAddSystemAbility(systemAbilityId, deviceId);
135 } catch (...) {
136 EXPECT_TRUE(false);
137 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest003 failed";
138 }
139 GTEST_LOG_(INFO) << "OnAddSystemAbilityTest003 end";
140 }
141
142 /**
143 * @tc.name: OnStartTest1
144 * @tc.desc: Verify the OnStart function
145 * @tc.type: FUNC
146 */
147 HWTEST_F(CloudDaemonTest, OnStartTest1, TestSize.Level1)
148 {
149 GTEST_LOG_(INFO) << "OnStartTest1 Start";
150 try {
151 cloudDaemon_->state_ = ServiceRunningState::STATE_NOT_START;
152 cloudDaemon_->registerToService_ = true;
153 EXPECT_CALL(*systemMock, GetParameter(AllOf(Ne(REGION_PARAMETER), Ne(VERSION_TYPE_PARAMETER)), _)).
154 WillRepeatedly(Return("0"));
155 EXPECT_CALL(*systemMock, GetParameter(StrEq(REGION_PARAMETER), _)).WillRepeatedly(Return("CN"));
156 EXPECT_CALL(*systemMock, GetParameter(StrEq(VERSION_TYPE_PARAMETER), _)).WillRepeatedly(Return("beta"));
157 cloudDaemon_->OnStart();
158 EXPECT_TRUE(true);
159 } catch (...) {
160 EXPECT_TRUE(false);
161 GTEST_LOG_(INFO) << "OnStartTest1 ERROR";
162 }
163 GTEST_LOG_(INFO) << "OnStartTest1 End";
164 }
165
166 /**
167 * @tc.name: OnStartTest2
168 * @tc.desc: Verify the OnStart function
169 * @tc.type: FUNC
170 */
171 HWTEST_F(CloudDaemonTest, OnStartTest2, TestSize.Level1)
172 {
173 GTEST_LOG_(INFO) << "OnStartTest2 Start";
174 try {
175 cloudDaemon_->state_ = ServiceRunningState::STATE_NOT_START;
176 cloudDaemon_->registerToService_ = true;
177 EXPECT_CALL(*systemMock, GetParameter(AllOf(Ne(REGION_PARAMETER), Ne(VERSION_TYPE_PARAMETER)), _)).
178 WillRepeatedly(Return("test"));
179 EXPECT_CALL(*systemMock, GetParameter(StrEq(REGION_PARAMETER), _)).WillRepeatedly(Return("US"));
180 EXPECT_CALL(*systemMock, GetParameter(StrEq(VERSION_TYPE_PARAMETER), _)).WillRepeatedly(Return("beta"));
181 cloudDaemon_->OnStart();
182 EXPECT_TRUE(true);
183 } catch (...) {
184 EXPECT_TRUE(false);
185 GTEST_LOG_(INFO) << "OnStartTest2 ERROR";
186 }
187 GTEST_LOG_(INFO) << "OnStartTest2 End";
188 }
189
190 /**
191 * @tc.name: OnStartTest3
192 * @tc.desc: Verify the OnStart function
193 * @tc.type: FUNC
194 */
195 HWTEST_F(CloudDaemonTest, OnStartTest3, TestSize.Level1)
196 {
197 GTEST_LOG_(INFO) << "OnStartTest3 Start";
198 try {
199 cloudDaemon_->state_ = ServiceRunningState::STATE_NOT_START;
200 cloudDaemon_->registerToService_ = true;
201 EXPECT_CALL(*systemMock, GetParameter(AllOf(Ne(REGION_PARAMETER), Ne(VERSION_TYPE_PARAMETER)), _)).
202 WillRepeatedly(Return("test"));
203 EXPECT_CALL(*systemMock, GetParameter(StrEq(REGION_PARAMETER), _)).WillRepeatedly(Return("CN"));
204 EXPECT_CALL(*systemMock, GetParameter(StrEq(VERSION_TYPE_PARAMETER), _)).WillRepeatedly(Return("commercial"));
205 cloudDaemon_->OnStart();
206 EXPECT_TRUE(true);
207 } catch (...) {
208 EXPECT_TRUE(false);
209 GTEST_LOG_(INFO) << "OnStartTest3 ERROR";
210 }
211 GTEST_LOG_(INFO) << "OnStartTest3 End";
212 }
213
214 /**
215 * @tc.name: OnStartTest4
216 * @tc.desc: Verify the OnStart function
217 * @tc.type: FUNC
218 */
219 HWTEST_F(CloudDaemonTest, OnStartTest4, TestSize.Level1)
220 {
221 GTEST_LOG_(INFO) << "OnStartTest4 Start";
222 try {
223 cloudDaemon_->state_ = ServiceRunningState::STATE_NOT_START;
224 cloudDaemon_->registerToService_ = true;
225 EXPECT_CALL(*systemMock, GetParameter(AllOf(Ne(REGION_PARAMETER), Ne(VERSION_TYPE_PARAMETER)), _)).
226 WillRepeatedly(Return("test"));
227 EXPECT_CALL(*systemMock, GetParameter(StrEq(REGION_PARAMETER), _)).WillRepeatedly(Return("US"));
228 EXPECT_CALL(*systemMock, GetParameter(StrEq(VERSION_TYPE_PARAMETER), _)).WillRepeatedly(Return("commercial"));
229 cloudDaemon_->OnStart();
230 EXPECT_TRUE(true);
231 } catch (...) {
232 EXPECT_TRUE(false);
233 GTEST_LOG_(INFO) << "OnStartTest4 ERROR";
234 }
235 GTEST_LOG_(INFO) << "OnStartTest4 End";
236 }
237 } // namespace OHOS::FileManagement::CloudSync::Test