1 /*
2 * Copyright (c) 2021-2025 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 #include <gmock/gmock.h>
18 #include "bus_center_server_proxy.h"
19 #include "bus_center_server_proxy_standard.h"
20 #include "client_bus_center_manager.h"
21 #include "lnn_log.h"
22 #include "softbus_access_token_test.h"
23 #include "softbus_bus_center.h"
24 #include "softbus_def.h"
25 #include "softbus_ddos.h"
26 #include "softbus_error_code.h"
27 #include "softbus_server_frame.h"
28 #include "softbus_utils.h"
29 #include <securec.h>
30
31 #define CAPABILITY_1 "capdata1"
32 #define CAPABILITY_3 "capdata3"
33 #define CAPABILITY_4 "capdata4"
34 #define USE_TIMES 100
35 #define GET_DEVICE_INFO_TIMES 300
36 #define DATA_CHANGE_FLAG 11
37 #define LEAVELNN_TIMES 20
38
39 namespace OHOS {
40 using namespace testing::ext;
41 using namespace testing;
42
43 constexpr char TEST_PKG_NAME[] = "com.softbus.test";
44 static int32_t g_subscribeId = 0;
45 static int32_t g_publishId = 0;
46 static bool g_flag = true;
47
48 class BusCenterSdkDdosTest : public testing::Test {
49 public:
50 static void SetUpTestCase();
51 static void TearDownTestCase();
52 void SetUp();
53 void TearDown();
54 };
55
SetUpTestCase()56 void BusCenterSdkDdosTest::SetUpTestCase()
57 {
58 SetAccessTokenPermission("busCenterTest");
59 if (BusCenterClientInit() != SOFTBUS_OK) {
60 GTEST_LOG_(INFO) << "bus center client init failed";
61 }
62 if (InitDdos()) {
63 GTEST_LOG_(INFO) << "ddos init failed";
64 }
65 }
66
TearDownTestCase()67 void BusCenterSdkDdosTest::TearDownTestCase() { }
68
SetUp()69 void BusCenterSdkDdosTest::SetUp() { }
70
TearDown()71 void BusCenterSdkDdosTest::TearDown() { }
72
GetSubscribeId(void)73 static int32_t GetSubscribeId(void)
74 {
75 g_subscribeId++;
76 return g_subscribeId;
77 }
78
GetPublishId(void)79 static int32_t GetPublishId(void)
80 {
81 g_publishId++;
82 return g_publishId;
83 }
84
85 static SubscribeInfo g_sInfo = { .subscribeId = 1,
86 .mode = DISCOVER_MODE_ACTIVE,
87 .medium = COAP,
88 .freq = MID,
89 .isSameAccount = true,
90 .isWakeRemote = false,
91 .capability = "dvKit",
92 .capabilityData = (unsigned char *)CAPABILITY_3,
93 .dataLen = strlen(CAPABILITY_3) };
94
95 static PublishInfo g_pInfo = { .publishId = 1,
96 .mode = DISCOVER_MODE_ACTIVE,
97 .medium = COAP,
98 .freq = MID,
99 .capability = "dvKit",
100 .capabilityData = (unsigned char *)CAPABILITY_4,
101 .dataLen = strlen(CAPABILITY_4) };
102
OnLeaveLNNDone(const char * networkId,int32_t retCode)103 static void OnLeaveLNNDone(const char *networkId, int32_t retCode)
104 {
105 (void)networkId;
106 (void)retCode;
107 }
108
OnTimeSyncResult(const TimeSyncResultInfo * info,int32_t retCode)109 static void OnTimeSyncResult(const TimeSyncResultInfo *info, int32_t retCode)
110 {
111 (void)info;
112 (void)retCode;
113 }
114
115 static ITimeSyncCb g_timeSyncCb = {
116 .onTimeSyncResult = OnTimeSyncResult,
117 };
118
TestDeviceFound(const DeviceInfo * device)119 static void TestDeviceFound(const DeviceInfo *device)
120 {
121 printf("[client]TestDeviceFound\n");
122 }
123
TestDiscoverResult(int32_t refreshId,RefreshResult reason)124 static void TestDiscoverResult(int32_t refreshId, RefreshResult reason)
125 {
126 printf("[client]TestDiscoverResult:%d\n", reason);
127 }
128
TestPublishResult(int32_t publishId,PublishResult reason)129 static void TestPublishResult(int32_t publishId, PublishResult reason)
130 {
131 printf("[client]TestPublishResult:%d\n", reason);
132 }
133
134 static IRefreshCallback g_refreshCb = { .OnDeviceFound = TestDeviceFound, .OnDiscoverResult = TestDiscoverResult };
135
136 static IPublishCb g_publishCb = { .OnPublishResult = TestPublishResult };
137 /*
138 * @tc.name: DDOS_GET_NODE_KEY_INFO_Test_001
139 * @tc.desc: get node key info interface test
140 * @tc.type: FUNC
141 * @tc.require: I5I7B9
142 */
143 HWTEST_F(BusCenterSdkDdosTest, DDOS_GET_NODE_KEY_INFO_Test_001, TestSize.Level0)
144 {
145 NodeBasicInfo info;
146 char udid[UDID_BUF_LEN] = {0};
147 EXPECT_TRUE(GetLocalNodeDeviceInfo(TEST_PKG_NAME, &info) == SOFTBUS_OK);
148 for (int i = 0; i < GET_DEVICE_INFO_TIMES; i++) {
149 GetNodeKeyInfo(TEST_PKG_NAME, info.networkId, NODE_KEY_UDID,
150 (uint8_t *)udid, UDID_BUF_LEN);
151 }
152 }
153
154 /*
155 * @tc.name: DDOS_SET_NODE_DATA_CHANGE_Test001
156 * @tc.desc: set node data change flag test
157 * @tc.type: FUNC
158 * @tc.require: I5I7B9
159 */
160 HWTEST_F(BusCenterSdkDdosTest, DDOS_SET_NODE_DATA_CHANGE_Test001, TestSize.Level0)
161 {
162 char networkId[] = "12313";
163 uint16_t dataChangeFlag = DATA_CHANGE_FLAG;
164 for (int i = 0; i < USE_TIMES; i++) {
165 SetNodeDataChangeFlag(TEST_PKG_NAME, networkId, dataChangeFlag);
166 }
167 SetNodeDataChangeFlag(TEST_PKG_NAME, networkId, dataChangeFlag);
168 EXPECT_TRUE(g_flag);
169 }
170
171 /*
172 * @tc.name: DDOS_START_TIME_SYNC_Test_001
173 * @tc.desc: start time sync interface test
174 * @tc.type: FUNC
175 * @tc.require: I5I7B9
176 */
177 HWTEST_F(BusCenterSdkDdosTest, DDOS_START_TIME_SYNC_Test_001, TestSize.Level0)
178 {
179 char networkId[] = "0123456789987654321001234567899876543210012345678998765432100123";
180 for (int i = 0; i < USE_TIMES; i++) {
181 StartTimeSync(TEST_PKG_NAME, networkId, LOW_ACCURACY, SHORT_PERIOD, &g_timeSyncCb);
182 }
183 StartTimeSync(TEST_PKG_NAME, networkId, LOW_ACCURACY, SHORT_PERIOD, &g_timeSyncCb);
184 EXPECT_TRUE(g_flag);
185 }
186
187 /**
188 * @tc.name: DDOS_PublishLNNTest001
189 * @tc.desc: Verify normal case
190 * @tc.type: FUNC
191 * @tc.require: I5I7B9 I5PTUS
192 */
193 HWTEST_F(BusCenterSdkDdosTest, DDOS_PublishLNNTest001, TestSize.Level0)
194 {
195 int32_t tmpId1 = GetPublishId();
196 g_pInfo.publishId = tmpId1;
197 for (int i = 0; i < USE_TIMES; i++) {
198 PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
199 StopPublishLNN(TEST_PKG_NAME, tmpId1);
200 }
201 PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
202 StopPublishLNN(TEST_PKG_NAME, tmpId1);
203 EXPECT_TRUE(g_flag);
204 }
205
206 /**
207 * @tc.name: DDOS_RefreshLNNTest001
208 * @tc.desc: Verify normal case
209 * @tc.type: FUNC
210 * @tc.require: I5I7B9 I5PTUS
211 */
212 HWTEST_F(BusCenterSdkDdosTest, DDOS_RefreshLNNTest001, TestSize.Level0)
213 {
214 int32_t tmpId1 = GetSubscribeId();
215 g_sInfo.subscribeId = tmpId1;
216 for (int i = 0; i < USE_TIMES; i++) {
217 RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
218 StopRefreshLNN(TEST_PKG_NAME, tmpId1);
219 }
220 RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
221 StopRefreshLNN(TEST_PKG_NAME, tmpId1);
222 EXPECT_TRUE(g_flag);
223 }
224
225 /*
226 * @tc.name: DDOS_Leave_Lnn_Test_001
227 * @tc.desc: bus center LeaveLNN interface exception test
228 * @tc.type: FUNC
229 * @tc.require: I5I7B9
230 */
231 HWTEST_F(BusCenterSdkDdosTest, DDOS_Leave_Lnn_Test_001, TestSize.Level0)
232 {
233 char networkId[] = "012345678998765432100123456789987654321001234567899876543210abcde";
234 for (int i = 0; i < USE_TIMES; i++) {
235 LeaveLNN(TEST_PKG_NAME, networkId, OnLeaveLNNDone);
236 }
237 EXPECT_TRUE(g_flag);
238 }
239
240 /*
241 * @tc.name: DDOS_Leave_Lnn_Test_002
242 * @tc.desc: bus center LeaveLNN interface exception test
243 * @tc.type: FUNC
244 * @tc.require: I5I7B9
245 */
246 HWTEST_F(BusCenterSdkDdosTest, DDOS_Leave_Lnn_Test_002, TestSize.Level0)
247 {
248 char networkId[] = "012345678998765432100123456789987654321001234567899876543210abcde";
249 constexpr char testPkgName[] = "com.softbus.test00";
250 constexpr char testPkgName1[] = "com.softbus.test01";
251 constexpr char testPkgName2[] = "com.softbus.test02";
252 constexpr char testPkgName3[] = "com.softbus.test03";
253 constexpr char testPkgName4[] = "com.softbus.test04";
254 constexpr char testPkgName5[] = "com.softbus.test05";
255 constexpr char testPkgName6[] = "com.softbus.test06";
256 constexpr char testPkgName7[] = "com.softbus.test07";
257 for (int i = 0; i < USE_TIMES; i++) {
258 LeaveLNN(testPkgName, networkId, OnLeaveLNNDone);
259 LeaveLNN(testPkgName1, networkId, OnLeaveLNNDone);
260 LeaveLNN(testPkgName2, networkId, OnLeaveLNNDone);
261 LeaveLNN(testPkgName3, networkId, OnLeaveLNNDone);
262 LeaveLNN(testPkgName4, networkId, OnLeaveLNNDone);
263 LeaveLNN(testPkgName5, networkId, OnLeaveLNNDone);
264 LeaveLNN(testPkgName6, networkId, OnLeaveLNNDone);
265 }
266 for (int i = 0; i < LEAVELNN_TIMES; i++) {
267 LeaveLNN(testPkgName7, networkId, OnLeaveLNNDone);
268 }
269 EXPECT_TRUE(g_flag);
270 }
271
272 /*
273 * @tc.name: DDOS_GET_LOCAL_NODE_INFO_Test_001
274 * @tc.desc: get local info interface test
275 * @tc.type: FUNC
276 * @tc.require: I5I7B9
277 */
278 HWTEST_F(BusCenterSdkDdosTest, DDOS_GET_LOCAL_NODE_INFO_Test_001, TestSize.Level0)
279 {
280 NodeBasicInfo info;
281 char networkId[] = "12313";
282 uint16_t dataChangeFlag = DATA_CHANGE_FLAG;
283 constexpr char testPkgName[] = "com.softbus.test00";
284 constexpr char testPkgName1[] = "com.softbus.test01";
285 constexpr char testPkgName2[] = "com.softbus.test02";
286 constexpr char testPkgName3[] = "com.softbus.test03";
287 for (int i = 0; i < USE_TIMES; i++) {
288 SetNodeDataChangeFlag(testPkgName, networkId, dataChangeFlag);
289 SetNodeDataChangeFlag(testPkgName1, networkId, dataChangeFlag);
290 SetNodeDataChangeFlag(testPkgName2, networkId, dataChangeFlag);
291 SetNodeDataChangeFlag(testPkgName3, networkId, dataChangeFlag);
292 }
293 for (int i = 0; i < USE_TIMES; i++) {
294 GetLocalNodeDeviceInfo(testPkgName, &info);
295 }
296 EXPECT_TRUE(g_flag);
297 sleep(10);
298 }
299 } // namespace OHOS
300