1 /*
2  * Copyright (c) 2021-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 <thread>
18 #include "account_log_wrapper.h"
19 #define private public
20 #include "app_account_control_manager.h"
21 #include "app_account_manager_service.h"
22 #undef private
23 
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AccountSA;
27 
28 namespace {
29 const std::string STRING_NAME = "name";
30 const std::string STRING_NAME_TWO = "name_two";
31 const std::string STRING_NAME_THREE = "name_three";
32 const std::string STRING_NAME_NOT_EXISTED = "name_not_existed";
33 const std::string STRING_EXTRA_INFO = "extra_info";
34 const std::string STRING_EXTRA_INFO_TWO = "extra_info_two";
35 const std::string STRING_BUNDLE_NAME = "com.example.third_party";
36 const std::string STRING_EMPTY = "";
37 const std::string STRING_OWNER = "com.example.owner";
38 const std::string APP_INDEX = "0";
39 const std::string HYPHEN = "#";
40 const std::string AUTHORIZED_ACCOUNTS = "authorizedAccounts";
41 
42 constexpr std::int32_t UID = 10000;
43 const bool SYNC_ENABLE_TRUE = true;
44 const bool SYNC_ENABLE_FALSE = false;
45 
46 constexpr std::size_t SIZE_ZERO = 0;
47 constexpr std::size_t SIZE_ONE = 1;
48 
49 class AppAccountManagerServiceSyncModuleTest : public testing::Test {
50 public:
51     static void SetUpTestCase(void);
52     static void TearDownTestCase(void);
53     void SetUp(void) override;
54     void TearDown(void) override;
55     void ClearDataStorage(std::shared_ptr<AppAccountDataStorage> &dataStoragePtr);
56     std::shared_ptr<AppAccountManagerService>
57         appAccountManagerServicePtr_ = std::make_shared<AppAccountManagerService>();
58 };
59 
SetUpTestCase(void)60 void AppAccountManagerServiceSyncModuleTest::SetUpTestCase(void)
61 {}
62 
TearDownTestCase(void)63 void AppAccountManagerServiceSyncModuleTest::TearDownTestCase(void)
64 {
65     GTEST_LOG_(INFO) << "TearDownTestCase!";
66 }
67 
SetUp(void)68 void AppAccountManagerServiceSyncModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
69 {
70     testing::UnitTest *test = testing::UnitTest::GetInstance();
71     ASSERT_NE(test, nullptr);
72     const testing::TestInfo *testinfo = test->current_test_info();
73     ASSERT_NE(testinfo, nullptr);
74     string testCaseName = string(testinfo->name());
75     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
76 
77     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID);
78     ClearDataStorage(dataStoragePtr);
79 #ifdef DISTRIBUTED_FEATURE_ENABLED
80     dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID, true);
81     ClearDataStorage(dataStoragePtr);
82 #endif // DISTRIBUTED_FEATURE_ENABLED
83 }
84 
ClearDataStorage(std::shared_ptr<AppAccountDataStorage> & dataStoragePtr)85 void AppAccountManagerServiceSyncModuleTest::ClearDataStorage(std::shared_ptr<AppAccountDataStorage> &dataStoragePtr)
86 {
87     std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
88     dataStoragePtr->LoadAllData(accounts);
89     if (!accounts.empty()) {
90         for (auto accountPtr : accounts) {
91             dataStoragePtr->RemoveValueFromKvStore(accountPtr.first);
92         }
93     }
94     dataStoragePtr->LoadAllData(accounts);
95 }
96 
TearDown(void)97 void AppAccountManagerServiceSyncModuleTest::TearDown(void)
98 {}
99 
100 /**
101  * @tc.name: AppAccountManagerServiceSync_AddAccount_0200
102  * @tc.desc: Set account sync enable with valid data.
103  * @tc.type: FUNC
104  * @tc.require: SR000GGV11
105  */
106 HWTEST_F(AppAccountManagerServiceSyncModuleTest, AppAccountManagerServiceSync_AddAccount_0200, TestSize.Level1)
107 {
108     ACCOUNT_LOGI("AppAccountManagerServiceSync_AddAccount_0200");
109 
110     ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
111     EXPECT_EQ(result, ERR_OK);
112 
113     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_TRUE);
114     EXPECT_EQ(result, ERR_OK);
115 
116 #ifdef DISTRIBUTED_FEATURE_ENABLED
117     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID, true);
118 #else
119     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID);
120 #endif // DISTRIBUTED_FEATURE_ENABLED
121     ASSERT_NE(dataStoragePtr, nullptr);
122 
123     std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
124     result = dataStoragePtr->LoadAllData(accounts);
125     EXPECT_EQ(result, ERR_OK);
126     ASSERT_EQ(accounts.size(), SIZE_ONE);
127 
128     auto accountPtr = accounts.begin();
129     auto appAccountInfoPtr = std::static_pointer_cast<AppAccountInfo>(accountPtr->second);
130     ASSERT_NE(appAccountInfoPtr, nullptr);
131 
132     std::string name;
133     appAccountInfoPtr->GetName(name);
134     EXPECT_EQ(name, STRING_NAME);
135 
136     result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
137     EXPECT_EQ(result, ERR_OK);
138 }
139 
140 /**
141  * @tc.name: AppAccountManagerServiceSync_DeleteAccount_0100
142  * @tc.desc: Set account sync enable with valid data.
143  * @tc.type: FUNC
144  * @tc.require: SR000GGV11
145  */
146 HWTEST_F(AppAccountManagerServiceSyncModuleTest, AppAccountManagerServiceSync_DeleteAccount_0100, TestSize.Level1)
147 {
148     ACCOUNT_LOGI("AppAccountManagerServiceSync_DeleteAccount_0100");
149 
150     ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
151     EXPECT_EQ(result, ERR_OK);
152 
153     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_TRUE);
154     EXPECT_EQ(result, ERR_OK);
155 
156     result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
157     EXPECT_EQ(result, ERR_OK);
158 
159 #ifdef DISTRIBUTED_FEATURE_ENABLED
160     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID, true);
161 #else
162     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID);
163 #endif // DISTRIBUTED_FEATURE_ENABLED
164     ASSERT_NE(dataStoragePtr, nullptr);
165 
166     std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
167     result = dataStoragePtr->LoadAllData(accounts);
168     EXPECT_EQ(result, ERR_OK);
169     EXPECT_EQ(accounts.size(), SIZE_ZERO);
170 }
171 
172 /**
173  * @tc.name: AppAccountManagerServiceSync_DeleteAccount_0200
174  * @tc.desc: Set account sync enable with valid data.
175  * @tc.type: FUNC
176  * @tc.require: SR000GGV11
177  */
178 HWTEST_F(AppAccountManagerServiceSyncModuleTest, AppAccountManagerServiceSync_DeleteAccount_0200, TestSize.Level1)
179 {
180     ACCOUNT_LOGI("AppAccountManagerServiceSync_DeleteAccount_0200");
181 
182     ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
183     EXPECT_EQ(result, ERR_OK);
184 
185     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_TRUE);
186     EXPECT_EQ(result, ERR_OK);
187 
188     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_FALSE);
189     EXPECT_EQ(result, ERR_OK);
190 
191     result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
192     EXPECT_EQ(result, ERR_OK);
193     {
194         auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID);
195         ASSERT_NE(dataStoragePtr, nullptr);
196 
197         std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
198         result = dataStoragePtr->LoadAllData(accounts);
199         EXPECT_EQ(result, ERR_OK);
200         EXPECT_EQ(accounts.size(), SIZE_ZERO);
201     }
202 }
203 
204 /**
205  * @tc.name: AppAccountManagerServiceSync_SetAccountExtraInfo_0300
206  * @tc.desc: Set account sync enable with valid data.
207  * @tc.type: FUNC
208  * @tc.require: SR000GGVFS
209  */
210 HWTEST_F(
211     AppAccountManagerServiceSyncModuleTest, AppAccountManagerServiceSync_SetAccountExtraInfo_0300, TestSize.Level1)
212 {
213     ACCOUNT_LOGI("AppAccountManagerServiceSync_SetAccountExtraInfo_0300");
214 
215     ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
216     ASSERT_EQ(result, ERR_OK);
217 
218     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_TRUE);
219     EXPECT_EQ(result, ERR_OK);
220 
221     result = appAccountManagerServicePtr_->SetAccountExtraInfo(STRING_NAME, STRING_EXTRA_INFO);
222     EXPECT_EQ(result, ERR_OK);
223 
224     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_FALSE);
225     EXPECT_EQ(result, ERR_OK);
226 
227     result = appAccountManagerServicePtr_->SetAccountExtraInfo(STRING_NAME, STRING_EXTRA_INFO_TWO);
228     EXPECT_EQ(result, ERR_OK);
229 
230     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_TRUE);
231     EXPECT_EQ(result, ERR_OK);
232 
233 #ifdef DISTRIBUTED_FEATURE_ENABLED
234     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID, true);
235 #else
236     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID);
237 #endif
238     ASSERT_NE(dataStoragePtr, nullptr);
239 
240     std::map<std::string, std::shared_ptr<IAccountInfo>> accounts;
241     result = dataStoragePtr->LoadAllData(accounts);
242     EXPECT_EQ(result, ERR_OK);
243     ASSERT_EQ(accounts.size(), SIZE_ONE);
244 
245     auto accountPtr = accounts.begin();
246     auto appAccountInfoPtr = std::static_pointer_cast<AppAccountInfo>(accountPtr->second);
247 
248     std::string name;
249     appAccountInfoPtr->GetName(name);
250     EXPECT_EQ(name, STRING_NAME);
251 
252     std::string extraInfo;
253     appAccountInfoPtr->GetExtraInfo(extraInfo);
254     EXPECT_EQ(extraInfo, STRING_EXTRA_INFO_TWO);
255 
256     result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
257     EXPECT_EQ(result, ERR_OK);
258 }
259 
260 /**
261  * @tc.name: AppAccountManagerServiceSync_EnableAppAccess_0100
262  * @tc.desc: Set account sync enable with valid data.
263  * @tc.type: FUNC
264  * @tc.require: SR000GGVFS
265  */
266 HWTEST_F(AppAccountManagerServiceSyncModuleTest, AppAccountManagerServiceSync_EnableAppAccess_0100, TestSize.Level1)
267 {
268     ACCOUNT_LOGI("AppAccountManagerServiceSync_EnableAppAccess_0100");
269 
270     ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
271     EXPECT_EQ(result, ERR_OK);
272 
273     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_TRUE);
274     EXPECT_EQ(result, ERR_OK);
275 
276     result = appAccountManagerServicePtr_->EnableAppAccess(STRING_NAME, STRING_BUNDLE_NAME);
277     EXPECT_EQ(result, ERR_OK);
278 
279 
280 #ifdef DISTRIBUTED_FEATURE_ENABLED
281     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID, true);
282 #else
283     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID);
284     ASSERT_NE(dataStoragePtr, nullptr);
285 
286     std::string authorizedAccounts;
287     result = dataStoragePtr->GetValueFromKvStore(AUTHORIZED_ACCOUNTS, authorizedAccounts);
288     ASSERT_EQ(result, ERR_OK);
289 
290     auto jsonObject = Json::parse(authorizedAccounts, nullptr, false);
291     EXPECT_EQ(jsonObject.is_discarded(), false);
292     EXPECT_NE(jsonObject.find(STRING_BUNDLE_NAME), jsonObject.end());
293     EXPECT_TRUE(jsonObject.at(STRING_BUNDLE_NAME).is_array());
294 
295     auto accessibleAccounts = jsonObject[STRING_BUNDLE_NAME].get<std::vector<std::string>>();
296     EXPECT_EQ(accessibleAccounts.size(), SIZE_ONE);
297 
298     auto accountPtr = accessibleAccounts.begin();
299     ASSERT_NE(accountPtr, accessibleAccounts.end());
300 
301     EXPECT_EQ(*accountPtr, STRING_OWNER + HYPHEN + APP_INDEX + HYPHEN + STRING_NAME + HYPHEN);
302 #endif // DISTRIBUTED_FEATURE_ENABLED
303 
304     result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
305     EXPECT_EQ(result, ERR_OK);
306 }
307 
308 /**
309  * @tc.name: AppAccountManagerServiceSync_EnableAppAccess_0200
310  * @tc.desc: Set account sync enable with valid data.
311  * @tc.type: FUNC
312  * @tc.require: SR000GGVFS
313  */
314 HWTEST_F(AppAccountManagerServiceSyncModuleTest, AppAccountManagerServiceSync_EnableAppAccess_0200, TestSize.Level1)
315 {
316     ACCOUNT_LOGI("AppAccountManagerServiceSync_EnableAppAccess_0200");
317 
318     ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
319     EXPECT_EQ(result, ERR_OK);
320 
321     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_TRUE);
322     EXPECT_EQ(result, ERR_OK);
323 
324     result = appAccountManagerServicePtr_->EnableAppAccess(STRING_NAME, STRING_BUNDLE_NAME);
325     EXPECT_EQ(result, ERR_OK);
326 
327     result = appAccountManagerServicePtr_->EnableAppAccess(STRING_NAME, STRING_BUNDLE_NAME);
328     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_ENABLE_APP_ACCESS_ALREADY_EXISTS);
329 
330 #ifdef DISTRIBUTED_FEATURE_ENABLED
331     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID, true);
332 #else
333     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID);
334 #endif // DISTRIBUTED_FEATURE_ENABLED
335     ASSERT_NE(dataStoragePtr, nullptr);
336 
337     std::string authorizedAccounts;
338     result = dataStoragePtr->GetValueFromKvStore(AUTHORIZED_ACCOUNTS,
339         authorizedAccounts);
340     ASSERT_EQ(result, ERR_OK);
341 
342     auto jsonObject = Json::parse(authorizedAccounts, nullptr, false);
343     EXPECT_EQ(jsonObject.is_discarded(), false);
344     EXPECT_NE(jsonObject.find(STRING_BUNDLE_NAME), jsonObject.end());
345     EXPECT_TRUE(jsonObject.at(STRING_BUNDLE_NAME).is_array());
346 
347     auto accessibleAccounts = jsonObject[STRING_BUNDLE_NAME].get<std::vector<std::string>>();
348     EXPECT_EQ(accessibleAccounts.size(), SIZE_ONE);
349 
350     auto accountPtr = accessibleAccounts.begin();
351     ASSERT_NE(accountPtr, accessibleAccounts.end());
352 
353     EXPECT_EQ(*accountPtr, STRING_OWNER + HYPHEN + APP_INDEX + HYPHEN + STRING_NAME + HYPHEN);
354 
355     result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
356     EXPECT_EQ(result, ERR_OK);
357 }
358 
359 /**
360  * @tc.name: AppAccountManagerServiceSync_DisableAppAccess_0100
361  * @tc.desc: Set account sync enable with valid data.
362  * @tc.type: FUNC
363  * @tc.require: SR000GGVFS
364  */
365 HWTEST_F(AppAccountManagerServiceSyncModuleTest, AppAccountManagerServiceSync_DisableAppAccess_0100, TestSize.Level1)
366 {
367     ACCOUNT_LOGI("AppAccountManagerServiceSync_DisableAppAccess_0100");
368 
369     ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
370     EXPECT_EQ(result, ERR_OK);
371 
372     result = appAccountManagerServicePtr_->SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_TRUE);
373     EXPECT_EQ(result, ERR_OK);
374 
375     result = appAccountManagerServicePtr_->EnableAppAccess(STRING_NAME, STRING_BUNDLE_NAME);
376     EXPECT_EQ(result, ERR_OK);
377 
378     result = appAccountManagerServicePtr_->DisableAppAccess(STRING_NAME, STRING_BUNDLE_NAME);
379     EXPECT_EQ(result, ERR_OK);
380 
381 #ifdef DISTRIBUTED_FEATURE_ENABLED
382     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID, true);
383 #else
384     auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(UID);
385 #endif // DISTRIBUTED_FEATURE_ENABLED
386     ASSERT_NE(dataStoragePtr, nullptr);
387 
388     std::string authorizedAccounts;
389     result = dataStoragePtr->GetValueFromKvStore(AUTHORIZED_ACCOUNTS,
390         authorizedAccounts);
391     ASSERT_EQ(result, ERR_OK);
392 
393     auto jsonObject = Json::parse(authorizedAccounts, nullptr, false);
394     EXPECT_EQ(jsonObject.is_discarded(), false);
395     EXPECT_NE(jsonObject.find(STRING_BUNDLE_NAME), jsonObject.end());
396     EXPECT_TRUE(jsonObject.at(STRING_BUNDLE_NAME).is_array());
397 
398     auto accessibleAccounts = jsonObject[STRING_BUNDLE_NAME].get<std::vector<std::string>>();
399     EXPECT_EQ(accessibleAccounts.size(), SIZE_ZERO);
400 
401     result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
402     EXPECT_EQ(result, ERR_OK);
403 }
404 }  // namespace
405