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 "user_idm_callback_service_test.h"
17
18 #include "user_idm_callback_service.h"
19 #include "iam_ptr.h"
20 #include "mock_credential_info.h"
21 #include "mock_secure_user_info.h"
22 #include "mock_user_idm_client_callback.h"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 using namespace testing;
28 using namespace testing::ext;
29
SetUpTestCase()30 void UserIdmCallbackServiceTest::SetUpTestCase()
31 {
32 }
33
TearDownTestCase()34 void UserIdmCallbackServiceTest::TearDownTestCase()
35 {
36 }
37
SetUp()38 void UserIdmCallbackServiceTest::SetUp()
39 {
40 }
41
TearDown()42 void UserIdmCallbackServiceTest::TearDown()
43 {
44 }
45
46 HWTEST_F(UserIdmCallbackServiceTest, UserIdmCallbackServiceTest001, TestSize.Level0)
47 {
48 int32_t testResult = FAIL;
49 Attributes testExtraInfo;
50
51 int32_t testModule = 52334;
52 int32_t testAcquireInfo = 57845;
53
54 std::shared_ptr<UserIdmClientCallback> idmClientCallback = nullptr;
55 auto service = Common::MakeShared<IdmCallbackService>(idmClientCallback);
56 EXPECT_NE(service, nullptr);
57 service->OnResult(testResult, testExtraInfo);
58 service->OnAcquireInfo(testModule, testAcquireInfo, testExtraInfo);
59 }
60
61 HWTEST_F(UserIdmCallbackServiceTest, UserIdmCallbackServiceTest002, TestSize.Level0)
62 {
63 int32_t testResult = FAIL;
64 Attributes testExtraInfo;
65
66 int32_t testModule = 52334;
67 int32_t testAcquireInfo = 57845;
68
69 auto idmClientCallback = Common::MakeShared<MockUserIdmClientCallback>();
70 EXPECT_NE(idmClientCallback, nullptr);
71 EXPECT_CALL(*idmClientCallback, OnResult(_, _)).Times(1);
72 ON_CALL(*idmClientCallback, OnResult)
73 .WillByDefault(
__anonea44ff830102(int32_t result, const Attributes &extraInfo) 74 [&testResult](int32_t result, const Attributes &extraInfo) {
75 EXPECT_EQ(result, testResult);
76 }
77 );
78 EXPECT_CALL(*idmClientCallback, OnAcquireInfo(_, _, _)).Times(1);
79 ON_CALL(*idmClientCallback, OnAcquireInfo)
80 .WillByDefault(
__anonea44ff830202(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) 81 [&testModule, &testAcquireInfo](int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) {
82 EXPECT_EQ(module, testModule);
83 EXPECT_EQ(acquireInfo, testAcquireInfo);
84 }
85 );
86 auto service = Common::MakeShared<IdmCallbackService>(idmClientCallback);
87 EXPECT_NE(service, nullptr);
88 service->OnResult(testResult, testExtraInfo);
89 service->OnAcquireInfo(testModule, testAcquireInfo, testExtraInfo);
90 }
91
92 HWTEST_F(UserIdmCallbackServiceTest, IdmGetCredInfoCallbackServiceTest001, TestSize.Level0)
93 {
94 std::vector<std::shared_ptr<IdmGetCredInfoCallbackInterface::CredentialInfo>> testInfoList;
95
96 std::shared_ptr<GetCredentialInfoCallback> getCredInfoCallback = nullptr;
97 auto service = Common::MakeShared<IdmGetCredInfoCallbackService>(getCredInfoCallback);
98 EXPECT_NE(service, nullptr);
99 service->OnCredentialInfos(testInfoList, std::nullopt);
100 }
101
102 HWTEST_F(UserIdmCallbackServiceTest, IdmGetCredInfoCallbackServiceTest002, TestSize.Level0)
103 {
104 uint64_t testCredentialId = 265326;
105 uint64_t testTemplateId = 62324;
106 AuthType testAuthType = PIN;
107 PinSubType testSubType = PIN_SIX;
108
109 std::vector<std::shared_ptr<IdmGetCredInfoCallbackInterface::CredentialInfo>> testInfoList;
110 testInfoList.push_back(nullptr);
111 auto testCredInfo = Common::MakeShared<MockCredentialInfo>();
112 EXPECT_NE(testCredInfo, nullptr);
113 testInfoList.push_back(testCredInfo);
114 EXPECT_CALL(*testCredInfo, GetCredentialId()).Times(1);
115 ON_CALL(*testCredInfo, GetCredentialId)
116 .WillByDefault(
__anonea44ff830302() 117 [&testCredentialId]() {
118 return testCredentialId;
119 }
120 );
121 EXPECT_CALL(*testCredInfo, GetTemplateId()).Times(1);
122 ON_CALL(*testCredInfo, GetTemplateId)
123 .WillByDefault(
__anonea44ff830402() 124 [&testTemplateId]() {
125 return testTemplateId;
126 }
127 );
128 EXPECT_CALL(*testCredInfo, GetAuthType()).Times(1);
129 ON_CALL(*testCredInfo, GetAuthType)
130 .WillByDefault(
__anonea44ff830502() 131 [&testAuthType]() {
132 return testAuthType;
133 }
134 );
135
136 auto getCredInfoCallback = Common::MakeShared<MockGetCredentialInfoCallback>();
137 EXPECT_NE(getCredInfoCallback, nullptr);
138 EXPECT_CALL(*getCredInfoCallback, OnCredentialInfo(_)).Times(1);
139 ON_CALL(*getCredInfoCallback, OnCredentialInfo)
140 .WillByDefault(
141 [&testCredentialId, &testTemplateId, &testAuthType, &testSubType](
__anonea44ff830602( const std::vector<CredentialInfo> &infoList) 142 const std::vector<CredentialInfo> &infoList) {
143 EXPECT_FALSE(infoList.empty());
144 EXPECT_EQ(infoList[0].credentialId, testCredentialId);
145 EXPECT_EQ(infoList[0].templateId, testTemplateId);
146 EXPECT_EQ(infoList[0].authType, testAuthType);
147 EXPECT_TRUE(infoList[0].pinType.has_value());
148 EXPECT_EQ(infoList[0].pinType.value(), testSubType);
149 }
150 );
151 auto service = Common::MakeShared<IdmGetCredInfoCallbackService>(getCredInfoCallback);
152 EXPECT_NE(service, nullptr);
153 service->OnCredentialInfos(testInfoList, testSubType);
154 }
155
156 HWTEST_F(UserIdmCallbackServiceTest, IdmGetSecureUserInfoCallbackServiceTest001, TestSize.Level0)
157 {
158 std::shared_ptr<IdmGetSecureUserInfoCallbackInterface::SecureUserInfo> testUserInfo = nullptr;
159
160 std::shared_ptr<GetSecUserInfoCallback> getSecInfoCallback = nullptr;
161 auto service = Common::MakeShared<IdmGetSecureUserInfoCallbackService>(getSecInfoCallback);
162 EXPECT_NE(service, nullptr);
163 service->OnSecureUserInfo(testUserInfo);
164 }
165
166 HWTEST_F(UserIdmCallbackServiceTest, IdmGetSecureUserInfoCallbackServiceTest002, TestSize.Level0)
167 {
168 uint64_t testSecUserId = 54871;
169 auto testUserInfo = Common::MakeShared<MockSecureUserInfo>();
170 EXPECT_NE(testUserInfo, nullptr);
171 EXPECT_CALL(*testUserInfo, GetSecUserId()).Times(1);
172 ON_CALL(*testUserInfo, GetSecUserId)
173 .WillByDefault(
__anonea44ff830702() 174 [&testSecUserId]() {
175 return testSecUserId;
176 }
177 );
178
179 auto getSecInfoCallback = Common::MakeShared<MockGetSecUserInfoCallback>();
180 EXPECT_NE(getSecInfoCallback, nullptr);
181 EXPECT_CALL(*getSecInfoCallback, OnSecUserInfo(_)).Times(1);
182 ON_CALL(*getSecInfoCallback, OnSecUserInfo)
183 .WillByDefault(
__anonea44ff830802(const SecUserInfo &info) 184 [&testSecUserId](const SecUserInfo &info) {
185 EXPECT_EQ(info.secureUid, testSecUserId);
186 }
187 );
188
189 auto service = Common::MakeShared<IdmGetSecureUserInfoCallbackService>(getSecInfoCallback);
190 EXPECT_NE(service, nullptr);
191 service->OnSecureUserInfo(testUserInfo);
192 }
193 } // namespace UserAuth
194 } // namespace UserIam
195 } // namespace OHOS