• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 =
35     "owner_out_of_range_"
36     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
37     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
38     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
39     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
40     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
41     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
42     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
43     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
44     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
45     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
46     "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
47 }  // namespace
48 
49 class AppAccountManagerSubscribeTest : 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 
56     std::shared_ptr<AppAccountSubscribeManager> appAccountSubscribeManagerPtr =
57         std::make_shared<AppAccountSubscribeManager>();
58 };
59 
SetUpTestCase(void)60 void AppAccountManagerSubscribeTest::SetUpTestCase(void)
61 {}
62 
TearDownTestCase(void)63 void AppAccountManagerSubscribeTest::TearDownTestCase(void)
64 {}
65 
SetUp(void)66 void AppAccountManagerSubscribeTest::SetUp(void)
67 {}
68 
TearDown(void)69 void AppAccountManagerSubscribeTest::TearDown(void)
70 {}
71 
72 class AppAccountSubscriberTest : public AppAccountSubscriber {
73 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)74     explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
75         : AppAccountSubscriber(subscribeInfo)
76     {
77         ACCOUNT_LOGI("enter");
78     }
79 
~AppAccountSubscriberTest()80     ~AppAccountSubscriberTest()
81     {}
82 
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)83     virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
84     {
85         ACCOUNT_LOGI("enter");
86     }
87 };
88 
89 /**
90  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0100
91  * @tc.desc: Subscribe app accounts with invalid data.
92  * @tc.type: FUNC
93  * @tc.require: SR000GGVFT
94  */
95 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0100, TestSize.Level1)
96 {
97     ACCOUNT_LOGI("AppAccountManagerSubscribe_SubscribeAppAccount_0100");
98 
99     // make owners
100     std::vector<std::string> owners;
101     owners.emplace_back(STRING_OWNER);
102 
103     // make subscribe info
104     AppAccountSubscribeInfo subscribeInfo(owners);
105 
106     // make a subscriber
107     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
108 
109     // unsubscribe app account
110     ErrCode result = AppAccountManager::UnsubscribeAppAccount(subscriberTestPtr);
111     EXPECT_EQ(result, ERR_APPACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED);
112 }
113 
114 /**
115  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0200
116  * @tc.desc: Subscribe app accounts with invalid data.
117  * @tc.type: FUNC
118  * @tc.require: SR000GGVFT
119  */
120 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0200, TestSize.Level1)
121 {
122     ACCOUNT_LOGI("AppAccountManagerSubscribe_SubscribeAppAccount_0200");
123 
124     // make owners
125     std::vector<std::string> owners;
126 
127     // make subscribe info
128     AppAccountSubscribeInfo subscribeInfo(owners);
129 
130     // make a subscriber
131     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
132 
133     // unsubscribe app account
134     ErrCode result = AppAccountManager::SubscribeAppAccount(subscriberTestPtr);
135     EXPECT_EQ(result, ERR_APPACCOUNT_KIT_SUBSCRIBER_HAS_NO_OWNER);
136 }
137 
138 /**
139  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0300
140  * @tc.desc: Subscribe app accounts with invalid data.
141  * @tc.type: FUNC
142  * @tc.require: SR000GGVFT
143  */
144 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0300, TestSize.Level1)
145 {
146     ACCOUNT_LOGI("AppAccountManagerSubscribe_SubscribeAppAccount_0300");
147 
148     // make owners
149     std::vector<std::string> owners;
150     owners.emplace_back(STRING_OWNER);
151     owners.emplace_back(STRING_OWNER_OUT_OF_RANGE);
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_APPACCOUNT_KIT_OWNER_OUT_OF_RANGE);
162 }
163 
164 /**
165  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0400
166  * @tc.desc: Subscribe app accounts with invalid data.
167  * @tc.type: FUNC
168  * @tc.require: SR000GGVFT
169  */
170 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0400, TestSize.Level1)
171 {
172     ACCOUNT_LOGI("AppAccountManagerSubscribe_SubscribeAppAccount_0400");
173 
174     // make owners
175     std::vector<std::string> owners;
176     owners.emplace_back(STRING_OWNER);
177 
178     // make subscribe info
179     AppAccountSubscribeInfo subscribeInfo(owners);
180 
181     // make a subscriber
182     auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
183 
184     // unsubscribe app account
185     ErrCode result = AppAccountManager::SubscribeAppAccount(subscriberTestPtr);
186     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME);
187 }
188 
189 /**
190  * @tc.name: AppAccountManagerSubscribe_SubscribeAppAccount_0500
191  * @tc.desc: Subscribe app accounts failed with subscribeInfoPtr is nullptr.
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_SubscribeAppAccount_0500, TestSize.Level1)
196 {
197     ASSERT_NE(appAccountSubscribeManagerPtr, nullptr);
198 
199     ErrCode result = appAccountSubscribeManagerPtr->SubscribeAppAccount(nullptr, nullptr, UID, BUNDLE_NAME, APP_INDEX);
200     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_SUBSCRIBE_INFO_PTR_IS_NULLPTR);
201 }
202 
203 
204 /**
205  * @tc.name: AppAccountManagerSubscribe_CheckAppAccess_0100
206  * @tc.desc: CheckAppAccess failed with subscribeInfoPtr is nullptr.
207  * @tc.type: FUNC
208  * @tc.require:
209  */
210 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_CheckAppAccess_0100, TestSize.Level1)
211 {
212     ASSERT_NE(appAccountSubscribeManagerPtr, nullptr);
213 
214     ErrCode result = appAccountSubscribeManagerPtr->CheckAppAccess(nullptr, UID, BUNDLE_NAME, APP_INDEX);
215     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_SUBSCRIBE_INFO_PTR_IS_NULLPTR);
216 }
217 
218 /**
219  * @tc.name: AppAccountManagerSubscribe_InsertSubscribeRecord_0100
220  * @tc.desc: InsertSubscribeRecord failed with subscribeInfoPtr is nullptr and owners is empty.
221  * @tc.type: FUNC
222  * @tc.require:
223  */
224 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_InsertSubscribeRecord_0100, TestSize.Level1)
225 {
226     ASSERT_NE(appAccountSubscribeManagerPtr, nullptr);
227     std::vector<std::string> owners;
228 
229     AppAccountSubscribeInfo subscribeInfo(owners);
230 
231     auto subscriberTestPtr = std::shared_ptr<AppAccountSubscribeRecord>();
232 
233     ErrCode result = appAccountSubscribeManagerPtr->InsertSubscribeRecord(owners, subscriberTestPtr);
234     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_OWNERS_SIZE_IS_ZERO);
235 
236     owners.emplace_back(STRING_OWNER);
237     result = appAccountSubscribeManagerPtr->InsertSubscribeRecord(owners, nullptr);
238     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_SUBSCRIBE_RECORD_PTR_IS_NULLPTR);
239 }
240 
241 /**
242  * @tc.name: AppAccountManagerSubscribe_RemoveSubscribeRecord_0100
243  * @tc.desc: RemoveSubscribeRecord failed with eventListener is nullptr.
244  * @tc.type: FUNC
245  * @tc.require:
246  */
247 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_RemoveSubscribeRecord_0100, TestSize.Level1)
248 {
249     ASSERT_NE(appAccountSubscribeManagerPtr, nullptr);
250     ErrCode result = appAccountSubscribeManagerPtr->RemoveSubscribeRecord(nullptr);
251     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_EVENT_LISTENER_IS_NULLPTR);
252 }
253 
254 /**
255  * @tc.name: AppAccountManagerSubscribe_GetAccessibleAccountsBySubscribeInfo_0100
256  * @tc.desc: RemoveSubscribeRecord failed with subscribeInfoPtr is nullptr.
257  * @tc.type: FUNC
258  * @tc.require:
259  */
260 HWTEST_F(AppAccountManagerSubscribeTest, AppAccountManagerSubscribe_GetAccessibleAccountsBySubscribeInfo_0100,
261     TestSize.Level1)
262 {
263     std::vector<AppAccountInfo> accessibleAccounts;
264     std::vector<AppAccountInfo> appAccounts;
265     ErrCode result =
266         appAccountSubscribeManagerPtr->GetAccessibleAccountsBySubscribeInfo(nullptr, accessibleAccounts, appAccounts);
267     EXPECT_EQ(result, ERR_APPACCOUNT_SERVICE_SUBSCRIBE_INFO_PTR_IS_NULLPTR);
268 }