1 /**
2 * Copyright (c) 2020-2021 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 * Description : testcases for distributedschedule_lite subsystem
16 * Create : 2020/04/19
17 */
18
19 #include "cmsis_os.h"
20 #include "hctest.h"
21 #include "samgr_lite.h"
22 #include "broadcast_interface.h"
23
24 #define PRESSURE_BASE (1024 * 10)
25 #define TEST_LEN 10
26 #define OPER_INTERVAL 200
27 #define BROADCASR_TEST_SERVICE "S_broadcast2"
28
C1Callback(Consumer * consumer,const Topic * topic,const Request * request)29 static void C1Callback(Consumer *consumer, const Topic *topic, const Request *request)
30 {
31 (void)consumer;
32 (void)topic;
33 (void)request;
34 }
C2Callback(Consumer * consumer,const Topic * topic,const Request * request)35 static void C2Callback(Consumer *consumer, const Topic *topic, const Request *request)
36 {
37 (void)consumer;
38 (void)topic;
39 (void)request;
40 }
41
Equal(const Consumer * current,const Consumer * other)42 static BOOL Equal(const Consumer *current, const Consumer *other)
43 {
44 if (current->Notify == other->Notify) {
45 return TRUE;
46 } else {
47 return FALSE;
48 }
49 }
50
GetName(Service * service)51 static const char *GetName(Service *service)
52 {
53 (void)service;
54 return BROADCASR_TEST_SERVICE;
55 };
56
57 static Identity g_identity = { -1, -1, NULL};
58
Initialize(Service * service,Identity identity)59 static BOOL Initialize(Service *service, Identity identity)
60 {
61 g_identity = identity;
62 (void)service;
63 return TRUE;
64 };
65
MessageHandle(Service * service,Request * msg)66 static BOOL MessageHandle(Service *service, Request *msg)
67 {
68 (void)service;
69 (void)msg;
70 return FALSE;
71 };
72
GetTaskConfig(Service * service)73 static TaskConfig GetTaskConfig(Service *service)
74 {
75 TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, 0x800, 20, SHARED_TASK}; // share with broadcast
76 (void)service;
77 return config;
78 };
79
80 static Service g_service = {GetName, Initialize, MessageHandle, GetTaskConfig};
Init(void)81 static void Init(void)
82 {
83 BOOL result = SAMGR_GetInstance()->RegisterService(&g_service);
84 if (result == FALSE) {
85 printf("[hctest]E RegisterService failed.\n");
86 }
87 }
88 SYSEX_SERVICE_INIT(Init);
89
CASE_GetIUnknown(void)90 static PubSubInterface *CASE_GetIUnknown(void)
91 {
92 PubSubInterface *fapi = NULL;
93 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(BROADCAST_SERVICE, PUB_SUB_FEATURE);
94 if (iUnknown == NULL) {
95 printf("[hctest]CASE_GetIUnknown error ");
96 return NULL;
97 }
98
99 int result = iUnknown->QueryInterface(iUnknown, 0, (void **)&fapi);
100 if (result != 0 || fapi == NULL) {
101 printf("[hctest]CASE_GetIUnknown error ");
102 }
103 return fapi;
104 }
105
CASE_ReleaseIUnknown(PubSubInterface * fapi)106 static void CASE_ReleaseIUnknown(PubSubInterface *fapi)
107 {
108 fapi->Release((IUnknown *)fapi);
109 }
110
111 LITE_TEST_SUIT(test, broadcast, Broadcast02TestSuite);
112
Broadcast02TestSuiteSetUp(void)113 static BOOL Broadcast02TestSuiteSetUp(void)
114 {
115 osDelay(OPER_INTERVAL);
116 return TRUE;
117 }
118
Broadcast02TestSuiteTearDown(void)119 static BOOL Broadcast02TestSuiteTearDown(void)
120 {
121 return TRUE;
122 }
123
124 /**
125 * @tc.number : DMSLite_SAMGR_Publish_0010
126 * @tc.name : Provider can publish a broadcast message
127 * @tc.desc : [C- SOFTWARE -0200]
128 */
129 LITE_TEST_CASE(Broadcast02TestSuite, testPublish0010, Function | MediumTest | Level1)
130 {
131 PubSubInterface *fapi = CASE_GetIUnknown();
132 if (fapi == NULL) {
133 TEST_FAIL();
134 }
135 Subscriber *subscriber = &fapi->subscriber;
136 if (subscriber == NULL) {
137 TEST_FAIL();
138 }
139
140 Provider *provider = &fapi->provider;
141 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
142 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
143
144 Topic topic0 = 0;
145 subscriber->AddTopic((IUnknown *)fapi, &topic0);
146 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
147 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
148
149 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8 *) "==>111<==", TEST_LEN);
150 TEST_ASSERT_EQUAL_INT(1, result);
151
152 CASE_ReleaseIUnknown(fapi);
153 }
154
155 /**
156 * @tc.number : DMSLite_SAMGR_Publish_0020
157 * @tc.name : Provider failed to publish broadcast for the specified topic not exist
158 * @tc.desc : [C- SOFTWARE -0200]
159 */
160 LITE_TEST_CASE(Broadcast02TestSuite, testPublish0020, Function | MediumTest | Level2)
161 {
162 PubSubInterface *fapi = CASE_GetIUnknown();
163 if (fapi == NULL) {
164 TEST_FAIL();
165 }
166 Subscriber *subscriber = &fapi->subscriber;
167 if (subscriber == NULL) {
168 TEST_FAIL();
169 }
170
171 Provider *provider = &fapi->provider;
172 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
173 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
174
175 Topic topic0 = 0;
176 subscriber->AddTopic((IUnknown *)fapi, &topic0);
177 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
178 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
179
180 Topic noExisttopic = 99;
181 BOOL result = provider->Publish((IUnknown *)fapi, &noExisttopic, (uint8 *) "==>111<==", TEST_LEN);
182 TEST_ASSERT_EQUAL_INT(result, 0);
183
184 CASE_ReleaseIUnknown(fapi);
185 }
186
187 /**
188 * @tc.number : DMSLite_SAMGR_Publish_0030
189 * @tc.name : Provider failed to publish broadcast for len and data not match
190 * @tc.desc : [C- SOFTWARE -0200]
191 */
192 LITE_TEST_CASE(Broadcast02TestSuite, testPublish0030, Function | MediumTest | Level2)
193 {
194 PubSubInterface *fapi = CASE_GetIUnknown();
195 if (fapi == NULL) {
196 TEST_FAIL();
197 }
198 Subscriber *subscriber = &fapi->subscriber;
199 if (subscriber == NULL) {
200 TEST_FAIL();
201 }
202
203 Provider *provider = &fapi->provider;
204 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
205 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
206
207 Topic topic0 = 0;
208 subscriber->AddTopic((IUnknown *)fapi, &topic0);
209 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
210 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
211
212 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8 *) "==>111<==", TEST_LEN - 1);
213 TEST_ASSERT_EQUAL_INT(result, TRUE);
214
215 result = provider->Publish((IUnknown *)fapi, &topic0, (uint8 *) "==>111<==", TEST_LEN + 1);
216 TEST_ASSERT_EQUAL_INT(result, TRUE);
217
218 CASE_ReleaseIUnknown(fapi);
219 }
220
221 /**
222 * @tc.number : DMSLite_SAMGR_Publish_0040
223 * @tc.name : Provider publish broadcast successfully for 0 and max length message
224 * @tc.desc : [C- SOFTWARE -0200]
225 */
226 LITE_TEST_CASE(Broadcast02TestSuite, testPublish0040, Function | MediumTest | Level2)
227 {
228 PubSubInterface *fapi = CASE_GetIUnknown();
229 if (fapi == NULL) {
230 TEST_FAIL();
231 }
232 Subscriber *subscriber = &fapi->subscriber;
233 if (subscriber == NULL) {
234 TEST_FAIL();
235 }
236
237 Provider *provider = &fapi->provider;
238 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
239 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
240
241 Topic topic0 = 0;
242 subscriber->AddTopic((IUnknown *)fapi, &topic0);
243 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
244 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
245
246 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8 *) "", 0);
247 TEST_ASSERT_EQUAL_INT(result, 0);
248
249 CASE_ReleaseIUnknown(fapi);
250 }
251
252 /**
253 * @tc.number : DMSLite_SAMGR_Publish_0050
254 * @tc.name : Provider publish broadcast successfully for no consumer
255 * @tc.desc : [C- SOFTWARE -0200]
256 */
257 LITE_TEST_CASE(Broadcast02TestSuite, testPublish0050, Function | MediumTest | Level2)
258 {
259 PubSubInterface *fapi = CASE_GetIUnknown();
260 if (fapi == NULL) {
261 TEST_FAIL();
262 }
263 Subscriber *subscriber = &fapi->subscriber;
264 if (subscriber == NULL) {
265 TEST_FAIL();
266 }
267
268 Provider *provider = &fapi->provider;
269 if (provider == NULL) {
270 printf("[hctest]E provider is NULL. \n");
271 }
272
273 Topic topic0 = 0;
274 subscriber->AddTopic((IUnknown *)fapi, &topic0);
275
276 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8 *) "==>111<==", TEST_LEN);
277 TEST_ASSERT_EQUAL_INT(result, 1);
278
279 CASE_ReleaseIUnknown(fapi);
280 }
281
282 /**
283 * @tc.number : DMSLite_SAMGR_Publish_0060
284 * @tc.name : Provider publish broadcast successfully for multi consumers
285 * @tc.desc : [C- SOFTWARE -0200]
286 */
287 LITE_TEST_CASE(Broadcast02TestSuite, testPublish0060, Function | MediumTest | Level2)
288 {
289 PubSubInterface *fapi = CASE_GetIUnknown();
290 if (fapi == NULL) {
291 TEST_FAIL();
292 }
293 Subscriber *subscriber = &fapi->subscriber;
294 if (subscriber == NULL) {
295 TEST_FAIL();
296 }
297
298 Provider *provider = &fapi->provider;
299 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
300 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
301
302 Topic topic0 = 0;
303 subscriber->AddTopic((IUnknown *)fapi, &topic0);
304 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
305 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
306
307 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8 *) "==>111<==", TEST_LEN);
308 TEST_ASSERT_EQUAL_INT(result, TRUE);
309
310 CASE_ReleaseIUnknown(fapi);
311 }
312
313 /**
314 * @tc.number : DMSLite_SAMGR_Publish_0070
315 * @tc.name : Provider send broadcast repeatedly, no memory leak
316 * @tc.desc : [C- SOFTWARE -0200]
317 */
318 LITE_TEST_CASE(Broadcast02TestSuite, testPublish0070, Function | MediumTest | Level2)
319 {
320 PubSubInterface *fapi = CASE_GetIUnknown();
321 if (fapi == NULL) {
322 TEST_FAIL();
323 }
324 Subscriber *subscriber = &fapi->subscriber;
325 if (subscriber == NULL) {
326 TEST_FAIL();
327 }
328
329 Provider *provider = &fapi->provider;
330 static Consumer c1 = {.identity = &g_identity, .Notify = C1Callback, .Equal = Equal};
331 static Consumer c2 = {.identity = &g_identity, .Notify = C2Callback, .Equal = Equal};
332
333 Topic topic0 = 0;
334 subscriber->AddTopic((IUnknown *)fapi, &topic0);
335 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c1);
336 subscriber->Subscribe((IUnknown *)fapi, &topic0, &c2);
337
338 for (int i = 0; i < PRESSURE_BASE; i++) {
339 BOOL result = provider->Publish((IUnknown *)fapi, &topic0, (uint8 *) "==>111<==", TEST_LEN);
340 TEST_ASSERT_EQUAL_INT(result, TRUE);
341 }
342 subscriber->Unsubscribe((IUnknown *)fapi, &topic0, &c1);
343 subscriber->Unsubscribe((IUnknown *)fapi, &topic0, &c2);
344 CASE_ReleaseIUnknown(fapi);
345 }
346
347 RUN_TEST_SUITE(Broadcast02TestSuite);