• 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 "hctest.h"
18 #include "samgr_lite.h"
19 
20 #define PRESSURE_BASE (1024 * 10)
21 
22 typedef struct DemoApi {
23     INHERIT_IUNKNOWN;
24 } DemoApi;
25 
26 typedef struct ExampleService {
27     INHERIT_SERVICE;
28     Identity identity;
29 } ExampleService;
30 
31 static DemoApi g_api = {
32     DEFAULT_IUNKNOWN_IMPL,
33 };
34 
GetName(Service * service)35 static const char *GetName(Service *service)
36 {
37     (void)service;
38     return "serviceName301";
39 }
40 
Initialize(Service * service,Identity identity)41 static BOOL Initialize(Service *service, Identity identity)
42 {
43     ExampleService *example = (ExampleService *)service;
44     example->identity = identity;
45     return TRUE;
46 }
47 
MessageHandle(Service * service,Request * msg)48 static BOOL MessageHandle(Service *service, Request *msg)
49 {
50     (void)service;
51     (void)msg;
52     return FALSE;
53 }
54 
GetTaskConfig(Service * service)55 static TaskConfig GetTaskConfig(Service *service)
56 {
57     (void)service;
58     TaskConfig config = {LEVEL_HIGH, PRI_ABOVE_NORMAL, 0x800, 20, SHARED_TASK};
59     return config;
60 }
61 
62 static ExampleService g_service = {
63     .GetName = GetName,
64     .Initialize = Initialize,
65     .MessageHandle = MessageHandle,
66     .GetTaskConfig = GetTaskConfig,
67     .identity = {-1, -1, NULL},
68 };
69 
70 LITE_TEST_SUIT(test, samgr, DefaultFeatureApiTestSuite);
71 
DefaultFeatureApiTestSuiteSetUp(void)72 static BOOL DefaultFeatureApiTestSuiteSetUp(void)
73 {
74     BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
75     return result;
76 }
77 
DefaultFeatureApiTestSuiteTearDown(void)78 static BOOL DefaultFeatureApiTestSuiteTearDown(void)
79 {
80     Service *resultService = SAMGR_GetInstance()->UnregisterService("serviceName301");
81     if (strcmp(resultService->GetName(resultService), "serviceName301") == 0) {
82         return TRUE;
83     } else {
84         printf("[hctest]failed to unregister service.\n");
85         return FALSE;
86     }
87 }
88 
89 /**
90  * @tc.number    : DMSLite_SAMGR_RegisterDefaultFeatureApi_0010
91  * @tc.name      : Valid API can be registered under default feature
92  * @tc.desc      : [C- SOFTWARE -0200]
93  */
94 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testRegisterDefaultFeatureApi0010, Function | MediumTest | Level1)
95 {
96     BOOL result = SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
97     TEST_ASSERT_EQUAL_INT(result, TRUE);    // TRUE: success; FALSE: fail
98 
99     SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
100 }
101 
102 /**
103  * @tc.number    : DMSLite_SAMGR_RegisterDefaultFeatureApi_0020
104  * @tc.name      : API failed to register if the specified service is null
105  * @tc.desc      : [C- SOFTWARE -0200]
106  */
107 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testRegisterDefaultFeatureApi0020, Function | MediumTest | Level2)
108 {
109     BOOL result = SAMGR_GetInstance()->RegisterDefaultFeatureApi(NULL, (IUnknown *)&g_api);
110     TEST_ASSERT_EQUAL_INT(result, FALSE);
111 }
112 
113 /**
114  * @tc.number    : DMSLite_SAMGR_RegisterDefaultFeatureApi_0030
115  * @tc.name      : API failed to register if the specified service does not exist
116  * @tc.desc      : [C- SOFTWARE -0200]
117  */
118 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testRegisterDefaultFeatureApi0030, Function | MediumTest | Level2)
119 {
120     BOOL result = SAMGR_GetInstance()->RegisterDefaultFeatureApi("noExistService", (IUnknown *)&g_api);
121     TEST_ASSERT_EQUAL_INT(result, FALSE);
122 }
123 
124 /**
125  * @tc.number    : DMSLite_SAMGR_RegisterDefaultFeatureApi_0040
126  * @tc.name      : API failed to register if the target API is NULL
127  * @tc.desc      : [C- SOFTWARE -0200]
128  */
129 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testRegisterDefaultFeatureApi0040, Function | MediumTest | Level2)
130 {
131     BOOL result = SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", NULL);
132     TEST_ASSERT_EQUAL_INT(result, FALSE);
133 }
134 
135 /**
136  * @tc.number    : DMSLite_SAMGR_RegisterDefaultFeatureApi_0050
137  * @tc.name      : API failed to register for the target API already exist
138  * @tc.desc      : [C- SOFTWARE -0200]
139  */
140 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testRegisterDefaultFeatureApi0050, Function | MediumTest | Level2)
141 {
142     SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
143     BOOL result = SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
144     TEST_ASSERT_EQUAL_INT(result, FALSE);
145 
146     SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
147 }
148 
149 /**
150  * @tc.number    : DMSLite_SAMGR_GetDefaultFeatureApi_0010
151  * @tc.name      : API can be queried out from service
152  * @tc.desc      : [C- SOFTWARE -0200]
153  */
154 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testGetDefaultFeatureApi0010, Function | MediumTest | Level1)
155 {
156     SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
157 
158     IUnknown *resultIUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi("serviceName301");
159     TEST_ASSERT_EQUAL_INT(resultIUnknown != NULL, TRUE);
160 
161     SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
162 }
163 
164 /**
165  * @tc.number    : DMSLite_SAMGR_GetDefaultFeatureApi_0020
166  * @tc.name      : It's NULL result if the specified service name is NULL
167  * @tc.desc      : [C- SOFTWARE -0200]
168  */
169 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testGetDefaultFeatureApi0020, Function | MediumTest | Level2)
170 {
171     SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
172 
173     IUnknown *resultIUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(NULL);
174     TEST_ASSERT_EQUAL_INT(resultIUnknown == NULL, TRUE);
175 
176     SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
177 }
178 
179 /**
180  * @tc.number    : DMSLite_SAMGR_GetDefaultFeatureApi_0030
181  * @tc.name      : The query result is NULL for the specified service does not exist
182  * @tc.desc      : [C- SOFTWARE -0200]
183  */
184 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testGetDefaultFeatureApi0030, Function | MediumTest | Level2)
185 {
186     SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
187 
188     IUnknown *resultIUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi("noExistService");
189     TEST_ASSERT_EQUAL_INT(resultIUnknown == NULL, TRUE);
190 
191     SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
192 }
193 
194 /**
195  * @tc.number    : DMSLite_SAMGR_GetDefaultFeatureApi_0040
196  * @tc.name      : The query result is the same for multi query operations
197  * @tc.desc      : [C- SOFTWARE -0200]
198  */
199 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testGetDefaultFeatureApi0040, Function | MediumTest | Level2)
200 {
201     SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
202 
203     IUnknown *resultIUnknown1 = SAMGR_GetInstance()->GetDefaultFeatureApi("serviceName301");
204     IUnknown *resultIUnknown2 = SAMGR_GetInstance()->GetDefaultFeatureApi("serviceName301");
205     TEST_ASSERT_EQUAL_INT(resultIUnknown1 == resultIUnknown2, TRUE);
206 
207     SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
208 }
209 
210 /**
211  * @tc.number    : DMSLite_SAMGR_UnregisterDefaultFeatureApi_0010
212  * @tc.name      : Default feature API can be unregistered
213  * @tc.desc      : [C- SOFTWARE -0200]
214  */
215 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testUnregisterDefaultFeatureApi0010, Function | MediumTest | Level1)
216 {
217     SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
218 
219     IUnknown *resultIUnknown = SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
220     TEST_ASSERT_EQUAL_INT(resultIUnknown != NULL, TRUE);
221 }
222 
223 /**
224  * @tc.number    : DMSLite_SAMGR_UnregisterDefaultFeatureApi_0020
225  * @tc.name      : The unregister operation failed if serviceName is NULL or space
226  * @tc.desc      : [C- SOFTWARE -0200]
227  */
228 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testUnregisterDefaultFeatureApi0020, Function | MediumTest | Level2)
229 {
230     SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
231 
232     IUnknown *resultIUnknown = SAMGR_GetInstance()->UnregisterDefaultFeatureApi(NULL);
233     TEST_ASSERT_EQUAL_INT(resultIUnknown == NULL, TRUE);
234 
235     resultIUnknown = SAMGR_GetInstance()->UnregisterDefaultFeatureApi("");
236     TEST_ASSERT_EQUAL_INT(resultIUnknown == NULL, TRUE);
237 
238     SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
239 }
240 
241 /**
242  * @tc.number    : DMSLite_SAMGR_UnregisterDefaultFeatureApi_0030
243  * @tc.name      : The unregister operation failed if the specified service does not exist
244  * @tc.desc      : [C- SOFTWARE -0200]
245  */
246 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testUnregisterDefaultFeatureApi0030, Function | MediumTest | Level2)
247 {
248     IUnknown *resultIUnknown = SAMGR_GetInstance()->UnregisterDefaultFeatureApi("noExistService");
249     TEST_ASSERT_EQUAL_INT(resultIUnknown == NULL, TRUE);
250 }
251 
252 /**
253  * @tc.number    : DMSLite_SAMGR_UnregisterDefaultFeatureApi_0040
254  * @tc.name      : Register and unregister default api repeatedly, no memory leak
255  * @tc.desc      : [C- SOFTWARE -0200]
256  */
257 LITE_TEST_CASE(DefaultFeatureApiTestSuite, testUnregisterDefaultFeatureApi0040, Function | MediumTest | Level2)
258 {
259     // RegisterService and unregister default api over and over again, if there is memory leak samgr will crash
260     for (int i = 0; i < PRESSURE_BASE; i++) {
261         BOOL result = SAMGR_GetInstance()->RegisterDefaultFeatureApi("serviceName301", (IUnknown *)&g_api);
262         TEST_ASSERT_EQUAL_INT(result, TRUE);
263 
264         IUnknown *resultIUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi("serviceName301");
265         TEST_ASSERT_EQUAL_INT(resultIUnknown != NULL, TRUE);
266 
267         resultIUnknown = SAMGR_GetInstance()->UnregisterDefaultFeatureApi("serviceName301");
268         TEST_ASSERT_EQUAL_INT(resultIUnknown != NULL, TRUE);
269 
270         resultIUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi("serviceName301");
271         TEST_ASSERT_EQUAL_INT(resultIUnknown == NULL, TRUE);
272     }
273 }
274 
275 RUN_TEST_SUITE(DefaultFeatureApiTestSuite);