1 /*
2 * Copyright (c) 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 <gtest/gtest.h>
17
18 #define private public
19 #include "app_debug_manager.h"
20 #include "app_debug_listener_proxy.h"
21 #undef private
22
23 #include "mock_app_debug_listener_stub.h"
24 #include "parcel.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 std::string DEBUG_START_NAME = "debugStartBundle";
32 std::string NO_DEBUG_START_NAME = "noDebugStartBundle";
33 const bool IS_DEBUG_START = true;
34 const bool NO_DEBUG_START = false;
35 const unsigned int SIZE_ONE = 1;
36 const unsigned int SIZE_TWO = 2;
37 const unsigned int SIZE_THREE = 3;
38 }
39 class AppDebugManagerTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp() override;
44 void TearDown() override;
45
46 std::shared_ptr<AppDebugManager> manager_;
47 sptr<MockAppDebugListenerStub> listener_;
48 };
49
SetUpTestCase(void)50 void AppDebugManagerTest::SetUpTestCase(void)
51 {}
52
TearDownTestCase(void)53 void AppDebugManagerTest::TearDownTestCase(void)
54 {}
55
SetUp()56 void AppDebugManagerTest::SetUp()
57 {
58 manager_ = std::make_shared<AppDebugManager>();
59 listener_ = new MockAppDebugListenerStub();
60 manager_->listeners_.insert(listener_);
61 }
62
TearDown()63 void AppDebugManagerTest::TearDown()
64 {}
65
66 /**
67 * @tc.name: RegisterAppDebugListener_0100
68 * @tc.desc: Register listener for app debug listener, check nullptr listener.
69 * @tc.type: FUNC
70 */
71 HWTEST_F(AppDebugManagerTest, RegisterAppDebugListener_0100, TestSize.Level1)
72 {
73 sptr<MockAppDebugListenerStub> listener = new MockAppDebugListenerStub();
74 AppDebugInfo appDebugInfo;
75 manager_->debugInfos_.push_back(appDebugInfo);
76
77 EXPECT_CALL(*listener, OnAppDebugStarted(_)).Times(1);
78 auto result = manager_->RegisterAppDebugListener(listener);
79 EXPECT_EQ(result, ERR_OK);
80 EXPECT_EQ(manager_->listeners_.size(), SIZE_TWO);
81
82 listener = nullptr;
83 result = manager_->RegisterAppDebugListener(listener);
84 EXPECT_EQ(result, ERR_INVALID_DATA);
85 EXPECT_EQ(manager_->listeners_.size(), SIZE_TWO);
86 }
87
88 /**
89 * @tc.name: UnregisterAppDebugListener_0100
90 * @tc.desc: Unregister listener for app debug listener, check nullptr listener.
91 * @tc.type: FUNC
92 */
93 HWTEST_F(AppDebugManagerTest, UnregisterAppDebugListener_0100, TestSize.Level1)
94 {
95 EXPECT_NE(manager_, nullptr);
96 sptr<MockAppDebugListenerStub> listener = nullptr;
97 auto result = manager_->UnregisterAppDebugListener(listener);
98 EXPECT_EQ(result, ERR_INVALID_DATA);
99 EXPECT_EQ(manager_->listeners_.size(), SIZE_ONE);
100
101 EXPECT_NE(listener_, nullptr);
102 result = manager_->UnregisterAppDebugListener(listener_);
103 EXPECT_EQ(result, ERR_OK);
104 EXPECT_TRUE(manager_->listeners_.empty());
105 }
106
107 /**
108 * @tc.name: StartDebug_0100
109 * @tc.desc: Start debug by AppDebugInfo, notify AppDebugListener.
110 * @tc.type: FUNC
111 */
112 HWTEST_F(AppDebugManagerTest, StartDebug_0100, TestSize.Level1)
113 {
114 EXPECT_NE(manager_, nullptr);
115 EXPECT_TRUE(manager_->debugInfos_.empty());
116 std::vector<AppDebugInfo> debugInfos;
117 AppDebugInfo info;
118 debugInfos.push_back(info);
119
120 EXPECT_NE(listener_, nullptr);
121 EXPECT_CALL(*listener_, OnAppDebugStarted(_)).Times(1);
122 manager_->StartDebug(debugInfos);
123 EXPECT_FALSE(manager_->debugInfos_.empty());
124 }
125
126 /**
127 * @tc.name: StopDebug_0100
128 * @tc.desc: Start debug by AppDebugInfo, notify AppDebugListener.
129 * @tc.type: FUNC
130 */
131 HWTEST_F(AppDebugManagerTest, StopDebug_0100, TestSize.Level1)
132 {
133 EXPECT_NE(manager_, nullptr);
134 EXPECT_TRUE(manager_->debugInfos_.empty());
135 std::vector<AppDebugInfo> debugInfos;
136 AppDebugInfo info;
137 info.bundleName = DEBUG_START_NAME;
138 info.isDebugStart = IS_DEBUG_START;
139 info.pid = 10;
140 info.uid = 12345;
141 debugInfos.push_back(info);
142 manager_->debugInfos_ = debugInfos;
143
144 EXPECT_NE(listener_, nullptr);
145 EXPECT_CALL(*listener_, OnAppDebugStoped(_)).Times(1);
146 manager_->StopDebug(debugInfos);
147 EXPECT_TRUE(manager_->debugInfos_.empty());
148 }
149
150 /**
151 * @tc.name: IsAttachDebug_0100
152 * @tc.desc: Given the bundleName, return true if not DebugStart, otherwise return false.
153 * @tc.type: FUNC
154 */
155 HWTEST_F(AppDebugManagerTest, IsAttachDebug_0100, TestSize.Level1)
156 {
157 EXPECT_NE(manager_, nullptr);
158 AppDebugInfo debugStart_info;
159 debugStart_info.bundleName = DEBUG_START_NAME;
160 debugStart_info.isDebugStart = IS_DEBUG_START;
161 manager_->debugInfos_.push_back(debugStart_info);
162
163 AppDebugInfo noDebugStart_info;
164 noDebugStart_info.bundleName = NO_DEBUG_START_NAME;
165 noDebugStart_info.isDebugStart = NO_DEBUG_START;
166 manager_->debugInfos_.push_back(noDebugStart_info);
167
168 auto result = manager_->IsAttachDebug(DEBUG_START_NAME);
169 EXPECT_FALSE(result);
170
171 result = manager_->IsAttachDebug(NO_DEBUG_START_NAME);
172 EXPECT_TRUE(result);
173 }
174
175 /**
176 * @tc.name: GetIncrementAppDebugInfo_0100
177 * @tc.desc: Add new debug info into debugInfos, or update the isDebugStart flag.
178 * @tc.type: FUNC
179 */
180 HWTEST_F(AppDebugManagerTest, GetIncrementAppDebugInfo_0100, TestSize.Level1)
181 {
182 EXPECT_NE(manager_, nullptr);
183 EXPECT_TRUE(manager_->debugInfos_.empty());
184
185 std::vector<AppDebugInfo> debugInfos;
186 std::vector<AppDebugInfo> increment;
187 AppDebugInfo debugStart_info;
188 debugStart_info.bundleName = DEBUG_START_NAME;
189 debugStart_info.isDebugStart = IS_DEBUG_START;
190
191 int pid = 10;
192 int uid = 12345;
193 AppDebugInfo noDebugStart_info;
194 noDebugStart_info.bundleName = NO_DEBUG_START_NAME;
195 noDebugStart_info.isDebugStart = NO_DEBUG_START;
196 noDebugStart_info.pid = pid;
197 noDebugStart_info.pid = uid;
198 debugInfos.push_back(debugStart_info);
199 debugInfos.push_back(noDebugStart_info);
200
201 manager_->GetIncrementAppDebugInfos(debugInfos, increment);
202 EXPECT_EQ(manager_->debugInfos_.size(), SIZE_TWO);
203
204 increment.clear();
205 debugInfos.clear();
206 noDebugStart_info.isDebugStart = IS_DEBUG_START;
207 debugInfos.push_back(noDebugStart_info);
208
209 manager_->GetIncrementAppDebugInfos(debugInfos, increment);
210 EXPECT_EQ(manager_->debugInfos_.size(), SIZE_TWO);
211 EXPECT_TRUE(manager_->debugInfos_.at(1).isDebugStart);
212 }
213
214 /**
215 * @tc.name: RemoveAppDebugInfo_0100
216 * @tc.desc: Remove app debug info with bundleName, pid, uid and isDebugStart flag.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(AppDebugManagerTest, RemoveAppDebugInfo_0100, TestSize.Level1)
220 {
221 EXPECT_NE(manager_, nullptr);
222 EXPECT_TRUE(manager_->debugInfos_.empty());
223
224 std::vector<AppDebugInfo> debugInfos;
225 int pid = 10;
226 int uid = 12345;
227 AppDebugInfo debugInfo;
228 debugInfo.bundleName = DEBUG_START_NAME;
229 debugInfo.pid = pid;
230 debugInfo.uid = uid;
231 debugInfo.isDebugStart = IS_DEBUG_START;
232
233 manager_->debugInfos_.push_back(debugInfo);
234 EXPECT_EQ(manager_->debugInfos_.size(), SIZE_ONE);
235
236 EXPECT_CALL(*listener_, OnAppDebugStoped(_)).Times(1);
237 manager_->RemoveAppDebugInfo(debugInfo);
238 EXPECT_TRUE(manager_->debugInfos_.empty());
239 }
240 HWTEST_F(AppDebugManagerTest, RegisterAppDebugListener_0200, TestSize.Level1)
241 {
242 sptr<MockAppDebugListenerStub> listener = new MockAppDebugListenerStub();
243 AppDebugInfo appDebugInfo;
244 manager_->debugInfos_.push_back(appDebugInfo);
245
246 EXPECT_CALL(*listener, OnAppDebugStarted(_)).Times(1);
247 manager_->listeners_.insert(nullptr);
248 auto result = manager_->RegisterAppDebugListener(listener);
249 EXPECT_EQ(result, ERR_OK);
250 EXPECT_EQ(manager_->listeners_.size(), SIZE_THREE);
251
252 listener = nullptr;
253 result = manager_->RegisterAppDebugListener(listener);
254 EXPECT_EQ(result, ERR_INVALID_DATA);
255 EXPECT_EQ(manager_->listeners_.size(), SIZE_THREE);
256 }
257
258 /**
259 * @tc.name: UnregisterAppDebugListener_0200
260 * @tc.desc: Unregister listener for app debug listener, check nullptr listener.
261 * @tc.type: FUNC
262 */
263 HWTEST_F(AppDebugManagerTest, UnregisterAppDebugListener_0200, TestSize.Level1)
264 {
265 EXPECT_NE(manager_, nullptr);
266 sptr<MockAppDebugListenerStub> listener = nullptr;
267 auto result = manager_->UnregisterAppDebugListener(listener);
268 EXPECT_EQ(result, ERR_INVALID_DATA);
269 EXPECT_EQ(manager_->listeners_.size(), SIZE_ONE);
270
271 EXPECT_NE(listener_, nullptr);
272 manager_->listeners_.insert(nullptr);
273 result = manager_->UnregisterAppDebugListener(listener_);
274 EXPECT_EQ(result, ERR_OK);
275 EXPECT_EQ(manager_->listeners_.size(), SIZE_ONE);
276 }
277
278 /**
279 * @tc.name: StartDebug_0200
280 * @tc.desc: Start debug by AppDebugInfo, notify AppDebugListener.
281 * @tc.type: FUNC
282 */
283 HWTEST_F(AppDebugManagerTest, StartDebug_0200, TestSize.Level1)
284 {
285 EXPECT_NE(manager_, nullptr);
286 EXPECT_TRUE(manager_->debugInfos_.empty());
287 std::vector<AppDebugInfo> debugInfos;
288 AppDebugInfo info;
289 debugInfos.push_back(info);
290
291 EXPECT_NE(listener_, nullptr);
292 EXPECT_CALL(*listener_, OnAppDebugStarted(_)).Times(1);
293 manager_->listeners_.insert(nullptr);
294 manager_->StartDebug(debugInfos);
295 EXPECT_FALSE(manager_->debugInfos_.empty());
296 }
297
298 /**
299 * @tc.name: StopDebug_0200
300 * @tc.desc: Start debug by AppDebugInfo, notify AppDebugListener.
301 * @tc.type: FUNC
302 */
303 HWTEST_F(AppDebugManagerTest, StopDebug_0200, TestSize.Level1)
304 {
305 EXPECT_NE(manager_, nullptr);
306 EXPECT_TRUE(manager_->debugInfos_.empty());
307 std::vector<AppDebugInfo> debugInfos;
308 AppDebugInfo info;
309 info.bundleName = DEBUG_START_NAME;
310 info.isDebugStart = IS_DEBUG_START;
311 info.pid = 10;
312 info.uid = 12345;
313 debugInfos.push_back(info);
314 manager_->debugInfos_ = debugInfos;
315
316 EXPECT_NE(listener_, nullptr);
317 EXPECT_CALL(*listener_, OnAppDebugStoped(_)).Times(1);
318 manager_->listeners_.insert(nullptr);
319 manager_->StopDebug(debugInfos);
320 EXPECT_TRUE(manager_->debugInfos_.empty());
321 }
322
323 /**
324 * @tc.name: RemoveAppDebugInfo_0200
325 * @tc.desc: Remove app debug info with bundleName, pid, uid and isDebugStart flag.
326 * @tc.type: FUNC
327 */
328 HWTEST_F(AppDebugManagerTest, RemoveAppDebugInfo_0200, TestSize.Level1)
329 {
330 EXPECT_NE(manager_, nullptr);
331 EXPECT_TRUE(manager_->debugInfos_.empty());
332
333 std::vector<AppDebugInfo> debugInfos;
334 int pid = 10;
335 int uid = 12345;
336 AppDebugInfo debugInfo;
337 debugInfo.bundleName = DEBUG_START_NAME;
338 debugInfo.pid = pid;
339 debugInfo.uid = uid;
340 debugInfo.isDebugStart = IS_DEBUG_START;
341
342 manager_->debugInfos_.push_back(debugInfo);
343 EXPECT_EQ(manager_->debugInfos_.size(), SIZE_ONE);
344
345 EXPECT_CALL(*listener_, OnAppDebugStoped(_)).Times(1);
346 manager_->listeners_.insert(nullptr);
347 manager_->RemoveAppDebugInfo(debugInfo);
348 EXPECT_TRUE(manager_->debugInfos_.empty());
349 }
350 } // namespace AppExecFwk
351 } // namespace OHOS