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