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_RegOnDeviceChange_001
79 * @tc.desc: test ProcessCommunicatorImpl RegOnDeviceChange.
80 * @tc.type: FUNC
81 */
82 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDeviceChange_001, TestSize.Level1)
83 {
84 ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
85 auto ret =
__anon9a6f44ed0202(const DistributedDB::DeviceInfos &devInfo, bool isOnline) 86 processCommunicator->RegOnDeviceChange([](const DistributedDB::DeviceInfos &devInfo, bool isOnline) -> void {
87 return;
88 });
89 EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
90 ret = processCommunicator->RegOnDeviceChange(nullptr);
91 EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
92 delete processCommunicator;
93 }
94
95 /**
96 * @tc.name: ProcessCommunicatorImpl_RegOnDeviceChange_002
97 * @tc.desc: test ProcessCommunicatorImpl RegOnDeviceChange.
98 * @tc.type: FUNC
99 */
100 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDeviceChange_002, TestSize.Level1)
101 {
102 ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
103 auto ret =
__anon9a6f44ed0302(const DistributedDB::DeviceInfos &devInfo, bool isOnline) 104 processCommunicator->RegOnDeviceChange([](const DistributedDB::DeviceInfos &devInfo, bool isOnline) -> void {
105 return;
106 });
107 EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
__anon9a6f44ed0402(const DistributedDB::DeviceInfos &devInfo, bool isOnline) 108 ret = processCommunicator->RegOnDeviceChange([](const DistributedDB::DeviceInfos &devInfo, bool isOnline) -> void {
109 return;
110 });
111 EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
112 ret = processCommunicator->RegOnDeviceChange(nullptr);
113 EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
114 delete processCommunicator;
115 }
116
117 /**
118 * @tc.name: ProcessCommunicatorImpl_RegOnDeviceChange_003
119 * @tc.desc: test ProcessCommunicatorImpl RegOnDeviceChange.
120 * @tc.type: FUNC
121 */
122 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDeviceChange_003, TestSize.Level1)
123 {
124 ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
125 auto ret =
__anon9a6f44ed0502(const DistributedDB::DeviceInfos &devInfo, bool isOnline) 126 processCommunicator->RegOnDeviceChange([](const DistributedDB::DeviceInfos &devInfo, bool isOnline) -> void {
127 return;
128 });
129 EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
130 ret = processCommunicator->RegOnDeviceChange(nullptr);
131 EXPECT_EQ(DistributedDB::DBStatus::OK, ret);
132 ret = processCommunicator->RegOnDeviceChange(nullptr);
133 EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
134 delete processCommunicator;
135 }
136
137 /**
138 * @tc.name: ProcessCommunicatorImpl_RegOnDataReceive_001
139 * @tc.desc: test ProcessCommunicatorImpl RegOnDataReceive.
140 * @tc.type: FUNC
141 */
142 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDataReceive_001, TestSize.Level1)
143 {
144 ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
145 auto ret = processCommunicator->RegOnDataReceive(
__anon9a6f44ed0602(const DistributedDB::DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length) 146 [](const DistributedDB::DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length) -> void {
147 return;
148 });
149 EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
150 delete processCommunicator;
151 }
152
153 /**
154 * @tc.name: ProcessCommunicatorImpl_RegOnDataReceive_002
155 * @tc.desc: test ProcessCommunicatorImpl RegOnDataReceive.
156 * @tc.type: FUNC
157 */
158 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_RegOnDataReceive_002, TestSize.Level1)
159 {
160 ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
161 auto ret = processCommunicator->RegOnDataReceive(nullptr);
162 EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
163 delete processCommunicator;
164 }
165
166 /**
167 * @tc.name: ProcessCommunicatorImpl_SendData_001
168 * @tc.desc: test ProcessCommunicatorImpl SendData.
169 * @tc.type: FUNC
170 */
171 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_SendData_001, TestSize.Level1)
172 {
173 std::string processLabel = "processLabel01";
174 DistributedDB::DeviceInfos deviceInfos = { "identifier" };
175 uint8_t data = 1;
176 uint32_t length = 1;
177 ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
178 auto ret = processCommunicator->SendData(deviceInfos, &data, length);
179 EXPECT_EQ(DistributedDB::DBStatus::DB_ERROR, ret);
180 delete processCommunicator;
181 }
182
183 /**
184 * @tc.name: ProcessCommunicatorImpl_GetMtuSize_001
185 * @tc.desc: test ProcessCommunicatorImpl GetMtuSize.
186 * @tc.type: FUNC
187 */
188 HWTEST_F(NativeProcessCommunicatorImplTest, ProcessCommunicatorImpl_GetMtuSize_001, TestSize.Level1)
189 {
190 ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
191 DistributedDB::DeviceInfos deviceInfos = { "identifier" };
192 auto ret = processCommunicator->GetMtuSize(deviceInfos);
193 EXPECT_EQ(MTU_SIZE, ret);
194 ret = processCommunicator->GetMtuSize();
195 EXPECT_EQ(MTU_SIZE, ret);
196 delete processCommunicator;
197 }
198
199 /**
200 * @tc.name: ProcessCommunicatorImpl_IsSameProcessLabelStartedOnPeerDevice_001
201 * @tc.desc: test ProcessCommunicatorImpl IsSameProcessLabelStartedOnPeerDevice.
202 * @tc.type: FUNC
203 */
204 HWTEST_F(NativeProcessCommunicatorImplTest, IsSameProcessLabelStartedOnPeerDevice_001, TestSize.Level1)
205 {
206 ProcessCommunicatorImpl *processCommunicator = new ProcessCommunicatorImpl();
207 DistributedDB::DeviceInfos deviceInfos = { "identifier" };
208 auto ret = processCommunicator->IsSameProcessLabelStartedOnPeerDevice(deviceInfos);
209 EXPECT_EQ(false, ret);
210 delete processCommunicator;
211 }
212 }
213