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
16 #include <string.h>
17 #include "cmsis_os.h"
18 #include "hctest.h"
19 #include "samgr_lite.h"
20
21 #define PRESSURE_BASE (1024 * 10)
22
23 typedef struct DemoApi {
24 INHERIT_IUNKNOWN;
25 } DemoApi;
26
27 typedef struct DemoFeature {
28 INHERIT_FEATURE;
29 INHERIT_IUNKNOWNENTRY(DemoApi);
30 Identity identity;
31 } DemoFeature;
32
33 typedef struct ExampleService {
34 INHERIT_SERVICE;
35 Identity identity;
36 } ExampleService;
37
GetName(Service * service)38 static const char *GetName(Service *service)
39 {
40 (void)service;
41 return "serviceName201";
42 }
43
Initialize(Service * service,Identity identity)44 static BOOL Initialize(Service *service, Identity identity)
45 {
46 ExampleService *example = (ExampleService *)service;
47 example->identity = identity;
48 return TRUE;
49 }
50
MessageHandle(Service * service,Request * msg)51 static BOOL MessageHandle(Service *service, Request *msg)
52 {
53 (void)msg;
54 (void)service;
55 return TRUE;
56 }
57
GetTaskConfig(Service * service)58 static TaskConfig GetTaskConfig(Service *service)
59 {
60 (void)service;
61 TaskConfig config = {
62 LEVEL_HIGH,
63 PRI_ABOVE_NORMAL,
64 0x800,
65 20,
66 SHARED_TASK
67 };
68 return config;
69 }
70
71 static ExampleService g_service = {
72 .GetName = GetName,
73 .Initialize = Initialize,
74 .MessageHandle = MessageHandle,
75 .GetTaskConfig = GetTaskConfig,
76 .identity = {-1, -1, NULL},
77 };
78
FEATURE_GetName(Feature * feature)79 static const char *FEATURE_GetName(Feature *feature)
80 {
81 (void)feature;
82 return "featureName201";
83 }
FEATURE_GetName2(Feature * feature)84 static const char *FEATURE_GetName2(Feature *feature)
85 {
86 (void)feature;
87 return "featureName202";
88 }
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)89 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
90 {
91 DemoFeature *demoFeature = (DemoFeature *)feature;
92 demoFeature->identity = identity;
93 (void)parent;
94 }
FEATURE_OnStop(Feature * feature,Identity identity)95 static void FEATURE_OnStop(Feature *feature, Identity identity)
96 {
97 (void)feature;
98 (void)identity;
99 }
100
FEATURE_OnMessage(Feature * feature,Request * request)101 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
102 {
103 (void)feature;
104 (void)request;
105 return TRUE;
106 }
107
108 static DemoFeature g_feature = {
109 .GetName = FEATURE_GetName,
110 .OnInitialize = FEATURE_OnInitialize,
111 .OnStop = FEATURE_OnStop,
112 .OnMessage = FEATURE_OnMessage,
113 .ref = 1,
114 .iUnknown = {
115 DEFAULT_IUNKNOWN_IMPL,
116 },
117 .identity = {-1, -1, NULL},
118 };
119 static DemoFeature g_feature2 = {
120 .GetName = FEATURE_GetName2,
121 .OnInitialize = FEATURE_OnInitialize,
122 .OnStop = FEATURE_OnStop,
123 .OnMessage = FEATURE_OnMessage,
124 .ref = 1,
125 .iUnknown = {
126 DEFAULT_IUNKNOWN_IMPL,
127 },
128 .identity = {-1, -1, NULL},
129 };
130
131 LITE_TEST_SUIT(test, samgr, FeatureTestSuite);
132
FeatureTestSuiteSetUp(void)133 static BOOL FeatureTestSuiteSetUp(void)
134 {
135 BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
136 return result;
137 }
138
FeatureTestSuiteTearDown(void)139 static BOOL FeatureTestSuiteTearDown(void)
140 {
141 Service *resultService = SAMGR_GetInstance()->UnregisterService("serviceName201");
142 if (strcmp(resultService->GetName(resultService), "serviceName201") == 0) {
143 return TRUE;
144 } else {
145 return FALSE;
146 }
147 }
148
149 /**
150 * @tc.number : DMSLite_SAMGR_RegisterFeature_0010
151 * @tc.name : Valid feature can be registered
152 * @tc.desc : [C- SOFTWARE -0200]
153 */
154 LITE_TEST_CASE(FeatureTestSuite, testRegisterFeature0010, Function | MediumTest | Level1)
155 {
156 BOOL result = SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
157 TEST_ASSERT_EQUAL_INT(result, TRUE);
158
159 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
160 }
161
162 /**
163 * @tc.number : DMSLite_SAMGR_RegisterFeature_0020
164 * @tc.name : If the specified service is null feature can not be registered
165 * @tc.desc : [C- SOFTWARE -0200]
166 */
167 LITE_TEST_CASE(FeatureTestSuite, testRegisterFeature0020, Function | MediumTest | Level2)
168 {
169 BOOL result = SAMGR_GetInstance()->RegisterFeature(NULL, (Feature *)&g_feature);
170 TEST_ASSERT_EQUAL_INT(result, FALSE);
171 }
172
173 /**
174 * @tc.number : DMSLite_SAMGR_RegisterFeature_0030
175 * @tc.name : If the specified feature is null feature can not be registered
176 * @tc.desc : [C- SOFTWARE -0200]
177 */
178 LITE_TEST_CASE(FeatureTestSuite, testRegisterFeature0030, Function | MediumTest | Level2)
179 {
180 BOOL result = SAMGR_GetInstance()->RegisterFeature("serviceName201", NULL);
181 TEST_ASSERT_EQUAL_INT(result, FALSE);
182 }
183
184 /**
185 * @tc.number : DMSLite_SAMGR_RegisterFeature_0040
186 * @tc.name : Feature can not be registered if the specified service does not exist
187 * @tc.desc : [C- SOFTWARE -0200]
188 */
189 LITE_TEST_CASE(FeatureTestSuite, testRegisterFeature0040, Function | MediumTest | Level2)
190 {
191 BOOL result = SAMGR_GetInstance()->RegisterFeature("noExistServiceName", (Feature *)&g_feature);
192 TEST_ASSERT_EQUAL_INT(result, FALSE);
193 }
194
195 /**
196 * @tc.number : DMSLite_SAMGR_RegisterFeature_0050
197 * @tc.name : Feature failed to register if the same feature name already exist in samgr
198 * @tc.desc : [C- SOFTWARE -0200]
199 */
200 LITE_TEST_CASE(FeatureTestSuite, testRegisterFeature0050, Function | MediumTest | Level2)
201 {
202 SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
203
204 BOOL result = SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
205 TEST_ASSERT_EQUAL_INT(result, FALSE);
206
207 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
208 }
209
210 /**
211 * @tc.number : DMSLite_SAMGR_RegisterFeature_0060
212 * @tc.name : Multi features can be registered under one service
213 * @tc.desc : [C- SOFTWARE -0200]
214 */
215 LITE_TEST_CASE(FeatureTestSuite, testRegisterFeature0060, Function | MediumTest | Level2)
216 {
217 BOOL result1 = SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
218 TEST_ASSERT_EQUAL_INT(result1, TRUE);
219 BOOL result2 = SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature2);
220 TEST_ASSERT_EQUAL_INT(result2, TRUE);
221
222 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
223 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName202");
224 }
225
226 /**
227 * @tc.number : DMSLite_SAMGR_UnregisterFeature_0010
228 * @tc.name : Feature can be unregistered
229 * @tc.desc : [C- SOFTWARE -0200]
230 */
231 LITE_TEST_CASE(FeatureTestSuite, testUnregisterFeature0010, Function | MediumTest | Level2)
232 {
233 SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
234
235 Feature *resultFeature = SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
236 TEST_ASSERT_EQUAL_INT(strcmp(resultFeature->GetName(resultFeature), "featureName201"), 0);
237 }
238
239 /**
240 * @tc.number : DMSLite_SAMGR_UnregisterFeature_0020
241 * @tc.name : Feature can not be unregistered if the specified service is null or space
242 * @tc.desc : [C- SOFTWARE -0200]
243 */
244 LITE_TEST_CASE(FeatureTestSuite, testUnregisterFeature0020, Function | MediumTest | Level2)
245 {
246 SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
247
248 Feature *resultFeature = SAMGR_GetInstance()->UnregisterFeature(NULL, "featureName201");
249 TEST_ASSERT_EQUAL_INT(resultFeature == NULL, TRUE);
250
251 resultFeature = SAMGR_GetInstance()->UnregisterFeature("", "featureName201");
252 TEST_ASSERT_EQUAL_INT(resultFeature == NULL, TRUE);
253
254 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
255 }
256
257 /**
258 * @tc.number : DMSLite_SAMGR_UnregisterFeature_0030
259 * @tc.name : Feature can not be unregistered if the specified feature is null or space
260 * @tc.desc : [C- SOFTWARE -0200]
261 */
262 LITE_TEST_CASE(FeatureTestSuite, testUnregisterFeature0030, Function | MediumTest | Level2)
263 {
264 SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
265
266 Feature *resultFeature = SAMGR_GetInstance()->UnregisterFeature("serviceName201", NULL);
267 TEST_ASSERT_EQUAL_INT(resultFeature == NULL, TRUE);
268
269 resultFeature = SAMGR_GetInstance()->UnregisterFeature("serviceName201", "");
270 TEST_ASSERT_EQUAL_INT(resultFeature == NULL, TRUE);
271
272 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
273 }
274
275 /**
276 * @tc.number : DMSLite_SAMGR_UnregisterFeature_0040
277 * @tc.name : Feature failed to unregister if the specified service not exist
278 * @tc.desc : [C- SOFTWARE -0200]
279 */
280 LITE_TEST_CASE(FeatureTestSuite, testUnregisterFeature0040, Function | MediumTest | Level2)
281 {
282 SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
283
284 Feature *resultFeature = SAMGR_GetInstance()->UnregisterFeature("noExistService", "featureName201");
285 TEST_ASSERT_EQUAL_INT(resultFeature == NULL, TRUE);
286
287 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
288 }
289
290 /**
291 * @tc.number : DMSLite_SAMGR_UnregisterFeature_0050
292 * @tc.name : Feature failed to unregister if the specified feature not exist
293 * @tc.desc : [C- SOFTWARE -0200]
294 */
295 LITE_TEST_CASE(FeatureTestSuite, testUnregisterFeature0050, Function | MediumTest | Level2)
296 {
297 SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
298
299 Feature *resultFeature = SAMGR_GetInstance()->UnregisterFeature("serviceName201", "noExistFeature");
300 TEST_ASSERT_EQUAL_INT(resultFeature == NULL, TRUE);
301
302 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
303 }
304
305 /**
306 * @tc.number : DMSLite_SAMGR_UnregisterFeature_0060
307 * @tc.name : Feature can not be unregistered if there is api under the feature
308 * @tc.desc : [C- SOFTWARE -0200]
309 */
310 LITE_TEST_CASE(FeatureTestSuite, testUnregisterFeature0060, Function | MediumTest | Level2)
311 {
312 SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
313 SAMGR_GetInstance()->RegisterFeatureApi("serviceName201", "featureName201", GET_IUNKNOWN(g_feature));
314
315 Feature *result = SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
316 TEST_ASSERT_EQUAL_INT(result == NULL, TRUE);
317
318 SAMGR_GetInstance()->UnregisterFeatureApi("serviceName201", "featureName201");
319 SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
320 }
321
322 /**
323 * @tc.number : DMSLite_SAMGR_UnregisterFeature_0070
324 * @tc.name : Register and unregister feature repeatedly, no memory leak
325 * @tc.desc : [C- SOFTWARE -0200]
326 */
327 LITE_TEST_CASE(FeatureTestSuite, testUnregisterFeature0070, Function | MediumTest | Level2)
328 {
329 // RegisterService and unregister feature over and over again, if there is memory leak samgr will crash
330 for (int i = 0; i < PRESSURE_BASE; i++) {
331 BOOL result = SAMGR_GetInstance()->RegisterFeature("serviceName201", (Feature *)&g_feature);
332 TEST_ASSERT_EQUAL_INT(result, TRUE);
333
334 Feature *resultFeature = SAMGR_GetInstance()->UnregisterFeature("serviceName201", "featureName201");
335 TEST_ASSERT_EQUAL_INT(strcmp(resultFeature->GetName(resultFeature), "featureName201"), 0);
336 }
337 }
338
339 RUN_TEST_SUITE(FeatureTestSuite);