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