1 /* 2 * Copyright (c) 2023-2024 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 <fstream> 17 #include <gtest/gtest.h> 18 19 #include "quota/quota_manager.h" 20 #include "storage_service_errno.h" 21 #include "storage_service_log.h" 22 #include "storage_service_constant.h" 23 #include "test/common/help_utils.h" 24 #include "utils/file_utils.h" 25 26 namespace OHOS { 27 namespace StorageDaemon { 28 using namespace testing::ext; 29 30 const std::string BUNDLE_NAME = "com.ohos.bundleName-0-1"; 31 const std::string BUNDLE_PATH = "/data/app/el2/100/base/com.ohos.bundleName-0-1"; 32 const int32_t UID = 20000000; 33 const int32_t LIMITSIZE = 1000; 34 const std::string EMPTY_STRING = ""; 35 36 class QuotaManagerTest : public testing::Test { 37 public: SetUpTestCase(void)38 static void SetUpTestCase(void) {}; TearDownTestCase(void)39 static void TearDownTestCase(void) {}; SetUp()40 void SetUp() {}; TearDown()41 void TearDown() {}; 42 }; 43 44 /** 45 * @tc.name: Storage_Service_QuotaManagerTest_GetInstance_001 46 * @tc.desc: Verify the GetInstance function. 47 * @tc.type: FUNC 48 * @tc.require: AR000HSKSO 49 */ 50 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_GetInstance_001, TestSize.Level1) 51 { 52 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_GetInstance_001 start"; 53 54 QuotaManager *QuotaManager = QuotaManager::GetInstance(); 55 ASSERT_TRUE(QuotaManager != nullptr); 56 57 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_GetInstance_001 end"; 58 } 59 60 /** 61 * @tc.name: Storage_Service_QuotaManagerTest_GetInstance_002 62 * @tc.desc: Verify the GetInstance function. 63 * @tc.type: FUNC 64 * @tc.require: AR000HSKSO 65 */ 66 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_GetInstance_002, TestSize.Level1) 67 { 68 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_GetInstance_002 start"; 69 70 QuotaManager *quotaManager1 = QuotaManager::GetInstance(); 71 ASSERT_TRUE(quotaManager1 != nullptr); 72 73 QuotaManager *quotaManager2 = QuotaManager::GetInstance(); 74 ASSERT_TRUE(quotaManager2 != nullptr); 75 76 ASSERT_TRUE(quotaManager1 == quotaManager2); 77 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_GetInstance_002 end"; 78 } 79 80 /** 81 * @tc.name: Storage_Service_QuotaManagerTest_SetBundleQuota_001 82 * @tc.desc: Test whether SetBundleQuota is called normally.(bundleName is empty) 83 * @tc.type: FUNC 84 * @tc.require: AR000HSKSO 85 */ 86 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_SetBundleQuota_001, TestSize.Level1) 87 { 88 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_001 start"; 89 90 QuotaManager *quotaManager = QuotaManager::GetInstance(); 91 ASSERT_TRUE(quotaManager != nullptr); 92 93 std::string bundleName = EMPTY_STRING; 94 int32_t uid = UID; 95 std::string bundleDataDirPath = BUNDLE_PATH; 96 int32_t limitSizeMb = LIMITSIZE; 97 int32_t result = quotaManager->SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb); 98 EXPECT_EQ(result, E_NON_EXIST); 99 100 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_001 end"; 101 } 102 103 /** 104 * @tc.name: Storage_Service_QuotaManagerTest_SetBundleQuota_002 105 * @tc.desc: Test whether SetBundleQuota is called normally.(uid < 0) 106 * @tc.type: FUNC 107 * @tc.require: AR000HSKSO 108 */ 109 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_SetBundleQuota_002, TestSize.Level1) 110 { 111 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_002 start"; 112 113 QuotaManager *quotaManager = QuotaManager::GetInstance(); 114 ASSERT_TRUE(quotaManager != nullptr); 115 116 std::string bundleName = BUNDLE_NAME; 117 int32_t uid = -1; 118 std::string bundleDataDirPath = BUNDLE_PATH; 119 int32_t limitSizeMb = LIMITSIZE; 120 int32_t result = quotaManager->SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb); 121 EXPECT_EQ(result, E_NON_EXIST); 122 123 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_002 end"; 124 } 125 126 /** 127 * @tc.name: Storage_Service_QuotaManagerTest_SetBundleQuota_003 128 * @tc.desc: Test whether SetBundleQuota is called normally.(bundleDataDirPath is empty) 129 * @tc.type: FUNC 130 * @tc.require: AR000HSKSO 131 */ 132 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_SetBundleQuota_003, TestSize.Level1) 133 { 134 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_003 start"; 135 136 QuotaManager *quotaManager = QuotaManager::GetInstance(); 137 ASSERT_TRUE(quotaManager != nullptr); 138 139 std::string bundleName = BUNDLE_NAME; 140 int32_t uid = UID; 141 std::string bundleDataDirPath = EMPTY_STRING; 142 int32_t limitSizeMb = LIMITSIZE; 143 int32_t result = quotaManager->SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb); 144 EXPECT_EQ(result, E_NON_EXIST); 145 146 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_003 end"; 147 } 148 149 /** 150 * @tc.name: Storage_Service_QuotaManagerTest_SetBundleQuota_004 151 * @tc.desc: Test whether SetBundleQuota is called normally.(limitSizeMb < 0) 152 * @tc.type: FUNC 153 * @tc.require: AR000HSKSO 154 */ 155 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_SetBundleQuota_004, TestSize.Level1) 156 { 157 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_004 start"; 158 159 QuotaManager *quotaManager = QuotaManager::GetInstance(); 160 ASSERT_TRUE(quotaManager != nullptr); 161 162 std::string bundleName = BUNDLE_NAME; 163 int32_t uid = UID; 164 std::string bundleDataDirPath = BUNDLE_PATH; 165 int32_t limitSizeMb = -1; 166 int32_t result = quotaManager->SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb); 167 EXPECT_EQ(result, E_NON_EXIST); 168 169 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_004 end"; 170 } 171 172 /** 173 * @tc.name: Storage_Service_QuotaManagerTest_SetBundleQuota_005 174 * @tc.desc: Test whether CreateBundleDataDir is called normally. 175 * @tc.type: FUNC 176 * @tc.require: AR000HSKSO 177 */ 178 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_SetBundleQuota_005, TestSize.Level1) 179 { 180 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_005 start"; 181 182 QuotaManager *quotaManager = QuotaManager::GetInstance(); 183 ASSERT_TRUE(quotaManager != nullptr); 184 185 std::string bundleName = BUNDLE_NAME; 186 int32_t uid = UID; 187 std::string bundleDataDirPath = BUNDLE_PATH; 188 int32_t limitSizeMb = LIMITSIZE; 189 int32_t result = quotaManager->SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb); 190 EXPECT_EQ(result, E_SYS_CALL); 191 192 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_SetBundleQuota_005 end"; 193 } 194 195 /** 196 * @tc.name: Storage_Service_QuotaManagerTest_CheckOverLongPath_001 197 * @tc.desc: Test overLong path. 198 * @tc.type: FUNC 199 * @tc.require: AR20240111379420 200 */ 201 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_CheckOverLongPath_001, TestSize.Level1) 202 { 203 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_CheckOverLongPath_001 start"; 204 205 std::string bundleDataDirPath = BUNDLE_PATH; 206 uint32_t len = bundleDataDirPath.length(); 207 int32_t result = CheckOverLongPath(bundleDataDirPath); 208 EXPECT_EQ(result, len); 209 210 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_CheckOverLongPath_001 end"; 211 } 212 213 /** 214 * @tc.name: Storage_Service_QuotaManagerTest_GetOccupiedSpace_001 215 * @tc.desc: Test whether GetOccupiedSpace is called normally. 216 * @tc.type: FUNC 217 * @tc.require: AR000HSKSO 218 */ 219 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_GetOccupiedSpace_001, TestSize.Level1) 220 { 221 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_GetOccupiedSpace_001 start"; 222 223 QuotaManager *quotaManager = QuotaManager::GetInstance(); 224 ASSERT_TRUE(quotaManager != nullptr); 225 226 int32_t idType = USRID; 227 int32_t uid = -1; 228 int64_t size = 0; 229 int32_t result = quotaManager->GetOccupiedSpace(idType, uid, size); 230 EXPECT_EQ(result, E_SYS_ERR); 231 uid = UID; 232 result = quotaManager->GetOccupiedSpace(idType, uid, size); 233 EXPECT_EQ(result, E_OK); 234 235 idType = GRPID; 236 int32_t gid = -1; 237 result = quotaManager->GetOccupiedSpace(idType, gid, size); 238 EXPECT_EQ(result, E_SYS_ERR); 239 gid = 1006; 240 result = quotaManager->GetOccupiedSpace(idType, gid, size); 241 EXPECT_EQ(result, E_OK); 242 243 idType = PRJID; 244 int32_t prjid = -1; 245 result = quotaManager->GetOccupiedSpace(idType, prjid, size); 246 EXPECT_EQ(result, E_SYS_ERR); 247 prjid = 0; 248 result = quotaManager->GetOccupiedSpace(idType, prjid, size); 249 EXPECT_EQ(result, E_OK); 250 251 idType = -1; 252 result = quotaManager->GetOccupiedSpace(idType, uid, size); 253 EXPECT_EQ(result, E_NON_EXIST); 254 255 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_GetOccupiedSpace_001 end"; 256 } 257 258 /** 259 * @tc.name: Storage_Service_QuotaManagerTest_GetBundleStatsForIncrease_001 260 * @tc.desc: Test whether GetBundleStatsForIncrease is called normally. 261 * @tc.type: FUNC 262 * @tc.require: AR000HSKSO 263 */ 264 HWTEST_F(QuotaManagerTest, Storage_Service_QuotaManagerTest_GetBundleStatsForIncrease_001, TestSize.Level1) 265 { 266 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_GetBundleStatsForIncrease_001 start"; 267 268 QuotaManager *quotaManager = QuotaManager::GetInstance(); 269 ASSERT_TRUE(quotaManager != nullptr); 270 271 // create parameter 272 uint32_t userId = 100; 273 std::vector<std::string> bundleNames; 274 std::string bundleName = "com.ohos.UserFile.ExternalFileManager"; 275 bundleNames.emplace_back(bundleName); 276 std::vector<int64_t> incrementalBackTimes; 277 int64_t lastIncrementalTime = 0; 278 incrementalBackTimes.emplace_back(lastIncrementalTime); 279 std::vector<int64_t> pkgFileSizes; 280 std::vector<int64_t> incPkgFileSizes; 281 // prepare config file 282 mode_t mode = 0771; 283 // backup_sa bundle path 284 std::string backupSaBundleDir = BACKUP_PATH_PREFIX + std::to_string(userId) + BACKUP_PATH_SURFFIX + bundleName + 285 FILE_SEPARATOR_CHAR; 286 if (!StorageTest::StorageTestUtils::CheckDir(backupSaBundleDir)) { 287 StorageTest::StorageTestUtils::MkDir(backupSaBundleDir, mode); 288 } 289 // backup_sa include/exclude 290 std::string incExFilePath = backupSaBundleDir + BACKUP_INCEXC_SYMBOL + std::to_string(lastIncrementalTime); 291 std::ofstream incExcFile; 292 incExcFile.open(incExFilePath.data(), std::ios::out | std::ios::trunc); 293 if (!incExcFile.is_open()) { 294 EXPECT_TRUE(false) << "Failed to create file for include-exclude config"; 295 } 296 incExcFile << BACKUP_INCLUDE << std::endl; 297 std::string include = "/storage/Users/currentUser/"; 298 incExcFile << include << std::endl; 299 300 incExcFile << BACKUP_EXCLUDE << std::endl; 301 std::string exclude = "/storage/Users/currentUser/.Trash/"; 302 incExcFile << exclude << std::endl; 303 incExcFile.close(); 304 305 // backup_sa stat 306 std::string statFilePath = backupSaBundleDir + BACKUP_STAT_SYMBOL + std::to_string(lastIncrementalTime); 307 std::ofstream statFile; 308 statFile.open(statFilePath.data(), std::ios::out | std::ios::trunc); 309 if (!statFile.is_open()) { 310 EXPECT_TRUE(false) << "Failed to create file for bundle stat"; 311 } 312 statFile.close(); 313 // invoke GetBundleStatsForIncrease 314 auto ret = quotaManager->GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, 315 incPkgFileSizes); 316 EXPECT_TRUE(ret == E_OK); 317 EXPECT_GE(pkgFileSizes[0], 0); 318 EXPECT_GE(incPkgFileSizes[0], 0); 319 320 if (StorageTest::StorageTestUtils::CheckDir(backupSaBundleDir)) { 321 StorageTest::StorageTestUtils::RmDirRecurse(backupSaBundleDir); 322 } 323 324 GTEST_LOG_(INFO) << "Storage_Service_QuotaManagerTest_GetBundleStatsForIncrease_001 end"; 325 } 326 } // STORAGE_DAEMON 327 } // OHOS