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
23 #define OPER_INTERVAL 200 // 200 ms
24
25 typedef struct DemoApi {
26 INHERIT_IUNKNOWN;
27 BOOL (*FeatureApi001)(IUnknown *iUnknown, char *para1);
28 } DemoApi;
29
30 typedef struct DemoFeature {
31 INHERIT_FEATURE;
32 INHERIT_IUNKNOWNENTRY(DemoApi);
33 Identity identity;
34 } DemoFeature;
35
36 typedef struct DefaultFeatureApi {
37 INHERIT_IUNKNOWN;
38 BOOL (*DefaultApi001)(IUnknown *iUnknown, char *para1);
39 } DefaultFeatureApi;
40
41 typedef struct DemoService {
42 INHERIT_SERVICE;
43 INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
44 Identity identity;
45 } DemoService;
46
DefaultApi001(IUnknown * iUnknown,char * para1)47 static BOOL DefaultApi001(IUnknown *iUnknown, char *para1)
48 {
49 (void)iUnknown;
50 (void)para1;
51 return TRUE;
52 }
53
FeatureApi001(IUnknown * iUnknown,char * para1)54 static BOOL FeatureApi001(IUnknown *iUnknown, char *para1)
55 {
56 (void)iUnknown;
57 (void)para1;
58 return TRUE;
59 }
60
GetName(Service * service)61 static const char *GetName(Service *service)
62 {
63 (void)service;
64 return "Service_NoTask";
65 }
66
Initialize(Service * service,Identity identity)67 static BOOL Initialize(Service *service, Identity identity)
68 {
69 DemoService *service1 = (DemoService *)service;
70 service1->identity = identity;
71 return TRUE;
72 }
73
MessageHandle(Service * service,Request * msg)74 static BOOL MessageHandle(Service *service, Request *msg)
75 {
76 (void)service;
77 (void)msg;
78 return FALSE;
79 }
80
GetTaskConfig(Service * service)81 static TaskConfig GetTaskConfig(Service *service)
82 {
83 (void)service;
84 TaskConfig config = {LEVEL_HIGH, PRI_NORMAL, 0x800, 20, NO_TASK};
85 return config;
86 }
87
88 static DemoService g_createService = {
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 }
98 };
99
FEATURE_GetName(Feature * feature)100 static const char *FEATURE_GetName(Feature *feature)
101 {
102 (void)feature;
103 return "featureName501";
104 }
105
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)106 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
107 {
108 DemoFeature *demoFeature = (DemoFeature *)feature;
109 demoFeature->identity = identity;
110 (void)parent;
111 }
112
FEATURE_OnStop(Feature * feature,Identity identity)113 static void FEATURE_OnStop(Feature *feature, Identity identity)
114 {
115 (void)feature;
116 (void)identity;
117 }
118
FEATURE_OnMessage(Feature * feature,Request * request)119 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
120 {
121 (void)feature;
122 Response response = {
123 .data = "Yes, you did!",
124 .len = 0
125 };
126 SAMGR_SendResponse(request, &response);
127 return TRUE;
128 }
129
130 static DemoFeature g_createFeature = {
131 .GetName = FEATURE_GetName,
132 .OnInitialize = FEATURE_OnInitialize,
133 .OnStop = FEATURE_OnStop,
134 .OnMessage = FEATURE_OnMessage,
135 .ref = 1,
136 .iUnknown = {
137 DEFAULT_IUNKNOWN_IMPL,
138 .FeatureApi001 = FeatureApi001,
139 },
140 .identity = {-1, -1, NULL},
141 };
142
ServiceInit(void)143 static void ServiceInit(void)
144 {
145 BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_createService);
146 if (result == FALSE) {
147 printf("[hctest]E RegisterService failed. \n");
148 }
149 }
150 SYSEX_SERVICE_INIT(ServiceInit);
151
FeatureInit(void)152 static void FeatureInit(void)
153 {
154 BOOL result1 = SAMGR_GetInstance()->RegisterDefaultFeatureApi("Service_NoTask", GET_IUNKNOWN(g_createService));
155
156 BOOL result2 = SAMGR_GetInstance()->RegisterFeature("Service_NoTask", (Feature *)&g_createFeature);
157 BOOL result3 = SAMGR_GetInstance()->RegisterFeatureApi("Service_NoTask", "featureName501",
158 GET_IUNKNOWN(g_createFeature));
159 if (result1 == FALSE || result2 == FALSE || result3 == FALSE) {
160 printf("[hctest]E failed to register feature or api.\n");
161 }
162 }
163 SYSEX_FEATURE_INIT(FeatureInit);
164
GetIUnknown(const char * serviceName,const char * featureName)165 static DemoApi *GetIUnknown(const char *serviceName, const char *featureName)
166 {
167 DemoApi *demoApi = NULL;
168 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(serviceName, featureName);
169 if (iUnknown == NULL) {
170 return NULL;
171 }
172 int result = iUnknown->QueryInterface(iUnknown, 0, (void **)&demoApi);
173 if (result == 0 && demoApi != NULL) {
174 return demoApi;
175 } else {
176 return NULL;
177 }
178 }
179
GetDefaultIUnknown(const char * serviceName)180 static DefaultFeatureApi *GetDefaultIUnknown(const char *serviceName)
181 {
182 DefaultFeatureApi *defaultApi = NULL;
183 IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(serviceName);
184 if (iUnknown == NULL) {
185 printf("[hctest]failed to GetFeatureApi.\n");
186 return NULL;
187 }
188 int result = iUnknown->QueryInterface(iUnknown, 0, (void **)&defaultApi);
189 if (result == 0 && defaultApi != NULL) {
190 return defaultApi;
191 } else {
192 printf("[hctest]failed to QueryInterface.\n");
193 return NULL;
194 }
195 }
196
ReleaseIUnknown(DemoApi * demoApi)197 static void ReleaseIUnknown(DemoApi *demoApi)
198 {
199 demoApi->Release((IUnknown *)demoApi);
200 }
201
202 LITE_TEST_SUIT(distributedschedule, samgr, TaskPoolNoTaskFuncTestSuite);
203
TaskPoolNoTaskFuncTestSuiteSetUp(void)204 static BOOL TaskPoolNoTaskFuncTestSuiteSetUp(void)
205 {
206 osDelay(OPER_INTERVAL);
207 return TRUE;
208 }
209
TaskPoolNoTaskFuncTestSuiteTearDown(void)210 static BOOL TaskPoolNoTaskFuncTestSuiteTearDown(void)
211 {
212 return TRUE;
213 }
214
215 /**
216 * @tc.number : DMSLite_SAMGR_Taskpool_NoTask_0010
217 * @tc.name : Service without task can handle direct calls
218 * @tc.desc : [C- SOFTWARE -0200]
219 */
220 LITE_TEST_CASE(TaskPoolNoTaskFuncTestSuite, testNoTask0010, Function | MediumTest | Level2)
221 {
222 DemoApi *demoApi = GetIUnknown("Service_NoTask", "featureName501");
223 if (demoApi == NULL) {
224 TEST_FAIL();
225 }
226 BOOL result = demoApi->FeatureApi001((IUnknown *)demoApi, "xxxx");
227 TEST_ASSERT_EQUAL_INT(result, TRUE);
228 ReleaseIUnknown(demoApi);
229
230 DefaultFeatureApi *defaultApi = GetDefaultIUnknown("Service_NoTask");
231 if (defaultApi == NULL) {
232 TEST_FAIL();
233 }
234 result = defaultApi->DefaultApi001((IUnknown *)defaultApi, "yyyy");
235 TEST_ASSERT_EQUAL_INT(result, TRUE);
236 };
237
238 RUN_TEST_SUITE(TaskPoolNoTaskFuncTestSuite);