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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <string>
19
20 #include "app_pipe_mgr.h"
21 #include "app_types.h"
22 #include "auto_launch_export.h"
23 #include "ipc_skeleton.h"
24 #include "objectstore_errors.h"
25 #include "mock_app_data_change_listener.h"
26
27 namespace {
28 using namespace testing::ext;
29 using namespace OHOS::ObjectStore;
30
31 class NativeAppPipeMgrTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 };
38
SetUpTestCase(void)39 void NativeAppPipeMgrTest::SetUpTestCase(void)
40 {
41 // input testsuit setup step,setup invoked before all testcases
42 }
43
TearDownTestCase(void)44 void NativeAppPipeMgrTest::TearDownTestCase(void)
45 {
46 // input testsuit teardown step,teardown invoked after all testcases
47 }
48
SetUp(void)49 void NativeAppPipeMgrTest::SetUp(void)
50 {
51 // input testcase setup step,setup invoked before each testcases
52 }
53
TearDown(void)54 void NativeAppPipeMgrTest::TearDown(void)
55 {
56 // input testcase teardown step,teardown invoked after each testcases
57 }
58
59 /**
60 * @tc.name: NativeAppPipeMgrTest_StartWatchDataChange_001
61 * @tc.desc: test NativeAppPipeMgrTest StartWatchDataChange. argument invalid
62 * @tc.type: FUNC
63 */
64 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StartWatchDataChange_001, TestSize.Level1)
65 {
66 PipeInfo pipeInfo = {""};
67 AppDataChangeListener *observer = new MockAppDataChangeListener();
68 AppPipeMgr *appPipeMgr = new AppPipeMgr();
69 auto ret = appPipeMgr->StartWatchDataChange(observer, pipeInfo);
70 EXPECT_EQ(Status::INVALID_ARGUMENT, ret);
71 }
72
73 /**
74 * @tc.name: NativeAppPipeMgrTest_StartWatchDataChange_002
75 * @tc.desc: test NativeAppPipeMgrTest StartWatchDataChange. pipId not found.
76 * @tc.type: FUNC
77 */
78 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StartWatchDataChange_002, TestSize.Level1)
79 {
80 PipeInfo pipeInfo = { "pipId01" };
81 AppDataChangeListener *observer = new MockAppDataChangeListener();
82 AppPipeMgr *appPipeMgr = new AppPipeMgr();
83 auto ret = appPipeMgr->StartWatchDataChange(observer, pipeInfo);
84 EXPECT_EQ(Status::ERROR, ret);
85 }
86
87 /**
88 * @tc.name: NativeAppPipeMgrTest_StopWatchDataChange_001
89 * @tc.desc: test NativeAppPipeMgrTest StopWatchDataChange. argument invalid
90 * @tc.type: FUNC
91 */
92 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StopWatchDataChange_001, TestSize.Level1)
93 {
94 PipeInfo pipeInfo = { "pipId01" };
95 AppPipeMgr *appPipeMgr = new AppPipeMgr();
96 auto ret = appPipeMgr->StopWatchDataChange(nullptr, pipeInfo);
97 EXPECT_EQ(Status::INVALID_ARGUMENT, ret);
98 }
99
100 /**
101 * @tc.name: NativeAppPipeMgrTest_StopWatchDataChange_002
102 * @tc.desc: test NativeAppPipeMgrTest StopWatchDataChange.
103 * @tc.type: FUNC
104 */
105 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StopWatchDataChange_002, TestSize.Level1)
106 {
107 PipeInfo pipeInfo = { "pipId01" };
108 AppPipeMgr *appPipeMgr = new AppPipeMgr();
109 AppDataChangeListener *observer = new MockAppDataChangeListener();
110 auto ret = appPipeMgr->StopWatchDataChange(observer, pipeInfo);
111 EXPECT_EQ(Status::ERROR, ret);
112 }
113
114 /**
115 * @tc.name: NativeAppPipeMgrTest_StopWatchDataChange_003
116 * @tc.desc: test NativeAppPipeMgrTest StopWatchDataChange.
117 * @tc.type: FUNC
118 */
119 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_StopWatchDataChange_003, TestSize.Level1)
120 {
121 PipeInfo pipeInfo = { "pipId01" };
122 AppPipeMgr *appPipeMgr = new AppPipeMgr();
123 AppDataChangeListener *observer = new MockAppDataChangeListener();
124 auto ret = appPipeMgr->StopWatchDataChange(observer, pipeInfo);
125 EXPECT_EQ(Status::ERROR, ret);
126 }
127
128 /**
129 * @tc.name: NativeAppPipeMgrTest_Start_001
130 * @tc.desc: test NativeAppPipeMgrTest Start. pipInfo is empty
131 * @tc.type: FUNC
132 */
133 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Start_001, TestSize.Level1)
134 {
135 PipeInfo pipeInfo = {};
136 AppPipeMgr *appPipeMgr = new AppPipeMgr();
137 auto ret = appPipeMgr->Start(pipeInfo);
138 EXPECT_EQ(Status::INVALID_ARGUMENT, ret);
139 }
140
141 /**
142 * @tc.name: NativeAppPipeMgrTest_Start_002
143 * @tc.desc: test NativeAppPipeMgrTest Start. invalid pipInfo
144 * @tc.type: FUNC
145 */
146 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Start_002, TestSize.Level1)
147 {
148 PipeInfo pipeInfo = {"INVALID_SESSION_NAME"};
149 AppPipeMgr *appPipeMgr = new AppPipeMgr();
150 auto ret = appPipeMgr->Start(pipeInfo);
151 EXPECT_EQ(Status::ILLEGAL_STATE, ret);
152 }
153
154 /**
155 * @tc.name: NativeAppPipeMgrTest_Start_003
156 * @tc.desc: test NativeAppPipeMgrTest Start.
157 * @tc.type: FUNC
158 */
159 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Start_003, TestSize.Level1)
160 {
161 PipeInfo pipeInfo = { "pipInfo01" };
162 AppPipeMgr *appPipeMgr = new AppPipeMgr();
163 auto ret = appPipeMgr->Start(pipeInfo);
164 EXPECT_EQ(Status::SUCCESS, ret);
165 ret = appPipeMgr->Stop(pipeInfo);
166 EXPECT_EQ(Status::SUCCESS, ret);
167 }
168
169 /**
170 * @tc.name: NativeAppPipeMgrTest_Start_004
171 * @tc.desc: test NativeAppPipeMgrTest Start. repeat start
172 * @tc.type: FUNC
173 */
174 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Start_004, TestSize.Level1)
175 {
176 PipeInfo pipeInfo = { "pipInfo01" };
177 AppPipeMgr *appPipeMgr = new AppPipeMgr();
178 auto ret = appPipeMgr->Start(pipeInfo);
179 EXPECT_EQ(Status::SUCCESS, ret);
180 ret = appPipeMgr->Start(pipeInfo);
181 EXPECT_EQ(Status::REPEATED_REGISTER, ret);
182 ret = appPipeMgr->Stop(pipeInfo);
183 EXPECT_EQ(Status::SUCCESS, ret);
184 }
185
186 /**
187 * @tc.name: NativeAppPipeMgrTest_Stop_001
188 * @tc.desc: test NativeAppPipeMgrTest Stop. pipInfo not found.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Stop_001, TestSize.Level1)
192 {
193 PipeInfo pipeInfo = { "pipInfo01" };
194 AppPipeMgr *appPipeMgr = new AppPipeMgr();
195 auto ret = appPipeMgr->Stop(pipeInfo);
196 EXPECT_EQ(Status::KEY_NOT_FOUND, ret);
197 }
198
199 /**
200 * @tc.name: NativeAppPipeMgrTest_Stop_002
201 * @tc.desc: test NativeAppPipeMgrTest Stop. RemoveSessionServer failed
202 * @tc.type: FUNC
203 */
204 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_Stop_002, TestSize.Level1)
205 {
206 PipeInfo pipeInfo = { "REMOVE_FAILED_SESSION_NAME" };
207 AppPipeMgr *appPipeMgr = new AppPipeMgr();
208 auto ret = appPipeMgr->Start(pipeInfo);
209 EXPECT_EQ(Status::SUCCESS, ret);
210 ret = appPipeMgr->Stop(pipeInfo);
211 EXPECT_EQ(Status::ERROR, ret);
212 }
213
214 /**
215 * @tc.name: NativeAppPipeMgrTest_SendData_001
216 * @tc.desc: test NativeAppPipeMgrTest SendData. input is invalid
217 * @tc.type: FUNC
218 */
219 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_SendData_001, TestSize.Level1)
220 {
221 PipeInfo pipeInfo = {};
222 DeviceId deviceId = {"devideId01"};
223 int size = 1;
224 uint8_t tmpNum = 1;
225 uint8_t *ptr = &tmpNum;
226 MessageInfo messageInfo = {MessageType::DEFAULT};
227 AppPipeMgr *appPipeMgr = new AppPipeMgr();
228 auto ret = appPipeMgr->SendData(pipeInfo, deviceId, ptr, size, messageInfo);
229 EXPECT_EQ(Status::ERROR, ret);
230 }
231
232 /**
233 * @tc.name: NativeAppPipeMgrTest_SendData_002
234 * @tc.desc: test NativeAppPipeMgrTest SendData. pipInfo not found
235 * @tc.type: FUNC
236 */
237 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_SendData_002, TestSize.Level1)
238 {
239 PipeInfo pipeInfo = {"pipInfo02"};
240 DeviceId deviceId = {"devideId02"};
241 int size = 1;
242 uint8_t tmpNum = 1;
243 uint8_t *ptr = &tmpNum;
244 MessageInfo messageInfo = { MessageType::DEFAULT };
245 AppPipeMgr *appPipeMgr = new AppPipeMgr();
246 auto ret = appPipeMgr->SendData(pipeInfo, deviceId, ptr, size, messageInfo);
247 EXPECT_EQ(Status::KEY_NOT_FOUND, ret);
248 }
249
250 /**
251 * @tc.name: NativeAppPipeMgrTest_IsSameStartedOnPeer_001
252 * @tc.desc: test NativeAppPipeMgrTest IsSameStartedOnPeer. pipInfo or deviceId is empty
253 * @tc.type: FUNC
254 */
255 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_IsSameStartedOnPeer_001, TestSize.Level1)
256 {
257 PipeInfo pipeInfo = { "pipInfo01" };
258 DeviceId deviceId = {};
259 AppPipeMgr *appPipeMgr = new AppPipeMgr();
260 auto ret = appPipeMgr->IsSameStartedOnPeer(pipeInfo, deviceId);
261 EXPECT_EQ(false, ret);
262 }
263
264 /**
265 * @tc.name: NativeAppPipeMgrTest_IsSameStartedOnPeer_002
266 * @tc.desc: test NativeAppPipeMgrTest IsSameStartedOnPeer. pipInfo not found
267 * @tc.type: FUNC
268 */
269 HWTEST_F(NativeAppPipeMgrTest, NativeAppPipeMgrTest_IsSameStartedOnPeer_002, TestSize.Level1)
270 {
271 PipeInfo pipeInfo = { "pipInfo02" };
272 DeviceId deviceId = {"deviceId02"};
273 AppPipeMgr *appPipeMgr = new AppPipeMgr();
274 auto ret = appPipeMgr->IsSameStartedOnPeer(pipeInfo, deviceId);
275 EXPECT_EQ(false, ret);
276 }
277
278 }
279