• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 #include <string.h>
16 #include <unistd.h>
17 #include <pthread.h>
18 
19 #include "hctest.h"
20 #include "securec.h"
21 #include "session.h"
22 #include "softbus_bus_center.h"
23 #include "softbus_errcode.h"
24 #include "discovery_service.h"
25 #include "softbus_server_frame.h"
26 #include "cmsis_os.h"
27 
28 #define UI_MAIN_TASK_DELAY 10000
29 
30 #define MAINLOOP_STACK_SIZE 5120
31 #define SLEEP_TIME 4
32 static int32_t serverInit = 0;
33 static int g_subscribeId = 0;
34 static int g_publishId = 0;
35 static const char *g_pkgName = "Softbus_Kits";
36 
37 static const int32_t ERRO_CAPDATA_LEN = 514;
38 
GetSubscribeId(void)39 static int GetSubscribeId(void)
40 {
41     g_subscribeId++;
42     return g_subscribeId;
43 }
44 
GetPublishId(void)45 static int GetPublishId(void)
46 {
47     g_publishId++;
48     return g_publishId;
49 }
50 
51 static SubscribeInfo g_sInfo = {
52     .subscribeId = 1,
53     .mode = DISCOVER_MODE_ACTIVE,
54     .medium = COAP,
55     .freq = MID,
56     .isSameAccount = true,
57     .isWakeRemote = false,
58     .capability = "dvKit",
59     .capabilityData = (unsigned char *)"capdata3",
60     .dataLen = sizeof("capdata3")
61 };
62 
63 static PublishInfo g_pInfo = {
64     .publishId = 1,
65     .mode = DISCOVER_MODE_ACTIVE,
66     .medium = COAP,
67     .freq = MID,
68     .capability = "dvKit",
69     .capabilityData = (unsigned char *)"capdata4",
70     .dataLen = sizeof("capdata4")
71 };
72 
73 static PublishInfo g_pInfo1 = {
74     .publishId = 1,
75     .mode = DISCOVER_MODE_ACTIVE,
76     .medium = COAP,
77     .freq = MID,
78     .capability = "dvKit",
79     .capabilityData = NULL,
80     .dataLen = 0
81 };
82 
83 static SubscribeInfo g_sInfo1 = {
84     .subscribeId = 1,
85     .mode = DISCOVER_MODE_ACTIVE,
86     .medium = COAP,
87     .freq = MID,
88     .isSameAccount = true,
89     .isWakeRemote = false,
90     .capability = "hicall",
91     .capabilityData = NULL,
92     .dataLen = 0
93 };
94 
TestDeviceFound(void)95 static void TestDeviceFound(void)
96 {
97     printf("[client]TestDeviceFound\n");
98 }
99 
TestDiscoverFailed(void)100 static void TestDiscoverFailed(void)
101 {
102     printf("[client]TestDiscoverFailed\n");
103 }
104 
TestDiscoverySuccess(void)105 static void TestDiscoverySuccess(void)
106 {
107     printf("[client]TestDiscoverySuccess\n");
108 }
109 
TestPublishSuccess(void)110 static void TestPublishSuccess(void)
111 {
112     printf("[client]TestPublishSuccess\n");
113 }
114 
TestPublishFail(void)115 static void TestPublishFail(void)
116 {
117     printf("[client]TestPublishFail\n");
118 }
119 
120 static IDiscoveryCallback g_subscribeCb = {
121     .OnDeviceFound = TestDeviceFound,
122     .OnDiscoverFailed = TestDiscoverFailed,
123     .OnDiscoverySuccess = TestDiscoverySuccess
124 };
125 
126 static IPublishCallback g_publishCb = {
127     .OnPublishSuccess = TestPublishSuccess,
128     .OnPublishFail = TestPublishFail
129 };
130 
131 LITE_TEST_SUIT(dsoftbus, discoveryservice, DiscoveryServiceTestSuite);
132 
ServerInit(void * arg)133 static void *ServerInit(void *arg)
134 {
135     printf("----------start ServerInit-------------\n");
136     InitSoftBusServer();
137     return NULL;
138 }
139 
ThreadCreateTest(void * (* entry)(void * arg))140 static void ThreadCreateTest(void *(*entry)(void *arg))
141 {
142     pthread_t tid;
143     pthread_attr_t threadAttr;
144     pthread_attr_init(&threadAttr);
145     pthread_attr_setstacksize(&threadAttr, MAINLOOP_STACK_SIZE);
146     pthread_create(&tid, &threadAttr, entry, 0);
147 }
148 
DiscoveryServiceTestSuiteSetUp(void)149 static BOOL DiscoveryServiceTestSuiteSetUp(void)
150 {
151     printf("----------test case with DiscoveryServiceTestSuite start-------------\n");
152     if (GetServerIsInit() == false) {
153         printf("----------Server Is not Init-------------\n");
154         ThreadCreateTest(ServerInit);
155         sleep(SLEEP_TIME);
156     }
157     serverInit = 1;
158     return TRUE;
159 }
160 
DiscoveryServiceTestSuiteTearDown(void)161 static BOOL DiscoveryServiceTestSuiteTearDown(void)
162 {
163     printf("----------test case with DiscoveryServiceTestSuite end-------------\n");
164     return TRUE;
165 }
166 
167 /**
168  * @tc.name: PublishServiceTest001
169  * @tc.desc: Verify wrong parameter
170  * @tc.type: FUNC
171  * @tc.require:
172  */
173 LITE_TEST_CASE(DiscoveryServiceTestSuite, PublishServiceTest001, Function | MediumTest | Level0)
174 {
175     int ret;
176     PublishInfo testInfo = {
177         .publishId = GetPublishId(),
178         .mode = DISCOVER_MODE_ACTIVE,
179         .medium = COAP,
180         .freq = MID,
181         .capability = "dvKit",
182         .capabilityData = (unsigned char *)"capdata2",
183         .dataLen = sizeof("capdata2")
184     };
185 
186     while (GetServerIsInit() == false) {
187         sleep(1);
188     }
189     osDelay(UI_MAIN_TASK_DELAY);
190     ret = PublishService(NULL, &testInfo, &g_publishCb);
191     TEST_ASSERT_TRUE(ret != 0);
192 
193     ret = PublishService(g_pkgName, NULL, &g_publishCb);
194     TEST_ASSERT_TRUE(ret != 0);
195 
196     ret = PublishService(g_pkgName, &testInfo, NULL);
197     TEST_ASSERT_TRUE(ret != 0);
198 
199     testInfo.medium = (ExchanageMedium)(COAP + 1);
200     ret = PublishService(g_pkgName, &testInfo, &g_publishCb);
201     TEST_ASSERT_TRUE(ret != 0);
202     testInfo.medium = COAP;
203 
204     testInfo.mode = (DiscoverMode)(DISCOVER_MODE_ACTIVE + 1);
205     ret = PublishService(g_pkgName, &testInfo, &g_publishCb);
206     TEST_ASSERT_TRUE(ret != 0);
207     testInfo.mode = DISCOVER_MODE_ACTIVE;
208 
209     testInfo.freq = (ExchangeFreq)(SUPER_HIGH + 1);
210     ret = PublishService(g_pkgName, &testInfo, &g_publishCb);
211     TEST_ASSERT_TRUE(ret != 0);
212     testInfo.freq = LOW;
213 
214     testInfo.capabilityData = NULL;
215     ret = PublishService(g_pkgName, &testInfo, &g_publishCb);
216     TEST_ASSERT_TRUE(ret != 0);
217     testInfo.capabilityData = (unsigned char *)"capdata1";
218 
219     testInfo.dataLen = ERRO_CAPDATA_LEN;
220     ret = PublishService(g_pkgName, &testInfo, &g_publishCb);
221     TEST_ASSERT_TRUE(ret != 0);
222     testInfo.dataLen = sizeof("capdata1");
223 }
224 
225 /**
226  * @tc.name: UnPublishServiceTest001
227  * @tc.desc: Verify wrong parameter
228  * @tc.type: FUNC
229  * @tc.require:
230  */
231 LITE_TEST_CASE(DiscoveryServiceTestSuite, UnPublishServiceTest001, Function | MediumTest | Level0)
232 {
233     int ret;
234     int tmpId = GetPublishId();
235     ret = UnPublishService(NULL, tmpId);
236     TEST_ASSERT_TRUE(ret != 0);
237 }
238 
239 /**
240  * @tc.name: StartDiscoveryTest001
241  * @tc.desc: Verify wrong parameter
242  * @tc.type: FUNC
243  * @tc.require:
244  */
245 LITE_TEST_CASE(DiscoveryServiceTestSuite, StartDiscoveryTest001, Function | MediumTest | Level0)
246 {
247     int ret;
248     SubscribeInfo testInfo = {
249         .subscribeId = GetSubscribeId(),
250         .mode = DISCOVER_MODE_ACTIVE,
251         .medium = COAP,
252         .freq = MID,
253         .isSameAccount = true,
254         .isWakeRemote = false,
255         .capability = "dvKit",
256         .capabilityData = (unsigned char *)"capdata3",
257         .dataLen = sizeof("capdata3")
258     };
259 
260     ret = StartDiscovery(NULL, &testInfo, &g_subscribeCb);
261     TEST_ASSERT_TRUE(ret != 0);
262 
263     ret = StartDiscovery(g_pkgName, NULL, &g_subscribeCb);
264     TEST_ASSERT_TRUE(ret != 0);
265 
266     ret = StartDiscovery(g_pkgName, &testInfo, NULL);
267     TEST_ASSERT_TRUE(ret != 0);
268 
269     testInfo.medium = (ExchanageMedium)(COAP + 1);
270     ret = StartDiscovery(g_pkgName, &testInfo, &g_subscribeCb);
271     TEST_ASSERT_TRUE(ret != 0);
272     testInfo.medium = COAP;
273 
274     testInfo.mode = (DiscoverMode)(DISCOVER_MODE_ACTIVE + 1);
275     ret = StartDiscovery(g_pkgName, &testInfo, &g_subscribeCb);
276     TEST_ASSERT_TRUE(ret != 0);
277     testInfo.mode = DISCOVER_MODE_ACTIVE;
278 
279     testInfo.freq = (ExchangeFreq)(SUPER_HIGH + 1);
280     ret = StartDiscovery(g_pkgName, &testInfo, &g_subscribeCb);
281     TEST_ASSERT_TRUE(ret != 0);
282     testInfo.freq = LOW;
283 
284     testInfo.capabilityData = NULL;
285     ret = StartDiscovery(g_pkgName, &testInfo, &g_subscribeCb);
286     TEST_ASSERT_TRUE(ret != 0);
287     testInfo.capabilityData = (unsigned char *)"capdata1";
288 
289     testInfo.dataLen = ERRO_CAPDATA_LEN;
290     ret = StartDiscovery(g_pkgName, &testInfo, &g_subscribeCb);
291     TEST_ASSERT_TRUE(ret != 0);
292     testInfo.dataLen = sizeof("capdata1");
293 }
294 
295 /**
296  * @tc.name: StopDiscoveryTest001
297  * @tc.desc: Verify wrong parameter
298  * @tc.type: FUNC
299  * @tc.require:
300  */
301 LITE_TEST_CASE(DiscoveryServiceTestSuite, StopDiscoveryTest001, Function | MediumTest | Level0)
302 {
303     int ret;
304     int tmpId = GetSubscribeId();
305     ret = StopDiscovery(NULL, tmpId);
306     TEST_ASSERT_TRUE(ret != 0);
307 }
308 
309 RUN_TEST_SUITE(DiscoveryServiceTestSuite);