1 /*
2  * Copyright (C) 2022-2023 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 "co_auth_client_test.h"
17 
18 #include "co_auth_client.h"
19 #include "iam_ptr.h"
20 
21 namespace OHOS {
22 namespace UserIam {
23 namespace UserAuth {
24 using namespace testing;
25 using namespace testing::ext;
26 
SetUpTestCase()27 void CoAuthClientTest::SetUpTestCase()
28 {
29 }
30 
TearDownTestCase()31 void CoAuthClientTest::TearDownTestCase()
32 {
33 }
34 
SetUp()35 void CoAuthClientTest::SetUp()
36 {
37 }
38 
TearDown()39 void CoAuthClientTest::TearDown()
40 {
41 }
42 
43 HWTEST_F(CoAuthClientTest, CoAuthClientRegister_001, TestSize.Level0)
44 {
45     ExecutorInfo testInfo = {};
46     std::shared_ptr<ExecutorRegisterCallback> testCallback = nullptr;
47 
48     CoAuthClient::GetInstance().Register(testInfo, testCallback);
49 
50     testInfo.authType = PIN;
51     testInfo.executorRole = COLLECTOR;
52     testInfo.executorSensorHint = 11;
53     testInfo.executorMatcher = 22;
54     testInfo.esl = ESL1;
55     testInfo.publicKey = {1, 2, 3, 4};
56 
57     uint64_t testExecutorIndex = 73265;
58 
59     testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
60     EXPECT_NE(testCallback, nullptr);
61 
62     auto service = Common::MakeShared<MockCoAuthService>();
63     EXPECT_NE(service, nullptr);
64     EXPECT_CALL(*service, ExecutorRegister(_, _)).Times(1);
65     ON_CALL(*service, ExecutorRegister)
66         .WillByDefault(
67             [&testInfo, &testExecutorIndex](const CoAuthInterface::ExecutorRegisterInfo &info,
__anon0a7644260102(const CoAuthInterface::ExecutorRegisterInfo &info, sptr<ExecutorCallbackInterface> &callback) 68                 sptr<ExecutorCallbackInterface> &callback) {
69                 EXPECT_EQ(testInfo.authType, info.authType);
70                 EXPECT_EQ(testInfo.executorRole, info.executorRole);
71                 EXPECT_EQ(testInfo.executorSensorHint, info.executorSensorHint);
72                 EXPECT_EQ(testInfo.executorMatcher, info.executorMatcher);
73                 EXPECT_EQ(testInfo.esl, info.esl);
74                 EXPECT_THAT(testInfo.publicKey, ElementsAreArray(info.publicKey));
75                 return testExecutorIndex;
76             }
77         );
78 
79     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
80     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
81     CallRemoteObject(service, obj, dr, 73265);
82 
83 
84     CoAuthClient::GetInstance().Register(testInfo, testCallback);
85     EXPECT_NE(dr, nullptr);
86     dr->OnRemoteDied(obj);
87     IpcClientUtils::ResetObj();
88 }
89 
CallRemoteObject(const std::shared_ptr<MockCoAuthService> service,const sptr<MockRemoteObject> & obj,sptr<IRemoteObject::DeathRecipient> & dr,uint64_t testExecutorIndex)90 void CoAuthClientTest::CallRemoteObject(const std::shared_ptr<MockCoAuthService> service,
91     const sptr<MockRemoteObject> &obj, sptr<IRemoteObject::DeathRecipient> &dr, uint64_t testExecutorIndex)
92 {
93     EXPECT_NE(obj, nullptr);
94     IpcClientUtils::SetObj(obj);
95     EXPECT_CALL(*obj, IsProxyObject()).WillRepeatedly(Return(true));
96     EXPECT_CALL(*obj, RemoveDeathRecipient(_)).WillRepeatedly(Return(true));
97     EXPECT_CALL(*obj, AddDeathRecipient(_))
98         .WillRepeatedly([&dr](const sptr<IRemoteObject::DeathRecipient> &recipient) {
99             dr = recipient;
100             return true;
101         });
102 
103     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).Times(1);
104     ON_CALL(*obj, SendRequest)
105         .WillByDefault([&service, testExecutorIndex](uint32_t code, MessageParcel &data, MessageParcel &reply,
106             MessageOption &option) {
107             service->OnRemoteRequest(code, data, reply, option);
108             uint64_t executorIndex = 0;
109             EXPECT_TRUE(reply.ReadUint64(executorIndex));
110             EXPECT_EQ(executorIndex, testExecutorIndex);
111             return OHOS::NO_ERROR;
112         });
113 }
114 
115 HWTEST_F(CoAuthClientTest, CoAuthClientRegister_002, TestSize.Level0)
116 {
117     ExecutorInfo testInfo = {};
118     IpcClientUtils::ResetObj();
119     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
120     EXPECT_NE(testCallback, nullptr);
121 
122     CoAuthClient::GetInstance().Register(testInfo, testCallback);
123 }
124 
125 HWTEST_F(CoAuthClientTest, CoAuthClientRegister_004, TestSize.Level0)
126 {
127     sptr<MockRemoteObject> obj(new (std::nothrow) MockRemoteObject());
128     EXPECT_NE(obj, nullptr);
129     EXPECT_CALL(*obj, IsProxyObject()).WillRepeatedly(Return(true));
130 
131     sptr<IRemoteObject::DeathRecipient> dr(nullptr);
132     EXPECT_CALL(*obj, RemoveDeathRecipient(_)).WillRepeatedly(Return(true));
133     EXPECT_CALL(*obj, AddDeathRecipient(_))
134         .WillOnce(Return(false))
__anon0a7644260402(const sptr<IRemoteObject::DeathRecipient> &recipient) 135         .WillRepeatedly([&dr](const sptr<IRemoteObject::DeathRecipient> &recipient) {
136             dr = recipient;
137             return true;
138         });
139 
140     EXPECT_CALL(*obj, SendRequest(_, _, _, _)).WillRepeatedly(Return(OHOS::NO_ERROR));
141 
142     IpcClientUtils::SetObj(obj);
143 
144     ExecutorInfo testInfo = {};
145     auto testCallback = Common::MakeShared<MockExecutorRegisterCallback>();
146     EXPECT_NE(testCallback, nullptr);
147     CoAuthClient::GetInstance().Register(testInfo, testCallback);
148     CoAuthClient::GetInstance().Register(testInfo, testCallback);
149     CoAuthClient::GetInstance().Register(testInfo, testCallback);
150 
151     EXPECT_NE(dr, nullptr);
152     sptr<IRemoteObject> remote(nullptr);
153     dr->OnRemoteDied(remote);
154     dr->OnRemoteDied(obj);
155     IpcClientUtils::ResetObj();
156 }
157 } // namespace UserAuth
158 } // namespace UserIam
159 } // namespace OHOS