1 /*
2 * Copyright (c) 2021-2022 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
18 #include <thread>
19
20 #include "account_log_wrapper.h"
21 #include "app_account_constants.h"
22 #define private public
23 #include "app_account_control_manager.h"
24 #undef private
25
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AccountSA;
29
30 namespace {
31 const std::string STRING_NAME = "name";
32 const std::string STRING_NAME_BACK = "end";
33 const std::string STRING_EXTRA_INFO = "extra_info";
34 const std::string STRING_OWNER = "com.example.owner";
35 const std::string AUTHORIZED_APP = "authorizedApp";
36 const std::string BUNDLE_NAME = "bundlename";
37
38 const std::size_t ACCOUNT_MAX_SIZE = 1000;
39
40 constexpr std::int32_t UID = 10000;
41 } // namespace
42
43 class AppAccountControlManagerModuleTest : public testing::Test {
44 public:
45 static void SetUpTestCase(void);
46 static void TearDownTestCase(void);
47 void SetUp(void) override;
48 void TearDown(void) override;
49 };
50
SetUpTestCase(void)51 void AppAccountControlManagerModuleTest::SetUpTestCase(void)
52 {}
53
TearDownTestCase(void)54 void AppAccountControlManagerModuleTest::TearDownTestCase(void)
55 {
56 GTEST_LOG_(INFO) << "TearDownTestCase enter";
57 }
58
SetUp(void)59 void AppAccountControlManagerModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
60 {
61 testing::UnitTest *test = testing::UnitTest::GetInstance();
62 ASSERT_NE(test, nullptr);
63 const testing::TestInfo *testinfo = test->current_test_info();
64 ASSERT_NE(testinfo, nullptr);
65 string testCaseName = string(testinfo->name());
66 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
67 }
68
TearDown(void)69 void AppAccountControlManagerModuleTest::TearDown(void)
70 {}
71
72 /**
73 * @tc.name: AppAccountControlManager_AccountMaxSize_0100
74 * @tc.desc: Check account max size with valid data.
75 * @tc.type: FUNC
76 * @tc.require: SR000GGVFV
77 */
78 HWTEST_F(AppAccountControlManagerModuleTest, AppAccountControlManager_AccountMaxSize_0100, TestSize.Level1)
79 {
80 ACCOUNT_LOGI("AppAccountControlManager_AccountMaxSize_0100");
81 EXPECT_EQ(AppAccountControlManager::GetInstance().ACCOUNT_MAX_SIZE, ACCOUNT_MAX_SIZE);
82 }
83
84 /**
85 * @tc.name: AppAccountControlManager_AccountMaxSize_0200
86 * @tc.desc: Check account max size with valid data.
87 * @tc.type: FUNC
88 * @tc.require: SR000GGVFV
89 */
90 HWTEST_F(AppAccountControlManagerModuleTest, AppAccountControlManager_AccountMaxSize_0200, TestSize.Level1)
91 {
92 ErrCode result;
93 AppAccountInfo appAccountInfo(STRING_NAME, STRING_OWNER+"max");
94 result = AppAccountControlManager::GetInstance().AddAccount(
95 STRING_NAME, STRING_EXTRA_INFO, UID, STRING_OWNER+"max", appAccountInfo);
96 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_ACCOUNT_MAX_SIZE);
97 }
98
99 /**
100 * @tc.name: AppAccountControlManager_dataStoragePtrIsNull_0100
101 * @tc.desc: dataStoragePtrIsNull test app account control manager dataStoragePtr is null.
102 * @tc.type: FUNC
103 * @tc.require:
104 */
105 HWTEST_F(AppAccountControlManagerModuleTest, AppAccountControlManager_dataStoragePtrIsNull_0100, TestSize.Level1)
106 {
107 ACCOUNT_LOGI("AppAccountControlManager_RemoveAuthorizedAccountFromDataStorage_0100");
108 std::shared_ptr<AppAccountDataStorage> dataStoragePtr = nullptr;
109 AppAccountInfo appAccountInfo;
110 ErrCode result =
111 AppAccountControlManager::GetInstance().RemoveAuthorizedAccountFromDataStorage(
112 AUTHORIZED_APP, appAccountInfo, dataStoragePtr);
113 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
114
115 result = AppAccountControlManager::GetInstance().SaveAuthorizedAccountIntoDataStorage(
116 AUTHORIZED_APP, appAccountInfo, dataStoragePtr);
117 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
118
119 result = AppAccountControlManager::GetInstance().RemoveAuthorizedAccount(
120 BUNDLE_NAME, appAccountInfo, dataStoragePtr, UID);
121 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
122
123 result = AppAccountControlManager::GetInstance().SaveAuthorizedAccount(
124 BUNDLE_NAME, appAccountInfo, dataStoragePtr, UID);
125 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
126
127 result = AppAccountControlManager::GetInstance().DeleteAccountInfoFromDataStorage(
128 appAccountInfo, dataStoragePtr, UID);
129 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
130
131 result = AppAccountControlManager::GetInstance().SaveAccountInfoIntoDataStorage(
132 appAccountInfo, dataStoragePtr, UID);
133 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
134
135 result = AppAccountControlManager::GetInstance().AddAccountInfoIntoDataStorage(
136 appAccountInfo, dataStoragePtr, UID);
137 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
138
139 result = AppAccountControlManager::GetInstance().GetAccountInfoFromDataStorage(appAccountInfo, dataStoragePtr);
140 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
141
142 std::vector<AppAccountInfo> appAccounts;
143 result = AppAccountControlManager::GetInstance().GetAllAccessibleAccountsFromDataStorage(
144 appAccounts, BUNDLE_NAME, dataStoragePtr, 0);
145 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
146 }
147
148 /**
149 * @tc.name: AppAccountControlManager_NeedSyncDataStorage_0100
150 * @tc.desc: NeedSyncDataStorage abnormal branch.
151 * @tc.type: FUNC
152 * @tc.require:
153 */
154 HWTEST_F(AppAccountControlManagerModuleTest, AppAccountControlManager_NeedSyncDataStorage_0100, TestSize.Level1)
155 {
156 ACCOUNT_LOGI("AppAccountControlManager_GNeedSyncDataStorage_0100");
157 AppAccountInfo appAccountInfo;
158 bool syncEnable = false;
159 appAccountInfo.SetSyncEnable(syncEnable);
160 syncEnable = true;
161 appAccountInfo.GetSyncEnable(syncEnable);
162 ASSERT_EQ(syncEnable, false);
163 bool result = AppAccountControlManager::GetInstance().NeedSyncDataStorage(appAccountInfo);
164 ASSERT_EQ(result, false);
165 }
166
167 /**
168 * @tc.name: AppAccountControlManager_GetAllAccountsFromDataStorage_0100
169 * @tc.desc: GetAllAccountsFromDataStorage abnormal branch.
170 * @tc.type: FUNC
171 * @tc.require:
172 */
173 HWTEST_F(
174 AppAccountControlManagerModuleTest, AppAccountControlManager_GetAllAccountsFromDataStorage_0100, TestSize.Level1)
175 {
176 ACCOUNT_LOGI("AppAccountControlManager_GetAllAccountsFromDataStorage_0100");
177 AppExecFwk::AbilityStateData abilityStateData;
178 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_TERMINATED);
179 AppAccountControlManager::GetInstance().OnAbilityStateChanged(abilityStateData);
180 ASSERT_EQ(AppAccountControlManager::GetInstance().associatedDataCache_.empty(), true);
181
182 abilityStateData.abilityState = static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_BACKGROUND);
183 AppAccountControlManager::GetInstance().OnAbilityStateChanged(abilityStateData);
184 std::vector<AppAccountInfo> appAccounts;
185
186 std::shared_ptr<AppAccountDataStorage> dataStoragePtr = nullptr;
187 ErrCode result = AppAccountControlManager::GetInstance().GetAllAccountsFromDataStorage(
188 STRING_OWNER, appAccounts, BUNDLE_NAME, dataStoragePtr);
189 ASSERT_EQ(result, ERR_APPACCOUNT_SERVICE_DATA_STORAGE_PTR_IS_NULLPTR);
190 }
191
192 /**
193 * @tc.name: AppAccountControlManager_GetAllOAuthTokens_0100
194 * @tc.desc: GetAllOAuthTokens abnormal branch.
195 * @tc.type: FUNC
196 * @tc.require:
197 */
198 HWTEST_F(AppAccountControlManagerModuleTest, AppAccountControlManager_GetAllOAuthTokens_0100, TestSize.Level1)
199 {
200 ACCOUNT_LOGI("AppAccountControlManager_GetAllOAuthTokens_0100");
201 AuthenticatorSessionRequest request;
202 request.name = STRING_NAME;
203 request.owner = STRING_OWNER;
204 request.callerBundleName = BUNDLE_NAME;
205 request.appIndex = 0;
206 request.callerUid = 0;
207 std::vector<OAuthTokenInfo> tokenInfos;
208 AppAccountInfo appAccountInfo(request.name, request.owner);
209 std::string authTypeOne = "test_authType1";
210 std::string authTokenOne = "test_authToken1";
211 std::string authTypeTwo = "test_authType2";
212 std::string authTokenTwo = "test_authToken2";
213 std::string authTypeThree = "test_authType3";
214 std::string authTokenThree = "";
215 appAccountInfo.SetOAuthToken(authTypeOne, authTokenOne);
216 appAccountInfo.SetOAuthToken(authTypeTwo, authTokenTwo);
217 appAccountInfo.SetOAuthToken(authTypeThree, authTokenThree);
218 appAccountInfo.SetOAuthTokenVisibility(authTypeOne, BUNDLE_NAME, true, Constants::API_VERSION8);
219 appAccountInfo.SetSyncEnable(true);
220 bool isSyncEnable = false;
221 appAccountInfo.GetSyncEnable(isSyncEnable);
222 ASSERT_NE(isSyncEnable, false);
223 auto dataStoragePtr = AppAccountControlManager::GetInstance().GetDataStorage(request.callerUid, false);
224 ASSERT_NE(dataStoragePtr, nullptr);
225 ErrCode result = AppAccountControlManager::GetInstance().AddAccountInfoIntoDataStorage(
226 appAccountInfo, dataStoragePtr, request.callerUid);
227 ASSERT_EQ(result, ERR_OK);
228 result = AppAccountControlManager::GetInstance().GetAllOAuthTokens(request, tokenInfos);
229 ASSERT_EQ(result, ERR_OK);
230 EXPECT_EQ(tokenInfos.size(), 1);
231 }
232