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 * @tc.number: SUB_STORAGE_Disk_manager_service_OnDiskDestroyed_0000 98 * @tc.name: Disk_manager_service_OnDiskDestroyed_0000 99 * @tc.desc: Test function of OnDiskDestroyed interface for SUCCESS. 100 * @tc.size: MEDIUM 101 * @tc.type: FUNC 102 * @tc.level Level 1 103 * @tc.require: SR000GGUPG 104 */ 105 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_OnDiskDestroyed_0000, testing::ext::TestSize.Level1) 106 { 107 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_OnDiskDestroyed_0000"; 108 std::shared_ptr<DiskManagerService> dmService = 109 DelayedSingleton<DiskManagerService>::GetInstance(); 110 std::string diskId = "diskId-1-3"; 111 int64_t sizeBytes = 1024; 112 std::string sysPath = "/"; 113 std::string vendor = "vendor-3"; 114 int32_t flag = 1; 115 std::shared_ptr<Disk> result; 116 Disk disk(diskId, sizeBytes, sysPath, vendor, flag); 117 if (dmService != nullptr) { 118 dmService->OnDiskCreated(disk); 119 result = dmService->GetDiskById(diskId); 120 EXPECT_NE(result, nullptr); 121 dmService->OnDiskDestroyed(diskId); 122 result = dmService->GetDiskById(diskId); 123 } 124 125 EXPECT_EQ(result, nullptr); 126 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_OnDiskDestroyed_0000"; 127 } 128 129 /** 130 * @tc.number: SUB_STORAGE_Disk_manager_service_OnDiskDestroyed_0001 131 * @tc.name: Disk_manager_service_OnDiskDestroyed_0001 132 * @tc.desc: Test function of OnDiskDestroyed interface for FAILED. 133 * @tc.size: MEDIUM 134 * @tc.type: FUNC 135 * @tc.level Level 1 136 * @tc.require: SR000GGUPG 137 */ 138 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_OnDiskDestroyed_0001, testing::ext::TestSize.Level1) 139 { 140 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_OnDiskDestroyed_0000"; 141 std::shared_ptr<DiskManagerService> dmService = 142 DelayedSingleton<DiskManagerService>::GetInstance(); 143 std::string diskId = "diskId-1-4"; 144 int64_t sizeBytes = 1024; 145 std::string sysPath = "/"; 146 std::string vendor = "vendor-4"; 147 int32_t flag = 1; 148 std::shared_ptr<Disk> result; 149 Disk disk(diskId, sizeBytes, sysPath, vendor, flag); 150 if (dmService != nullptr) { 151 dmService->OnDiskDestroyed(diskId); 152 result = dmService->GetDiskById(diskId); 153 } 154 155 EXPECT_EQ(result, nullptr); 156 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_OnDiskDestroyed_0000"; 157 } 158 159 /** 160 * @tc.number: SUB_STORAGE_Disk_manager_service_GetDiskById_0000 161 * @tc.name: Disk_manager_service_GetDiskById_0000 162 * @tc.desc: Test function of GetDiskById interface for SUCCESS. 163 * @tc.size: MEDIUM 164 * @tc.type: FUNC 165 * @tc.level Level 1 166 * @tc.require: SR000GGUPG 167 */ 168 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_GetDiskById_0000, testing::ext::TestSize.Level1) 169 { 170 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_GetDiskById_0000"; 171 std::shared_ptr<DiskManagerService> dmService = 172 DelayedSingleton<DiskManagerService>::GetInstance(); 173 std::string diskId = "diskId-1-5"; 174 int64_t sizeBytes = 1024; 175 std::string sysPath = "/"; 176 std::string vendor = "vendor-5"; 177 int32_t flag = 1; 178 std::shared_ptr<Disk> result; 179 Disk disk(diskId, sizeBytes, sysPath, vendor, flag); 180 if (dmService != nullptr) { 181 dmService->OnDiskCreated(disk); 182 result = dmService->GetDiskById(diskId); 183 dmService->OnDiskDestroyed(diskId); 184 } 185 EXPECT_NE(result, nullptr); 186 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_GetDiskById_0000"; 187 } 188 189 /** 190 * @tc.number: SUB_STORAGE_Disk_manager_service_GetDiskById_0001 191 * @tc.name: Disk_manager_service_GetDiskById_0001 192 * @tc.desc: Test function of GetDiskById interface for SUCCESS. 193 * @tc.size: MEDIUM 194 * @tc.type: FUNC 195 * @tc.level Level 1 196 * @tc.require: SR000GGUPG 197 */ 198 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_GetDiskById_0001, testing::ext::TestSize.Level1) 199 { 200 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_GetDiskById_0001"; 201 std::shared_ptr<DiskManagerService> dmService = 202 DelayedSingleton<DiskManagerService>::GetInstance(); 203 std::string diskId = "diskId-1-6"; 204 int64_t sizeBytes = 1024; 205 std::string sysPath = "/"; 206 std::string vendor = "vendor-6"; 207 int32_t flag = 1; 208 int32_t result; 209 Disk disk(diskId, sizeBytes, sysPath, vendor, flag); 210 if (dmService != nullptr) { 211 dmService->OnDiskCreated(disk); 212 result = dmService->GetDiskById(diskId, disk); 213 dmService->OnDiskDestroyed(diskId); 214 } 215 EXPECT_EQ(result, E_OK); 216 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_GetDiskById_0001"; 217 } 218 219 /** 220 * @tc.number: SUB_STORAGE_Disk_manager_service_GetDiskById_0002 221 * @tc.name: Disk_manager_service_GetDiskById_0002 222 * @tc.desc: Test function of GetDiskById interface for FAILED. 223 * @tc.size: MEDIUM 224 * @tc.type: FUNC 225 * @tc.level Level 1 226 * @tc.require: SR000GGUPG 227 */ 228 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_GetDiskById_0002, testing::ext::TestSize.Level1) 229 { 230 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_GetDiskById_0002"; 231 std::shared_ptr<DiskManagerService> dmService = 232 DelayedSingleton<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; 240 Disk disk(diskId, sizeBytes, sysPath, vendor, flag); 241 if (dmService != nullptr) { 242 result = dmService->GetDiskById(diskId_1, disk); 243 } 244 EXPECT_EQ(result, E_NON_EXIST); 245 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_GetDiskById_0002"; 246 } 247 248 /** 249 * @tc.number: SUB_STORAGE_Disk_manager_service_Partition_0000 250 * @tc.name: Disk_manager_service_Partition_0000 251 * @tc.desc: Test function of Partition interface for SUCCESS. 252 * @tc.size: MEDIUM 253 * @tc.type: FUNC 254 * @tc.level Level 1 255 * @tc.require: SR000GGUPG 256 */ 257 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_Partition_0000, testing::ext::TestSize.Level1) 258 { 259 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_Partition_0000"; 260 std::shared_ptr<DiskManagerService> dmService = 261 DelayedSingleton<DiskManagerService>::GetInstance(); 262 std::string diskId = "diskId-1-8"; 263 int64_t sizeBytes = 1024; 264 std::string sysPath = "/"; 265 std::string vendor = "vendor-8"; 266 int32_t flag = 1; 267 int32_t type = 1; 268 int32_t result; 269 Disk disk(diskId, sizeBytes, sysPath, vendor, flag); 270 if (dmService != nullptr) { 271 dmService->OnDiskCreated(disk); 272 result = dmService->Partition(diskId, type); 273 dmService->OnDiskDestroyed(diskId); 274 } 275 EXPECT_EQ(result, E_NON_EXIST); 276 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_Partition_0000"; 277 } 278 279 /** 280 * @tc.number: SUB_STORAGE_Disk_manager_service_Partition_0001 281 * @tc.name: Disk_manager_service_Partition_0001 282 * @tc.desc: Test function of Partition interface for SUCCESS. 283 * @tc.size: MEDIUM 284 * @tc.type: FUNC 285 * @tc.level Level 1 286 * @tc.require: SR000GGUPG 287 */ 288 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_Partition_0001, testing::ext::TestSize.Level1) 289 { 290 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_Partition_0001"; 291 std::shared_ptr<DiskManagerService> dmService = 292 DelayedSingleton<DiskManagerService>::GetInstance(); 293 std::string diskId = "diskId-1-9"; 294 int64_t sizeBytes = 1024; 295 std::string sysPath = "/"; 296 std::string vendor = "vendor-9"; 297 int32_t flag = 1; 298 int32_t type = 1; 299 int32_t result; 300 Disk disk(diskId, sizeBytes, sysPath, vendor, flag); 301 if (dmService != nullptr) { 302 result = dmService->Partition(diskId, type); 303 } 304 EXPECT_EQ(result, E_NON_EXIST); 305 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service_Partition_0001"; 306 } 307 308 /** 309 * @tc.number: SUB_STORAGE_Disk_manager_service_GetAllDisks_0000 310 * @tc.name: Disk_manager_service_GetAllDisks_0000 311 * @tc.desc: Test function of GetAllDisks interface for SUCCESS. 312 * @tc.size: MEDIUM 313 * @tc.type: FUNC 314 * @tc.level Level 1 315 * @tc.require: SR000GGUPG 316 */ 317 HWTEST_F(DiskManagerServiceTest, Disk_manager_service_GetAllDisks_0000, testing::ext::TestSize.Level1) 318 { 319 GTEST_LOG_(INFO) << "DiskManagerServiceTest-begin Disk_manager_service_GetAllDisks_0000"; 320 std::shared_ptr<DiskManagerService> dmService = 321 DelayedSingleton<DiskManagerService>::GetInstance(); 322 std::string diskId = "diskId-1-10"; 323 int64_t sizeBytes = 1024; 324 std::string sysPath = "/"; 325 std::string vendor = "vendor-10"; 326 int32_t flag = 1; 327 Disk disk(diskId, sizeBytes, sysPath, vendor, flag); 328 dmService->OnDiskCreated(disk); 329 vector<Disk> result = dmService->GetAllDisks(); 330 if (result.size() > 0) 331 { 332 GTEST_LOG_(INFO) << result[0].GetSizeBytes(); 333 GTEST_LOG_(INFO) << result[0].GetDiskId(); 334 GTEST_LOG_(INFO) << result[0].GetSysPath(); 335 GTEST_LOG_(INFO) << result[0].GetVendor(); 336 GTEST_LOG_(INFO) << result[0].GetFlag(); 337 } 338 EXPECT_GE(result.size(), 0); 339 dmService->OnDiskDestroyed(diskId); 340 GTEST_LOG_(INFO) << "DiskManagerServiceTest-end Disk_manager_service__0000"; 341 } 342 } // namespace 343