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_auth_proxy_test.h"
17
18 #include "iam_ptr.h"
19 #include "user_auth_proxy.h"
20 #include "mock_remote_object.h"
21 #include "mock_user_auth_service.h"
22 #include "mock_user_auth_client_callback.h"
23 #include "user_auth_callback_service.h"
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 using namespace testing;
29 using namespace testing::ext;
30
SetUpTestCase()31 void UserAuthProxyTest::SetUpTestCase()
32 {
33 }
34
TearDownTestCase()35 void UserAuthProxyTest::TearDownTestCase()
36 {
37 }
38
SetUp()39 void UserAuthProxyTest::SetUp()
40 {
41 }
42
TearDown()43 void UserAuthProxyTest::TearDown()
44 {
45 }
46
47 HWTEST_F(UserAuthProxyTest, UserAuthProxyGetAvailableStatus, TestSize.Level0)
48 {
49 static const int32_t testApiVersion = 0;
50 static const AuthType testAuthType = FACE;
51 static const AuthTrustLevel testAuthTrustLevel = ATL3;
52 sptr<MockRemoteObject> obj = new MockRemoteObject();
53 EXPECT_NE(obj, nullptr);
54 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
55 EXPECT_NE(proxy, nullptr);
56 auto service = Common::MakeShared<MockUserAuthService>();
57 EXPECT_NE(service, nullptr);
58 EXPECT_CALL(*service, GetAvailableStatus(_, _, _))
59 .Times(Exactly(1))
__anonc44a677e0102(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel) 60 .WillOnce([](int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel) {
61 EXPECT_EQ(testApiVersion, apiVersion);
62 EXPECT_EQ(testAuthType, authType);
63 EXPECT_EQ(testAuthTrustLevel, authTrustLevel);
64 return SUCCESS;
65 });
66 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
67 ON_CALL(*obj, SendRequest)
__anonc44a677e0202(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 68 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
69 service->OnRemoteRequest(code, data, reply, option);
70 return SUCCESS;
71 });
72 proxy->GetAvailableStatus(testApiVersion, testAuthType, testAuthTrustLevel);
73 }
74
75 HWTEST_F(UserAuthProxyTest, UserAuthProxyGetProperty, TestSize.Level0)
76 {
77 static const int32_t testUserId = 200;
78 static const AuthType testAuthType = FACE;
79 std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
80 Attributes::ATTR_SCHEDULE_MODE};
81
82 sptr<MockRemoteObject> obj = new MockRemoteObject();
83 EXPECT_NE(obj, nullptr);
84 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
85 EXPECT_NE(proxy, nullptr);
86 auto getPropCallback = Common::MakeShared<MockGetPropCallback>();
87 EXPECT_NE(getPropCallback, nullptr);
88 sptr<GetExecutorPropertyCallbackInterface> testCallback =
89 new (std::nothrow) GetExecutorPropertyCallbackService(getPropCallback);
90 auto service = Common::MakeShared<MockUserAuthService>();
91 EXPECT_NE(service, nullptr);
92 EXPECT_CALL(*service, GetProperty(_, _, _, _))
93 .Times(Exactly(1))
94 .WillOnce([&testCallback](std::optional<int32_t> userId, AuthType authType,
__anonc44a677e0302(std::optional<int32_t> userId, AuthType authType, const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) 95 const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback) {
96 EXPECT_TRUE(userId.has_value());
97 EXPECT_EQ(userId.value(), testUserId);
98 EXPECT_EQ(authType, testAuthType);
99 EXPECT_THAT(keys, ElementsAre(Attributes::ATTR_RESULT_CODE, Attributes::ATTR_SIGNATURE,
100 Attributes::ATTR_SCHEDULE_MODE));
101 EXPECT_EQ(callback, testCallback);
102 });
103 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
104 ON_CALL(*obj, SendRequest)
__anonc44a677e0402(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 105 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
106 service->OnRemoteRequest(code, data, reply, option);
107 return SUCCESS;
108 });
109 proxy->GetProperty(testUserId, testAuthType, testKeys, testCallback);
110 }
111
112 HWTEST_F(UserAuthProxyTest, UserAuthProxySetProperty, TestSize.Level0)
113 {
114 static const AuthType testAuthType = FACE;
115
116 Attributes testAttr;
117 EXPECT_EQ(testAttr.SetInt32Value(Attributes::ATTR_RESULT_CODE, 1), true);
118
119 sptr<MockRemoteObject> obj = new MockRemoteObject();
120 EXPECT_NE(obj, nullptr);
121 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
122 EXPECT_NE(proxy, nullptr);
123 auto setPropCallback = Common::MakeShared<MockSetPropCallback>();
124 EXPECT_NE(setPropCallback, nullptr);
125 sptr<SetExecutorPropertyCallbackInterface> testCallback =
126 new (std::nothrow) SetExecutorPropertyCallbackService(setPropCallback);
127 auto service = Common::MakeShared<MockUserAuthService>();
128 EXPECT_NE(service, nullptr);
129 EXPECT_CALL(*service, SetProperty(_, _, _, _))
130 .Times(Exactly(1))
131 .WillOnce([&testCallback](int32_t userId, AuthType authType,
__anonc44a677e0502(int32_t userId, AuthType authType, const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) 132 const Attributes &attributes, sptr<SetExecutorPropertyCallbackInterface> &callback) {
133 EXPECT_EQ(userId, 0);
134 EXPECT_EQ(authType, testAuthType);
135 int32_t resultCode;
136 EXPECT_EQ(attributes.GetInt32Value(Attributes::ATTR_RESULT_CODE, resultCode), true);
137 EXPECT_EQ(resultCode, 1);
138 EXPECT_EQ(callback, testCallback);
139 });
140 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
141 ON_CALL(*obj, SendRequest)
__anonc44a677e0602(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 142 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
143 service->OnRemoteRequest(code, data, reply, option);
144 return SUCCESS;
145 });
146 proxy->SetProperty(0, testAuthType, testAttr, testCallback);
147 }
148
149 HWTEST_F(UserAuthProxyTest, UserAuthProxyAuth, TestSize.Level0)
150 {
151 static const int32_t testApiVersion = 0;
152 static const AuthType testAuthType = FACE;
153 static const AuthTrustLevel testAtl = ATL1;
154 const std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
155
156 sptr<MockRemoteObject> obj = new MockRemoteObject();
157 EXPECT_NE(obj, nullptr);
158 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
159 EXPECT_NE(proxy, nullptr);
160 auto authCallback = Common::MakeShared<MockAuthenticationCallback>();
161 EXPECT_NE(authCallback, nullptr);
162 sptr<UserAuthCallbackInterface> testCallback =
163 new (std::nothrow) UserAuthCallbackService(authCallback);
164 auto service = Common::MakeShared<MockUserAuthService>();
165 EXPECT_NE(service, nullptr);
166 EXPECT_CALL(*service, Auth(_, _, _, _, _))
167 .Times(Exactly(1))
168 .WillOnce([&testCallback](int32_t apiVersion, const std::vector<uint8_t> &challenge,
__anonc44a677e0702(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) 169 AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) {
170 EXPECT_EQ(apiVersion, testApiVersion);
171 EXPECT_THAT(challenge, ElementsAre(1, 2, 3, 4));
172 EXPECT_EQ(authType, testAuthType);
173 EXPECT_EQ(authTrustLevel, testAtl);
174 EXPECT_EQ(callback, testCallback);
175 return 0;
176 });
177 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
178 ON_CALL(*obj, SendRequest)
__anonc44a677e0802(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 179 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
180 service->OnRemoteRequest(code, data, reply, option);
181 return SUCCESS;
182 });
183 proxy->Auth(testApiVersion, testChallenge, testAuthType, testAtl, testCallback);
184 }
185
186 HWTEST_F(UserAuthProxyTest, UserAuthProxyAuthUser, TestSize.Level0)
187 {
188 static const int32_t testUserId = 200;
189 static const AuthType testAuthType = FACE;
190 static const AuthTrustLevel testAtl = ATL1;
191 const std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
192
193 sptr<MockRemoteObject> obj = new MockRemoteObject();
194 EXPECT_NE(obj, nullptr);
195 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
196 EXPECT_NE(proxy, nullptr);
197 auto authCallback = Common::MakeShared<MockAuthenticationCallback>();
198 EXPECT_NE(authCallback, nullptr);
199 sptr<UserAuthCallbackInterface> testCallback =
200 new (std::nothrow) UserAuthCallbackService(authCallback);
201 auto service = Common::MakeShared<MockUserAuthService>();
202 EXPECT_NE(service, nullptr);
203 EXPECT_CALL(*service, AuthUser(_, _, _, _, _))
204 .Times(Exactly(1))
205 .WillOnce([&testCallback](int32_t userId, const std::vector<uint8_t> &challenge,
__anonc44a677e0902(int32_t userId, const std::vector<uint8_t> &challenge, AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) 206 AuthType authType, AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback) {
207 EXPECT_EQ(userId, testUserId);
208 EXPECT_THAT(challenge, ElementsAre(1, 2, 3, 4));
209 EXPECT_EQ(authType, testAuthType);
210 EXPECT_EQ(authTrustLevel, testAtl);
211 EXPECT_EQ(callback, testCallback);
212 return 0;
213 });
214 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
215 ON_CALL(*obj, SendRequest)
__anonc44a677e0a02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 216 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
217 service->OnRemoteRequest(code, data, reply, option);
218 return SUCCESS;
219 });
220 proxy->AuthUser(testUserId, testChallenge, testAuthType, testAtl, testCallback);
221 }
222
223 HWTEST_F(UserAuthProxyTest, UserAuthProxyCancelAuthOrIdentify, TestSize.Level0)
224 {
225 static const uint64_t testContextId = 200;
226
227 sptr<MockRemoteObject> obj = new MockRemoteObject();
228 EXPECT_NE(obj, nullptr);
229 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
230 EXPECT_NE(proxy, nullptr);
231 auto service = Common::MakeShared<MockUserAuthService>();
232 EXPECT_NE(service, nullptr);
233 EXPECT_CALL(*service, CancelAuthOrIdentify(_))
234 .Times(Exactly(1))
__anonc44a677e0b02(uint64_t contextId) 235 .WillOnce([](uint64_t contextId) {
236 EXPECT_EQ(contextId, testContextId);
237 return SUCCESS;
238 });
239 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
240 ON_CALL(*obj, SendRequest)
__anonc44a677e0c02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 241 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
242 service->OnRemoteRequest(code, data, reply, option);
243 return SUCCESS;
244 });
245 proxy->CancelAuthOrIdentify(testContextId);
246 }
247
248 HWTEST_F(UserAuthProxyTest, UserAuthProxyIdentify, TestSize.Level0)
249 {
250 static const AuthType testAuthType = FACE;
251 const std::vector<uint8_t> testChallenge = {1, 2, 3, 4};
252
253 sptr<MockRemoteObject> obj = new MockRemoteObject();
254 EXPECT_NE(obj, nullptr);
255 auto proxy = Common::MakeShared<UserAuthProxy>(obj);
256 EXPECT_NE(proxy, nullptr);
257 auto identifyCallback = Common::MakeShared<MockIdentificationCallback>();
258 EXPECT_NE(identifyCallback, nullptr);
259 sptr<UserAuthCallbackInterface> testCallback =
260 new (std::nothrow) UserAuthCallbackService(identifyCallback);
261 auto service = Common::MakeShared<MockUserAuthService>();
262 EXPECT_NE(service, nullptr);
263 EXPECT_CALL(*service, Identify(_, _, _))
264 .Times(Exactly(1))
265 .WillOnce([&testCallback](const std::vector<uint8_t> &challenge, AuthType authType,
__anonc44a677e0d02(const std::vector<uint8_t> &challenge, AuthType authType, sptr<UserAuthCallbackInterface> &callback) 266 sptr<UserAuthCallbackInterface> &callback) {
267 EXPECT_THAT(challenge, ElementsAre(1, 2, 3, 4));
268 EXPECT_EQ(authType, testAuthType);
269 EXPECT_EQ(callback, testCallback);
270 return 0;
271 });
272 EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
273 ON_CALL(*obj, SendRequest)
__anonc44a677e0e02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 274 .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
275 service->OnRemoteRequest(code, data, reply, option);
276 return SUCCESS;
277 });
278 proxy->Identify(testChallenge, testAuthType, testCallback);
279 }
280 } // namespace UserAuth
281 } // namespace UserIam
282 } // namespace OHOS