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_proxy_test.h"
17 
18 #include "iam_ptr.h"
19 #include "user_idm_proxy.h"
20 #include "mock_remote_object.h"
21 #include "mock_user_idm_service.h"
22 #include "mock_user_idm_client_callback.h"
23 #include "user_idm_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 UserIdmProxyTest::SetUpTestCase()
32 {
33 }
34 
TearDownTestCase()35 void UserIdmProxyTest::TearDownTestCase()
36 {
37 }
38 
SetUp()39 void UserIdmProxyTest::SetUp()
40 {
41 }
42 
TearDown()43 void UserIdmProxyTest::TearDown()
44 {
45 }
46 
47 HWTEST_F(UserIdmProxyTest, UserIdmProxyOpenSession, TestSize.Level0)
48 {
49     static const int32_t testUserId = 200;
50     std::vector<uint8_t> testChallenge;
51 
52     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
53     EXPECT_NE(obj, nullptr);
54     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
55     EXPECT_NE(proxy, nullptr);
56     auto service = Common::MakeShared<MockUserIdmService>();
57     EXPECT_NE(service, nullptr);
58     EXPECT_CALL(*service, OpenSession(_, _))
59         .Times(Exactly(1))
__anonad2ce9fe0102(int32_t userId, std::vector<uint8_t> &challenge) 60         .WillOnce([](int32_t userId, std::vector<uint8_t> &challenge) {
61             EXPECT_EQ(testUserId, userId);
62             return SUCCESS;
63         });
64     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
65     ON_CALL(*obj, SendRequest)
__anonad2ce9fe0202(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 66         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
67             service->OnRemoteRequest(code, data, reply, option);
68             return SUCCESS;
69         });
70     proxy->OpenSession(testUserId, testChallenge);
71 }
72 
73 HWTEST_F(UserIdmProxyTest, UserIdmProxyCloseSession, TestSize.Level0)
74 {
75     static const int32_t testUserId = 200;
76 
77     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
78     EXPECT_NE(obj, nullptr);
79     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
80     EXPECT_NE(proxy, nullptr);
81     auto service = Common::MakeShared<MockUserIdmService>();
82     EXPECT_NE(service, nullptr);
83     EXPECT_CALL(*service, CloseSession(_))
84         .Times(Exactly(1))
__anonad2ce9fe0302(int32_t userId) 85         .WillOnce([](int32_t userId) {
86             EXPECT_EQ(testUserId, userId);
87             return SUCCESS;
88         });
89     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
90     ON_CALL(*obj, SendRequest)
__anonad2ce9fe0402(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 91         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
92             service->OnRemoteRequest(code, data, reply, option);
93             return SUCCESS;
94         });
95     proxy->CloseSession(testUserId);
96 }
97 
98 HWTEST_F(UserIdmProxyTest, UserIdmProxyGetCredentialInfo, TestSize.Level0)
99 {
100     static const int32_t testUserId = 200;
101     static const AuthType testAuthType = PIN;
102 
103     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
104     EXPECT_NE(obj, nullptr);
105     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
106     EXPECT_NE(proxy, nullptr);
107     auto getCredInfoCallback = Common::MakeShared<MockGetCredentialInfoCallback>();
108     EXPECT_NE(getCredInfoCallback, nullptr);
109     sptr<IdmGetCredInfoCallbackInterface> testCallback(
110         new (std::nothrow) IdmGetCredInfoCallbackService(getCredInfoCallback));
111     auto service = Common::MakeShared<MockUserIdmService>();
112     EXPECT_NE(service, nullptr);
113     EXPECT_CALL(*service, GetCredentialInfo(_, _, _))
114         .Times(Exactly(1))
115         .WillOnce([&testCallback](int32_t userId, AuthType authType,
__anonad2ce9fe0502(int32_t userId, AuthType authType, const sptr<IdmGetCredInfoCallbackInterface> &callback) 116             const sptr<IdmGetCredInfoCallbackInterface> &callback) {
117             EXPECT_EQ(testUserId, userId);
118             EXPECT_EQ(testAuthType, authType);
119             EXPECT_EQ(testCallback, callback);
120             return SUCCESS;
121         });
122     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
123     ON_CALL(*obj, SendRequest)
__anonad2ce9fe0602(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 124         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
125             service->OnRemoteRequest(code, data, reply, option);
126             return SUCCESS;
127         });
128     proxy->GetCredentialInfo(testUserId, testAuthType, testCallback);
129 }
130 
131 HWTEST_F(UserIdmProxyTest, UserIdmProxyGetSecInfo, TestSize.Level0)
132 {
133     static const int32_t testUserId = 200;
134 
135     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
136     EXPECT_NE(obj, nullptr);
137     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
138     EXPECT_NE(proxy, nullptr);
139     auto getSecInfoCallback = Common::MakeShared<MockGetSecUserInfoCallback>();
140     EXPECT_NE(getSecInfoCallback, nullptr);
141     sptr<IdmGetSecureUserInfoCallbackInterface> testCallback(
142         new (std::nothrow) IdmGetSecureUserInfoCallbackService(getSecInfoCallback));
143     auto service = Common::MakeShared<MockUserIdmService>();
144     EXPECT_NE(service, nullptr);
145     EXPECT_CALL(*service, GetSecInfo(_, _))
146         .Times(Exactly(1))
__anonad2ce9fe0702(int32_t userId, const sptr<IdmGetSecureUserInfoCallbackInterface> &callback) 147         .WillOnce([&testCallback](int32_t userId, const sptr<IdmGetSecureUserInfoCallbackInterface> &callback) {
148             EXPECT_EQ(testUserId, userId);
149             EXPECT_EQ(testCallback, callback);
150             return SUCCESS;
151         });
152     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
153     ON_CALL(*obj, SendRequest)
__anonad2ce9fe0802(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 154         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
155             service->OnRemoteRequest(code, data, reply, option);
156             return SUCCESS;
157         });
158     proxy->GetSecInfo(testUserId, testCallback);
159 }
160 
161 HWTEST_F(UserIdmProxyTest, UserIdmProxyAddCredential, TestSize.Level0)
162 {
163     static const int32_t testUserId = 200;
164     UserIdmInterface::CredentialPara testCredPara = {};
165     testCredPara.authType = FACE;
166     testCredPara.pinType = PIN_SIX;
167     testCredPara.token = {1, 2, 3, 4};
168 
169     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
170     EXPECT_NE(obj, nullptr);
171     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
172     EXPECT_NE(proxy, nullptr);
173     auto idmCallback = Common::MakeShared<MockUserIdmClientCallback>();
174     EXPECT_NE(idmCallback, nullptr);
175     sptr<IdmCallbackInterface> testCallback(new (std::nothrow) IdmCallbackService(idmCallback));
176     auto service = Common::MakeShared<MockUserIdmService>();
177     EXPECT_NE(service, nullptr);
178     EXPECT_CALL(*service, AddCredential(_, _, _, _))
179         .Times(Exactly(1))
180         .WillOnce([&testCredPara](int32_t userId, const UserIdmInterface::CredentialPara &credPara,
__anonad2ce9fe0902(int32_t userId, const UserIdmInterface::CredentialPara &credPara, const sptr<IdmCallbackInterface> &callback, bool isUpdate) 181             const sptr<IdmCallbackInterface> &callback, bool isUpdate) {
182             EXPECT_EQ(userId, testUserId);
183             EXPECT_EQ(credPara.authType, testCredPara.authType);
184             EXPECT_EQ(credPara.pinType, testCredPara.pinType);
185             EXPECT_THAT(credPara.token, ElementsAreArray(testCredPara.token));
186             return SUCCESS;
187         });
188     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
189     ON_CALL(*obj, SendRequest)
__anonad2ce9fe0a02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 190         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
191             service->OnRemoteRequest(code, data, reply, option);
192             return SUCCESS;
193         });
194     proxy->AddCredential(testUserId, testCredPara, testCallback, false);
195 }
196 
197 HWTEST_F(UserIdmProxyTest, UserIdmProxyUpdateCredential, TestSize.Level0)
198 {
199     static const int32_t testUserId = 200;
200     UserIdmInterface::CredentialPara testCredPara = {};
201     testCredPara.authType = FACE;
202     testCredPara.pinType = PIN_SIX;
203     testCredPara.token = {1, 2, 3, 4};
204 
205     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
206     EXPECT_NE(obj, nullptr);
207     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
208     EXPECT_NE(proxy, nullptr);
209     auto idmCallback = Common::MakeShared<MockUserIdmClientCallback>();
210     EXPECT_NE(idmCallback, nullptr);
211     sptr<IdmCallbackInterface> testCallback(new (std::nothrow) IdmCallbackService(idmCallback));
212     auto service = Common::MakeShared<MockUserIdmService>();
213     EXPECT_NE(service, nullptr);
214     EXPECT_CALL(*service, UpdateCredential(_, _, _))
215         .Times(Exactly(1))
216         .WillOnce([&testCredPara](int32_t userId, const UserIdmInterface::CredentialPara &credPara,
__anonad2ce9fe0b02(int32_t userId, const UserIdmInterface::CredentialPara &credPara, const sptr<IdmCallbackInterface> &callback) 217             const sptr<IdmCallbackInterface> &callback) {
218             EXPECT_EQ(userId, testUserId);
219             EXPECT_EQ(credPara.authType, testCredPara.authType);
220             EXPECT_EQ(credPara.pinType, testCredPara.pinType);
221             EXPECT_THAT(credPara.token, ElementsAreArray(testCredPara.token));
222             return SUCCESS;
223         });
224     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
225     ON_CALL(*obj, SendRequest)
__anonad2ce9fe0c02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 226         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
227             service->OnRemoteRequest(code, data, reply, option);
228             return SUCCESS;
229         });
230     proxy->UpdateCredential(testUserId, testCredPara, testCallback);
231 }
232 
233 HWTEST_F(UserIdmProxyTest, UserIdmProxyCancel, TestSize.Level0)
234 {
235     static const int32_t testUserId = 200;
236 
237     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
238     EXPECT_NE(obj, nullptr);
239     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
240     EXPECT_NE(proxy, nullptr);
241     auto service = Common::MakeShared<MockUserIdmService>();
242     EXPECT_NE(service, nullptr);
243     EXPECT_CALL(*service, Cancel(_))
244         .Times(Exactly(1))
__anonad2ce9fe0d02(int32_t userId) 245         .WillOnce([](int32_t userId) {
246             EXPECT_EQ(testUserId, userId);
247             return SUCCESS;
248         });
249     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
250     ON_CALL(*obj, SendRequest)
__anonad2ce9fe0e02(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 251         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
252             service->OnRemoteRequest(code, data, reply, option);
253             return SUCCESS;
254         });
255     proxy->Cancel(testUserId);
256 }
257 
258 HWTEST_F(UserIdmProxyTest, UserIdmProxyEnforceDelUser, TestSize.Level0)
259 {
260     static const int32_t testUserId = 200;
261 
262     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
263     EXPECT_NE(obj, nullptr);
264     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
265     EXPECT_NE(proxy, nullptr);
266     auto idmCallback = Common::MakeShared<MockUserIdmClientCallback>();
267     EXPECT_NE(idmCallback, nullptr);
268     sptr<IdmCallbackInterface> testCallback(new (std::nothrow) IdmCallbackService(idmCallback));
269     auto service = Common::MakeShared<MockUserIdmService>();
270     EXPECT_NE(service, nullptr);
271     EXPECT_CALL(*service, EnforceDelUser(_, _))
272         .Times(Exactly(1))
__anonad2ce9fe0f02(int32_t userId, const sptr<IdmCallbackInterface> &callback) 273         .WillOnce([&testCallback](int32_t userId, const sptr<IdmCallbackInterface> &callback) {
274             EXPECT_EQ(testUserId, userId);
275             EXPECT_EQ(testCallback, callback);
276             return SUCCESS;
277         });
278     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
279     ON_CALL(*obj, SendRequest)
__anonad2ce9fe1002(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 280         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
281             service->OnRemoteRequest(code, data, reply, option);
282             return SUCCESS;
283         });
284     proxy->EnforceDelUser(testUserId, testCallback);
285 }
286 
287 HWTEST_F(UserIdmProxyTest, UserIdmProxyDelUser, TestSize.Level0)
288 {
289     static const int32_t testUserId = 200;
290     static const std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
291 
292     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
293     EXPECT_NE(obj, nullptr);
294     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
295     EXPECT_NE(proxy, nullptr);
296     auto idmCallback = Common::MakeShared<MockUserIdmClientCallback>();
297     EXPECT_NE(idmCallback, nullptr);
298     sptr<IdmCallbackInterface> testCallback(new (std::nothrow) IdmCallbackService(idmCallback));
299     auto service = Common::MakeShared<MockUserIdmService>();
300     EXPECT_NE(service, nullptr);
301     EXPECT_CALL(*service, DelUser(_, _, _))
302         .Times(Exactly(1))
303         .WillOnce([&testCallback](int32_t userId, const std::vector<uint8_t> authToken,
__anonad2ce9fe1102(int32_t userId, const std::vector<uint8_t> authToken, const sptr<IdmCallbackInterface> &callback) 304             const sptr<IdmCallbackInterface> &callback) {
305             EXPECT_EQ(testUserId, userId);
306             EXPECT_THAT(testAuthToken, ElementsAre(1, 2, 3, 4));
307             EXPECT_EQ(testCallback, callback);
308             return SUCCESS;
309         });
310     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
311     ON_CALL(*obj, SendRequest)
__anonad2ce9fe1202(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 312         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
313             service->OnRemoteRequest(code, data, reply, option);
314             return SUCCESS;
315         });
316     proxy->DelUser(testUserId, testAuthToken, testCallback);
317 }
318 
319 HWTEST_F(UserIdmProxyTest, UserIdmProxyDelCredential, TestSize.Level0)
320 {
321     static const int32_t testUserId = 200;
322     static const uint64_t testCredentialId = 300;
323     static const std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
324 
325     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
326     EXPECT_NE(obj, nullptr);
327     auto proxy = Common::MakeShared<UserIdmProxy>(obj);
328     EXPECT_NE(proxy, nullptr);
329     auto idmCallback = Common::MakeShared<MockUserIdmClientCallback>();
330     EXPECT_NE(idmCallback, nullptr);
331     sptr<IdmCallbackInterface> testCallback(new (std::nothrow) IdmCallbackService(idmCallback));
332     auto service = Common::MakeShared<MockUserIdmService>();
333     EXPECT_NE(service, nullptr);
334     EXPECT_CALL(*service, DelCredential(_, _, _, _))
335         .Times(Exactly(1))
336         .WillOnce([&testCallback](int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken,
__anonad2ce9fe1302(int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, const sptr<IdmCallbackInterface> &callback) 337             const sptr<IdmCallbackInterface> &callback) {
338             EXPECT_EQ(testUserId, userId);
339             EXPECT_EQ(testCredentialId, credentialId);
340             EXPECT_THAT(testAuthToken, ElementsAre(1, 2, 3, 4));
341             EXPECT_EQ(testCallback, callback);
342             return SUCCESS;
343         });
344     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
345     ON_CALL(*obj, SendRequest)
__anonad2ce9fe1402(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 346         .WillByDefault([&service](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
347             service->OnRemoteRequest(code, data, reply, option);
348             return SUCCESS;
349         });
350     proxy->DelCredential(testUserId, testCredentialId, testAuthToken, testCallback);
351 }
352 } // namespace UserAuth
353 } // namespace UserIam
354 } // namespace OHOS