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 "app_pipe_mgr.h"
17
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20
21 #include <string>
22
23 #include "app_types.h"
24 #include "auto_launch_export.h"
25 #include "ipc_skeleton.h"
26 #include "mock_app_data_change_listener.h"
27 #include "objectstore_errors.h"
28
29 namespace {
30 using namespace testing::ext;
31 using namespace OHOS::ObjectStore;
32
33 class NativeAppPipeMgrTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase(void)41 void NativeAppPipeMgrTest::SetUpTestCase(void)
42 {
43 // input testsuit setup step,setup invoked before all testcases
44 }
45
TearDownTestCase(void)46 void NativeAppPipeMgrTest::TearDownTestCase(void)
47 {
48 // input testsuit teardown step,teardown invoked after all testcases
49 }
50
SetUp(void)51 void NativeAppPipeMgrTest::SetUp(void)
52 {
53 // input testcase setup step,setup invoked before each testcases
54 }
55
TearDown(void)56 void NativeAppPipeMgrTest::TearDown(void)
57 {
58 // input testcase teardown step,teardown invoked after each testcases
59 }
60
61 /**
62 * @tc.name: NativeAppPipeMgrTest_StartWatchDataChange_001
63 * @tc.desc: test NativeAppPipeMgrTest StartWatchDataChange. argument invalid
64 * @tc.type: FUNC
65 */
66 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StartWatchDataChange_001, TestSize.Level1)
67 {
68 PipeInfo pipeInfo = { "" };
69 AppDataChangeListener *observer = new MockAppDataChangeListener();
70 AppPipeMgr *appPipeMgr = new AppPipeMgr();
71 auto ret = appPipeMgr->StartWatchDataChange(observer, pipeInfo);
72 EXPECT_EQ(Status::INVALID_ARGUMENT, ret);
73 }
74
75 /**
76 * @tc.name: NativeAppPipeMgrTest_StartWatchDataChange_002
77 * @tc.desc: test NativeAppPipeMgrTest StartWatchDataChange. pipId not found.
78 * @tc.type: FUNC
79 */
80 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StartWatchDataChange_002, TestSize.Level1)
81 {
82 PipeInfo pipeInfo = { "pipId01" };
83 AppDataChangeListener *observer = new MockAppDataChangeListener();
84 AppPipeMgr *appPipeMgr = new AppPipeMgr();
85 auto ret = appPipeMgr->StartWatchDataChange(observer, pipeInfo);
86 EXPECT_EQ(Status::ERROR, ret);
87 }
88
89 /**
90 * @tc.name: NativeAppPipeMgrTest_StartWatchDataChange_003
91 * @tc.desc: test NativeAppPipeMgrTest StartWatchDataChange. observer is nullptr.
92 * @tc.type: FUNC
93 */
94 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StartWatchDataChange_003, TestSize.Level1)
95 {
96 PipeInfo pipeInfo = { "pipId01" };
97 AppPipeMgr appPipeMgr;
98 auto ret = appPipeMgr.StartWatchDataChange(nullptr, pipeInfo);
99 EXPECT_EQ(Status::INVALID_ARGUMENT, ret);
100 }
101
102 /**
103 * @tc.name: NativeAppPipeMgrTest_StartWatchDataChange_004
104 * @tc.desc: test NativeAppPipeMgrTest StartWatchDataChange. the pipId can be found.
105 * @tc.type: FUNC
106 */
107 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StartWatchDataChange_004, TestSize.Level1)
108 {
109 PipeInfo pipeInfo = { "pipId01" };
110 AppPipeMgr appPipeMgr;
111 auto ret = appPipeMgr.Start(pipeInfo);
112 EXPECT_EQ(Status::SUCCESS, ret);
113 MockAppDataChangeListener observer;
114 ret = appPipeMgr.StartWatchDataChange(&observer, pipeInfo);
115 EXPECT_EQ(Status::SUCCESS, ret);
116 }
117
118 /**
119 * @tc.name: NativeAppPipeMgrTest_StopWatchDataChange_001
120 * @tc.desc: test NativeAppPipeMgrTest StopWatchDataChange. argument invalid
121 * @tc.type: FUNC
122 */
123 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StopWatchDataChange_001, TestSize.Level1)
124 {
125 PipeInfo pipeInfo = { "pipId01" };
126 AppPipeMgr *appPipeMgr = new AppPipeMgr();
127 auto ret = appPipeMgr->StopWatchDataChange(nullptr, pipeInfo);
128 EXPECT_EQ(Status::INVALID_ARGUMENT, ret);
129 }
130
131 /**
132 * @tc.name: NativeAppPipeMgrTest_StopWatchDataChange_002
133 * @tc.desc: test NativeAppPipeMgrTest StopWatchDataChange.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StopWatchDataChange_002, TestSize.Level1)
137 {
138 PipeInfo pipeInfo = { "pipId01" };
139 AppPipeMgr *appPipeMgr = new AppPipeMgr();
140 AppDataChangeListener *observer = new MockAppDataChangeListener();
141 auto ret = appPipeMgr->StopWatchDataChange(observer, pipeInfo);
142 EXPECT_EQ(Status::ERROR, ret);
143 }
144
145 /**
146 * @tc.name: NativeAppPipeMgrTest_StopWatchDataChange_003
147 * @tc.desc: test NativeAppPipeMgrTest StopWatchDataChange.
148 * @tc.type: FUNC
149 */
150 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StopWatchDataChange_003, TestSize.Level1)
151 {
152 PipeInfo pipeInfo = { "pipId02" };
153 AppPipeMgr appPipeMgr;
154 auto ret = appPipeMgr.Start(pipeInfo);
155 EXPECT_EQ(Status::SUCCESS, ret);
156 MockAppDataChangeListener observer;
157 ret = appPipeMgr.StartWatchDataChange(&observer, pipeInfo);
158 EXPECT_EQ(Status::SUCCESS, ret);
159 ret = appPipeMgr.StopWatchDataChange(&observer, pipeInfo);
160 EXPECT_EQ(Status::SUCCESS, ret);
161 }
162
163 /**
164 * @tc.name: NativeAppPipeMgrTest_Start_001
165 * @tc.desc: test NativeAppPipeMgrTest Start. pipInfo is empty
166 * @tc.type: FUNC
167 */
168 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Start_001, TestSize.Level1)
169 {
170 PipeInfo pipeInfo = {};
171 AppPipeMgr *appPipeMgr = new AppPipeMgr();
172 auto ret = appPipeMgr->Start(pipeInfo);
173 EXPECT_EQ(Status::INVALID_ARGUMENT, ret);
174 }
175
176 /**
177 * @tc.name: NativeAppPipeMgrTest_Start_002
178 * @tc.desc: test NativeAppPipeMgrTest Start. invalid pipInfo
179 * @tc.type: FUNC
180 */
181 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Start_002, TestSize.Level1)
182 {
183 PipeInfo pipeInfo = { "INVALID_SESSION_NAME" };
184 AppPipeMgr *appPipeMgr = new AppPipeMgr();
185 auto ret = appPipeMgr->Start(pipeInfo);
186 EXPECT_EQ(Status::ILLEGAL_STATE, ret);
187 }
188
189 /**
190 * @tc.name: NativeAppPipeMgrTest_Start_003
191 * @tc.desc: test NativeAppPipeMgrTest Start.
192 * @tc.type: FUNC
193 */
194 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Start_003, TestSize.Level1)
195 {
196 PipeInfo pipeInfo = { "pipInfo01" };
197 AppPipeMgr *appPipeMgr = new AppPipeMgr();
198 auto ret = appPipeMgr->Start(pipeInfo);
199 EXPECT_EQ(Status::SUCCESS, ret);
200 ret = appPipeMgr->Stop(pipeInfo);
201 EXPECT_EQ(Status::SUCCESS, ret);
202 }
203
204 /**
205 * @tc.name: NativeAppPipeMgrTest_Start_004
206 * @tc.desc: test NativeAppPipeMgrTest Start. repeat start
207 * @tc.type: FUNC
208 */
209 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Start_004, TestSize.Level1)
210 {
211 PipeInfo pipeInfo = { "pipInfo01" };
212 AppPipeMgr *appPipeMgr = new AppPipeMgr();
213 auto ret = appPipeMgr->Start(pipeInfo);
214 EXPECT_EQ(Status::SUCCESS, ret);
215 ret = appPipeMgr->Start(pipeInfo);
216 EXPECT_EQ(Status::REPEATED_REGISTER, ret);
217 ret = appPipeMgr->Stop(pipeInfo);
218 EXPECT_EQ(Status::SUCCESS, ret);
219 }
220
221 /**
222 * @tc.name: NativeAppPipeMgrTest_Stop_001
223 * @tc.desc: test NativeAppPipeMgrTest Stop. pipInfo not found.
224 * @tc.type: FUNC
225 */
226 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Stop_001, TestSize.Level1)
227 {
228 PipeInfo pipeInfo = { "pipInfo01" };
229 AppPipeMgr *appPipeMgr = new AppPipeMgr();
230 auto ret = appPipeMgr->Stop(pipeInfo);
231 EXPECT_EQ(Status::KEY_NOT_FOUND, ret);
232 }
233
234 /**
235 * @tc.name: NativeAppPipeMgrTest_Stop_002
236 * @tc.desc: test NativeAppPipeMgrTest Stop. RemoveSessionServer failed
237 * @tc.type: FUNC
238 */
239 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Stop_002, TestSize.Level1)
240 {
241 PipeInfo pipeInfo = { "REMOVE_FAILED_SESSION_NAME" };
242 AppPipeMgr *appPipeMgr = new AppPipeMgr();
243 auto ret = appPipeMgr->Start(pipeInfo);
244 EXPECT_EQ(Status::SUCCESS, ret);
245 ret = appPipeMgr->Stop(pipeInfo);
246 EXPECT_EQ(Status::SUCCESS, ret);
247 }
248
249 /**
250 * @tc.name: NativeAppPipeMgrTest_SendData_001
251 * @tc.desc: test NativeAppPipeMgrTest SendData. input is invalid
252 * @tc.type: FUNC
253 */
254 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_SendData_001, TestSize.Level1)
255 {
256 PipeInfo pipeInfo = {};
257 DeviceId deviceId = { "devideId01" };
258 uint32_t size = 1;
259 uint8_t tmpNum = 1;
260 uint8_t *ptr = &tmpNum;
261 const DataInfo dataInfo = { const_cast<uint8_t *>(ptr), size };
262 MessageInfo messageInfo = { MessageType::DEFAULT };
263 AppPipeMgr appPipeMgr;
264 // pipeInfo is empty
265 auto ret = appPipeMgr.SendData(pipeInfo, deviceId, dataInfo, size, messageInfo);
266 EXPECT_EQ(Status::ERROR, ret);
267
268 // deviceId is empty
269 PipeInfo pipeInfo1 = { "pipeInfo" };
270 DeviceId deviceId1 = { "" };
271 ret = appPipeMgr.SendData(pipeInfo1, deviceId1, dataInfo, size, messageInfo);
272 EXPECT_EQ(Status::ERROR, ret);
273
274 // dataInfo.length is less than or equal to 0
275 DeviceId deviceId2 = { "devideId01" };
276 const DataInfo dataInfo1 = { const_cast<uint8_t *>(ptr), 0 };
277 ret = appPipeMgr.SendData(pipeInfo1, deviceId2, dataInfo1, size, messageInfo);
278 EXPECT_EQ(Status::ERROR, ret);
279
280 // dataInfo.length exceeds limit
281 static const int MAX_TRANSFER_SIZE = 1024 * 1024 * 5;
282 const DataInfo dataInfo2 = { const_cast<uint8_t *>(ptr), MAX_TRANSFER_SIZE + 1 };
283 ret = appPipeMgr.SendData(pipeInfo1, deviceId2, dataInfo2, size, messageInfo);
284 EXPECT_EQ(Status::ERROR, ret);
285
286 // dataInfo.data is nullptr
287 const DataInfo dataInfo3 = { nullptr, size };
288 ret = appPipeMgr.SendData(pipeInfo1, deviceId2, dataInfo3, size, messageInfo);
289 EXPECT_EQ(Status::ERROR, ret);
290 }
291
292 /**
293 * @tc.name: NativeAppPipeMgrTest_SendData_002
294 * @tc.desc: test NativeAppPipeMgrTest SendData. pipInfo not found
295 * @tc.type: FUNC
296 */
297 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_SendData_002, TestSize.Level1)
298 {
299 PipeInfo pipeInfo = { "pipInfo02" };
300 DeviceId deviceId = { "devideId02" };
301 uint32_t size = 1;
302 uint8_t tmpNum = 1;
303 uint8_t *ptr = &tmpNum;
304 const DataInfo dataInfo = { const_cast<uint8_t *>(ptr), size };
305 MessageInfo messageInfo = { MessageType::DEFAULT };
306 AppPipeMgr *appPipeMgr = new AppPipeMgr();
307 auto ret = appPipeMgr->SendData(pipeInfo, deviceId, dataInfo, size, messageInfo);
308 EXPECT_EQ(Status::KEY_NOT_FOUND, ret);
309 }
310
311 /**
312 * @tc.name: NativeAppPipeMgrTest_SendData_003
313 * @tc.desc: test NativeAppPipeMgrTest SendData. pipInfo not found
314 * @tc.type: FUNC
315 */
316 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_SendData_003, TestSize.Level1)
317 {
318 PipeInfo pipeInfo = { "pipInfo02" };
319 DeviceId deviceId = { "devideId02" };
320 uint32_t size = 1;
321 uint8_t tmpNum = 1;
322 uint8_t *ptr = &tmpNum;
323 const DataInfo dataInfo = { const_cast<uint8_t *>(ptr), size };
324 MessageInfo messageInfo = { MessageType::DEFAULT };
325 AppPipeMgr appPipeMgr;
326 Status ret = appPipeMgr.Start(pipeInfo);
327 EXPECT_EQ(Status::SUCCESS, ret);
328 ret = appPipeMgr.SendData(pipeInfo, deviceId, dataInfo, size, messageInfo);
329 EXPECT_EQ(Status::SUCCESS, ret);
330 }
331
332 /**
333 * @tc.name: NativeAppPipeMgrTest_IsSameStartedOnPeer_001
334 * @tc.desc: test NativeAppPipeMgrTest IsSameStartedOnPeer. pipInfo or deviceId is empty
335 * @tc.type: FUNC
336 */
337 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_IsSameStartedOnPeer_001, TestSize.Level1)
338 {
339 PipeInfo pipeInfo = { "pipInfo01" };
340 DeviceId deviceId = {};
341 AppPipeMgr *appPipeMgr = new AppPipeMgr();
342 auto ret = appPipeMgr->IsSameStartedOnPeer(pipeInfo, deviceId);
343 EXPECT_EQ(false, ret);
344 }
345
346 /**
347 * @tc.name: NativeAppPipeMgrTest_IsSameStartedOnPeer_002
348 * @tc.desc: test NativeAppPipeMgrTest IsSameStartedOnPeer. pipInfo not found
349 * @tc.type: FUNC
350 */
351 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_IsSameStartedOnPeer_002, TestSize.Level1)
352 {
353 PipeInfo pipeInfo = { "pipInfo02" };
354 DeviceId deviceId = { "deviceId02" };
355 AppPipeMgr *appPipeMgr = new AppPipeMgr();
356 auto ret = appPipeMgr->IsSameStartedOnPeer(pipeInfo, deviceId);
357 EXPECT_EQ(false, ret);
358 }
359
360 /**
361 * @tc.name: NativeAppPipeMgrTest_IsSameStartedOnPeer_003
362 * @tc.desc: test NativeAppPipeMgrTest IsSameStartedOnPeer. pipInfo not found
363 * @tc.type: FUNC
364 */
365 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_IsSameStartedOnPeer_003, TestSize.Level1)
366 {
367 PipeInfo pipeInfo = { "pipInfo02" };
368 DeviceId deviceId = { "deviceId02" };
369 AppPipeMgr appPipeMgr;
370 appPipeMgr.Start(pipeInfo);
371 auto ret = appPipeMgr.IsSameStartedOnPeer(pipeInfo, deviceId);
372 EXPECT_EQ(true, ret);
373 }
374 } // namespace
375