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