1 /*
2  * Copyright (c) 2021 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_error_no.h"
19 #include "account_log_wrapper.h"
20 #define private public
21 #include "app_account.h"
22 #include "app_account_authenticator_callback.h"
23 #include "app_account_authenticator_callback_stub.h"
24 #include "app_account_authenticator_stub.h"
25 #include "app_account_constants.h"
26 #undef private
27 #include "mock_app_account_stub.h"
28 #include "iremote_object.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS;
32 using namespace OHOS::AccountSA;
33 
34 namespace {
35 const std::string STRING_EMPTY = "";
36 const std::string STRING_NAME = "name";
37 const std::string STRING_NAME_CONTAINS_SPECIAL_CHARACTERS = " name";
38 const std::string STRING_NAME_CONTAINS_SPECIAL_CHARACTERS_TWO = "n ame";
39 const std::string STRING_NAME_CONTAINS_SPECIAL_CHARACTERS_THREE = "name ";
40 const std::string STRING_NAME_OUT_OF_RANGE(513, '1');  // length 513
41 const std::string STRING_EXTRA_INFO = "extra_info";
42 const std::string STRING_EXTRA_INFO_OUT_OF_RANGE(1200, '1');  // length 1200
43 const std::string STRING_NAME_EMPTY = STRING_EMPTY;
44 const std::string STRING_EXTRA_INFO_EMPTY = STRING_EMPTY;
45 const std::string STRING_OWNER = "com.example.owner";
46 
47 constexpr std::size_t SUBSCRIBER_ZERO = 0;
48 constexpr std::size_t SUBSCRIBER_ONE = 1;
49 const uint32_t INVALID_IPC_CODE = 1000;
50 const int32_t MAX_CUSTOM_DATA_SIZE = 1300;
51 }  // namespace
52 
53 class AppAccountTest : public testing::Test {
54 public:
55     static void SetUpTestCase(void);
56     static void TearDownTestCase(void);
57     void SetUp(void) override;
58     void TearDown(void) override;
59 
60     sptr<IRemoteObject> MakeMockObjects(void) const;
61 };
62 
MakeMockObjects(void) const63 sptr<IRemoteObject> AppAccountTest::MakeMockObjects(void) const
64 {
65     // mock a stub
66     auto mockStub = sptr<IRemoteObject>(new (std::nothrow) MockAppAccountStub());
67 
68     return mockStub;
69 }
70 
SetUpTestCase(void)71 void AppAccountTest::SetUpTestCase(void)
72 {}
73 
TearDownTestCase(void)74 void AppAccountTest::TearDownTestCase(void)
75 {}
76 
SetUp(void)77 void AppAccountTest::SetUp(void) __attribute__((no_sanitize("cfi")))
78 {
79     testing::UnitTest *test = testing::UnitTest::GetInstance();
80     ASSERT_NE(test, nullptr);
81     const testing::TestInfo *testinfo = test->current_test_info();
82     ASSERT_NE(testinfo, nullptr);
83     string testCaseName = string(testinfo->name());
84     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
85 
86     // mock a proxy
87     auto mockProxy = iface_cast<IAppAccount>(MakeMockObjects());
88 
89     // add the mock proxy
90     AppAccount::GetInstance().proxy_ = mockProxy;
91 }
92 
TearDown(void)93 void AppAccountTest::TearDown(void)
94 {
95     AppAccount::GetInstance().eventListeners_.clear();
96 }
97 
98 class AppAccountSubscriberTest : public AppAccountSubscriber {
99 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)100     explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
101         : AppAccountSubscriber(subscribeInfo)
102     {}
103 
~AppAccountSubscriberTest()104     ~AppAccountSubscriberTest()
105     {}
106 
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)107     void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
108     {}
109 };
110 
111 /**
112  * @tc.name: AppAccountAuthenticatorCallbackStub_OnRemoteRequest_0100
113  * @tc.desc: OnRemoteRequest with wrong message code.
114  * @tc.type: FUNC
115  * @tc.require:
116  */
117 HWTEST_F(AppAccountTest, AppAccountAuthenticatorCallbackStub_OnRemoteRequest_0100, TestSize.Level0)
118 {
119     MessageParcel data;
120     MessageParcel reply;
121     MessageOption option = {MessageOption::TF_SYNC};
122     data.WriteInterfaceToken(AppAccountAuthenticatorCallbackStub::GetDescriptor());
123 
124     std::string sessionId = "sessionId";
125     sptr<AppAccountAuthenticatorCallbackStub> stub = new (std::nothrow) AppAccountAuthenticatorCallback(sessionId);
126     ASSERT_NE(nullptr, stub);
127     int32_t ret = stub->OnRemoteRequest(INVALID_IPC_CODE, data, reply, option);
128     EXPECT_EQ(IPC_STUB_UNKNOW_TRANS_ERR, ret);
129 }
130 
131 /**
132  * @tc.name: AppAccount_AddAccount_0100
133  * @tc.desc: Add an app account with valid data.
134  * @tc.type: FUNC
135  * @tc.require: SR000GGV11
136  */
137 HWTEST_F(AppAccountTest, AppAccount_AddAccount_0100, TestSize.Level0)
138 {
139     ACCOUNT_LOGI("AppAccount_AddAccount_0100");
140 
141     ErrCode result = AppAccount::GetInstance().AddAccount(STRING_NAME, STRING_EXTRA_INFO);
142 
143     EXPECT_EQ(result, ERR_OK);
144 }
145 
146 /**
147  * @tc.name: AppAccount_AddAccount_0200
148  * @tc.desc: Add an app account with invalid data.
149  * @tc.type: FUNC
150  * @tc.require: SR000GGV11
151  */
152 HWTEST_F(AppAccountTest, AppAccount_AddAccount_0200, TestSize.Level1)
153 {
154     ACCOUNT_LOGI("AppAccount_AddAccount_0200");
155 
156     ErrCode result = AppAccount::GetInstance().AddAccount(STRING_NAME_EMPTY, STRING_EXTRA_INFO);
157 
158     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
159 }
160 
161 /**
162  * @tc.name: AppAccount_AddAccount_0300
163  * @tc.desc: Add an app account with valid data.
164  * @tc.type: FUNC
165  * @tc.require: SR000GGV11
166  */
167 HWTEST_F(AppAccountTest, AppAccount_AddAccount_0300, TestSize.Level1)
168 {
169     ACCOUNT_LOGI("AppAccount_AddAccount_0300");
170 
171     ErrCode result = AppAccount::GetInstance().AddAccount(STRING_NAME, STRING_EXTRA_INFO_EMPTY);
172 
173     EXPECT_EQ(result, ERR_OK);
174 }
175 
176 /**
177  * @tc.name: AppAccount_AddAccount_0400
178  * @tc.desc: Add an app account with invalid data.
179  * @tc.type: FUNC
180  * @tc.require: SR000GGV11
181  */
182 HWTEST_F(AppAccountTest, AppAccount_AddAccount_0400, TestSize.Level1)
183 {
184     ACCOUNT_LOGI("AppAccount_AddAccount_0400");
185 
186     ErrCode result = AppAccount::GetInstance().AddAccount(STRING_NAME_OUT_OF_RANGE, STRING_EXTRA_INFO);
187 
188     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
189 }
190 
191 /**
192  * @tc.name: AppAccount_AddAccount_0500
193  * @tc.desc: Add an app account with invalid data.
194  * @tc.type: FUNC
195  * @tc.require: SR000GGV11
196  */
197 HWTEST_F(AppAccountTest, AppAccount_AddAccount_0500, TestSize.Level1)
198 {
199     ACCOUNT_LOGI("AppAccount_AddAccount_0500");
200 
201     ErrCode result = AppAccount::GetInstance().AddAccount(STRING_NAME_CONTAINS_SPECIAL_CHARACTERS, STRING_EXTRA_INFO);
202 
203     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
204 }
205 
206 /**
207  * @tc.name: AppAccount_AddAccount_0600
208  * @tc.desc: Add an app account with invalid data.
209  * @tc.type: FUNC
210  * @tc.require: SR000GGV11
211  */
212 HWTEST_F(AppAccountTest, AppAccount_AddAccount_0600, TestSize.Level1)
213 {
214     ACCOUNT_LOGI("AppAccount_AddAccount_0600");
215 
216     ErrCode result = AppAccount::GetInstance().AddAccount(
217         STRING_NAME_CONTAINS_SPECIAL_CHARACTERS_TWO, STRING_EXTRA_INFO);
218     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
219 }
220 
221 /**
222  * @tc.name: AppAccount_AddAccount_0700
223  * @tc.desc: Add an app account with invalid data.
224  * @tc.type: FUNC
225  * @tc.require: SR000GGV11
226  */
227 HWTEST_F(AppAccountTest, AppAccount_AddAccount_0700, TestSize.Level1)
228 {
229     ACCOUNT_LOGI("AppAccount_AddAccount_0700");
230 
231     ErrCode result = AppAccount::GetInstance().AddAccount(
232         STRING_NAME_CONTAINS_SPECIAL_CHARACTERS_THREE, STRING_EXTRA_INFO);
233     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
234 }
235 
236 /**
237  * @tc.name: AppAccount_AddAccount_0800
238  * @tc.desc: Add an app account with invalid data.
239  * @tc.type: FUNC
240  * @tc.require: SR000GGV11
241  */
242 HWTEST_F(AppAccountTest, AppAccount_AddAccount_0800, TestSize.Level1)
243 {
244     ACCOUNT_LOGI("AppAccount_AddAccount_0800");
245 
246     ErrCode result = AppAccount::GetInstance().AddAccount(STRING_NAME, STRING_EXTRA_INFO_OUT_OF_RANGE);
247 
248     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
249 }
250 
251 /**
252  * @tc.name: AppAccount_DeleteAccount_0100
253  * @tc.desc: Delete an app account with valid data.
254  * @tc.type: FUNC
255  * @tc.require: SR000GGV11
256  */
257 HWTEST_F(AppAccountTest, AppAccount_DeleteAccount_0100, TestSize.Level1)
258 {
259     ACCOUNT_LOGI("AppAccount_DeleteAccount_0100");
260 
261     ErrCode result = AppAccount::GetInstance().DeleteAccount(STRING_NAME);
262 
263     EXPECT_EQ(result, ERR_OK);
264 }
265 
266 /**
267  * @tc.name: AppAccount_DeleteAccount_0200
268  * @tc.desc: Delete an app account with invalid data.
269  * @tc.type: FUNC
270  * @tc.require: SR000GGV11
271  */
272 HWTEST_F(AppAccountTest, AppAccount_DeleteAccount_0200, TestSize.Level1)
273 {
274     ACCOUNT_LOGI("AppAccount_DeleteAccount_0200");
275 
276     ErrCode result = AppAccount::GetInstance().DeleteAccount(STRING_NAME_EMPTY);
277 
278     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
279 }
280 
281 /**
282  * @tc.name: AppAccount_DeleteAccount_0300
283  * @tc.desc: Delete an app account with invalid data.
284  * @tc.type: FUNC
285  * @tc.require: SR000GGV11
286  */
287 HWTEST_F(AppAccountTest, AppAccount_DeleteAccount_0300, TestSize.Level1)
288 {
289     ACCOUNT_LOGI("AppAccount_DeleteAccount_0300");
290 
291     ErrCode result = AppAccount::GetInstance().DeleteAccount(STRING_NAME_OUT_OF_RANGE);
292 
293     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
294 }
295 
296 /**
297  * @tc.name: AppAccount_SubscribeAppAccount_0100
298  * @tc.desc: Subscribe app accounts with valid data.
299  * @tc.type: FUNC
300  * @tc.require: SR000GGVFT
301  */
302 HWTEST_F(AppAccountTest, AppAccount_SubscribeAppAccount_0100, TestSize.Level0)
303 {
304     ACCOUNT_LOGI("AppAccount_SubscribeAppAccount_0100");
305 
306     // make owners
307     std::vector<std::string> owners;
308     owners.emplace_back(STRING_OWNER);
309 
310     // make subscribe info
311     AppAccountSubscribeInfo subscribeInfo;
312     subscribeInfo.SetOwners(owners);
313 
314     // make a subscriber
315     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
316     // subscribe app account
317     ErrCode result = AppAccount::GetInstance().SubscribeAppAccount(subscriberTestPtr);
318 
319     EXPECT_EQ(result, ERR_OK);
320 }
321 
322 /**
323  * @tc.name: AppAccount_SubscribeAppAccount_0200
324  * @tc.desc: Subscribe app accounts with valid data.
325  * @tc.type: FUNC
326  * @tc.require: SR000GGVFT
327  */
328 HWTEST_F(AppAccountTest, AppAccount_SubscribeAppAccount_0200, TestSize.Level1)
329 {
330     ACCOUNT_LOGI("AppAccount_SubscribeAppAccount_0200");
331 
332     // make owners
333     std::vector<std::string> owners;
334     owners.emplace_back(STRING_OWNER);
335     owners.emplace_back(STRING_OWNER);
336 
337     // make subscribe info
338     AppAccountSubscribeInfo subscribeInfo;
339     subscribeInfo.SetOwners(owners);
340 
341     // make a subscriber
342     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
343     // subscribe app account
344     ErrCode result = AppAccount::GetInstance().SubscribeAppAccount(subscriberTestPtr);
345 
346     EXPECT_EQ(result, ERR_OK);
347 }
348 
349 /**
350  * @tc.name: AppAccount_SubscribeAppAccount_0300
351  * @tc.desc: Subscribe app accounts with invalid data.
352  * @tc.type: FUNC
353  * @tc.require: SR000GGVFT
354  */
355 HWTEST_F(AppAccountTest, AppAccount_SubscribeAppAccount_0300, TestSize.Level1)
356 {
357     ACCOUNT_LOGI("AppAccount_SubscribeAppAccount_0300");
358 
359     // subscribe app account with nullptr
360     ErrCode result = AppAccount::GetInstance().SubscribeAppAccount(nullptr);
361 
362     EXPECT_EQ(result, ERR_APPACCOUNT_KIT_SUBSCRIBER_IS_NULLPTR);
363 }
364 
365 /**
366  * @tc.name: AppAccount_GetAppAccountProxy_0100
367  * @tc.desc: Get app account proxy.
368  * @tc.type: FUNC
369  * @tc.require: SR000GGVFQ
370  */
371 HWTEST_F(AppAccountTest, AppAccount_GetAppAccountProxy_0100, TestSize.Level1)
372 {
373     ACCOUNT_LOGI("AppAccount_GetAppAccountProxy_0100");
374 
375     // get app account proxy
376     auto proxy = AppAccount::GetInstance().GetAppAccountProxy();
377 
378     EXPECT_NE(proxy, nullptr);
379     EXPECT_NE(AppAccount::GetInstance().proxy_, nullptr);
380 }
381 
382 /**
383  * @tc.name: AppAccount_ResetAppAccountProxy_0100
384  * @tc.desc: Reset app account proxy.
385  * @tc.type: FUNC
386  * @tc.require: SR000GGVFQ
387  */
388 HWTEST_F(AppAccountTest, AppAccount_ResetAppAccountProxy_0100, TestSize.Level1)
389 {
390     ACCOUNT_LOGI("AppAccount_ResetAppAccountProxy_0100");
391 
392     // get app account proxy
393     sptr<IAppAccount> proxy = AppAccount::GetInstance().GetAppAccountProxy();
394     EXPECT_NE(AppAccount::GetInstance().proxy_, nullptr);
395     EXPECT_NE(proxy, nullptr);
396     EXPECT_NE(AppAccount::GetInstance().proxy_, nullptr);
397 
398     // reset app account proxy
399     ErrCode result = AppAccount::GetInstance().ResetAppAccountProxy();
400 
401     EXPECT_EQ(result, ERR_OK);
402     EXPECT_EQ(AppAccount::GetInstance().proxy_, nullptr);
403 }
404 
405 /**
406  * @tc.name: AppAccount_CreateAppAccountEventListener_0100
407  * @tc.desc: create app account event listener.
408  * @tc.type: FUNC
409  * @tc.require: SR000GGVFT
410  */
411 HWTEST_F(AppAccountTest, AppAccount_CreateAppAccountEventListener_0100, TestSize.Level1)
412 {
413     ACCOUNT_LOGI("AppAccount_CreateAppAccountEventListener_0100");
414 
415     ErrCode result = -1;
416 
417     // make owners
418     std::vector<std::string> owners;
419     owners.emplace_back(STRING_OWNER);
420 
421     // make subscribe info
422     AppAccountSubscribeInfo subscribeInfo;
423     result = subscribeInfo.SetOwners(owners);
424 
425     EXPECT_EQ(result, ERR_OK);
426 
427     // make a subscriber
428     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
429     sptr<IRemoteObject> appAccountEventListener = nullptr;
430 
431     EXPECT_EQ(AppAccount::GetInstance().eventListeners_.size(), SUBSCRIBER_ZERO);
432 
433     // initial subscription
434     result = AppAccount::GetInstance().CreateAppAccountEventListener(subscriberTestPtr, appAccountEventListener);
435     EXPECT_EQ(result, AppAccount::SubscribeState::INITIAL_SUBSCRIPTION);
436     EXPECT_EQ(AppAccount::GetInstance().eventListeners_.size(), SUBSCRIBER_ONE);
437 
438     // already subscribed
439     result = AppAccount::GetInstance().CreateAppAccountEventListener(subscriberTestPtr, appAccountEventListener);
440     EXPECT_EQ(result, AppAccount::SubscribeState::ALREADY_SUBSCRIBED);
441     EXPECT_EQ(AppAccount::GetInstance().eventListeners_.size(), SUBSCRIBER_ONE);
442 }
443 
444 /**
445  * @tc.name: AppAccount_CreateAppAccountEventListener_0200
446  * @tc.desc: create app account event listener.
447  * @tc.type: FUNC
448  * @tc.require: SR000GGVFT
449  */
450 HWTEST_F(AppAccountTest, AppAccount_CreateAppAccountEventListener_0200, TestSize.Level1)
451 {
452     ACCOUNT_LOGI("AppAccount_CreateAppAccountEventListener_0200");
453 
454     ErrCode result = -1;
455 
456     // make owners
457     std::vector<std::string> owners;
458     owners.emplace_back(STRING_OWNER);
459 
460     // make subscribe info
461     AppAccountSubscribeInfo subscribeInfo;
462     result = subscribeInfo.SetOwners(owners);
463 
464     EXPECT_EQ(result, ERR_OK);
465 
466     EXPECT_EQ(AppAccount::GetInstance().eventListeners_.size(), SUBSCRIBER_ZERO);
467 
468     // make max subscribers
469     for (std::size_t counter = 1; counter <= Constants::APP_ACCOUNT_SUBSCRIBER_MAX_SIZE + 1; counter += 1) {
470         auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
471         sptr<IRemoteObject> appAccountEventListener = nullptr;
472 
473         result = AppAccount::GetInstance().CreateAppAccountEventListener(subscriberTestPtr, appAccountEventListener);
474         if (counter <= Constants::APP_ACCOUNT_SUBSCRIBER_MAX_SIZE) {
475             EXPECT_EQ(result, AppAccount::SubscribeState::INITIAL_SUBSCRIPTION);
476             EXPECT_EQ(AppAccount::GetInstance().eventListeners_.size(), counter);
477         } else {
478             EXPECT_EQ(result, AppAccount::SubscribeState::SUBSCRIBE_FAILED);
479             EXPECT_EQ(AppAccount::GetInstance().eventListeners_.size(), counter - 1);
480         }
481     }
482 }
483 
484 /**
485  * @tc.name: AppAccount_CreateAccount_001
486  * @tc.desc: Function CreateAccount normal branch.
487  * @tc.type: FUNC
488  * @tc.require:
489  */
490 HWTEST_F(AppAccountTest, AppAccount_CreateAccount_001, TestSize.Level1)
491 {
492     ACCOUNT_LOGI("AppAccount_CreateAccount_001");
493 
494     CreateAccountOptions option;
495     ErrCode result = AppAccount::GetInstance().CreateAccount("test", option);
496     EXPECT_EQ(result, ERR_OK);
497 
498     result = AppAccount::GetInstance().DeleteAccount("test");
499     EXPECT_EQ(result, ERR_OK);
500 }
501 
502 /**
503  * @tc.name: AppAccount_CreateAccount_002
504  * @tc.desc: Function CreateAccount abnormal branch.
505  * @tc.type: FUNC
506  * @tc.require:
507  */
508 HWTEST_F(AppAccountTest, AppAccount_CreateAccount_002, TestSize.Level1)
509 {
510     ACCOUNT_LOGI("AppAccount_CreateAccount_002");
511 
512     CreateAccountOptions option;
513 
514     ErrCode result = AppAccount::GetInstance().CreateAccount("", option);
515 
516     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
517 
518     result = AppAccount::GetInstance().CreateAccount(STRING_NAME_OUT_OF_RANGE, option);
519 
520     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
521 }
522 
523 /**
524  * @tc.name: AppAccount_CreateAccount_003
525  * @tc.desc: Function CreateAccount customData is oversize.
526  * @tc.type: FUNC
527  * @tc.require:
528  */
529 HWTEST_F(AppAccountTest, AppAccount_CreateAccount_003, TestSize.Level1)
530 {
531     ACCOUNT_LOGI("AppAccount_CreateAccount_002");
532 
533     CreateAccountOptions option;
534     for (int i = 0; i <= MAX_CUSTOM_DATA_SIZE; i++) {
535         std::string key = std::to_string(i);
536         std::string value = key;
537         option.customData[key] = value;
538     }
539     ErrCode result = AppAccount::GetInstance().CreateAccount("test", option);
540     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
541 }
542