1 /*
2 * Copyright (c) 2024 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
18 #include "client_death_handler.h"
19 #include "iremote_object.h"
20 #include "mmi_log.h"
21
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "ClientDeathHandlerTest"
24
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 using namespace testing::ext;
29 } // namespace
30
31 class ClientDeathHandlerTest : public testing::Test {
32 public:
SetUpTestCase(void)33 static void SetUpTestCase(void) {}
TearDownTestCase(void)34 static void TearDownTestCase(void) {}
SetUp()35 void SetUp() {}
TearDown()36 void TearDown() {}
37 };
38
FunctionTest(int32_t)39 void FunctionTest(int32_t) {}
40
41 class RemoteObjectTest : public IRemoteObject {
42 public:
RemoteObjectTest(std::u16string descriptor)43 explicit RemoteObjectTest(std::u16string descriptor) : IRemoteObject(descriptor) {}
~RemoteObjectTest()44 ~RemoteObjectTest() {}
45
GetObjectRefCount()46 int32_t GetObjectRefCount() { return 0; }
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)47 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { return 0; }
AddDeathRecipient(const sptr<DeathRecipient> & recipient)48 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient)
49 {
50 return result;
51 }
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)52 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) { return true; }
Dump(int fd,const std::vector<std::u16string> & args)53 int Dump(int fd, const std::vector<std::u16string> &args) { return 0; }
54
55 public:
56 bool result = false;
57 };
58
59 /**
60 * @tc.name: ClientDeathHandlerTest_RegisterClientDeathRecipient
61 * @tc.desc: Test RegisterClientDeathRecipient
62 * @tc.type: FUNC
63 * @tc.require:
64 */
65 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_RegisterClientDeathRecipient, TestSize.Level1)
66 {
67 CALL_TEST_DEBUG;
68 ClientDeathHandler clientDeathHdl;
69 sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
70 int32_t pid = 10;
71 remote->result = false;
72 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
73 EXPECT_FALSE(clientDeathHdl.RegisterClientDeathRecipient(remote, pid));
74 }
75
76 /**
77 * @tc.name: ClientDeathHandlerTest_RegisterClientDeathRecipient_001
78 * @tc.desc: Test RegisterClientDeathRecipient
79 * @tc.type: FUNC
80 * @tc.require:
81 */
82 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_RegisterClientDeathRecipient_001, TestSize.Level1)
83 {
84 CALL_TEST_DEBUG;
85 ClientDeathHandler clientDeathHdl;
86 sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
87 int32_t pid = 10;
88 remote->result = true;
89 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
__anon83cc367c0202(const wptr<IRemoteObject> &object) 90 auto deathCallback = [this](const wptr<IRemoteObject> &object) {
91 CALL_DEBUG_ENTER;
92 };
93 clientDeathHdl.deathRecipient_ = new (std::nothrow) InputBinderClientDeathRecipient(deathCallback);
94 EXPECT_TRUE(clientDeathHdl.RegisterClientDeathRecipient(remote, pid));
95 }
96
97 /**
98 * @tc.name: ClientDeathHandlerTest_AddClientDeathCallback
99 * @tc.desc: Test AddClientDeathCallback
100 * @tc.type: FUNC
101 * @tc.require:
102 */
103 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_AddClientDeathCallback, TestSize.Level1)
104 {
105 CALL_TEST_DEBUG;
106 ClientDeathHandler clientDeathHdl;
107 CallBackType type = CallBackType::CALLBACK_TYPE_AUTHORIZE_HELPER;
108 ClientDeathCallback callback = FunctionTest;
109 clientDeathHdl.deathCallbacks_.insert(std::make_pair(type, callback));
110 EXPECT_FALSE(clientDeathHdl.AddClientDeathCallback(type, callback));
111 }
112
113 /**
114 * @tc.name: ClientDeathHandlerTest_AddClientDeathCallback_001
115 * @tc.desc: Test AddClientDeathCallback
116 * @tc.type: FUNC
117 * @tc.require:
118 */
119 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_AddClientDeathCallback_001, TestSize.Level1)
120 {
121 CALL_TEST_DEBUG;
122 ClientDeathHandler clientDeathHdl;
123 CallBackType type = CallBackType::CALLBACK_TYPE_AUTHORIZE_HELPER;
124 ClientDeathCallback callback = FunctionTest;
125 clientDeathHdl.deathCallbacks_.insert(std::make_pair(type, callback));
126 type = static_cast<CallBackType>(10);
127 EXPECT_TRUE(clientDeathHdl.AddClientDeathCallback(type, callback));
128 }
129
130 /**
131 * @tc.name: ClientDeathHandlerTest_AddClientPid
132 * @tc.desc: Test AddClientPid
133 * @tc.type: FUNC
134 * @tc.require:
135 */
136 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_AddClientPid, TestSize.Level1)
137 {
138 CALL_TEST_DEBUG;
139 ClientDeathHandler clientDeathHdl;
140 sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
141 int32_t pid = 10;
142 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
143 EXPECT_TRUE(clientDeathHdl.AddClientPid(remote, pid));
144
145 pid = 11;
146 EXPECT_TRUE(clientDeathHdl.AddClientPid(remote, pid));
147 }
148
149 /**
150 * @tc.name: ClientDeathHandlerTest_RemoveClientPid
151 * @tc.desc: Test RemoveClientPid
152 * @tc.type: FUNC
153 * @tc.require:
154 */
155 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_RemoveClientPid, TestSize.Level1)
156 {
157 CALL_TEST_DEBUG;
158 ClientDeathHandler clientDeathHdl;
159 sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
160 int32_t pid = 123456;
161 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
162 pid = 123;
163 EXPECT_NO_FATAL_FAILURE(clientDeathHdl.RemoveClientPid(pid));
164
165 pid = 123456;
166 EXPECT_NO_FATAL_FAILURE(clientDeathHdl.RemoveClientPid(pid));
167 }
168
169 /**
170 * @tc.name: ClientDeathHandlerTest_FindClientPid
171 * @tc.desc: Test FindClientPid
172 * @tc.type: FUNC
173 * @tc.require:
174 */
175 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_FindClientPid, TestSize.Level1)
176 {
177 CALL_TEST_DEBUG;
178 ClientDeathHandler clientDeathHdl;
179 sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
180 int32_t pid = 1000;
181 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
182 EXPECT_EQ(clientDeathHdl.FindClientPid(remote), pid);
183 pid = 2000;
184 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
185 EXPECT_EQ(clientDeathHdl.FindClientPid(remote), 1000);
186 }
187
188 /**
189 * @tc.name: ClientDeathHandlerTest_FindClientPid_001
190 * @tc.desc: Test FindClientPid
191 * @tc.type: FUNC
192 * @tc.require:
193 */
194 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_FindClientPid_001, TestSize.Level1)
195 {
196 CALL_TEST_DEBUG;
197 ClientDeathHandler clientDeathHdl;
198 sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
199 int32_t pid = 1000;
200 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
201 sptr<RemoteObjectTest> remoteObject = new RemoteObjectTest(u"test1");
202 EXPECT_EQ(clientDeathHdl.FindClientPid(remoteObject), INVALID_PID);
203 }
204
205 /**
206 * @tc.name: ClientDeathHandlerTest_OnDeath
207 * @tc.desc: Test OnDeath
208 * @tc.type: FUNC
209 * @tc.require:
210 */
211 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_OnDeath, TestSize.Level1)
212 {
213 CALL_TEST_DEBUG;
214 ClientDeathHandler clientDeathHdl;
215 sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
216 int32_t pid = 1000;
217 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
218 wptr<RemoteObjectTest> remoteObject = new RemoteObjectTest(u"test1");
219 EXPECT_NO_FATAL_FAILURE(clientDeathHdl.OnDeath(remoteObject));
220 }
221
222 /**
223 * @tc.name: ClientDeathHandlerTest_OnDeath_001
224 * @tc.desc: Test OnDeath
225 * @tc.type: FUNC
226 * @tc.require:
227 */
228 HWTEST_F(ClientDeathHandlerTest, ClientDeathHandlerTest_OnDeath_001, TestSize.Level1)
229 {
230 CALL_TEST_DEBUG;
231 ClientDeathHandler clientDeathHdl;
232 sptr<RemoteObjectTest> remote = new RemoteObjectTest(u"test");
233 int32_t pid = 1000;
234 clientDeathHdl.clientPidMap_.insert(std::make_pair(pid, remote));
235 EXPECT_NO_FATAL_FAILURE(clientDeathHdl.OnDeath(remote));
236 }
237 } // namespace MMI
238 } // namespace OHOS