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 #include <thread>
18 #include "account_log_wrapper.h"
19 #define private public
20 #include "app_account.h"
21 #include "app_account_control_manager.h"
22 #include "app_account_manager_service.h"
23 #undef private
24 #include "app_account_subscriber.h"
25 #include "datetime_ex.h"
26 #include "singleton.h"
27
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::AccountSA;
31
32 namespace {
33 const std::string STRING_NAME = "app_account_subscribe_mt_name";
34 const std::string STRING_NAME_TWO = "app_account_subscribe_mt_name_two";
35 const std::string STRING_OWNER = "com.example.owner";
36 const std::string STRING_BUNDLE_NAME = "com.example.third_party";
37 const std::string STRING_BUNDLE_NAME_TWO = "com.example.third_party_two";
38 const std::string STRING_EXTRA_INFO = "extra_info";
39 std::mutex g_mtx;
40 const time_t TIME_OUT_SECONDS_LIMIT = 5;
41
42 constexpr std::int32_t UID = 10000;
43 constexpr std::size_t SIZE_ZERO = 0;
44 constexpr std::size_t SIZE_ONE = 1;
45 std::int32_t g_counter = 0;
46 constexpr std::int32_t COUNTER_MAX = 2;
47 } // namespace
48
49 class AppAccountManagerServiceSubscribeModuleTest : 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 AppAccountManagerService* appAccountManagerServicePtr_ = new AppAccountManagerService();
56 };
57
SetUpTestCase(void)58 void AppAccountManagerServiceSubscribeModuleTest::SetUpTestCase(void)
59 {
60 GTEST_LOG_(INFO) << "SetUpTestCase";
61 }
62
TearDownTestCase(void)63 void AppAccountManagerServiceSubscribeModuleTest::TearDownTestCase(void)
64 {
65 GTEST_LOG_(INFO) << "TearDownTestCase";
66 }
67
SetUp(void)68 void AppAccountManagerServiceSubscribeModuleTest::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
TearDown(void)78 void AppAccountManagerServiceSubscribeModuleTest::TearDown(void)
79 {}
80
81 class AppAccountSubscriberTest : public AppAccountSubscriber {
82 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)83 explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
84 : AppAccountSubscriber(subscribeInfo)
85 {
86 ACCOUNT_LOGI("enter");
87 }
88
~AppAccountSubscriberTest()89 ~AppAccountSubscriberTest()
90 {}
91
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)92 virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
93 {
94 ACCOUNT_LOGI("enter");
95
96 g_mtx.unlock();
97
98 std::string owner;
99 std::string name;
100 std::string extraInfo;
101
102 ACCOUNT_LOGI("accounts.size() = %{public}zu", accounts.size());
103
104 for (auto account : accounts) {
105 account.GetOwner(owner);
106 ACCOUNT_LOGI("owner = %{public}s", owner.c_str());
107
108 account.GetName(name);
109 ACCOUNT_LOGI("name = %{public}s", name.c_str());
110
111 account.GetExtraInfo(extraInfo);
112 ACCOUNT_LOGI("extraInfo = %{public}s", extraInfo.c_str());
113 }
114 }
115 };
116
117 class AppAccountSubscriberTestTwo : public AppAccountSubscriber {
118 public:
AppAccountSubscriberTestTwo(const AppAccountSubscribeInfo & subscribeInfo)119 explicit AppAccountSubscriberTestTwo(const AppAccountSubscribeInfo &subscribeInfo)
120 : AppAccountSubscriber(subscribeInfo)
121 {
122 ACCOUNT_LOGI("enter");
123 }
124
~AppAccountSubscriberTestTwo()125 ~AppAccountSubscriberTestTwo()
126 {}
127
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)128 virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
129 {
130 ACCOUNT_LOGI("enter");
131
132 g_mtx.unlock();
133
134 ACCOUNT_LOGI("accounts.size() = %{public}zu", accounts.size());
135
136 EXPECT_EQ(accounts.size(), SIZE_ZERO);
137 }
138 };
139
140 class AppAccountSubscriberTestThree : public AppAccountSubscriber {
141 public:
AppAccountSubscriberTestThree(const AppAccountSubscribeInfo & subscribeInfo)142 explicit AppAccountSubscriberTestThree(const AppAccountSubscribeInfo &subscribeInfo)
143 : AppAccountSubscriber(subscribeInfo)
144 {
145 ACCOUNT_LOGI("enter");
146 }
147
~AppAccountSubscriberTestThree()148 ~AppAccountSubscriberTestThree()
149 {}
150
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)151 virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
152 {
153 ACCOUNT_LOGI("enter");
154
155 g_mtx.unlock();
156
157 std::string owner;
158 std::string name;
159 std::string extraInfo;
160
161 ACCOUNT_LOGI("accounts.size() = %{public}zu", accounts.size());
162
163 EXPECT_EQ(accounts.size(), SIZE_ONE);
164
165 for (auto account : accounts) {
166 account.GetOwner(owner);
167 EXPECT_EQ(owner, STRING_BUNDLE_NAME);
168
169 account.GetName(name);
170 EXPECT_EQ(name, STRING_NAME);
171
172 account.GetExtraInfo(extraInfo);
173 EXPECT_EQ(extraInfo, STRING_EXTRA_INFO);
174 }
175 }
176 };
177
178 class AppAccountSubscriberTestFour : public AppAccountSubscriber {
179 public:
AppAccountSubscriberTestFour(const AppAccountSubscribeInfo & subscribeInfo)180 explicit AppAccountSubscriberTestFour(const AppAccountSubscribeInfo &subscribeInfo)
181 : AppAccountSubscriber(subscribeInfo)
182 {
183 ACCOUNT_LOGI("enter");
184 }
185
~AppAccountSubscriberTestFour()186 ~AppAccountSubscriberTestFour()
187 {}
188
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)189 virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
190 {
191 ACCOUNT_LOGI("enter");
192 GTEST_LOG_(INFO) << "AppAccountSubscriberTestFour::OnAccountsChanged()";
193
194 g_counter++;
195
196 ACCOUNT_LOGI("g_counter = %{public}d", g_counter);
197 GTEST_LOG_(INFO) << "g_counter = " << g_counter;
198
199 if (g_counter == COUNTER_MAX) {
200 g_mtx.unlock();
201 }
202
203 std::string owner;
204 std::string name;
205 std::string extraInfo;
206
207 ACCOUNT_LOGI("accounts.size() = %{public}zu", accounts.size());
208
209 EXPECT_EQ(accounts.size(), SIZE_ONE);
210
211 for (auto account : accounts) {
212 account.GetOwner(owner);
213 EXPECT_EQ(owner, STRING_OWNER);
214
215 account.GetName(name);
216 EXPECT_EQ(name, STRING_NAME);
217
218 account.GetExtraInfo(extraInfo);
219 EXPECT_EQ(extraInfo, STRING_EXTRA_INFO);
220 }
221 }
222 };
223
Wait()224 static void Wait()
225 {
226 // record start time
227 struct tm startTime = {0};
228 EXPECT_EQ(GetSystemCurrentTime(&startTime), true);
229
230 // record current time
231 struct tm doingTime = {0};
232
233 int64_t seconds = 0;
234 while (!g_mtx.try_lock()) {
235 // get current time and compare it with the start time
236 EXPECT_EQ(GetSystemCurrentTime(&doingTime), true);
237 seconds = GetSecondsBetween(startTime, doingTime);
238 if (seconds >= TIME_OUT_SECONDS_LIMIT) {
239 break;
240 }
241 }
242
243 // the subscriber should receive the event within 5 seconds
244 EXPECT_LT(seconds, TIME_OUT_SECONDS_LIMIT);
245 }
246
247 /**
248 * @tc.name: AppAccountManagerServiceSubscribe_SubscribeAppAccount_0100
249 * @tc.desc: Subscribe app accounts with valid data.
250 * @tc.type: FUNC
251 * @tc.require: SR000GGVFT
252 */
253 HWTEST_F(AppAccountManagerServiceSubscribeModuleTest, AppAccountManagerServiceSubscribe_SubscribeAppAccount_0100,
254 TestSize.Level0)
255 {
256 ACCOUNT_LOGI("AppAccountManagerServiceSubscribe_SubscribeAppAccount_0100");
257
258 // make owners
259 std::vector<std::string> owners;
260 owners.emplace_back(STRING_OWNER);
261
262 // make subscribe info
263 AppAccountSubscribeInfo subscribeInfo(owners);
264
265 // make a subscriber
266 auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
267
268 // make an event listener
269 sptr<IRemoteObject> appAccountEventListener = nullptr;
270
271 ErrCode subscribeState = AppAccount::GetInstance().CreateAppAccountEventListener(
272 subscriberTestPtr, appAccountEventListener);
273 EXPECT_EQ(subscribeState, AppAccount::INITIAL_SUBSCRIPTION);
274
275 subscribeState =
276 AppAccount::GetInstance().CreateAppAccountEventListener(nullptr, appAccountEventListener);
277 EXPECT_EQ(subscribeState, AppAccount::SUBSCRIBE_FAILED);
278
279 // subscribe app account
280 ErrCode result = appAccountManagerServicePtr_->SubscribeAppAccount(subscribeInfo, appAccountEventListener);
281 EXPECT_EQ(result, ERR_OK);
282
283 // lock the mutex
284 g_mtx.lock();
285
286 // add app account
287 result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
288 EXPECT_EQ(result, ERR_OK);
289
290 // set app account extra info
291 result = appAccountManagerServicePtr_->SetAccountExtraInfo(STRING_NAME, STRING_EXTRA_INFO);
292 EXPECT_EQ(result, ERR_OK);
293
294 Wait();
295
296 // unsubscribe app account
297 result = appAccountManagerServicePtr_->UnsubscribeAppAccount(appAccountEventListener);
298 EXPECT_EQ(result, ERR_OK);
299
300 // delete account
301 result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
302 EXPECT_EQ(result, ERR_OK);
303
304 // unlock the mutex
305 g_mtx.unlock();
306 }
307
308 /**
309 * @tc.name: AppAccountManagerServiceSubscribe_SubscribeAppAccount_0300
310 * @tc.desc: Subscribe app accounts with invalid data.
311 * @tc.type: FUNC
312 * @tc.require: SR000GGVFT
313 */
314 HWTEST_F(AppAccountManagerServiceSubscribeModuleTest, AppAccountManagerServiceSubscribe_SubscribeAppAccount_0300,
315 TestSize.Level1)
316 {
317 ACCOUNT_LOGI("AppAccountManagerServiceSubscribe_SubscribeAppAccount_0300");
318
319 // make owners
320 std::vector<std::string> owners;
321 owners.emplace_back(STRING_BUNDLE_NAME);
322
323 // make subscribe info
324 AppAccountSubscribeInfo subscribeInfo(owners);
325
326 // make a subscriber
327 auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
328
329 // make an event listener
330 sptr<IRemoteObject> appAccountEventListener = nullptr;
331
332 ErrCode subscribeState = AppAccount::GetInstance().CreateAppAccountEventListener(
333 subscriberTestPtr, appAccountEventListener);
334 EXPECT_EQ(subscribeState, AppAccount::INITIAL_SUBSCRIPTION);
335
336 // subscribe app account
337 ErrCode result = appAccountManagerServicePtr_->SubscribeAppAccount(subscribeInfo, appAccountEventListener);
338 EXPECT_EQ(result, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
339 }
340
341 /**
342 * @tc.name: AppAccountManagerServiceSubscribe_SubscribeAppAccount_0500
343 * @tc.desc: Subscribe app accounts with invalid data.
344 * @tc.type: FUNC
345 * @tc.require: SR000GGVFT
346 */
347 HWTEST_F(AppAccountManagerServiceSubscribeModuleTest, AppAccountManagerServiceSubscribe_SubscribeAppAccount_0500,
348 TestSize.Level1)
349 {
350 ACCOUNT_LOGI("AppAccountManagerServiceSubscribe_SubscribeAppAccount_0500");
351
352 // add an account
353 ErrCode result = appAccountManagerServicePtr_->innerManager_->AddAccount(STRING_NAME,
354 STRING_EXTRA_INFO, UID, STRING_BUNDLE_NAME, 0);
355 EXPECT_EQ(result, ERR_OK);
356
357 // enable app access
358 AppAccountCallingInfo appAccountCallingInfo;
359 appAccountCallingInfo.callingUid = UID;
360 appAccountCallingInfo.bundleName = STRING_BUNDLE_NAME;
361 result = appAccountManagerServicePtr_->innerManager_->EnableAppAccess(STRING_NAME,
362 STRING_OWNER, appAccountCallingInfo);
363 EXPECT_EQ(result, ERR_OK);
364
365 // make owners
366 std::vector<std::string> owners;
367 owners.emplace_back(STRING_BUNDLE_NAME);
368
369 // make subscribe info
370 AppAccountSubscribeInfo subscribeInfo(owners);
371
372 // make a subscriber
373 auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTestTwo>(subscribeInfo);
374
375 // make an event listener
376 sptr<IRemoteObject> appAccountEventListener = nullptr;
377
378 ErrCode subscribeState = AppAccount::GetInstance().CreateAppAccountEventListener(
379 subscriberTestPtr, appAccountEventListener);
380 EXPECT_EQ(subscribeState, AppAccount::INITIAL_SUBSCRIPTION);
381
382 // subscribe app account
383 result = appAccountManagerServicePtr_->SubscribeAppAccount(subscribeInfo, appAccountEventListener);
384 EXPECT_EQ(result, ERR_OK);
385
386 // lock the mutex
387 g_mtx.lock();
388
389 // disable app access
390 appAccountCallingInfo.callingUid = UID;
391 appAccountCallingInfo.bundleName = STRING_BUNDLE_NAME;
392 result = appAccountManagerServicePtr_->innerManager_->DisableAppAccess(STRING_NAME,
393 STRING_OWNER, appAccountCallingInfo);
394 EXPECT_EQ(result, ERR_OK);
395
396 // set extra info
397 result = appAccountManagerServicePtr_->innerManager_->SetAccountExtraInfo(STRING_NAME,
398 STRING_EXTRA_INFO, UID, STRING_BUNDLE_NAME, 0);
399 EXPECT_EQ(result, ERR_OK);
400
401 Wait();
402
403 // unsubscribe app account
404 result = appAccountManagerServicePtr_->UnsubscribeAppAccount(appAccountEventListener);
405 EXPECT_EQ(result, ERR_OK);
406
407 // delete account
408 result = appAccountManagerServicePtr_->innerManager_->DeleteAccount(STRING_NAME, UID, STRING_BUNDLE_NAME, 0);
409 EXPECT_EQ(result, ERR_OK);
410
411 // unlock the mutex
412 g_mtx.unlock();
413 }
414
415 /**
416 * @tc.name: AppAccountManagerServiceSubscribe_SubscribeAppAccount_0600
417 * @tc.desc: Subscribe app accounts with valid data.
418 * @tc.type: FUNC
419 * @tc.require: SR000GGVFT
420 */
421 HWTEST_F(AppAccountManagerServiceSubscribeModuleTest, AppAccountManagerServiceSubscribe_SubscribeAppAccount_0600,
422 TestSize.Level1)
423 {
424 ACCOUNT_LOGI("AppAccountManagerServiceSubscribe_SubscribeAppAccount_0600");
425 auto accountInnerManager = appAccountManagerServicePtr_->innerManager_;
426 // add an account
427 ErrCode result = accountInnerManager->AddAccount(STRING_NAME, STRING_EXTRA_INFO, UID, STRING_BUNDLE_NAME, 0);
428 EXPECT_EQ(result, ERR_OK);
429 EXPECT_EQ(accountInnerManager->AddAccount(STRING_NAME_TWO, STRING_EXTRA_INFO, UID, STRING_BUNDLE_NAME, 0), ERR_OK);
430
431 // enable app access
432 AppAccountCallingInfo appAccountCallingInfo;
433 appAccountCallingInfo.callingUid = UID;
434 appAccountCallingInfo.bundleName = STRING_BUNDLE_NAME;
435 result = accountInnerManager->EnableAppAccess(STRING_NAME, STRING_OWNER, appAccountCallingInfo);
436 EXPECT_EQ(result, ERR_OK);
437
438 // make owners
439 std::vector<std::string> owners;
440 owners.emplace_back(STRING_BUNDLE_NAME);
441
442 // make subscribe info
443 AppAccountSubscribeInfo subscribeInfo(owners);
444
445 // make a subscriber
446 auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTestTwo>(subscribeInfo);
447
448 // make an event listener
449 sptr<IRemoteObject> appAccountEventListener = nullptr;
450
451 ErrCode subscribeState = AppAccount::GetInstance().CreateAppAccountEventListener(
452 subscriberTestPtr, appAccountEventListener);
453 EXPECT_EQ(subscribeState, AppAccount::INITIAL_SUBSCRIPTION);
454
455 // subscribe app account
456 result = appAccountManagerServicePtr_->SubscribeAppAccount(subscribeInfo, appAccountEventListener);
457 EXPECT_EQ(result, ERR_OK);
458
459 // lock the mutex
460 g_mtx.lock();
461
462 // disable app access
463 appAccountCallingInfo.callingUid = UID;
464 appAccountCallingInfo.bundleName = STRING_BUNDLE_NAME;
465 result = accountInnerManager->DisableAppAccess(STRING_NAME,
466 STRING_OWNER, appAccountCallingInfo);
467 EXPECT_EQ(result, ERR_OK);
468
469 // set extra info
470 result = accountInnerManager->SetAccountExtraInfo(STRING_NAME,
471 STRING_EXTRA_INFO, UID, STRING_BUNDLE_NAME, 0);
472 EXPECT_EQ(result, ERR_OK);
473
474 Wait();
475
476 // unsubscribe app account
477 result = appAccountManagerServicePtr_->UnsubscribeAppAccount(appAccountEventListener);
478 EXPECT_EQ(result, ERR_OK);
479
480 // delete account
481 EXPECT_EQ(accountInnerManager->DeleteAccount(STRING_NAME, UID, STRING_BUNDLE_NAME, 0), ERR_OK);
482 EXPECT_EQ(accountInnerManager->DeleteAccount(STRING_NAME_TWO, UID, STRING_BUNDLE_NAME, 0), ERR_OK);
483
484 // unlock the mutex
485 g_mtx.unlock();
486 }
487
488 /**
489 * @tc.name: AppAccountManagerServiceSubscribe_SubscribeAppAccount_0700
490 * @tc.desc: Subscribe app accounts with valid data.
491 * @tc.type: FUNC
492 * @tc.require: SR000GGVFT
493 */
494 HWTEST_F(AppAccountManagerServiceSubscribeModuleTest, AppAccountManagerServiceSubscribe_SubscribeAppAccount_0700,
495 TestSize.Level1)
496 {
497 ACCOUNT_LOGI("AppAccountManagerServiceSubscribe_SubscribeAppAccount_0700");
498
499 // add an account
500 ErrCode result = appAccountManagerServicePtr_->innerManager_->AddAccount(STRING_NAME,
501 STRING_EXTRA_INFO, UID, STRING_BUNDLE_NAME, 0);
502 EXPECT_EQ(result, ERR_OK);
503 result = appAccountManagerServicePtr_->innerManager_->AddAccount(STRING_NAME_TWO,
504 STRING_EXTRA_INFO, UID, STRING_BUNDLE_NAME, 0);
505 EXPECT_EQ(result, ERR_OK);
506
507 // enable app access
508 AppAccountCallingInfo appAccountCallingInfo;
509 appAccountCallingInfo.callingUid = UID;
510 appAccountCallingInfo.bundleName = STRING_BUNDLE_NAME;
511 result = appAccountManagerServicePtr_->innerManager_->EnableAppAccess(STRING_NAME,
512 STRING_OWNER, appAccountCallingInfo);
513 EXPECT_EQ(result, ERR_OK);
514
515 // make owners
516 std::vector<std::string> owners;
517 owners.emplace_back(STRING_BUNDLE_NAME);
518
519 // make subscribe info
520 AppAccountSubscribeInfo subscribeInfo(owners);
521
522 // make a subscriber
523 auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTestThree>(subscribeInfo);
524
525 // make an event listener
526 sptr<IRemoteObject> appAccountEventListener = nullptr;
527
528 ErrCode subscribeState = AppAccount::GetInstance().CreateAppAccountEventListener(
529 subscriberTestPtr, appAccountEventListener);
530 EXPECT_EQ(subscribeState, AppAccount::INITIAL_SUBSCRIPTION);
531
532 // subscribe app account
533 result = appAccountManagerServicePtr_->SubscribeAppAccount(subscribeInfo, appAccountEventListener);
534 EXPECT_EQ(result, ERR_OK);
535
536 // lock the mutex
537 g_mtx.lock();
538
539 // set extra info
540 result = appAccountManagerServicePtr_->innerManager_->SetAccountExtraInfo(STRING_NAME,
541 STRING_EXTRA_INFO, UID, STRING_BUNDLE_NAME, 0);
542 EXPECT_EQ(result, ERR_OK);
543
544 Wait();
545
546 // unsubscribe app account
547 result = appAccountManagerServicePtr_->UnsubscribeAppAccount(appAccountEventListener);
548 EXPECT_EQ(result, ERR_OK);
549
550 // delete account
551 result = appAccountManagerServicePtr_->innerManager_->DeleteAccount(STRING_NAME, UID, STRING_BUNDLE_NAME, 0);
552 EXPECT_EQ(result, ERR_OK);
553 result = appAccountManagerServicePtr_->innerManager_->DeleteAccount(STRING_NAME_TWO, UID, STRING_BUNDLE_NAME, 0);
554 EXPECT_EQ(result, ERR_OK);
555
556 // unlock the mutex
557 g_mtx.unlock();
558 }
559
560 /**
561 * @tc.name: AppAccountManagerServiceSubscribe_SubscribeAppAccount_0800
562 * @tc.desc: Subscribe app accounts with valid data.
563 * @tc.type: FUNC
564 * @tc.require: SR000GGVFT
565 */
566 HWTEST_F(AppAccountManagerServiceSubscribeModuleTest, AppAccountManagerServiceSubscribe_SubscribeAppAccount_0800,
567 TestSize.Level1)
568 {
569 ACCOUNT_LOGI("AppAccountManagerServiceSubscribe_SubscribeAppAccount_0800");
570
571 // add app account
572 ErrCode result = appAccountManagerServicePtr_->AddAccount(STRING_NAME, STRING_EXTRA_INFO);
573 EXPECT_EQ(result, ERR_OK);
574
575 // make owners
576 std::vector<std::string> owners;
577 owners.emplace_back(STRING_OWNER);
578
579 // make subscribe info
580 AppAccountSubscribeInfo subscribeInfo(owners);
581
582 // make a subscriber
583 auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTestFour>(subscribeInfo);
584
585 // make an event listener
586 sptr<IRemoteObject> appAccountEventListener = nullptr;
587
588 ErrCode subscribeState = AppAccount::GetInstance().CreateAppAccountEventListener(
589 subscriberTestPtr, appAccountEventListener);
590 EXPECT_EQ(subscribeState, AppAccount::INITIAL_SUBSCRIPTION);
591
592 // subscribe app account
593 result = appAccountManagerServicePtr_->SubscribeAppAccount(subscribeInfo, appAccountEventListener);
594 EXPECT_EQ(result, ERR_OK);
595
596 // make a subscriber
597 auto subscriberTestPtrTwo = std::make_shared<AppAccountSubscriberTestFour>(subscribeInfo);
598
599 // make an event listener
600 sptr<IRemoteObject> appAccountEventListenerTwo = nullptr;
601
602 subscribeState = AppAccount::GetInstance().CreateAppAccountEventListener(
603 subscriberTestPtrTwo, appAccountEventListenerTwo);
604 EXPECT_EQ(subscribeState, AppAccount::INITIAL_SUBSCRIPTION);
605
606 // subscribe app account
607 result = appAccountManagerServicePtr_->SubscribeAppAccount(subscribeInfo, appAccountEventListenerTwo);
608 EXPECT_EQ(result, ERR_OK);
609
610 // lock the mutex
611 g_mtx.lock();
612
613 // set extra info
614 result = appAccountManagerServicePtr_->SetAccountExtraInfo(STRING_NAME, STRING_EXTRA_INFO);
615 EXPECT_EQ(result, ERR_OK);
616
617 Wait();
618
619 // unsubscribe app account
620 result = appAccountManagerServicePtr_->UnsubscribeAppAccount(appAccountEventListener);
621 EXPECT_EQ(result, ERR_OK);
622
623 // unsubscribe app account
624 result = appAccountManagerServicePtr_->UnsubscribeAppAccount(appAccountEventListenerTwo);
625 EXPECT_EQ(result, ERR_OK);
626
627 // delete app account
628 result = appAccountManagerServicePtr_->DeleteAccount(STRING_NAME);
629 EXPECT_EQ(result, ERR_OK);
630
631 // unlock the mutex
632 g_mtx.unlock();
633 }
634