• 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 
16 #include <string.h>
17 #include "cmsis_os.h"
18 #include "hctest.h"
19 #include "samgr_lite.h"
20 
21 #define OPER_INTERVAL 200    // 200 ms
22 #define PRESSURE_BASE (1024 * 10)
23 
24 typedef struct DefaultFeatureApi {
25     INHERIT_IUNKNOWN;
26 } DefaultFeatureApi;
27 
28 typedef struct ExampleService {
29     INHERIT_SERVICE;
30     INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
31     Identity identity;
32 } ExampleService;
33 
34 typedef struct DemoApi {
35     INHERIT_IUNKNOWN;
36 } DemoApi;
37 
38 typedef struct DemoFeature {
39     INHERIT_FEATURE;
40     INHERIT_IUNKNOWNENTRY(DemoApi);
41     Identity identity;
42 } DemoFeature;
43 
GetName(Service * service)44 static const char *GetName(Service *service)
45 {
46     (void)service;
47     return "serviceName101";
48 }
49 
Initialize(Service * service,Identity identity)50 static BOOL Initialize(Service *service, Identity identity)
51 {
52     ExampleService *example = (ExampleService *)service;
53     example->identity = identity;
54     return TRUE;
55 }
56 
MessageHandle(Service * service,Request * msg)57 static BOOL MessageHandle(Service *service, Request *msg)
58 {
59     (void)msg;
60     (void)service;
61     return FALSE;
62 }
63 
GetTaskConfig(Service * service)64 static TaskConfig GetTaskConfig(Service *service)
65 {
66     (void)service;
67     TaskConfig config = {LEVEL_HIGH, PRI_ABOVE_NORMAL, 0x800, 20, SHARED_TASK};
68     return config;
69 }
70 
71 static ExampleService g_service = {
72     .GetName = GetName,
73     .Initialize = Initialize,
74     .MessageHandle = MessageHandle,
75     .GetTaskConfig = GetTaskConfig,
76     .ref = 1,
77     .iUnknown = {
78         DEFAULT_IUNKNOWN_IMPL,
79     }
80 };
81 
FEATURE_GetName(Feature * feature)82 static const char *FEATURE_GetName(Feature *feature)
83 {
84     (void)feature;
85     return "featureName101";
86 }
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)87 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
88 {
89     DemoFeature *demoFeature = (DemoFeature *)feature;
90     demoFeature->identity = identity;
91     (void)parent;
92 }
FEATURE_OnStop(Feature * feature,Identity identity)93 static void FEATURE_OnStop(Feature *feature, Identity identity)
94 {
95     (void)feature;
96     (void)identity;
97 }
98 
FEATURE_OnMessage(Feature * feature,Request * request)99 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
100 {
101     (void)feature;
102     (void)request;
103     return TRUE;
104 }
105 
106 static DemoFeature g_feature = {
107     .GetName = FEATURE_GetName,
108     .OnInitialize = FEATURE_OnInitialize,
109     .OnStop = FEATURE_OnStop,
110     .OnMessage = FEATURE_OnMessage,
111     .ref = 1,
112     .iUnknown = {
113         DEFAULT_IUNKNOWN_IMPL,
114     },
115     .identity = {-1, -1, NULL},
116 };
117 
118 LITE_TEST_SUIT(test, samgr, ServiceTestSuite);
119 
ServiceTestSuiteSetUp(void)120 static BOOL ServiceTestSuiteSetUp(void)
121 {
122     osDelay(OPER_INTERVAL);
123     return TRUE;
124 }
125 
ServiceTestSuiteTearDown(void)126 static BOOL ServiceTestSuiteTearDown(void)
127 {
128     return TRUE;
129 }
130 
131 /**
132  * @tc.number    : DMSLite_SAMGR_RegisterService_0010
133  * @tc.name      : Valid service can be registered
134  * @tc.desc      : [C- SOFTWARE -0200]
135  */
136 LITE_TEST_CASE(ServiceTestSuite, testRegisterService0010, Function | MediumTest | Level1)
137 {
138     BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
139     TEST_ASSERT_EQUAL_INT(result, TRUE);  // TRUE: regist success; FALSE: regist fail
140 
141     SAMGR_GetInstance()->UnregisterService("serviceName101");
142 };
143 
144 /**
145  * @tc.number    : DMSLite_SAMGR_RegisterService_0020
146  * @tc.name      : Service which is NULL failed to register
147  * @tc.desc      : [C- SOFTWARE -0200]
148  */
149 LITE_TEST_CASE(ServiceTestSuite, testRegisterService0020, Function | MediumTest | Level2)
150 {
151     BOOL result = SAMGR_GetInstance()->RegisterService(NULL);
152     TEST_ASSERT_EQUAL_INT(result, FALSE);
153 }
154 
155 /**
156  * @tc.number    : DMSLite_SAMGR_RegisterService_0030
157  * @tc.name      : Service which already exist in samgr failed to register
158  * @tc.desc      : [C- SOFTWARE -0200]
159  */
160 LITE_TEST_CASE(ServiceTestSuite, testRegisterService0030, Function | MediumTest | Level2)
161 {
162     SAMGR_GetInstance()->RegisterService((Service *)&g_service);
163     BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
164     TEST_ASSERT_EQUAL_INT(result, FALSE);
165     SAMGR_GetInstance()->UnregisterService("serviceName101");
166 }
167 
168 /**
169  * @tc.number    : DMSLite_SAMGR_UnregisterService_0010
170  * @tc.name      : Service can be unregistered
171  * @tc.desc      : [C- SOFTWARE -0200]
172  */
173 LITE_TEST_CASE(ServiceTestSuite, testUnregisterService0010, Function | MediumTest | Level2)
174 {
175     SAMGR_GetInstance()->RegisterService((Service *)&g_service);
176     Service *resultService = SAMGR_GetInstance()->UnregisterService("serviceName101");
177 
178     // when unregist service samgr will return the target service
179     TEST_ASSERT_EQUAL_INT(strcmp(resultService->GetName(resultService), "serviceName101"), 0);
180 }
181 
182 /**
183  * @tc.number    : DMSLite_SAMGR_UnregisterService_0020
184  * @tc.name      : Service which contains NULL failed to unregister
185  * @tc.desc      : [C- SOFTWARE -0200]
186  */
187 LITE_TEST_CASE(ServiceTestSuite, testUnregisterService0020, Function | MediumTest | Level2)
188 {
189     Service *resultService = SAMGR_GetInstance()->UnregisterService(NULL);
190     TEST_ASSERT_EQUAL_INT(resultService == NULL, TRUE);
191 
192     resultService = SAMGR_GetInstance()->UnregisterService("");
193     TEST_ASSERT_EQUAL_INT(resultService == NULL, TRUE);
194 }
195 
196 /**
197  * @tc.number    : DMSLite_SAMGR_UnregisterService_0030
198  * @tc.name      : Service which does not exist in samgr failed to unregister
199  * @tc.desc      : [C- SOFTWARE -0200]
200  */
201 LITE_TEST_CASE(ServiceTestSuite, testUnregisterService0030, Function | MediumTest | Level2)
202 {
203     Service *resultService = SAMGR_GetInstance()->UnregisterService("noExistServiceName");
204     TEST_ASSERT_EQUAL_INT(resultService == NULL, TRUE);
205 }
206 
207 /**
208  * @tc.number    : DMSLite_SAMGR_UnregisterService_0040
209  * @tc.name      : Service which contains api failed to unregister
210  * @tc.desc      : [C- SOFTWARE -0200]
211  */
212 LITE_TEST_CASE(ServiceTestSuite, testUnregisterService0040, Function | MediumTest | Level2)
213 {
214     SAMGR_GetInstance()->RegisterService((Service *)&g_service);
215     SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName101", GET_IUNKNOWN(g_service));
216 
217     Service *resultService = SAMGR_GetInstance()->UnregisterService("serviceName101");
218     TEST_ASSERT_EQUAL_INT(resultService == NULL, TRUE);
219 
220     SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName101");
221     SAMGR_GetInstance()->UnregisterService("serviceName101");
222 }
223 
224 /**
225  * @tc.number    : DMSLite_SAMGR_UnregisterService_0050
226  * @tc.name      : Service which contains feature failed to unregister
227  * @tc.desc      : [C- SOFTWARE -0200]
228  */
229 LITE_TEST_CASE(ServiceTestSuite, testUnregisterService0050, Function | MediumTest | Level2)
230 {
231     SAMGR_GetInstance()->RegisterService((Service *)&g_service);
232     SAMGR_GetInstance()->RegisterFeature("serviceName101", (Feature *)&g_feature);
233 
234     Service *resultService = SAMGR_GetInstance()->UnregisterService("serviceName101");
235     TEST_ASSERT_EQUAL_INT(resultService == NULL, TRUE);
236 
237     SAMGR_GetInstance()->UnregisterFeature("serviceName101", "featureName101");
238     SAMGR_GetInstance()->UnregisterService("serviceName101");
239 }
240 
241 /**
242  * @tc.number    : DMSLite_SAMGR_UnregisterService_0060
243  * @tc.name      : Register and unregister service repeatedly, no memory leak
244  * @tc.desc      : [C- SOFTWARE -0200]
245  */
246 LITE_TEST_CASE(ServiceTestSuite, testUnregisterService0060, Function | MediumTest | Level2)
247 {
248     // RegisterService and unregister service over and over again, if there is memory leak samgr will crash
249     for (int i = 0; i < PRESSURE_BASE; i++) {
250         BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
251         TEST_ASSERT_EQUAL_INT(result, TRUE);
252 
253         Service *resultService = SAMGR_GetInstance()->UnregisterService("serviceName101");
254         TEST_ASSERT_EQUAL_INT(strcmp(resultService->GetName(resultService), "serviceName101"), 0);
255     }
256 }
257 
258 /**
259  * @tc.number    : DMSLite_SAMGR_Bootstrap_0010
260  * @tc.name      : Restart SAMGR service function is normal.
261  * @tc.desc      : [C- SOFTWARE -0200]
262  */
263 LITE_TEST_CASE(ServiceTestSuite, testBootstrap0010, Function | MediumTest | Level2)
264 {
265     SAMGR_Bootstrap();
266     osDelay(OPER_INTERVAL);
267 
268     SamgrLite *samgrLite = SAMGR_GetInstance();
269     TEST_ASSERT_EQUAL_INT(TRUE, samgrLite != NULL);
270 };
271 
272 RUN_TEST_SUITE(ServiceTestSuite);