1 /*
2 * Copyright (c) 2021 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 <cstdio>
17 #include <gtest/gtest.h>
18
19 #include "disk/disk_manager_service.h"
20 #include "storage_service_errno.h"
21 #include "storage_service_log.h"
22 #include "mock/file_utils_mock.h"
23
24 namespace {
25 using namespace std;
26 using namespace OHOS;
27 using namespace StorageManager;
28 using namespace StorageDaemon;
29 class DiskManagerServiceTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase();
SetUp()33 void SetUp() {};
TearDown()34 void TearDown() {};
35 static inline std::shared_ptr<FileUtilMoc> fileUtilMoc_ = nullptr;
36 };
37
SetUpTestCase(void)38 void DiskManagerServiceTest::SetUpTestCase(void)
39 {
40 GTEST_LOG_(INFO) << "SetUpTestCase Start";
41 fileUtilMoc_ = std::make_shared<FileUtilMoc>();
42 FileUtilMoc::fileUtilMoc = fileUtilMoc_;
43 }
44
TearDownTestCase()45 void DiskManagerServiceTest::TearDownTestCase()
46 {
47 GTEST_LOG_(INFO) << "TearDownTestCase Start";
48 FileUtilMoc::fileUtilMoc = nullptr;
49 fileUtilMoc_ = nullptr;
50 }
51
52 /**
53 * @tc.number: SUB_STORAGE_Disk_manager_service_OnDiskCreated_0000
54 * @tc.name: Disk_manager_service_OnDiskCreated_0000
55 * @tc.desc: Test function of OnDiskCreated interface for SUCCESS.
56 * @tc.size: MEDIUM
57 * @tc.type: FUNC
58 * @tc.level Level 1
59 * @tc.require: SR000GGUPG
60 */
61 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_OnDiskCreated_0000, testing::ext::TestSize.Level1)
62 {
63 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_OnDiskCreated_0000";
64 DiskManagerService& dmService = DiskManagerService::GetInstance();
65 std::string diskId = "diskId-1-1";
66 int64_t sizeBytes = 1024;
67 std::string sysPath = "/";
68 std::string vendor = "vendor-1";
69 int32_t flag = 1;
70 std::shared_ptr<Disk> result;
71 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
72 dmService.OnDiskCreated(disk);
73 result = dmService.GetDiskById(diskId);
74 dmService.OnDiskDestroyed(diskId);
75
76 EXPECT_NE(result, nullptr);
77 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_OnDiskCreated_0000";
78 }
79
80 /**
81 * @tc.number: SUB_STORAGE_Disk_manager_service_OnDiskCreated_0001
82 * @tc.name: Disk_manager_service_OnDiskCreated_0001
83 * @tc.desc: Test function of OnDiskCreated interface for SUCCESS.
84 * @tc.size: MEDIUM
85 * @tc.type: FUNC
86 * @tc.level Level 1
87 * @tc.require: SR000GGUPG
88 */
89 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_OnDiskCreated_0001, testing::ext::TestSize.Level1)
90 {
91 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_OnDiskCreated_0001";
92 DiskManagerService& dmService = DiskManagerService::GetInstance();
93 std::string diskId = "diskId-1-2";
94 int64_t sizeBytes = 1024;
95 std::string sysPath = "/";
96 std::string vendor = "vendor-2";
97 int32_t flag = 1;
98 std::shared_ptr<Disk> result;
99 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
100 dmService.OnDiskCreated(disk);
101 dmService.OnDiskCreated(disk);
102 result = dmService.GetDiskById(diskId);
103 dmService.OnDiskDestroyed(diskId);
104
105 EXPECT_NE(result, nullptr);
106 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_OnDiskCreated_0001";
107 }
108
109 /**
110 * @tc.number: SUB_STORAGE_Disk_manager_service_OnDiskDestroyed_0000
111 * @tc.name: Disk_manager_service_OnDiskDestroyed_0000
112 * @tc.desc: Test function of OnDiskDestroyed interface for SUCCESS.
113 * @tc.size: MEDIUM
114 * @tc.type: FUNC
115 * @tc.level Level 1
116 * @tc.require: SR000GGUPG
117 */
118 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_OnDiskDestroyed_0000, testing::ext::TestSize.Level1)
119 {
120 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_OnDiskDestroyed_0000";
121 DiskManagerService& dmService = DiskManagerService::GetInstance();
122 std::string diskId = "diskId-1-3";
123 int64_t sizeBytes = 1024;
124 std::string sysPath = "/";
125 std::string vendor = "vendor-3";
126 int32_t flag = 1;
127 std::shared_ptr<Disk> result;
128 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
129 dmService.OnDiskCreated(disk);
130 result = dmService.GetDiskById(diskId);
131 EXPECT_NE(result, nullptr);
132 dmService.OnDiskDestroyed(diskId);
133 result = dmService.GetDiskById(diskId);
134
135 EXPECT_EQ(result, nullptr);
136 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_OnDiskDestroyed_0000";
137 }
138
139 /**
140 * @tc.number: SUB_STORAGE_Disk_manager_service_OnDiskDestroyed_0001
141 * @tc.name: Disk_manager_service_OnDiskDestroyed_0001
142 * @tc.desc: Test function of OnDiskDestroyed interface for FAILED.
143 * @tc.size: MEDIUM
144 * @tc.type: FUNC
145 * @tc.level Level 1
146 * @tc.require: SR000GGUPG
147 */
148 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_OnDiskDestroyed_0001, testing::ext::TestSize.Level1)
149 {
150 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_OnDiskDestroyed_0000";
151 DiskManagerService& dmService = DiskManagerService::GetInstance();
152 std::string diskId = "diskId-1-4";
153 int64_t sizeBytes = 1024;
154 std::string sysPath = "/";
155 std::string vendor = "vendor-4";
156 int32_t flag = 1;
157 std::shared_ptr<Disk> result;
158 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
159 dmService.OnDiskDestroyed(diskId);
160 result = dmService.GetDiskById(diskId);
161
162 EXPECT_EQ(result, nullptr);
163 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_OnDiskDestroyed_0000";
164 }
165
166 /**
167 * @tc.number: SUB_STORAGE_Disk_manager_service_GetDiskById_0000
168 * @tc.name: Disk_manager_service_GetDiskById_0000
169 * @tc.desc: Test function of GetDiskById interface for SUCCESS.
170 * @tc.size: MEDIUM
171 * @tc.type: FUNC
172 * @tc.level Level 1
173 * @tc.require: SR000GGUPG
174 */
175 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_GetDiskById_0000, testing::ext::TestSize.Level1)
176 {
177 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_GetDiskById_0000";
178 DiskManagerService& dmService = DiskManagerService::GetInstance();
179 std::string diskId = "diskId-1-5";
180 int64_t sizeBytes = 1024;
181 std::string sysPath = "/";
182 std::string vendor = "vendor-5";
183 int32_t flag = 1;
184 std::shared_ptr<Disk> result;
185 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
186 dmService.OnDiskCreated(disk);
187 result = dmService.GetDiskById(diskId);
188 dmService.OnDiskDestroyed(diskId);
189 EXPECT_NE(result, nullptr);
190 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_GetDiskById_0000";
191 }
192
193 /**
194 * @tc.number: SUB_STORAGE_Disk_manager_service_GetDiskById_0001
195 * @tc.name: Disk_manager_service_GetDiskById_0001
196 * @tc.desc: Test function of GetDiskById interface for SUCCESS.
197 * @tc.size: MEDIUM
198 * @tc.type: FUNC
199 * @tc.level Level 1
200 * @tc.require: SR000GGUPG
201 */
202 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_GetDiskById_0001, testing::ext::TestSize.Level1)
203 {
204 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_GetDiskById_0001";
205 DiskManagerService& dmService = DiskManagerService::GetInstance();
206 std::string diskId = "diskId-1-6";
207 int64_t sizeBytes = 1024;
208 std::string sysPath = "/";
209 std::string vendor = "vendor-6";
210 int32_t flag = 1;
211 int32_t result = E_OK;
212 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
213 dmService.OnDiskCreated(disk);
214 result = dmService.GetDiskById(diskId, disk);
215 dmService.OnDiskDestroyed(diskId);
216 EXPECT_EQ(result, E_OK);
217 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_GetDiskById_0001";
218 }
219
220 /**
221 * @tc.number: SUB_STORAGE_Disk_manager_service_GetDiskById_0002
222 * @tc.name: Disk_manager_service_GetDiskById_0002
223 * @tc.desc: Test function of GetDiskById interface for FAILED.
224 * @tc.size: MEDIUM
225 * @tc.type: FUNC
226 * @tc.level Level 1
227 * @tc.require: SR000GGUPG
228 */
229 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_GetDiskById_0002, testing::ext::TestSize.Level1)
230 {
231 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_GetDiskById_0002";
232 DiskManagerService& dmService = DiskManagerService::GetInstance();
233 std::string diskId = "diskId-1-7";
234 std::string diskId_1 = "diskId-1-11";
235 int64_t sizeBytes = 1024;
236 std::string sysPath = "/";
237 std::string vendor = "vendor-7";
238 int32_t flag = 1;
239 int32_t result = E_OK;
240 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
241 result = dmService.GetDiskById(diskId_1, disk);
242 EXPECT_EQ(result, E_NON_EXIST);
243 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_GetDiskById_0002";
244 }
245
246 /**
247 * @tc.number: SUB_STORAGE_Disk_manager_service_Partition_0000
248 * @tc.name: Disk_manager_service_Partition_0000
249 * @tc.desc: Test function of Partition interface for SUCCESS.
250 * @tc.size: MEDIUM
251 * @tc.type: FUNC
252 * @tc.level Level 1
253 * @tc.require: SR000GGUPG
254 */
255 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_Partition_0000, testing::ext::TestSize.Level1)
256 {
257 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_Partition_0000";
258 DiskManagerService& dmService = DiskManagerService::GetInstance();
259 std::string diskId = "diskId-1-8";
260 int64_t sizeBytes = 1024;
261 std::string sysPath = "/";
262 std::string vendor = "vendor-8";
263 int32_t flag = 1;
264 int32_t type = 1;
265 int32_t result = E_OK;
266 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
267 dmService.OnDiskCreated(disk);
268 result = dmService.Partition(diskId, type);
269 dmService.OnDiskDestroyed(diskId);
270 EXPECT_EQ(result, E_NON_EXIST);
271 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_Partition_0000";
272 }
273
274 /**
275 * @tc.number: SUB_STORAGE_Disk_manager_service_Partition_0001
276 * @tc.name: Disk_manager_service_Partition_0001
277 * @tc.desc: Test function of Partition interface for SUCCESS.
278 * @tc.size: MEDIUM
279 * @tc.type: FUNC
280 * @tc.level Level 1
281 * @tc.require: SR000GGUPG
282 */
283 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_Partition_0001, testing::ext::TestSize.Level1)
284 {
285 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_Partition_0001";
286 EXPECT_CALL(*fileUtilMoc_, IsUsbFuse()).WillOnce(testing::Return(false));
287 DiskManagerService& dmService = DiskManagerService::GetInstance();
288 std::string diskId = "diskId-1-9";
289 int64_t sizeBytes = 1024;
290 std::string sysPath = "/";
291 std::string vendor = "vendor-9";
292 int32_t flag = 1;
293 int32_t type = 1;
294 int32_t result = E_OK;
295 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
296 result = dmService.Partition(diskId, type);
297 EXPECT_EQ(result, E_NON_EXIST);
298 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_Partition_0001";
299 }
300
301 /**
302 * @tc.number: SUB_STORAGE_Disk_manager_service_Partition_0002
303 * @tc.name: Disk_manager_service_Partition_0002
304 * @tc.desc: Test function of Partition interface for SUCCESS.
305 * @tc.size: MEDIUM
306 * @tc.type: FUNC
307 * @tc.level Level 1
308 * @tc.require: SR000GGUPG
309 */
310 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_Partition_0002, testing::ext::TestSize.Level1)
311 {
312 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_Partition_0002";
313 DiskManagerService& dmService = DiskManagerService::GetInstance();
314 std::string diskId = "diskId-1-9";
315 int64_t sizeBytes = 1024;
316 std::string sysPath = "/";
317 std::string vendor = "vendor-9";
318 int32_t flag = 1;
319 int32_t type = 1;
320 int32_t result = E_OK;
321 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
322 EXPECT_CALL(*fileUtilMoc_, IsUsbFuse()).WillOnce(testing::Return(true));
323 result = dmService.Partition(diskId, type);
324 EXPECT_EQ(result, E_NOT_SUPPORT);
325 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_Partition_0002";
326 }
327
328 /**
329 * @tc.number: SUB_STORAGE_Disk_manager_service_GetAllDisks_0000
330 * @tc.name: Disk_manager_service_GetAllDisks_0000
331 * @tc.desc: Test function of GetAllDisks interface for SUCCESS.
332 * @tc.size: MEDIUM
333 * @tc.type: FUNC
334 * @tc.level Level 1
335 * @tc.require: SR000GGUPG
336 */
337 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_GetAllDisks_0000, testing::ext::TestSize.Level1)
338 {
339 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_GetAllDisks_0000";
340 DiskManagerService& dmService = DiskManagerService::GetInstance();
341 std::string diskId = "diskId-1-10";
342 int64_t sizeBytes = 1024;
343 std::string sysPath = "/";
344 std::string vendor = "vendor-10";
345 int32_t flag = 1;
346 Disk disk(diskId, sizeBytes, sysPath, vendor, flag);
347 dmService.OnDiskCreated(disk);
348 vector<Disk> result = dmService.GetAllDisks();
349 if (result.size() > 0)
350 {
351 GTEST_LOG_(INFO) << result[0].GetSizeBytes();
352 GTEST_LOG_(INFO) << result[0].GetDiskId();
353 GTEST_LOG_(INFO) << result[0].GetSysPath();
354 GTEST_LOG_(INFO) << result[0].GetVendor();
355 GTEST_LOG_(INFO) << result[0].GetFlag();
356 }
357 EXPECT_GE(result.size(), 0);
358 dmService.OnDiskDestroyed(diskId);
359 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service__0000";
360 }
361 } // namespace
362