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 "account_log_wrapper.h"
19 #include "app_account_manager.h"
20 #include "app_account_subscriber.h"
21
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::AccountSA;
25
26 namespace {
27 const std::string STRING_NAME = "name";
28 const std::string STRING_NAME_MAX_SIZE(512, '1'); // length 512
29 const std::string STRING_EXTRA_INFO = "extra_info";
30 const std::string STRING_AUTHORIZED_APP = "com.example.authorized_app";
31 const std::string STRING_KEY = "key";
32 const std::string STRING_VALUE = "value";
33 const std::string STRING_CREDENTIAL_TYPE = "password";
34 const std::string STRING_CREDENTIAL = "1024";
35 const std::string STRING_TOKEN = "1024";
36 const std::string STRING_OWNER = "com.example.owner";
37 const std::string STRING_AUTH_TYPE = "all";
38
39 const bool SYNC_ENABLE_FALSE = false;
40 } // namespace
41
42 class AppAccountManagerModuleTest : public testing::Test {
43 public:
44 static void SetUpTestCase(void);
45 static void TearDownTestCase(void);
46 void SetUp(void) override;
47 void TearDown(void) override;
48 };
49
SetUpTestCase(void)50 void AppAccountManagerModuleTest::SetUpTestCase(void)
51 {}
52
TearDownTestCase(void)53 void AppAccountManagerModuleTest::TearDownTestCase(void)
54 {}
55
SetUp(void)56 void AppAccountManagerModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
57 {
58 testing::UnitTest *test = testing::UnitTest::GetInstance();
59 ASSERT_NE(test, nullptr);
60 const testing::TestInfo *testinfo = test->current_test_info();
61 ASSERT_NE(testinfo, nullptr);
62 string testCaseName = string(testinfo->name());
63 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
64 }
65
TearDown(void)66 void AppAccountManagerModuleTest::TearDown(void)
67 {}
68
69 class AppAccountSubscriberTest : public AppAccountSubscriber {
70 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)71 explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
72 : AppAccountSubscriber(subscribeInfo)
73 {
74 ACCOUNT_LOGI("enter");
75 }
76
~AppAccountSubscriberTest()77 ~AppAccountSubscriberTest()
78 {}
79
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)80 virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
81 {
82 ACCOUNT_LOGI("enter");
83 }
84 };
85
86 /**
87 * @tc.name: AppAccountManager_AddAccount_0100
88 * @tc.desc: Add an account with valid data.
89 * @tc.type: FUNC
90 * @tc.require: SR000GGV11
91 */
92 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_AddAccount_0100, TestSize.Level0)
93 {
94 ACCOUNT_LOGI("AppAccountManager_AddAccount_0100");
95
96 ErrCode result = AppAccountManager::AddAccount(STRING_NAME, STRING_EXTRA_INFO);
97 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
98 }
99
100 /**
101 * @tc.name: AppAccountManager_AddAccount_0200
102 * @tc.desc: Add an account with valid data.
103 * @tc.type: FUNC
104 * @tc.require: SR000GGV11
105 */
106 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_AddAccount_0200, TestSize.Level1)
107 {
108 ACCOUNT_LOGI("AppAccountManager_AddAccount_0200");
109
110 ErrCode result = AppAccountManager::AddAccount(STRING_NAME_MAX_SIZE, STRING_EXTRA_INFO);
111 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
112 }
113
114 /**
115 * @tc.name: AppAccountManager_CreateAccount_0100
116 * @tc.desc: Add an account with valid data.
117 * @tc.type: FUNC
118 * @tc.require: issueI5RWXN
119 */
120 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_CreateAccount_0100, TestSize.Level0)
121 {
122 ACCOUNT_LOGI("AppAccountManager_CreateAccount_0100");
123 CreateAccountOptions option;
124 ErrCode result = AppAccountManager::CreateAccount(STRING_NAME, option);
125 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
126 }
127
128 /**
129 * @tc.name: AppAccountManager_CreateAccount_0200
130 * @tc.desc: Add an account with valid data.
131 * @tc.type: FUNC
132 * @tc.require: issueI5RWXN
133 */
134 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_CreateAccount_0200, TestSize.Level1)
135 {
136 ACCOUNT_LOGI("AppAccountManager_CreateAccount_0200");
137 CreateAccountOptions option;
138 option.customData.emplace(STRING_KEY, STRING_VALUE);
139 ErrCode result = AppAccountManager::CreateAccount(STRING_NAME_MAX_SIZE, option);
140 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
141 }
142
143 /**
144 * @tc.name: AppAccountManager_DeleteAccount_0100
145 * @tc.desc: Delete an account with valid data.
146 * @tc.type: FUNC
147 * @tc.require: SR000GGV11
148 */
149 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_DeleteAccount_0100, TestSize.Level0)
150 {
151 ACCOUNT_LOGI("AppAccountManager_DeleteAccount_0100");
152
153 ErrCode result = AppAccountManager::DeleteAccount(STRING_NAME);
154 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
155 }
156
157 /**
158 * @tc.name: AppAccountManager_GetAccountExtraInfo_0100
159 * @tc.desc: Get account extra info with valid data.
160 * @tc.type: FUNC
161 * @tc.require: SR000GGVFS
162 */
163 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_GetAccountExtraInfo_0100, TestSize.Level1)
164 {
165 ACCOUNT_LOGI("AppAccountManager_GetAccountExtraInfo_0100");
166
167 std::string extraInfo;
168 ErrCode result = AppAccountManager::GetAccountExtraInfo(STRING_NAME, extraInfo);
169 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
170 }
171
172 /**
173 * @tc.name: AppAccountManager_SetAccountExtraInfo_0100
174 * @tc.desc: Set account extra info with valid data.
175 * @tc.type: FUNC
176 * @tc.require: SR000GGVFS
177 */
178 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_SetAccountExtraInfo_0100, TestSize.Level1)
179 {
180 ACCOUNT_LOGI("AppAccountManager_SetAccountExtraInfo_0100");
181
182 ErrCode result = AppAccountManager::SetAccountExtraInfo(STRING_NAME, STRING_EXTRA_INFO);
183 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
184 }
185
186 /**
187 * @tc.name: AppAccountManager_EnableAppAccess_0100
188 * @tc.desc: Enable app access with valid data.
189 * @tc.type: FUNC
190 * @tc.require: SR000GGVFS
191 */
192 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_EnableAppAccess_0100, TestSize.Level1)
193 {
194 ACCOUNT_LOGI("AppAccountManager_EnableAppAccess_0100");
195
196 ErrCode result = AppAccountManager::EnableAppAccess(STRING_NAME, STRING_AUTHORIZED_APP);
197 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
198 }
199
200 /**
201 * @tc.name: AppAccountManager_SetAppAccess_0100
202 * @tc.desc: Enable app access with valid data.
203 * @tc.type: FUNC
204 * @tc.require:
205 */
206 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_SetAppAccess_0100, TestSize.Level1)
207 {
208 ACCOUNT_LOGI("AppAccountManager_SetAppAccess_0100");
209
210 ErrCode result = AppAccountManager::SetAppAccess(STRING_NAME, STRING_AUTHORIZED_APP, true);
211 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
212 }
213
214 /**
215 * @tc.name: AppAccountManager_DisableAppAccess_0100
216 * @tc.desc: Disable app access with valid data.
217 * @tc.type: FUNC
218 * @tc.require: SR000GGVFS
219 */
220 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_DisableAppAccess_0100, TestSize.Level1)
221 {
222 ACCOUNT_LOGI("AppAccountManager_DisableAppAccess_0100");
223
224 ErrCode result = AppAccountManager::DisableAppAccess(STRING_NAME, STRING_AUTHORIZED_APP);
225 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
226 }
227
228 /**
229 * @tc.name: AppAccountManager_SetAppAccess_0200
230 * @tc.desc: Disable app access with valid data.
231 * @tc.type: FUNC
232 * @tc.require:
233 */
234 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_SetAppAccess_0200, TestSize.Level1)
235 {
236 ACCOUNT_LOGI("AppAccountManager_SetAppAccess_0200");
237
238 ErrCode result = AppAccountManager::SetAppAccess(STRING_NAME, STRING_AUTHORIZED_APP, false);
239 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
240 }
241
242 /**
243 * @tc.name: AppAccountManager_CheckAppAccountSyncEnable_0100
244 * @tc.desc: Check account sync enable with valid data.
245 * @tc.type: FUNC
246 * @tc.require: SR000GGVFS
247 */
248 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_CheckAppAccountSyncEnable_0100, TestSize.Level1)
249 {
250 ACCOUNT_LOGI("AppAccountManager_CheckAppAccountSyncEnable_0100");
251
252 bool syncEnable = SYNC_ENABLE_FALSE;
253 ErrCode result = AppAccountManager::CheckAppAccountSyncEnable(STRING_NAME, syncEnable);
254 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
255 }
256
257 /**
258 * @tc.name: AppAccountManager_SetAppAccountSyncEnable_0100
259 * @tc.desc: Set account sync enable with valid data.
260 * @tc.type: FUNC
261 * @tc.require: SR000GGVFS
262 */
263 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_SetAppAccountSyncEnable_0100, TestSize.Level1)
264 {
265 ACCOUNT_LOGI("AppAccountManager_SetAppAccountSyncEnable_0100");
266
267 ErrCode result = AppAccountManager::SetAppAccountSyncEnable(STRING_NAME, SYNC_ENABLE_FALSE);
268 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
269 }
270
271 /**
272 * @tc.name: AppAccountManager_GetAssociatedData_0100
273 * @tc.desc: Get associated data with valid data.
274 * @tc.type: FUNC
275 * @tc.require: SR000GGVFS
276 */
277 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_GetAssociatedData_0100, TestSize.Level1)
278 {
279 ACCOUNT_LOGI("AppAccountManager_GetAssociatedData_0100");
280
281 std::string value;
282 ErrCode result = AppAccountManager::GetAssociatedData(STRING_NAME, STRING_KEY, value);
283 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_APP_INDEX);
284 }
285
286 /**
287 * @tc.name: AppAccountManager_SetAssociatedData_0100
288 * @tc.desc: Set associated data with valid data.
289 * @tc.type: FUNC
290 * @tc.require: SR000GGVFS
291 */
292 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_SetAssociatedData_0100, TestSize.Level1)
293 {
294 ACCOUNT_LOGI("AppAccountManager_SetAssociatedData_0100");
295
296 ErrCode result = AppAccountManager::SetAssociatedData(STRING_NAME, STRING_KEY, STRING_VALUE);
297 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
298 }
299
300 /**
301 * @tc.name: AppAccountManager_GetAccountCredential_0100
302 * @tc.desc: Get account credential with valid data.
303 * @tc.type: FUNC
304 * @tc.require: SR000GGVFS
305 */
306 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_GetAccountCredential_0100, TestSize.Level1)
307 {
308 ACCOUNT_LOGI("AppAccountManager_GetAccountCredential_0100");
309
310 std::string credential;
311 ErrCode result = AppAccountManager::GetAccountCredential(STRING_NAME, STRING_CREDENTIAL_TYPE, credential);
312 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
313 }
314
315 /**
316 * @tc.name: AppAccountManager_SetAccountCredential_0100
317 * @tc.desc: Set account credential with valid data.
318 * @tc.type: FUNC
319 * @tc.require: SR000GGVFS
320 */
321 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_SetAccountCredential_0100, TestSize.Level1)
322 {
323 ACCOUNT_LOGI("AppAccountManager_SetAccountCredential_0100");
324
325 ErrCode result = AppAccountManager::SetAccountCredential(STRING_NAME, STRING_CREDENTIAL_TYPE, STRING_CREDENTIAL);
326 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
327 }
328
329 /**
330 * @tc.name: AppAccountManager_GetOAuthToken_0100
331 * @tc.desc: Get oauth token with valid data.
332 * @tc.type: FUNC
333 * @tc.require: SR000GGVFU
334 */
335 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_GetOAuthToken_0100, TestSize.Level1)
336 {
337 ACCOUNT_LOGI("AppAccountManager_GetOAuthToken_0100");
338
339 std::string token;
340 ErrCode result = AppAccountManager::GetOAuthToken(STRING_NAME, STRING_OWNER, STRING_AUTH_TYPE, token);
341 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
342 }
343
344 /**
345 * @tc.name: AppAccountManager_SetOAuthToken_0100
346 * @tc.desc: Set oauth token with invalid data.
347 * @tc.type: FUNC
348 * @tc.require: SR000GGVFU
349 */
350 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_SetOAuthToken_0100, TestSize.Level1)
351 {
352 ACCOUNT_LOGI("AppAccountManager_SetOAuthToken_0100");
353
354 ErrCode result = AppAccountManager::SetOAuthToken(STRING_NAME, STRING_AUTH_TYPE, STRING_TOKEN);
355 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
356 }
357
358 /**
359 * @tc.name: AppAccountManager_GetAllAccounts_0100
360 * @tc.desc: Get all accounts with valid data.
361 * @tc.type: FUNC
362 * @tc.require: SR000GGVFR
363 */
364 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_GetAllAccounts_0100, TestSize.Level1)
365 {
366 ACCOUNT_LOGI("AppAccountManager_GetAllAccounts_0100");
367
368 std::vector<AppAccountInfo> appAccounts;
369 ErrCode result = AppAccountManager::GetAllAccounts(STRING_NAME, appAccounts);
370 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
371 }
372
373 /**
374 * @tc.name: AppAccountManager_SubscribeAppAccount_0100
375 * @tc.desc: Subscribe app account with valid data.
376 * @tc.type: FUNC
377 * @tc.require: SR000GGVFT
378 */
379 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_SubscribeAppAccount_0100, TestSize.Level1)
380 {
381 ACCOUNT_LOGI("AppAccountManager_SubscribeAppAccount_0100");
382
383 // make owners
384 std::vector<std::string> owners;
385 owners.emplace_back(STRING_OWNER);
386
387 // make subscribe info
388 AppAccountSubscribeInfo subscribeInfo(owners);
389
390 // make a subscriber
391 auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
392
393 ErrCode result = AppAccountManager::SubscribeAppAccount(subscriberTestPtr);
394 EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
395 }
396
397 /**
398 * @tc.name: AppAccountManager_UnsubscribeAppAccount_0100
399 * @tc.desc: Unsubscribe app account with valid data.
400 * @tc.type: FUNC
401 * @tc.require: SR000GGVFT
402 */
403 HWTEST_F(AppAccountManagerModuleTest, AppAccountManager_UnsubscribeAppAccount_0100, TestSize.Level1)
404 {
405 ACCOUNT_LOGI("AppAccountManager_UnsubscribeAppAccount_0100");
406
407 // make owners
408 std::vector<std::string> owners;
409 owners.emplace_back(STRING_OWNER);
410
411 // make subscribe info
412 AppAccountSubscribeInfo subscribeInfo(owners);
413
414 // make a subscriber
415 auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
416
417 ErrCode result = AppAccountManager::UnsubscribeAppAccount(subscriberTestPtr);
418 EXPECT_EQ(result, ERR_APPACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED);
419 }
420