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 #define private public
19 #include "app_account_subscribe_manager.h"
20 #undef private
21 
22 #include "account_log_wrapper.h"
23 #include "app_account_manager.h"
24 #include "app_account_subscriber.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::AccountSA;
29 namespace {
30 const uid_t UID = 1;
31 const uint32_t APP_INDEX = 1;
32 const std::string BUNDLE_NAME = "testname";
33 const std::string STRING_OWNER = "com.example.owner";
34 const std::string STRING_OWNER_OUT_OF_RANGE(1200, '1');  // length 1200
35 }  // namespace
36 
37 class AppAccountManagerSubscribeTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp(void) override;
42     void TearDown(void) override;
43 
44     std::shared_ptr<AppAccountSubscribeManager> appAccountSubscribeManagerPtr =
45         std::make_shared<AppAccountSubscribeManager>();
46 };
47 
SetUpTestCase(void)48 void AppAccountManagerSubscribeTest::SetUpTestCase(void)
49 {}
50 
TearDownTestCase(void)51 void AppAccountManagerSubscribeTest::TearDownTestCase(void)
52 {}
53 
SetUp(void)54 void AppAccountManagerSubscribeTest::SetUp(void) __attribute__((no_sanitize("cfi")))
55 {
56     testing::UnitTest *test = testing::UnitTest::GetInstance();
57     ASSERT_NE(test, nullptr);
58     const testing::TestInfo *testinfo = test->current_test_info();
59     ASSERT_NE(testinfo, nullptr);
60     string testCaseName = string(testinfo->name());
61     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
62 }
63 
TearDown(void)64 void AppAccountManagerSubscribeTest::TearDown(void)
65 {}
66 
67 class AppAccountSubscriberTest : public AppAccountSubscriber {
68 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)69     explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
70         : AppAccountSubscriber(subscribeInfo)
71     {
72         ACCOUNT_LOGI("enter");
73     }
74 
~AppAccountSubscriberTest()75     ~AppAccountSubscriberTest()
76     {}
77 
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)78     virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
79     {
80         ACCOUNT_LOGI("enter");
81     }
82 };
83 
84 /**
85  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0100
86  * @tc.desc: Subscribe app accounts with invalid data.
87  * @tc.type: FUNC
88  * @tc.require: SR000GGVFT
89  */
90 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0100, TestSize.Level1)
91 {
92     ACCOUNT_LOGI("AppAccountManagerSubscribe_SubscribeAppAccount_0100");
93 
94     // make owners
95     std::vector<std::string> owners;
96     owners.emplace_back(STRING_OWNER);
97 
98     // make subscribe info
99     AppAccountSubscribeInfo subscribeInfo(owners);
100 
101     // make a subscriber
102     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
103 
104     // unsubscribe app account
105     ErrCode result = AppAccountManager::UnsubscribeAppAccount(subscriberTestPtr);
106     EXPECT_EQ(result, ERR_APPACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED);
107 }
108 
109 /**
110  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0200
111  * @tc.desc: Subscribe app accounts with invalid data.
112  * @tc.type: FUNC
113  * @tc.require: SR000GGVFT
114  */
115 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0200, TestSize.Level1)
116 {
117     ACCOUNT_LOGI("AppAccountManagerSubscribe_SubscribeAppAccount_0200");
118 
119     // make owners
120     std::vector<std::string> owners;
121 
122     // make subscribe info
123     AppAccountSubscribeInfo subscribeInfo(owners);
124 
125     // make a subscriber
126     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
127 
128     // unsubscribe app account
129     ErrCode result = AppAccountManager::SubscribeAppAccount(subscriberTestPtr);
130     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
131 }
132 
133 /**
134  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0300
135  * @tc.desc: Subscribe app accounts with invalid data.
136  * @tc.type: FUNC
137  * @tc.require: SR000GGVFT
138  */
139 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0300, TestSize.Level1)
140 {
141     ACCOUNT_LOGI("AppAccountManagerSubscribe_SubscribeAppAccount_0300");
142 
143     // make owners
144     std::vector<std::string> owners;
145     owners.emplace_back(STRING_OWNER);
146     owners.emplace_back(STRING_OWNER_OUT_OF_RANGE);
147 
148     // make subscribe info
149     AppAccountSubscribeInfo subscribeInfo(owners);
150 
151     // make a subscriber
152     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
153 
154     // unsubscribe app account
155     ErrCode result = AppAccountManager::SubscribeAppAccount(subscriberTestPtr);
156     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
157 }
158 
159 /**
160  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0400
161  * @tc.desc: Subscribe app accounts with invalid data.
162  * @tc.type: FUNC
163  * @tc.require: SR000GGVFT
164  */
165 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0400, TestSize.Level1)
166 {
167     ACCOUNT_LOGI("AppAccountManagerSubscribe_SubscribeAppAccount_0400");
168 
169     // make owners
170     std::vector<std::string> owners;
171     owners.emplace_back(STRING_OWNER);
172 
173     // make subscribe info
174     AppAccountSubscribeInfo subscribeInfo(owners);
175 
176     // make a subscriber
177     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
178 
179     // unsubscribe app account
180     ErrCode result = AppAccountManager::SubscribeAppAccount(subscriberTestPtr);
181     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
182 }
183 
184 /**
185  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0500
186  * @tc.desc: Subscribe app accounts failed with subscribeInfoPtr is nullptr.
187  * @tc.type: FUNC
188  * @tc.require:
189  */
190 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0500, TestSize.Level1)
191 {
192     ASSERT_NE(appAccountSubscribeManagerPtr, nullptr);
193 
194     ErrCode result = appAccountSubscribeManagerPtr->SubscribeAppAccount(nullptr, nullptr, UID, BUNDLE_NAME, APP_INDEX);
195     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
196 }
197 
198 
199 /**
200  * @tc.name: AppAccountManagerSubscribe_CheckAppAccess_0100
201  * @tc.desc: CheckAppAccess failed with subscribeInfoPtr is nullptr.
202  * @tc.type: FUNC
203  * @tc.require:
204  */
205 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_CheckAppAccess_0100, TestSize.Level1)
206 {
207     ASSERT_NE(appAccountSubscribeManagerPtr, nullptr);
208 
209     ErrCode result = appAccountSubscribeManagerPtr->CheckAppAccess(nullptr, UID, BUNDLE_NAME, APP_INDEX);
210     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
211 }
212 
213 /**
214  * @tc.name: AppAccountManagerSubscribe_InsertSubscribeRecord_0100
215  * @tc.desc: InsertSubscribeRecord failed with subscribeInfoPtr is nullptr and owners is empty.
216  * @tc.type: FUNC
217  * @tc.require:
218  */
219 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_InsertSubscribeRecord_0100, TestSize.Level1)
220 {
221     ASSERT_NE(appAccountSubscribeManagerPtr, nullptr);
222     std::vector<std::string> owners;
223 
224     AppAccountSubscribeInfo subscribeInfo(owners);
225 
226     auto subscriberTestPtr = std::shared_ptr<AppAccountSubscribeRecord>();
227 
228     ErrCode result = appAccountSubscribeManagerPtr->InsertSubscribeRecord(owners, subscriberTestPtr);
229     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_OWNERS_SIZE_IS_ZERO);
230 
231     owners.emplace_back(STRING_OWNER);
232     result = appAccountSubscribeManagerPtr->InsertSubscribeRecord(owners, nullptr);
233     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
234 }
235 
236 /**
237  * @tc.name: AppAccountManagerSubscribe_RemoveSubscribeRecord_0100
238  * @tc.desc: RemoveSubscribeRecord failed with eventListener is nullptr.
239  * @tc.type: FUNC
240  * @tc.require:
241  */
242 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_RemoveSubscribeRecord_0100, TestSize.Level1)
243 {
244     ASSERT_NE(appAccountSubscribeManagerPtr, nullptr);
245     ErrCode result = appAccountSubscribeManagerPtr->RemoveSubscribeRecord(nullptr);
246     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
247 }
248 
249 /**
250  * @tc.name: AppAccountManagerSubscribe_GetAccessibleAccountsBySubscribeInfo_0100
251  * @tc.desc: RemoveSubscribeRecord failed with subscribeInfoPtr is nullptr.
252  * @tc.type: FUNC
253  * @tc.require:
254  */
255 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_GetAccessibleAccountsBySubscribeInfo_0100,
256     TestSize.Level1)
257 {
258     std::vector<AppAccountInfo> accessibleAccounts;
259     std::vector<AppAccountInfo> appAccounts;
260     ErrCode result =
261         appAccountSubscribeManagerPtr->GetAccessibleAccountsBySubscribeInfo(nullptr, accessibleAccounts, appAccounts);
262     EXPECT_EQ(result, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
263 }