1 /*
2 * Copyright (c) 2023 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 "i_daemon_mock.h"
21 #include "i_file_trans_listener.h"
22 #include "ipc/daemon.h"
23 #include "ipc_skeleton.h"
24 #include "iremote_object.h"
25 #include "remote_file_share.h"
26 #include "system_ability_definition.h"
27 #include "utils_log.h"
28
29 namespace {
30 bool g_getCallingUidTrue = true;
31 int32_t g_uidMode = 0;
32 int32_t g_getDfsUrisDirFromLocal = OHOS::FileManagement::E_OK;
33 constexpr pid_t PASTE_BOARD_UID = 3816;
34 constexpr pid_t UDMF_UID = 3012;
35 constexpr pid_t DAEMON_UID = 1009;
36 static pid_t UID = DAEMON_UID;
37 }
38
39 namespace OHOS {
40 #ifdef CONFIG_IPC_SINGLE
41 using namespace IPC_SINGLE;
42 #endif
GetCallingUid()43 pid_t IPCSkeleton::GetCallingUid()
44 {
45 if (!g_getCallingUidTrue) {
46 return -1;
47 }
48 switch (g_uidMode) {
49 case 0:
50 UID = PASTE_BOARD_UID;
51 break;
52 case 1:
53 UID = UDMF_UID;
54 break;
55 default:
56 UID = -1;
57 }
58 return UID;
59 }
60 }
61
62 namespace OHOS::AppFileService::ModuleRemoteFileShare {
GetDfsUrisDirFromLocal(const std::vector<std::string> & uriList,const int32_t & userId,std::unordered_map<std::string,AppFileService::ModuleRemoteFileShare::HmdfsUriInfo> & uriToDfsUriMaps)63 int32_t RemoteFileShare::GetDfsUrisDirFromLocal(const std::vector<std::string> &uriList, const int32_t &userId,
64 std::unordered_map<std::string, AppFileService::ModuleRemoteFileShare::HmdfsUriInfo> &uriToDfsUriMaps)
65 {
66 return g_getDfsUrisDirFromLocal;
67 }
68 }
69
70 namespace OHOS::Storage::DistributedFile::Test {
71 using namespace testing;
72 using namespace testing::ext;
73 using namespace std;
74
75 class DaemonTest : public testing::Test {
76 public:
77 static void SetUpTestCase(void);
78 static void TearDownTestCase(void);
79 void SetUp();
80 void TearDown();
81 std::shared_ptr<Daemon> daemon_;
82 };
83
84 class MockFileTransListener : public IRemoteStub<IFileTransListener> {
85 public:
86 MOCK_METHOD2(OnFileReceive, int32_t(uint64_t totalBytes, uint64_t processedBytes));
87 MOCK_METHOD2(OnFailed, int32_t(const std::string &sessionName, int32_t errorCode));
88 MOCK_METHOD1(OnFinished, int32_t(const std::string &sessionName));
89 };
90
91 DistributedHardware::DmDeviceInfo deviceInfo = {
92 .deviceId = "testdevid",
93 .networkId = "testnetworkid",
94 };
95
SetUpTestCase(void)96 void DaemonTest::SetUpTestCase(void)
97 {
98 GTEST_LOG_(INFO) << "SetUpTestCase";
99 }
100
TearDownTestCase(void)101 void DaemonTest::TearDownTestCase(void)
102 {
103 GTEST_LOG_(INFO) << "TearDownTestCase";
104 }
105
SetUp(void)106 void DaemonTest::SetUp(void)
107 {
108 int32_t saID = FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID;
109 bool runOnCreate = true;
110 daemon_ = std::make_shared<Daemon>(saID, runOnCreate);
111 GTEST_LOG_(INFO) << "SetUp";
112 }
113
TearDown(void)114 void DaemonTest::TearDown(void)
115 {
116 GTEST_LOG_(INFO) << "TearDown";
117 }
118
119 /**
120 * @tc.name: OnStopTest
121 * @tc.desc: Verify the OnStop function
122 * @tc.type: FUNC
123 * @tc.require: issueI7M6L1
124 */
125 HWTEST_F(DaemonTest, OnStopTest, TestSize.Level0)
126 {
127 GTEST_LOG_(INFO) << "OnStop Start";
128 try {
129 daemon_->state_ = ServiceRunningState::STATE_NOT_START;
130 daemon_->registerToService_ = false;
131 daemon_->OnStop();
132 EXPECT_TRUE(true);
133 } catch (...) {
134 EXPECT_TRUE(false);
135 GTEST_LOG_(INFO) << "OnStop ERROR";
136 }
137 GTEST_LOG_(INFO) << "OnStop End";
138 }
139
140 /**
141 * @tc.name: DaemonTest_PublishSA_0100
142 * @tc.desc: Verify the PublishSA function.
143 * @tc.type: FUNC
144 * @tc.require: issueI7SP3A
145 */
146 HWTEST_F(DaemonTest, DaemonTest_PublishSA_0100, TestSize.Level0)
147 {
148 GTEST_LOG_(INFO) << "DaemonTest_PublishSA_0100 start";
149 try {
150 daemon_->registerToService_ = true;
151 daemon_->PublishSA();
152 EXPECT_TRUE(daemon_->registerToService_);
153 } catch (const exception &e) {
154 LOGE("Error:%{public}s", e.what());
155 EXPECT_TRUE(false);
156 }
157 GTEST_LOG_(INFO) << "DaemonTest_PublishSA_0100 end";
158 }
159
160 /**
161 * @tc.name: DaemonTest_OnStart_0100
162 * @tc.desc: Verify the OnStart function.
163 * @tc.type: FUNC
164 * @tc.require: issueI7SP3A
165 */
166 HWTEST_F(DaemonTest, DaemonTest_OnStart_0100, TestSize.Level0)
167 {
168 GTEST_LOG_(INFO) << "DaemonTest_OnStart_0100 start";
169 try {
170 daemon_->state_ = ServiceRunningState::STATE_RUNNING;
171 daemon_->OnStart();
172 EXPECT_EQ(daemon_->state_, ServiceRunningState::STATE_RUNNING);
173 } catch (const exception &e) {
174 LOGE("Error:%{public}s", e.what());
175 EXPECT_TRUE(false);
176 }
177 GTEST_LOG_(INFO) << "DaemonTest_OnStart_0100 end";
178 }
179
180 /**
181 * @tc.name: DaemonTest_OnRemoveSystemAbility_0100
182 * @tc.desc: Verify the OnRemoveSystemAbility function.
183 * @tc.type: FUNC
184 * @tc.require: issueI7SP3A
185 */
186 HWTEST_F(DaemonTest, DaemonTest_OnRemoveSystemAbility_0100, TestSize.Level0)
187 {
188 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0100 start";
189 try {
190 daemon_->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID + 1, "");
191 EXPECT_TRUE(true);
192 } catch (const exception &e) {
193 LOGE("Error:%{public}s", e.what());
194 EXPECT_TRUE(false);
195 }
196 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0100 end";
197 }
198
199 /**
200 * @tc.name: DaemonTest_OnRemoveSystemAbility_0200
201 * @tc.desc: Verify the OnRemoveSystemAbility function.
202 * @tc.type: FUNC
203 * @tc.require: issueI7SP3A
204 */
205 HWTEST_F(DaemonTest, DaemonTest_OnRemoveSystemAbility_0200, TestSize.Level0)
206 {
207 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0200 start";
208 try {
209 daemon_->subScriber_ = nullptr;
210 daemon_->OnRemoveSystemAbility(COMMON_EVENT_SERVICE_ID, "");
211 EXPECT_TRUE(true);
212 } catch (const exception &e) {
213 LOGE("Error:%{public}s", e.what());
214 EXPECT_TRUE(false);
215 }
216 GTEST_LOG_(INFO) << "DaemonTest_OnRemoveSystemAbility_0200 end";
217 }
218
219 /**
220 * @tc.name: DaemonTest_PrepareSession_0100
221 * @tc.desc: Verify the PrepareSession function.
222 * @tc.type: FUNC
223 * @tc.require: issueI90MOB
224 */
225 HWTEST_F(DaemonTest, DaemonTest_PrepareSession_0100, TestSize.Level0)
226 {
227 GTEST_LOG_(INFO) << "DaemonTest_PrepareSession_0100 start";
228 try {
229 const std::string srcUri = "file://docs/storage/Users/currentUser/Documents?networkid=xxxxx";
230 const std::string dstUri = "file://docs/storage/Users/currentUser/Documents";
231 const std::string srcDeviceId = "testSrcDeviceId";
232 auto listener = sptr<IRemoteObject>(new MockFileTransListener());
233 const std::string copyPath = "tmpDir";
234 const std::string sessionName = "DistributedDevice0";
235 HmdfsInfo fileInfo = {
236 .copyPath = copyPath,
237 .dirExistFlag = false,
238 .sessionName = sessionName,
239 };
240 EXPECT_EQ(daemon_->PrepareSession(srcUri, dstUri, srcDeviceId, listener, fileInfo),
241 FileManagement::E_SA_LOAD_FAILED);
242 } catch (const exception &e) {
243 LOGE("Error:%{public}s", e.what());
244 EXPECT_TRUE(false);
245 }
246 GTEST_LOG_(INFO) << "DaemonTest_PrepareSession_0100 end";
247 }
248
249 /**
250 * @tc.name: DaemonTest_GetDfsUrisDirFromLocal_0100
251 * @tc.desc: Verify the GetDfsUrisDirFromLocal function.
252 * @tc.type: FUNC
253 * @tc.require: issueI90MOB
254 */
255 HWTEST_F(DaemonTest, DaemonTest_GetDfsUrisDirFromLocal_0100, TestSize.Level0)
256 {
257 GTEST_LOG_(INFO) << "DaemonTest_GetDfsUrisDirFromLocal_0100 start";
258 try {
259 std::vector<std::string> uriList;
260 int32_t userId = 100;
261 std::string normalUri = "file://docs/data/storage/el2/cloud";
262 std::string errUri = "file://docs/data/storage/../el2/cloud";
263 std::unordered_map<std::string, AppFileService::ModuleRemoteFileShare::HmdfsUriInfo> uriToDfsUriMaps;
264 g_getCallingUidTrue = false;
265 auto ret = daemon_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps);
266 EXPECT_EQ(ret, FileManagement::E_PERMISSION_DENIED);
267
268 g_getCallingUidTrue = true;
269 uriList.emplace_back(normalUri);
270 uriList.emplace_back(errUri);
271 ret = daemon_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps);
272 EXPECT_EQ(ret, FileManagement::E_ILLEGAL_URI);
273
274 g_getCallingUidTrue = true;
275 uriList.erase(uriList.begin() + 1); // 1: errUri
276 g_getDfsUrisDirFromLocal = FileManagement::E_INVAL_ARG;
277 ret = daemon_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps);
278 EXPECT_EQ(ret, FileManagement::E_INVAL_ARG);
279
280 g_getDfsUrisDirFromLocal = OHOS::FileManagement::E_OK;
281 ret = daemon_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps);
282 EXPECT_EQ(ret, OHOS::FileManagement::E_OK);
283
284 g_uidMode = 1; // 1: UDMF_UID
285 uriList.emplace_back(errUri);
286 ret = daemon_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps);
287 EXPECT_EQ(ret, FileManagement::E_ILLEGAL_URI);
288
289 uriList.erase(uriList.begin() + 1); // 1: errUri
290 g_getDfsUrisDirFromLocal = FileManagement::E_INVAL_ARG;
291 ret = daemon_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps);
292 EXPECT_EQ(ret, FileManagement::E_INVAL_ARG);
293
294 g_getDfsUrisDirFromLocal = OHOS::FileManagement::E_OK;
295 ret = daemon_->GetDfsUrisDirFromLocal(uriList, userId, uriToDfsUriMaps);
296 EXPECT_EQ(ret, OHOS::FileManagement::E_OK);
297 } catch (const exception &e) {
298 LOGE("Error:%{public}s", e.what());
299 EXPECT_TRUE(false);
300 }
301 GTEST_LOG_(INFO) << "DaemonTest_GetDfsUrisDirFromLocal_0100 end";
302 }
303 } // namespace OHOS::Storage::DistributedFile::Test
304