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 #include <gmock/gmock.h>
18
19 #include <thread>
20
21 #include "account_log_wrapper.h"
22 #include "app_account_common.h"
23 #define private public
24 #include "app_account_authenticator_callback.h"
25 #include "app_account_authenticator_manager.h"
26 #include "app_account_authenticator_proxy.h"
27 #include "app_account_authenticator_stub.h"
28 #include "app_account_constants.h"
29 #undef private
30
31 using namespace testing::ext;
32 using namespace OHOS;
33 using namespace OHOS::AccountSA;
34
35 namespace {
36 const std::string STRING_NAME = "name";
37 const std::string STRING_AUTH_TYPE = "test.authType";
38 const std::string STRING_ABILITY_NAME = "test.mainAbility";
39 const std::string CALLER_BUNDLE_NAME = "test.callerbundlename";
40 } // namespace
41
42 class MockAuthenticatorCallback final : public AppAccountAuthenticatorCallbackStub {
43 public:
44 MOCK_METHOD2(OnResult, void(int32_t resultCode, const AAFwk::Want &result));
45 MOCK_METHOD1(OnRequestRedirected, void(AAFwk::Want &request));
46 MOCK_METHOD0(OnRequestContinued, void());
47 };
48
49 class MockAppAccountAuthenticator : public AppAccountAuthenticatorStub {
50 public:
51 ErrCode AddAccountImplicitly(const std::string &authType, const std::string &callerBundleName,
52 const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback);
53 ErrCode Authenticate(
54 const std::string &name, const std::string &authType, const std::string &callerBundleName,
55 const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback);
56 ErrCode VerifyCredential(
57 const std::string &name, const VerifyCredentialOptions &options, const sptr<IRemoteObject> &callback);
58 ErrCode CheckAccountLabels(
59 const std::string &name, const std::vector<std::string> &labels, const sptr<IRemoteObject> &callback);
60 ErrCode SetProperties(const SetPropertiesOptions &options, const sptr<IRemoteObject> &callback);
61 ErrCode IsAccountRemovable(const std::string &name, const sptr<IRemoteObject> &callback);
62 ErrCode CreateAccountImplicitly(
63 const CreateAccountImplicitlyOptions &options, const sptr<IRemoteObject> &callback);
64 ErrCode Auth(const std::string &name, const std::string &authType,
65 const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback);
66 };
67
AddAccountImplicitly(const std::string & authType,const std::string & callerBundleName,const AAFwk::WantParams & options,const sptr<IRemoteObject> & callback)68 ErrCode MockAppAccountAuthenticator::AddAccountImplicitly(
69 const std::string &authType, const std::string &callerBundleName,
70 const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback)
71 {
72 ACCOUNT_LOGI("mock enter");
73 return ERR_OK;
74 }
75
Authenticate(const std::string & name,const std::string & authType,const std::string & callerBundleName,const AAFwk::WantParams & options,const sptr<IRemoteObject> & callback)76 ErrCode MockAppAccountAuthenticator::Authenticate(
77 const std::string &name, const std::string &authType, const std::string &callerBundleName,
78 const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback)
79 {
80 ACCOUNT_LOGI("mock enter");
81 return ERR_OK;
82 }
83
VerifyCredential(const std::string & name,const VerifyCredentialOptions & options,const sptr<IRemoteObject> & callback)84 ErrCode MockAppAccountAuthenticator::VerifyCredential(
85 const std::string &name, const VerifyCredentialOptions &options, const sptr<IRemoteObject> &callback)
86 {
87 ACCOUNT_LOGI("mock enter");
88 return ERR_OK;
89 }
90
CheckAccountLabels(const std::string & name,const std::vector<std::string> & labels,const sptr<IRemoteObject> & callback)91 ErrCode MockAppAccountAuthenticator::CheckAccountLabels(
92 const std::string &name, const std::vector<std::string> &labels, const sptr<IRemoteObject> &callback)
93 {
94 ACCOUNT_LOGI("mock enter");
95 return ERR_OK;
96 }
97
SetProperties(const SetPropertiesOptions & options,const sptr<IRemoteObject> & callback)98 ErrCode MockAppAccountAuthenticator::SetProperties(
99 const SetPropertiesOptions &options, const sptr<IRemoteObject> &callback)
100 {
101 ACCOUNT_LOGI("mock enter");
102 return ERR_OK;
103 }
104
IsAccountRemovable(const std::string & name,const sptr<IRemoteObject> & callback)105 ErrCode MockAppAccountAuthenticator::IsAccountRemovable(const std::string &name, const sptr<IRemoteObject> &callback)
106 {
107 ACCOUNT_LOGI("mock enter");
108 return ERR_OK;
109 }
110
CreateAccountImplicitly(const CreateAccountImplicitlyOptions & options,const sptr<IRemoteObject> & callback)111 ErrCode MockAppAccountAuthenticator::CreateAccountImplicitly(
112 const CreateAccountImplicitlyOptions &options, const sptr<IRemoteObject> &callback)
113 {
114 ACCOUNT_LOGI("mock enter");
115 return ERR_OK;
116 }
117
Auth(const std::string & name,const std::string & authType,const AAFwk::WantParams & options,const sptr<IRemoteObject> & callback)118 ErrCode MockAppAccountAuthenticator::Auth(const std::string &name, const std::string &authType,
119 const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback)
120 {
121 ACCOUNT_LOGI("mock enter");
122 return ERR_OK;
123 }
124
125 class AppAccountAuthenticateModuleTest : public testing::Test {
126 public:
127 static void SetUpTestCase(void);
128 static void TearDownTestCase(void);
129 void SetUp(void) override;
130 void TearDown(void) override;
131 sptr<AppAccountAuthenticatorProxy> authenticateProxyPtr_;
132 };
133
SetUpTestCase(void)134 void AppAccountAuthenticateModuleTest::SetUpTestCase(void)
135 {}
136
TearDownTestCase(void)137 void AppAccountAuthenticateModuleTest::TearDownTestCase(void)
138 {
139 GTEST_LOG_(INFO) << "TearDownTestCase enter";
140 }
141
SetUp(void)142 void AppAccountAuthenticateModuleTest::SetUp(void)
143 {
144 sptr<MockAppAccountAuthenticator> mockServicePtr_ = new (std::nothrow) MockAppAccountAuthenticator();
145
146 sptr<IRemoteObject> authenticorService_ = mockServicePtr_->AsObject();
147 authenticateProxyPtr_ = new (std::nothrow) AppAccountAuthenticatorProxy(authenticorService_);
148 }
149
TearDown(void)150 void AppAccountAuthenticateModuleTest::TearDown(void)
151 {}
152
153 /**
154 * @tc.name: AppAccountAuthenticateTest_CreateAccountImplicitly_0100
155 * @tc.desc: test authenticate proxy func CreateAccountImplicitly.
156 * @tc.type: FUNC
157 * @tc.require: issueI5RWXN
158 */
159 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_CreateAccountImplicitly_0100, TestSize.Level1)
160 {
161 ACCOUNT_LOGI("AppAccountAuthenticateTest_CreateAccountImplicitly_0100");
162
163 CreateAccountImplicitlyOptions options;
164 sptr<IRemoteObject> callback = nullptr;
165 options.parameters.SetParam(Constants::KEY_CALLER_ABILITY_NAME, STRING_ABILITY_NAME);
166 ErrCode result = authenticateProxyPtr_->CreateAccountImplicitly(options, callback);
167 EXPECT_NE(result, ERR_OK);
168 }
169
170 /**
171 * @tc.name: AppAccountAuthenticateTest_Auth_0100
172 * @tc.desc: test authenticate proxy func Auth.
173 * @tc.type: FUNC
174 * @tc.require: issueI5RWXN
175 */
176 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_Auth_0100, TestSize.Level1)
177 {
178 ACCOUNT_LOGI("AppAccountAuthenticateTest_Auth_0100");
179
180 AAFwk::Want want;
181 want.SetParam(Constants::KEY_CALLER_ABILITY_NAME, STRING_ABILITY_NAME);
182 sptr<IRemoteObject> callback = nullptr;
183 ErrCode result = authenticateProxyPtr_->Auth(STRING_NAME, STRING_AUTH_TYPE, want.GetParams(), callback);
184 EXPECT_NE(result, ERR_OK);
185 }
186
187 /**
188 * @tc.name: AppAccountAuthenticateTest_CreateAccountImplicitly_0200
189 * @tc.desc: test authenticate proxy func CreateAccountImplicitly.
190 * @tc.type: FUNC
191 * @tc.require: issueI5RWXN
192 */
193 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_CreateAccountImplicitly_0200, TestSize.Level1)
194 {
195 ACCOUNT_LOGI("AppAccountAuthenticateTest_CreateAccountImplicitly_0200");
196
197 CreateAccountImplicitlyOptions options;
198 options.parameters.SetParam(Constants::KEY_CALLER_ABILITY_NAME, STRING_ABILITY_NAME);
199 sptr<IAppAccountAuthenticatorCallback> oauthCallbackPtr = new (std::nothrow) MockAuthenticatorCallback();
200 sptr<IRemoteObject> callback = oauthCallbackPtr->AsObject();
201 EXPECT_NE(callback, nullptr);
202 ErrCode result = authenticateProxyPtr_->CreateAccountImplicitly(options, callback);
203 EXPECT_EQ(result, ERR_OK);
204 }
205
206 /**
207 * @tc.name: AppAccountAuthenticateTest_Auth_0200
208 * @tc.desc: test authenticate proxy func Auth.
209 * @tc.type: FUNC
210 * @tc.require: issueI5RWXN
211 */
212 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_Auth_0200, TestSize.Level1)
213 {
214 ACCOUNT_LOGI("AppAccountAuthenticateTest_Auth_0200");
215
216 AAFwk::Want want;
217 sptr<IAppAccountAuthenticatorCallback> oauthCallbackPtr = new (std::nothrow) MockAuthenticatorCallback();
218 sptr<IRemoteObject> callback = oauthCallbackPtr->AsObject();
219 EXPECT_NE(callback, nullptr);
220 ErrCode result = authenticateProxyPtr_->Auth(STRING_NAME, STRING_AUTH_TYPE, want.GetParams(), callback);
221 EXPECT_EQ(result, ERR_OK);
222 }
223
224
225 /**
226 * @tc.name: AppAccountAuthenticateTest_AddAccountImplicitly_0100
227 * @tc.desc: test authenticate proxy func AddAccountImplicitly.
228 * @tc.type: FUNC
229 * @tc.require
230 */
231 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_AddAccountImplicitly_0100, TestSize.Level1)
232 {
233 ACCOUNT_LOGI("AppAccountAuthenticateTest_AddAccountImplicitly_0100");
234 AAFwk::Want want;
235 sptr<IRemoteObject> callback = nullptr;
236 ErrCode result =
237 authenticateProxyPtr_->AddAccountImplicitly(STRING_AUTH_TYPE, CALLER_BUNDLE_NAME, want.GetParams(), callback);
238 EXPECT_NE(result, ERR_OK);
239
240 want.SetParam(Constants::KEY_CALLER_ABILITY_NAME, STRING_ABILITY_NAME);
241 result =
242 authenticateProxyPtr_->AddAccountImplicitly(STRING_AUTH_TYPE, CALLER_BUNDLE_NAME, want.GetParams(), callback);
243 EXPECT_NE(result, ERR_OK);
244
245 sptr<IAppAccountAuthenticatorCallback> oauthCallbackPtr = new (std::nothrow) MockAuthenticatorCallback();
246 ASSERT_NE(oauthCallbackPtr, nullptr);
247 callback = oauthCallbackPtr->AsObject();
248 ASSERT_NE(callback, nullptr);
249 result =
250 authenticateProxyPtr_->AddAccountImplicitly(STRING_AUTH_TYPE, CALLER_BUNDLE_NAME, want.GetParams(), callback);
251 EXPECT_EQ(result, ERR_OK);
252 }
253
254 /**
255 * @tc.name: AppAccountAuthenticateTest_Authenticate_0100
256 * @tc.desc: test authenticate proxy func Authenticate.
257 * @tc.type: FUNC
258 * @tc.require
259 */
260 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_Authenticate_0100, TestSize.Level1)
261 {
262 ACCOUNT_LOGI("AppAccountAuthenticateTest_Authenticate_0100");
263 AAFwk::Want want;
264 sptr<IRemoteObject> callback = nullptr;
265 ErrCode result = authenticateProxyPtr_->Authenticate(
266 STRING_NAME, STRING_AUTH_TYPE, CALLER_BUNDLE_NAME, want.GetParams(), callback);
267 EXPECT_NE(result, ERR_OK);
268
269 want.SetParam(Constants::KEY_CALLER_ABILITY_NAME, STRING_ABILITY_NAME);
270 result = authenticateProxyPtr_->Authenticate(
271 STRING_NAME, STRING_AUTH_TYPE, CALLER_BUNDLE_NAME, want.GetParams(), callback);
272 EXPECT_NE(result, ERR_OK);
273
274 sptr<IAppAccountAuthenticatorCallback> oauthCallbackPtr = new (std::nothrow) MockAuthenticatorCallback();
275 ASSERT_NE(oauthCallbackPtr, nullptr);
276 callback = oauthCallbackPtr->AsObject();
277 ASSERT_NE(callback, nullptr);
278 result = authenticateProxyPtr_->Authenticate(
279 STRING_NAME, STRING_AUTH_TYPE, CALLER_BUNDLE_NAME, want.GetParams(), callback);
280 EXPECT_EQ(result, ERR_OK);
281 }
282
283 /**
284 * @tc.name: AppAccountAuthenticateTest_VerifyCredential_0100
285 * @tc.desc: test authenticate proxy func VerifyCredential.
286 * @tc.type: FUNC
287 * @tc.require
288 */
289 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_VerifyCredential_0100, TestSize.Level1)
290 {
291 ACCOUNT_LOGI("AppAccountAuthenticateTest_VerifyCredential_0100");
292 VerifyCredentialOptions options;
293 sptr<IRemoteObject> callback = nullptr;
294 ErrCode result = authenticateProxyPtr_->VerifyCredential(STRING_NAME, options, callback);
295 EXPECT_NE(result, ERR_OK);
296
297 sptr<IAppAccountAuthenticatorCallback> oauthCallbackPtr = new (std::nothrow) MockAuthenticatorCallback();
298 ASSERT_NE(oauthCallbackPtr, nullptr);
299 callback = oauthCallbackPtr->AsObject();
300 ASSERT_NE(callback, nullptr);
301 result = authenticateProxyPtr_->VerifyCredential(STRING_NAME, options, callback);
302 EXPECT_EQ(result, ERR_OK);
303 }
304
305 /**
306 * @tc.name: AppAccountAuthenticateTest_CheckAccountLabels_0100
307 * @tc.desc: test authenticate proxy func VerifyCredential.
308 * @tc.type: FUNC
309 * @tc.require
310 */
311 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_CheckAccountLabels_0100, TestSize.Level1)
312 {
313 ACCOUNT_LOGI("AppAccountAuthenticateTest_CheckAccountLabels_0100");
314 std::vector<std::string> labels;
315 sptr<IRemoteObject> callback = nullptr;
316 ErrCode result = authenticateProxyPtr_->CheckAccountLabels(STRING_NAME, labels, callback);
317 EXPECT_NE(result, ERR_OK);
318
319 sptr<IAppAccountAuthenticatorCallback> oauthCallbackPtr = new (std::nothrow) MockAuthenticatorCallback();
320 ASSERT_NE(oauthCallbackPtr, nullptr);
321 callback = oauthCallbackPtr->AsObject();
322 ASSERT_NE(callback, nullptr);
323 result = authenticateProxyPtr_->CheckAccountLabels(STRING_NAME, labels, callback);
324 EXPECT_EQ(result, ERR_OK);
325 }
326
327 /**
328 * @tc.name: AppAccountAuthenticateTest_SetProperties_0100
329 * @tc.desc: test authenticate proxy func VerifyCredential.
330 * @tc.type: FUNC
331 * @tc.require
332 */
333 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_SetProperties_0100, TestSize.Level1)
334 {
335 ACCOUNT_LOGI("AppAccountAuthenticateTest_SetProperties_0100");
336 SetPropertiesOptions options;
337 sptr<IRemoteObject> callback = nullptr;
338 ErrCode result = authenticateProxyPtr_->SetProperties(options, callback);
339 EXPECT_NE(result, ERR_OK);
340
341 sptr<IAppAccountAuthenticatorCallback> oauthCallbackPtr = new (std::nothrow) MockAuthenticatorCallback();
342 ASSERT_NE(oauthCallbackPtr, nullptr);
343 callback = oauthCallbackPtr->AsObject();
344 ASSERT_NE(callback, nullptr);
345 result = authenticateProxyPtr_->SetProperties(options, callback);
346 EXPECT_EQ(result, ERR_OK);
347 }
348
349 /**
350 * @tc.name: AppAccountAuthenticateTest_IsAccountRemovable_0100
351 * @tc.desc: test authenticate proxy func VerifyCredential.
352 * @tc.type: FUNC
353 * @tc.require
354 */
355 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticateTest_IsAccountRemovable_0100, TestSize.Level1)
356 {
357 ACCOUNT_LOGI("AppAccountAuthenticateTest_IsAccountRemovable_0100");
358 sptr<IRemoteObject> callback = nullptr;
359 ErrCode result = authenticateProxyPtr_->IsAccountRemovable(STRING_NAME, callback);
360 EXPECT_NE(result, ERR_OK);
361
362 sptr<IAppAccountAuthenticatorCallback> oauthCallbackPtr = new (std::nothrow) MockAuthenticatorCallback();
363 ASSERT_NE(oauthCallbackPtr, nullptr);
364 callback = oauthCallbackPtr->AsObject();
365 ASSERT_NE(callback, nullptr);
366 result = authenticateProxyPtr_->IsAccountRemovable(STRING_NAME, callback);
367 EXPECT_EQ(result, ERR_OK);
368 }
369
370 /**
371 * @tc.name: AppAccountAuthenticatorManagerTest_GetAuthenticatorInfo_0100
372 * @tc.desc: test GetAuthenticatorInfo with not init.
373 * @tc.type: FUNC
374 * @tc.require
375 */
376 HWTEST_F(AppAccountAuthenticateModuleTest, AppAccountAuthenticatorManagerTest_GetAuthenticatorInfo_0100,
377 TestSize.Level1)
378 {
379 auto appAccountAuthenticatorManagerPtr = std::make_shared<AppAccountAuthenticatorManager>();
380 ASSERT_NE(appAccountAuthenticatorManagerPtr, nullptr);
381 appAccountAuthenticatorManagerPtr->isInitialized_ = false;
382 std::string owner = "owner";
383 int32_t userId = 1;
384 AuthenticatorInfo info;
385 ErrCode result = appAccountAuthenticatorManagerPtr->GetAuthenticatorInfo(owner, userId, info);
386 ASSERT_NE(result, ERR_OK);
387 ASSERT_EQ(appAccountAuthenticatorManagerPtr->isInitialized_, true);
388 }