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 <gtest/gtest.h> 17 #include <gmock/gmock.h> 18 #include <thread> 19 20 #include "account_log_wrapper.h" 21 #include "accesstoken_kit.h" 22 #define private public 23 #include "app_account_common.h" 24 #include "app_account_constants.h" 25 #include "app_account_control_manager.h" 26 #include "app_account_manager_service.h" 27 #undef private 28 #include "datetime_ex.h" 29 #include "token_setproc.h" 30 31 using namespace testing::ext; 32 using namespace OHOS; 33 using namespace OHOS::AccountSA; 34 using namespace OHOS::AppExecFwk; 35 #ifdef HAS_CES_PART 36 using namespace OHOS::EventFwk; 37 #endif // HAS_CES_PART 38 namespace { 39 const std::string STRING_NAME = "name"; 40 const std::string STRING_EXTRA_INFO = "extra_info"; 41 const std::string STRING_KEY = "key"; 42 const std::string STRING_KEY_TWO = "key_two"; 43 const std::string STRING_VALUE = "value"; 44 const std::string STRING_VALUE_TWO = "value_two"; 45 const std::string STRING_EMPTY = ""; 46 constexpr std::int32_t UID = 10000; 47 std::shared_ptr<AppAccountManagerService> g_accountManagerService = 48 std::make_shared<AppAccountManagerService>(); 49 static constexpr int32_t DEFAULT_API_VERSION = 8; 50 uint64_t g_tokenId = GetSelfTokenID(); 51 static OHOS::Security::AccessToken::PermissionStateFull g_testState1 = { 52 .permissionName = "", 53 .isGeneral = true, 54 .resDeviceID = {"local"}, 55 .grantStatus = {OHOS::Security::AccessToken::PermissionState::PERMISSION_GRANTED}, 56 .grantFlags = {1} 57 }; 58 59 static OHOS::Security::AccessToken::HapPolicyParams g_PolicyPrams1 = { 60 .apl = OHOS::Security::AccessToken::APL_NORMAL, 61 .domain = "test.domain.xxx", 62 .permList = {}, 63 .permStateList = {g_testState1} 64 }; 65 66 static OHOS::Security::AccessToken::HapInfoParams g_info = { 67 .userID = 0, 68 .bundleName = "com.example.owner", 69 .instIndex = 0, 70 .appIDDesc = "test.demo", 71 .apiVersion = DEFAULT_API_VERSION, 72 .isSystemApp = true 73 }; 74 } // namespace 75 76 class AppAccountManagerServiceAssocaitedDataTest : public testing::Test { 77 public: 78 static void SetUpTestCase(void); 79 static void TearDownTestCase(void); 80 void SetUp(void) override; 81 void TearDown(void) override; 82 void ClearDataStorage(); 83 }; 84 ClearDataStorage()85 void AppAccountManagerServiceAssocaitedDataTest::ClearDataStorage() 86 { 87 auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID); 88 ASSERT_NE(dataStoragePtr, nullptr); 89 std::map<std::string, std::shared_ptr<IAccountInfo>> accounts; 90 dataStoragePtr->LoadAllData(accounts); 91 if (!accounts.empty()) { 92 for (auto accountPtr : accounts) { 93 dataStoragePtr->RemoveValueFromKvStore(accountPtr.first); 94 } 95 } 96 dataStoragePtr->LoadAllData(accounts); 97 GTEST_LOG_(INFO) << "ClearDataStorage end, accounts.size =" << accounts.size(); 98 } 99 SetUpTestCase(void)100 void AppAccountManagerServiceAssocaitedDataTest::SetUpTestCase(void) 101 { 102 GTEST_LOG_(INFO) << "SetUpTestCase"; 103 } 104 TearDownTestCase(void)105 void AppAccountManagerServiceAssocaitedDataTest::TearDownTestCase(void) 106 { 107 GTEST_LOG_(INFO) << "TearDownTestCase enter"; 108 auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID); 109 ASSERT_NE(dataStoragePtr, nullptr); 110 111 ErrCode result = dataStoragePtr->DeleteKvStore(); 112 ASSERT_EQ(result, ERR_OK); 113 114 #ifdef DISTRIBUTED_FEATURE_ENABLED 115 dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID, true); 116 ASSERT_NE(dataStoragePtr, nullptr); 117 118 result = dataStoragePtr->DeleteKvStore(); 119 ASSERT_EQ(result, ERR_OK); 120 #endif // DISTRIBUTED_FEATURE_ENABLED 121 GTEST_LOG_(INFO) << "TearDownTestCase exit"; 122 } 123 SetUp(void)124 void AppAccountManagerServiceAssocaitedDataTest::SetUp(void) __attribute__((no_sanitize("cfi"))) 125 { 126 testing::UnitTest *test = testing::UnitTest::GetInstance(); 127 ASSERT_NE(test, nullptr); 128 const testing::TestInfo *testinfo = test->current_test_info(); 129 ASSERT_NE(testinfo, nullptr); 130 string testCaseName = string(testinfo->name()); 131 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str()); 132 133 ClearDataStorage(); 134 } 135 TearDown(void)136 void AppAccountManagerServiceAssocaitedDataTest::TearDown(void) 137 {} 138 139 /** 140 * @tc.name: AppAccountManagerService_GetAssociatedData_0100 141 * @tc.desc: Get associated data with valid data. 142 * @tc.type: FUNC 143 * @tc.require: issueI5N90B 144 */ 145 HWTEST_F(AppAccountManagerServiceAssocaitedDataTest, AppAccountManagerService_GetAssociatedData_0100, TestSize.Level1) 146 { 147 ACCOUNT_LOGI("AppAccountManagerService_GetAssociatedData_0100"); 148 149 ErrCode result = g_accountManagerService->AddAccount(STRING_NAME, STRING_EXTRA_INFO); 150 EXPECT_EQ(result, ERR_OK); 151 152 result = g_accountManagerService->SetAssociatedData(STRING_NAME, STRING_KEY, STRING_VALUE); 153 EXPECT_EQ(result, ERR_OK); 154 155 std::string value; 156 result = g_accountManagerService->GetAssociatedData(STRING_NAME, STRING_KEY, value); 157 EXPECT_EQ(result, ERR_OK); 158 EXPECT_EQ(value, STRING_VALUE); 159 160 result = g_accountManagerService->DeleteAccount(STRING_NAME); 161 EXPECT_EQ(result, ERR_OK); 162 } 163 164 /** 165 * @tc.name: AppAccountManagerService_GetAssociatedData_0200 166 * @tc.desc: Get associated data with invalid data. 167 * @tc.type: FUNC 168 * @tc.require: issueI5N90B 169 */ 170 HWTEST_F(AppAccountManagerServiceAssocaitedDataTest, AppAccountManagerService_GetAssociatedData_0200, TestSize.Level1) 171 { 172 ACCOUNT_LOGI("AppAccountManagerService_GetAssociatedData_0200"); 173 174 ErrCode result = g_accountManagerService->AddAccount(STRING_NAME, STRING_EXTRA_INFO); 175 EXPECT_EQ(result, ERR_OK); 176 177 std::string value; 178 result = g_accountManagerService->GetAssociatedData(STRING_NAME, STRING_KEY, value); 179 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_ASSOCIATED_DATA_KEY_NOT_EXIST); 180 EXPECT_EQ(value, STRING_EMPTY); 181 182 result = g_accountManagerService->DeleteAccount(STRING_NAME); 183 EXPECT_EQ(result, ERR_OK); 184 } 185 186 /** 187 * @tc.name: AppAccountManagerService_GetAssociatedData_0300 188 * @tc.desc: Get associated data with invalid data. 189 * @tc.type: FUNC 190 * @tc.require: issueI5N90B 191 */ 192 HWTEST_F(AppAccountManagerServiceAssocaitedDataTest, AppAccountManagerService_GetAssociatedData_0300, TestSize.Level1) 193 { 194 ACCOUNT_LOGI("AppAccountManagerService_GetAssociatedData_0300"); 195 196 std::string value; 197 ErrCode result = g_accountManagerService->GetAssociatedData(STRING_NAME, STRING_KEY, value); 198 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_ACCOUNT_INFO_BY_ID); 199 EXPECT_EQ(value, STRING_EMPTY); 200 } 201 202 /** 203 * @tc.name: AppAccountManagerService_GetAssociatedData_0400 204 * @tc.desc: Get associated data with valid data. 205 * @tc.type: FUNC 206 * @tc.require: issueI5N90B 207 */ 208 HWTEST_F(AppAccountManagerServiceAssocaitedDataTest, AppAccountManagerService_GetAssociatedData_0400, TestSize.Level1) 209 { 210 ACCOUNT_LOGI("AppAccountManagerService_GetAssociatedData_0400"); 211 212 ErrCode result = g_accountManagerService->AddAccount(STRING_NAME, STRING_EXTRA_INFO); 213 EXPECT_EQ(result, ERR_OK); 214 215 result = g_accountManagerService->SetAssociatedData(STRING_NAME, STRING_KEY, STRING_VALUE); 216 EXPECT_EQ(result, ERR_OK); 217 218 result = g_accountManagerService->SetAssociatedData(STRING_NAME, STRING_KEY_TWO, STRING_VALUE_TWO); 219 EXPECT_EQ(result, ERR_OK); 220 221 std::string value; 222 result = g_accountManagerService->GetAssociatedData(STRING_NAME, STRING_KEY, value); 223 EXPECT_EQ(result, ERR_OK); 224 EXPECT_EQ(value, STRING_VALUE); 225 226 result = g_accountManagerService->GetAssociatedData(STRING_NAME, STRING_KEY_TWO, value); 227 EXPECT_EQ(result, ERR_OK); 228 EXPECT_EQ(value, STRING_VALUE_TWO); 229 230 result = g_accountManagerService->DeleteAccount(STRING_NAME); 231 EXPECT_EQ(result, ERR_OK); 232 } 233 234 /** 235 * @tc.name: AppAccountManagerService_SetAssociatedData_0100 236 * @tc.desc: Set associated data with valid data. 237 * @tc.type: FUNC 238 * @tc.require: issueI5N90B 239 */ 240 HWTEST_F(AppAccountManagerServiceAssocaitedDataTest, AppAccountManagerService_SetAssociatedData_0100, TestSize.Level1) 241 { 242 ACCOUNT_LOGI("AppAccountManagerService_SetAssociatedData_0100"); 243 244 ErrCode result = g_accountManagerService->AddAccount(STRING_NAME, STRING_EXTRA_INFO); 245 EXPECT_EQ(result, ERR_OK); 246 247 result = g_accountManagerService->SetAssociatedData(STRING_NAME, STRING_KEY, STRING_VALUE); 248 EXPECT_EQ(result, ERR_OK); 249 250 result = g_accountManagerService->DeleteAccount(STRING_NAME); 251 EXPECT_EQ(result, ERR_OK); 252 } 253 254 /** 255 * @tc.name: AppAccountManagerService_SetAssociatedData_0200 256 * @tc.desc: Set associated data with valid data. 257 * @tc.type: FUNC 258 * @tc.require: issueI5N90B 259 */ 260 HWTEST_F(AppAccountManagerServiceAssocaitedDataTest, AppAccountManagerService_SetAssociatedData_0200, TestSize.Level1) 261 { 262 ACCOUNT_LOGI("AppAccountManagerService_SetAssociatedData_0200"); 263 264 ErrCode result = g_accountManagerService->AddAccount(STRING_NAME, STRING_EXTRA_INFO); 265 EXPECT_EQ(result, ERR_OK); 266 267 result = g_accountManagerService->SetAssociatedData(STRING_NAME, STRING_KEY, STRING_VALUE); 268 EXPECT_EQ(result, ERR_OK); 269 270 result = g_accountManagerService->SetAssociatedData(STRING_NAME, STRING_KEY, STRING_VALUE_TWO); 271 EXPECT_EQ(result, ERR_OK); 272 273 std::string value; 274 result = g_accountManagerService->GetAssociatedData(STRING_NAME, STRING_KEY, value); 275 EXPECT_EQ(result, ERR_OK); 276 EXPECT_EQ(value, STRING_VALUE_TWO); 277 278 result = g_accountManagerService->DeleteAccount(STRING_NAME); 279 EXPECT_EQ(result, ERR_OK); 280 } 281 282 /** 283 * @tc.name: AppAccountManagerService_SetAssociatedData_0300 284 * @tc.desc: Set associated data with invalid data. 285 * @tc.type: FUNC 286 * @tc.require: issueI5N90B 287 */ 288 HWTEST_F(AppAccountManagerServiceAssocaitedDataTest, AppAccountManagerService_SetAssociatedData_0300, TestSize.Level1) 289 { 290 ACCOUNT_LOGI("AppAccountManagerService_SetAssociatedData_0300"); 291 292 ErrCode result = g_accountManagerService->SetAssociatedData(STRING_NAME, STRING_KEY, STRING_VALUE); 293 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_ACCOUNT_INFO_BY_ID); 294 }