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