• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/05/16
17  */
18 
19 #include <stdlib.h>
20 #include "cmsis_os.h"
21 #include "securec.h"
22 #include "cmsis_os2.h"
23 #include "hctest.h"
24 #include "samgr_lite.h"
25 
26 #define OPER_INTERVAL 200    // 200 ms
27 #define TARGET_NUM 2
28 
29 static const char *GetName(Service *service);
30 static BOOL Initialize(Service *service, Identity identity);
31 static BOOL MessageHandle(Service *service, Request *msg);
32 static TaskConfig GetTaskConfig(Service *service);
33 
34 static void *g_servicePoint1;
35 static void *g_servicePoint2;
36 
37 typedef struct DemoApi {
38     INHERIT_IUNKNOWN;
39     BOOL(*FeatureApi001)
40     (IUnknown *iUnknown, char *para1);
41     int32 (*SendRequestProxyF)(const Identity *identity, const Request *request, Handler handler);
42 } DemoApi;
43 
44 typedef struct DemoFeature {
45     INHERIT_FEATURE;
46     INHERIT_IUNKNOWNENTRY(DemoApi);
47     Identity identity;
48     int featureCalledCount;
49 } DemoFeature;
50 
51 typedef struct DefaultFeatureApi {
52     INHERIT_IUNKNOWN;
53     BOOL(*DefaultApi001)
54     (IUnknown *iUnknown, char *para1);
55     int32 (*SendRequestProxyDF)(const Identity *identity, const Request *request, Handler handler);
56 } DefaultFeatureApi;
57 
58 typedef struct DemoService {
59     INHERIT_SERVICE;
60     INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
61     Identity identity;
62     int serviceCalledCount;
63 } DemoService;
64 
DefaultApi001(IUnknown * iUnknown,char * para1)65 static BOOL DefaultApi001(IUnknown *iUnknown, char *para1)
66 {
67     (void)iUnknown;
68     (void)para1;
69     return TRUE;
70 }
SendRequestProxyDF(const Identity * identity,const Request * request,Handler handler)71 static int32 SendRequestProxyDF(const Identity *identity, const Request *request, Handler handler)
72 {
73     return SAMGR_SendRequest(identity, request, handler);
74 }
75 
FeatureApi001(IUnknown * iUnknown,char * para1)76 static BOOL FeatureApi001(IUnknown *iUnknown, char *para1)
77 {
78     (void)iUnknown;
79     (void)para1;
80     return TRUE;
81 }
SendRequestProxyF(const Identity * identity,const Request * request,Handler handler)82 static int32 SendRequestProxyF(const Identity *identity, const Request *request, Handler handler)
83 {
84     return SAMGR_SendRequest(identity, request, handler);
85 }
86 
87 static DemoService g_service[] = {
88     {
89         .GetName = GetName,
90         .Initialize = Initialize,
91         .MessageHandle = MessageHandle,
92         .GetTaskConfig = GetTaskConfig,
93         .ref = 1,
94         .iUnknown = {
95             DEFAULT_IUNKNOWN_IMPL,
96             .DefaultApi001 = DefaultApi001,
97             .SendRequestProxyDF = SendRequestProxyDF,
98         },
99         .serviceCalledCount = 0,
100     },
101     {
102         .GetName = GetName,
103         .Initialize = Initialize,
104         .MessageHandle = MessageHandle,
105         .GetTaskConfig = GetTaskConfig,
106         .ref = 1,
107         .iUnknown = {
108             DEFAULT_IUNKNOWN_IMPL,
109             .DefaultApi001 = DefaultApi001,
110             .SendRequestProxyDF = SendRequestProxyDF,
111         },
112         .serviceCalledCount = 0,
113     }
114 };
115 
GetName(Service * service)116 static const char *GetName(Service *service)
117 {
118     if (service == (Service *)&g_service[0]) {
119         return "SpecifiedT01";
120     } else {
121         return "SpecifiedT02";
122     }
123 }
124 
Initialize(Service * service,Identity identity)125 static BOOL Initialize(Service *service, Identity identity)
126 {
127     DemoService *demoService = (DemoService *)service;
128     demoService->identity = identity;
129 
130     return TRUE;
131 }
132 
MessageHandle(Service * service,Request * msg)133 static BOOL MessageHandle(Service *service, Request *msg)
134 {
135     (void)msg;
136     DemoService *demoService = (DemoService *)service;
137     demoService->serviceCalledCount++;
138 
139     if (service == (Service *)&g_service[0]) {
140         g_servicePoint1 =  osThreadGetId();
141     } else {
142         g_servicePoint2 =  osThreadGetId();
143     }
144 
145     return TRUE;
146 }
147 
GetTaskConfig(Service * service)148 static TaskConfig GetTaskConfig(Service *service)
149 {
150     (void)service;
151     TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, 1600, 20, SPECIFIED_TASK};
152     return config;
153 }
154 
FEATURE_GetName(Feature * feature)155 static const char *FEATURE_GetName(Feature *feature)
156 {
157     (void)feature;
158     return "featureName501";
159 }
160 
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)161 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
162 {
163     (void)parent;
164     DemoFeature *demoFeature = (DemoFeature *)feature;
165     demoFeature->identity = identity;
166 }
167 
FEATURE_OnStop(Feature * feature,Identity identity)168 static void FEATURE_OnStop(Feature *feature, Identity identity)
169 {
170     (void)feature;
171     (void)identity;
172 }
173 
FEATURE_OnMessage(Feature * feature,Request * request)174 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
175 {
176     (void)request;
177     DemoFeature *demoFeature = (DemoFeature *)feature;
178     demoFeature->featureCalledCount++;
179 
180     return TRUE;
181 }
182 
183 static DemoFeature g_createFeature = {
184     .GetName = FEATURE_GetName,
185     .OnInitialize = FEATURE_OnInitialize,
186     .OnStop = FEATURE_OnStop,
187     .OnMessage = FEATURE_OnMessage,
188     .ref = 1,
189     .iUnknown = {
190         DEFAULT_IUNKNOWN_IMPL,
191         .FeatureApi001 = FeatureApi001,
192         .SendRequestProxyF = SendRequestProxyF,
193     },
194     .identity = {-1, -1, NULL},
195     .featureCalledCount = 0,
196 };
197 
ServiceInit(void)198 static void ServiceInit(void)
199 {
200     for (int i = 0; i < TARGET_NUM; i++) {
201         SAMGR_GetInstance()->RegisterService((Service *)&g_service[i]);
202     }
203 }
204 SYS_SERVICE_INIT(ServiceInit);
205 
FeatureInit(void)206 static void FeatureInit(void)
207 {
208     for (int i = 0; i < TARGET_NUM; i++) {
209         SAMGR_GetInstance()->RegisterDefaultFeatureApi(g_service[i].GetName((Service *)&g_service[i]),
210                                                        GET_IUNKNOWN(g_service[i]));
211 
212         SAMGR_GetInstance()->RegisterFeature(g_service[i].GetName((Service *)&g_service[i]),
213                                              (Feature *)&g_createFeature);
214         SAMGR_GetInstance()->RegisterFeatureApi(g_service[i].GetName((Service *)&g_service[i]),
215                                                 "featureName501", GET_IUNKNOWN(g_createFeature));
216     }
217 }
218 SYS_FEATURE_INIT(FeatureInit);
219 
GetIUnknown(const char * serviceName,const char * featureName)220 static DemoApi *GetIUnknown(const char *serviceName, const char *featureName)
221 {
222     DemoApi *demoApi = NULL;
223     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(serviceName, featureName);
224     if (iUnknown == NULL) {
225         return NULL;
226     }
227     int result = iUnknown->QueryInterface(iUnknown, 0, (void **)&demoApi);
228     if (result == 0 && demoApi != NULL) {
229         return demoApi;
230     } else {
231         return NULL;
232     }
233 }
234 
GetDefaultIUnknown(const char * serviceName)235 static DefaultFeatureApi *GetDefaultIUnknown(const char *serviceName)
236 {
237     DefaultFeatureApi *defaultApi = NULL;
238     IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(serviceName);
239     if (iUnknown == NULL) {
240         return NULL;
241     }
242     int result = iUnknown->QueryInterface(iUnknown, 0, (void **)&defaultApi);
243     if (result == 0 && defaultApi != NULL) {
244         return defaultApi;
245     } else {
246         return NULL;
247     }
248 }
249 
250 LITE_TEST_SUIT(distributedschedule, taskpool, SpecifiedTaskFuncTestSuite);
251 
SpecifiedTaskFuncTestSuiteSetUp(void)252 static BOOL SpecifiedTaskFuncTestSuiteSetUp(void)
253 {
254     osDelay(OPER_INTERVAL);
255     return TRUE;
256 }
257 
SpecifiedTaskFuncTestSuiteTearDown(void)258 static BOOL SpecifiedTaskFuncTestSuiteTearDown(void)
259 {
260     return TRUE;
261 }
262 
263 /**
264  * @tc.number    : DMSLite_SAMGR_Taskpool_SpecifiedTask_0010
265  * @tc.name      : Service share taskpool by the same level, the first task function is ok
266  * @tc.desc      : [C- SOFTWARE -0200]
267  */
268 LITE_TEST_CASE(SpecifiedTaskFuncTestSuite, testSpecifiedTask0010, Function | MediumTest | Level2)
269 {
270     DemoApi *demoApi = GetIUnknown("SpecifiedT01", "featureName501");
271     if (demoApi == NULL) {
272         TEST_FAIL();
273     }
274     BOOL result = demoApi->FeatureApi001((IUnknown *)demoApi, "xxxx");
275     TEST_ASSERT_EQUAL_INT(result, TRUE);
276 
277     g_createFeature.featureCalledCount = 0;
278     Request request = {.msgId = 0, .msgValue = 0};
279     char *body = "I wanna async call good result!";
280     request.len = (int16)(strlen(body) + 1);
281     request.data = malloc(request.len);
282     if (request.data == NULL) {
283         TEST_FAIL();
284     }
285     strcpy_s(request.data, request.len, body);
286     int32 result2 = demoApi->SendRequestProxyF(&(g_createFeature.identity), &request, NULL);
287     TEST_ASSERT_EQUAL_INT(result2 == 0, TRUE);
288     osDelay(OPER_INTERVAL);
289     TEST_ASSERT_EQUAL_INT(g_createFeature.featureCalledCount == 1, TRUE);
290 
291     // *************************** //
292     DefaultFeatureApi *defaultApi = GetDefaultIUnknown("SpecifiedT01");
293     if (defaultApi == NULL) {
294         TEST_FAIL();
295     }
296     result = defaultApi->DefaultApi001((IUnknown *)defaultApi, "yyyy");
297     TEST_ASSERT_EQUAL_INT(result, TRUE);
298 
299     g_service[0].serviceCalledCount = 0;
300     Request request2 = {.msgId = 0, .msgValue = 0};
301     char *body2 = "I want to call defaultFeature!";
302     request2.len = (int16)(strlen(body2) + 1);
303     request2.data = malloc(request2.len);
304     if (request2.data == NULL) {
305         TEST_FAIL();
306     }
307     strcpy_s(request2.data, request2.len, body2);
308     result2 = defaultApi->SendRequestProxyDF(&(g_service[0].identity), &request2, NULL);
309     TEST_ASSERT_EQUAL_INT(result2 == 0, TRUE);
310     osDelay(OPER_INTERVAL);
311     TEST_ASSERT_EQUAL_INT(g_service[0].serviceCalledCount == 1, TRUE);
312 };
313 
314 /**
315  * @tc.number    : DMSLite_SAMGR_Taskpool_SpecifiedTask_0020
316  * @tc.name      : Service share taskpool by the same level, the second task function is ok
317  * @tc.desc      : [C- SOFTWARE -0200]
318  */
319 LITE_TEST_CASE(SpecifiedTaskFuncTestSuite, testSpecifiedTask0020, Function | MediumTest | Level2)
320 {
321     DemoApi *demoApi = GetIUnknown("SpecifiedT02", "featureName501");
322     if (demoApi == NULL) {
323         TEST_FAIL();
324     }
325     BOOL result = demoApi->FeatureApi001((IUnknown *)demoApi, "xxxx");
326     TEST_ASSERT_EQUAL_INT(result, TRUE);
327 
328     g_createFeature.featureCalledCount = 0;
329     Request request = {.msgId = 0, .msgValue = 0};
330     char *body = "I wanna async call good result!";
331     request.len = (int16)(strlen(body) + 1);
332     request.data = malloc(request.len);
333     if (request.data == NULL) {
334         TEST_FAIL();
335     }
336     strcpy_s(request.data, request.len, body);
337     int32 result2 = demoApi->SendRequestProxyF(&(g_createFeature.identity), &request, NULL);
338     TEST_ASSERT_EQUAL_INT(result2 == 0, TRUE);
339     osDelay(OPER_INTERVAL);
340     TEST_ASSERT_EQUAL_INT(g_createFeature.featureCalledCount == 1, TRUE);
341 
342     // *************************** //
343     DefaultFeatureApi *defaultApi = GetDefaultIUnknown("SpecifiedT02");
344     if (defaultApi == NULL) {
345         TEST_FAIL();
346     }
347     result = defaultApi->DefaultApi001((IUnknown *)defaultApi, "yyyy");
348     TEST_ASSERT_EQUAL_INT(result, TRUE);
349 
350     g_service[0].serviceCalledCount = 0;
351     Request request2 = {.msgId = 0, .msgValue = 0};
352     char *body2 = "I want to call defaultFeature!";
353     request2.len = (int16)(strlen(body2) + 1);
354     request2.data = malloc(request2.len);
355     if (request2.data == NULL) {
356         TEST_FAIL();
357     }
358     strcpy_s(request2.data, request2.len, body2);
359     result2 = defaultApi->SendRequestProxyDF(&(g_service[0].identity), &request2, NULL);
360     TEST_ASSERT_EQUAL_INT(result2 == 0, TRUE);
361     osDelay(OPER_INTERVAL);
362     TEST_ASSERT_EQUAL_INT(g_service[0].serviceCalledCount == 1, TRUE);
363 };
364 
365 /**
366  * @tc.number    : DMSLite_SAMGR_Taskpool_SpecifiedTask_0030
367  * @tc.name      : Service share taskpool by the same level, the thread id is the same
368  * @tc.desc      : [C- SOFTWARE -0200]
369  */
370 LITE_TEST_CASE(SpecifiedTaskFuncTestSuite, testSpecifiedTask0030, Function | MediumTest | Level2)
371 {
372     DefaultFeatureApi *defaultApi = GetDefaultIUnknown("SpecifiedT01");
373     if (defaultApi == NULL) {
374         TEST_FAIL();
375     }
376     Request request = {.msgId = 0, .msgValue = 0};
377     char *body = "I want to call defaultFeature!";
378     request.len = (int16)(strlen(body) + 1);
379     request.data = malloc(request.len);
380     if (request.data == NULL) {
381         TEST_FAIL();
382     }
383     strcpy_s(request.data, request.len, body);
384     defaultApi->SendRequestProxyDF(&(g_service[0].identity), &request, NULL);
385 
386     DefaultFeatureApi *defaultApi2 = GetDefaultIUnknown("SpecifiedT01");
387     if (defaultApi2 == NULL) {
388         TEST_FAIL();
389     }
390     Request request2 = {.msgId = 0, .msgValue = 0};
391     char *body2 = "I want to call defaultFeature!";
392     request2.len = (int16)(strlen(body2) + 1);
393     request2.data = malloc(request2.len);
394     if (request2.data == NULL) {
395         TEST_FAIL();
396     }
397     strcpy_s(request2.data, request2.len, body2);
398     defaultApi2->SendRequestProxyDF(&(g_service[1].identity), &request2, NULL);
399 
400     osDelay(OPER_INTERVAL);
401     TEST_ASSERT_EQUAL_INT(g_servicePoint1 == g_servicePoint2, TRUE);
402 };
403 RUN_TEST_SUITE(SpecifiedTaskFuncTestSuite);