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 "process_communicator_impl.h"
17 
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <string>
21 
22 #include "app_types.h"
23 #include "auto_launch_export.h"
24 #include "ipc_skeleton.h"
25 #include "objectstore_errors.h"
26 
27 namespace {
28 using namespace testing::ext;
29 using namespace OHOS::ObjectStore;
30 
31 static constexpr uint32_t MTU_SIZE = 4096 * 1024;
32 
33 class NativeProcessCommunicatorImplTest : 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 NativeProcessCommunicatorImplTest::SetUpTestCase(void)
42 {
43     // input testsuit setup step,setup invoked before all testcases
44 }
45 
TearDownTestCase(void)46 void NativeProcessCommunicatorImplTest::TearDownTestCase(void)
47 {
48     // input testsuit teardown step,teardown invoked after all testcases
49 }
50 
SetUp(void)51 void NativeProcessCommunicatorImplTest::SetUp(void)
52 {
53     // input testcase setup step,setup invoked before each testcases
54 }
55 
TearDown(void)56 void NativeProcessCommunicatorImplTest::TearDown(void)
57 {
58     // input testcase teardown step,teardown invoked after each testcases
59 }
60 
61 /**
62  * @tc.name: ProcessCommunicatorImpl_Start_Stop_001
63  * @tc.desc: test ProcessCommunicatorImpl Start and Stop.
64  * @tc.type: FUNC
65  */
66 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_Start_Stop_001, TestSize.Level1)
67 {
68     std::string processLabel = "INVALID_SESSION_NAME";
69     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
70     auto ret = processCommunicator->Start(processLabel);
71     EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
72     ret = processCommunicator->Stop();
73     EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
74     delete processCommunicator;
75 }
76 
77 /**
78  * @tc.name: ProcessCommunicatorImpl_Start_Stop_002
79  * @tc.desc: test ProcessCommunicatorImpl Start and Stop.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_Start_Stop_002, TestSize.Level1)
83 {
84     std::string processLabel = "processLabel";
85     ProcessCommunicatorImpl processCommunicator;
86     auto ret = processCommunicator.Start(processLabel);
87     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
88     ret = processCommunicator.Stop();
89     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
90 }
91 
92 /**
93  * @tc.name: ProcessCommunicatorImpl_RegOnDeviceChange_001
94  * @tc.desc: test ProcessCommunicatorImpl RegOnDeviceChange.
95  * @tc.type: FUNC
96  */
97 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDeviceChange_001, TestSize.Level1)
98 {
99     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
100     auto ret =
__anone4338ea50202(const DistributedDB::DeviceInfos &devInfo, bool isOnline) 101         processCommunicator->RegOnDeviceChange([](const DistributedDB::DeviceInfos &devInfo, bool isOnline) -> void {
102             return;
103         });
104     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
105     ret = processCommunicator->RegOnDeviceChange(nullptr);
106     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
107     delete processCommunicator;
108 }
109 
110 /**
111  * @tc.name: ProcessCommunicatorImpl_RegOnDeviceChange_002
112  * @tc.desc: test ProcessCommunicatorImpl RegOnDeviceChange.
113  * @tc.type: FUNC
114  */
115 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDeviceChange_002, TestSize.Level1)
116 {
117     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
118     auto ret =
__anone4338ea50302(const DistributedDB::DeviceInfos &devInfo, bool isOnline) 119         processCommunicator->RegOnDeviceChange([](const DistributedDB::DeviceInfos &devInfo, bool isOnline) -> void {
120             return;
121         });
122     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
__anone4338ea50402(const DistributedDB::DeviceInfos &devInfo, bool isOnline) 123     ret = processCommunicator->RegOnDeviceChange([](const DistributedDB::DeviceInfos &devInfo, bool isOnline) -> void {
124         return;
125     });
126     EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
127     ret = processCommunicator->RegOnDeviceChange(nullptr);
128     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
129     delete processCommunicator;
130 }
131 
132 /**
133  * @tc.name: ProcessCommunicatorImpl_RegOnDeviceChange_003
134  * @tc.desc: test ProcessCommunicatorImpl RegOnDeviceChange.
135  * @tc.type: FUNC
136  */
137 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDeviceChange_003, TestSize.Level1)
138 {
139     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
140     auto ret =
__anone4338ea50502(const DistributedDB::DeviceInfos &devInfo, bool isOnline) 141         processCommunicator->RegOnDeviceChange([](const DistributedDB::DeviceInfos &devInfo, bool isOnline) -> void {
142             return;
143         });
144     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
145     ret = processCommunicator->RegOnDeviceChange(nullptr);
146     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
147     ret = processCommunicator->RegOnDeviceChange(nullptr);
148     EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
149     delete processCommunicator;
150 }
151 
152 /**
153  * @tc.name: ProcessCommunicatorImpl_RegOnDataReceive_001
154  * @tc.desc: test ProcessCommunicatorImpl RegOnDataReceive.
155  * @tc.type: FUNC
156  */
157 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDataReceive_001, TestSize.Level1)
158 {
159     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
160     auto ret = processCommunicator->RegOnDataReceive(
__anone4338ea50602(const DistributedDB::DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length) 161         [](const DistributedDB::DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length) -> void {
162             return;
163         });
164     EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
165     delete processCommunicator;
166 }
167 
168 /**
169  * @tc.name: ProcessCommunicatorImpl_RegOnDataReceive_002
170  * @tc.desc: test ProcessCommunicatorImpl RegOnDataReceive.
171  * @tc.type: FUNC
172  */
173 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDataReceive_002, TestSize.Level1)
174 {
175     std::string processLabel = "processLabel";
176     ProcessCommunicatorImpl processCommunicator;
177     auto ret = processCommunicator.Start(processLabel);
178     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
179 
180     ret = processCommunicator.RegOnDataReceive(
__anone4338ea50702(const DistributedDB::DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length) 181         [](const DistributedDB::DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length) -> void { return; });
182     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
183 
184     ret = processCommunicator.RegOnDataReceive(nullptr);
185     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
186 }
187 
188 /**
189  * @tc.name: ProcessCommunicatorImpl_SendData_001
190  * @tc.desc: test ProcessCommunicatorImpl SendData.
191  * @tc.type: FUNC
192  */
193 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_SendData_001, TestSize.Level1)
194 {
195     std::string processLabel = "processLabel01";
196     DistributedDB::DeviceInfos deviceInfos = { "identifier" };
197     uint8_t data = 1;
198     uint32_t length = 1;
199     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
200     auto ret = processCommunicator->SendData(deviceInfos, &data, length);
201     EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
202     delete processCommunicator;
203 }
204 
205 /**
206  * @tc.name: ProcessCommunicatorImpl_SendData_002
207  * @tc.desc: test ProcessCommunicatorImpl SendData.
208  * @tc.type: FUNC
209  */
210 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_SendData_002, TestSize.Level1)
211 {
212     std::string processLabel = "processLabel02";
213     ProcessCommunicatorImpl processCommunicator;
214     auto ret = processCommunicator.Start(processLabel);
215     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
216 
217     DistributedDB::DeviceInfos deviceInfos = { "identifier" };
218     uint8_t data = 1;
219     uint32_t length = 1;
220     ret = processCommunicator.SendData(deviceInfos, &data, length);
221     EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
222 }
223 
224 /**
225  * @tc.name: ProcessCommunicatorImpl_GetMtuSize_001
226  * @tc.desc: test ProcessCommunicatorImpl GetMtuSize.
227  * @tc.type: FUNC
228  */
229 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_GetMtuSize_001, TestSize.Level1)
230 {
231     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
232     DistributedDB::DeviceInfos deviceInfos = { "identifier" };
233     auto ret = processCommunicator->GetMtuSize(deviceInfos);
234     EXPECT_EQ(MTU_SIZE, ret);
235     ret = processCommunicator->GetMtuSize();
236     EXPECT_EQ(MTU_SIZE, ret);
237     delete processCommunicator;
238 }
239 
240 /**
241  * @tc.name: ProcessCommunicatorImpl_GetMtuSize_002
242  * @tc.desc: test ProcessCommunicatorImpl GetMtuSize.
243  * @tc.type: FUNC
244  */
245 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_GetMtuSize_002, TestSize.Level1)
246 {
247     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
248     DistributedDB::DeviceInfos deviceInfos = { "" };
249     auto ret = processCommunicator->GetMtuSize(deviceInfos);
250     EXPECT_EQ(MTU_SIZE, ret);
251     ret = processCommunicator->GetMtuSize();
252     EXPECT_EQ(MTU_SIZE, ret);
253     delete processCommunicator;
254 }
255 
256 /**
257  * @tc.name: ProcessCommunicatorImpl_IsSameProcessLabelStartedOnPeerDevice_001
258  * @tc.desc: test ProcessCommunicatorImpl IsSameProcessLabelStartedOnPeerDevice.
259  * @tc.type: FUNC
260  */
261 HWTEST_F(NativeProcessCommunicatorImplTest, IsSameProcessLabelStartedOnPeerDevice_001, TestSize.Level1)
262 {
263     ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
264     DistributedDB::DeviceInfos deviceInfos = { "identifier" };
265     auto ret = processCommunicator->IsSameProcessLabelStartedOnPeerDevice(deviceInfos);
266     EXPECT_EQ(false, ret);
267     delete processCommunicator;
268 }
269 }
270