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