1 /*
2 * Copyright (c) 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 #include "account_log_wrapper.h"
19 #include "app_account_common.h"
20 #include "app_account_event_listener.h"
21 #include "app_account_subscriber.h"
22 #define private public
23 #include "app_account_subscribe_death_recipient.h"
24 #include "app_account_subscribe_manager.h"
25 #undef private
26
27 using namespace testing::ext;
28 using namespace OHOS;
29 using namespace OHOS::AccountSA;
30
31 namespace {
32 const uid_t TEST_UID = 1;
33 const std::string TEST_BUNDLE_NAME = "com.example.owner";
34 const std::string STRING_OWNER = "com.example.owner";
35 const std::string TEST_OWNER_NAME = "com.bundle.owner";
36 const uint32_t TEST_APP_INDEX = 0;
37 } // namespace
38
39 class AppAccountSubscribeManagerTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp(void) override;
44 void TearDown(void) override;
45 };
46
SetUpTestCase(void)47 void AppAccountSubscribeManagerTest::SetUpTestCase(void)
48 {}
49
TearDownTestCase(void)50 void AppAccountSubscribeManagerTest::TearDownTestCase(void)
51 {}
52
SetUp(void)53 void AppAccountSubscribeManagerTest::SetUp(void) __attribute__((no_sanitize("cfi")))
54 {
55 testing::UnitTest *test = testing::UnitTest::GetInstance();
56 ASSERT_NE(test, nullptr);
57 const testing::TestInfo *testinfo = test->current_test_info();
58 ASSERT_NE(testinfo, nullptr);
59 string testCaseName = string(testinfo->name());
60 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
61 }
62
TearDown(void)63 void AppAccountSubscribeManagerTest::TearDown(void)
64 {}
65
66 /**
67 * @tc.name: AppAccountSubscribeManager_SubscribeAppAccount_0100
68 * @tc.desc: SubscribeAppAccount with nullptr param.
69 * @tc.type: FUNC
70 * @tc.require:
71 */
72 HWTEST_F(AppAccountSubscribeManagerTest, SubscribeAppAccount_0100, TestSize.Level1)
73 {
74 ACCOUNT_LOGI("AppAccountSubscribeManager_SubscribeAppAccount_0100");
75 ErrCode ret = AppAccountSubscribeManager::GetInstance().SubscribeAppAccount(
76 nullptr, nullptr, TEST_UID, TEST_BUNDLE_NAME, TEST_APP_INDEX);
77 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
78
79 // check eventListener is nullptr.
80 std::vector<std::string> owners;
81 owners.emplace_back(STRING_OWNER);
82
83 // make subscribe info with owners.
84 AppAccountSubscribeInfo subscribeInfo(owners);
85 auto subscribeInfoPtr = std::make_shared<AppAccountSubscribeInfo>(subscribeInfo);
86 ret = AppAccountSubscribeManager::GetInstance().SubscribeAppAccount(
87 subscribeInfoPtr, nullptr, TEST_UID, TEST_BUNDLE_NAME, TEST_APP_INDEX);
88 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
89 ACCOUNT_LOGI("end AppAccountSubscribeManager_SubscribeAppAccount_0100");
90 }
91
92 /**
93 * @tc.name: AppAccountSubscribeManager_UnsubscribeAppAccount_0100
94 * @tc.desc: SubscribeAppAccount with nullptr param.
95 * @tc.type: FUNC
96 * @tc.require:
97 */
98 HWTEST_F(AppAccountSubscribeManagerTest, UnsubscribeAppAccount_0100, TestSize.Level1)
99 {
100 ACCOUNT_LOGI("AppAccountSubscribeManager_UnsubscribeAppAccount_0100");
101
102 ErrCode ret = AppAccountSubscribeManager::GetInstance().UnsubscribeAppAccount(nullptr);
103 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
104 }
105
106 /**
107 * @tc.name: AppAccountSubscribeManager_CheckAppAccess_0100
108 * @tc.desc: CheckAppAccess with nullptr param.
109 * @tc.type: FUNC
110 * @tc.require:
111 */
112 HWTEST_F(AppAccountSubscribeManagerTest, CheckAppAccess_0100, TestSize.Level1)
113 {
114 ACCOUNT_LOGI("AppAccountSubscribeManager_CheckAppAccess_0100");
115 // check subscribeInfoPtr is nullptr.
116 ErrCode ret = AppAccountSubscribeManager::GetInstance().CheckAppAccess(
117 nullptr, TEST_UID, TEST_BUNDLE_NAME, TEST_APP_INDEX);
118 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
119 }
120
121 /**
122 * @tc.name: AppAccountSubscribeManager_CheckAppAccess_0200
123 * @tc.desc: CheckAppAccess with nullptr param.
124 * @tc.type: FUNC
125 * @tc.require:
126 */
127 HWTEST_F(AppAccountSubscribeManagerTest, CheckAppAccess_0200, TestSize.Level1)
128 {
129 ACCOUNT_LOGI("AppAccountSubscribeManager_CheckAppAccess_0200");
130
131 // check subscribeInfoPtr
132 std::vector<std::string> owners;
133 owners.emplace_back(STRING_OWNER);
134 AppAccountSubscribeInfo subscribeInfo(owners);
135 auto subscribeInfoPtr = std::make_shared<AppAccountSubscribeInfo>(subscribeInfo);
136
137 ErrCode ret = AppAccountSubscribeManager::GetInstance().CheckAppAccess(
138 subscribeInfoPtr, TEST_UID, TEST_BUNDLE_NAME, TEST_APP_INDEX);
139 EXPECT_EQ(ret, ERR_OK);
140 }
141
142 /**
143 * @tc.name: AppAccountSubscribeManager_CheckAppAccess_0300
144 * @tc.desc: CheckAppAccess with nullptr param.
145 * @tc.type: FUNC
146 * @tc.require:
147 */
148 HWTEST_F(AppAccountSubscribeManagerTest, CheckAppAccess_0300, TestSize.Level1)
149 {
150 ACCOUNT_LOGI("AppAccountSubscribeManager_CheckAppAccess_0300");
151
152 // check subscribeInfoPtr
153 std::vector<std::string> owners;
154 owners.emplace_back(STRING_OWNER);
155 AppAccountSubscribeInfo subscribeInfo(owners);
156 auto subscribeInfoPtr = std::make_shared<AppAccountSubscribeInfo>(subscribeInfo);
157
158 ErrCode ret = AppAccountSubscribeManager::GetInstance().CheckAppAccess(
159 subscribeInfoPtr, TEST_UID, TEST_OWNER_NAME, TEST_APP_INDEX);
160 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
161 }
162
163 /**
164 * @tc.name: AppAccountSubscribeManager_InsertSubscribeRecord_0100
165 * @tc.desc: InsertSubscribeRecord with nullptr param.
166 * @tc.type: FUNC
167 * @tc.require:
168 */
169 HWTEST_F(AppAccountSubscribeManagerTest, InsertSubscribeRecord_0100, TestSize.Level1)
170 {
171 ACCOUNT_LOGI("AppAccountSubscribeManager_InsertSubscribeRecord_0100");
172
173 // check owners is empty.
174 std::vector<std::string> owners;
175 ErrCode ret = AppAccountSubscribeManager::GetInstance().InsertSubscribeRecord(owners, nullptr);
176 EXPECT_EQ(ret, ERR_APPACCOUNT_SERVICE_OWNERS_SIZE_IS_ZERO);
177
178 // check subscribeRecordPtr is nullptr.
179 owners.emplace_back(TEST_BUNDLE_NAME);
180 ret = AppAccountSubscribeManager::GetInstance().InsertSubscribeRecord(owners, nullptr);
181 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
182 }
183
184 /**
185 * @tc.name: AppAccountSubscribeManager_RemoveSubscribeRecord_0100
186 * @tc.desc: RemoveSubscribeRecord with nullptr param.
187 * @tc.type: FUNC
188 * @tc.require:
189 */
190 HWTEST_F(AppAccountSubscribeManagerTest, RemoveSubscribeRecord_0100, TestSize.Level1)
191 {
192 ACCOUNT_LOGI("AppAccountSubscribeManager_RemoveSubscribeRecord_0100");
193 // check eventListener is nullptr.
194 ErrCode ret = AppAccountSubscribeManager::GetInstance().RemoveSubscribeRecord(nullptr);
195 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
196 }
197
198 /**
199 * @tc.name: AppAccountSubscribeManager_GetAccessibleAccountsBySubscribeInfo_0100
200 * @tc.desc: GetAccessibleAccountsBySubscribeInfo with nullptr param.
201 * @tc.type: FUNC
202 * @tc.require:
203 */
204 HWTEST_F(AppAccountSubscribeManagerTest, GetAccessibleAccountsBySubscribeInfo_0100, TestSize.Level1)
205 {
206 ACCOUNT_LOGI("AppAccountSubscribeManager_GetAccessibleAccountsBySubscribeInfo_0100");
207
208 // check subscribeInfoPtr is nullptr.
209 std::vector<AppAccountInfo> accessibleAccounts;
210 std::vector<AppAccountInfo> appAccounts;
211 ErrCode ret = AppAccountSubscribeManager::GetInstance().
212 GetAccessibleAccountsBySubscribeInfo(nullptr, accessibleAccounts, appAccounts);
213 EXPECT_EQ(ret, ERR_ACCOUNT_COMMON_NULL_PTR_ERROR);
214 }
215