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