• 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 <securec.h>
17 #include <ohos_init.h>
18 #include "gtest/gtest.h"
19 #include "utils/SamgrTestBase.h"
20 
21 using namespace testing::ext;
22 
23 enum MessageId {
24     MSG_NO, // receiver does not send response
25     MSG_RT, // receiver send response
26     MSG_FW, // receiver forward message
27 };
28 
29 struct Payload {
30     int id;
31     const char *name;
32     int value;
33 };
34 
35 struct DemoApi {
36     INHERIT_IUNKNOWN;
37     uint32 *(*SAMGR_SendSharedRequestProxy)(const Identity *identity, const Request *request, uint32 *token,
38                                             Handler handler);
39     int32 (*SAMGR_SendSharedDirectRequestProxy)(const Identity *id, const Request *req, const Response *resp,
40                                                 uint32 **ref, Handler handler);
41 };
42 
43 struct DemoFeature {
44     INHERIT_FEATURE;
45     INHERIT_IUNKNOWNENTRY(DemoApi);
46     Identity identity;
47     int featureCalledCount;
48     BOOL featureStatus;
49     char *latestRequest;
50 };
51 
52 struct DefaultFeatureApi {
53     INHERIT_IUNKNOWN;
54 };
55 
56 struct ExampleService {
57     INHERIT_SERVICE;
58     INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
59     Identity identity;
60 };
61 
SAMGR_SendSharedRequestProxy(const Identity * identity,const Request * request,uint32 * token,Handler handler)62 static uint32 *SAMGR_SendSharedRequestProxy(const Identity *identity, const Request *request, uint32 *token,
63                                             Handler handler)
64 {
65     return SAMGR_SendSharedRequest(identity, request, token, handler);
66 }
67 
SAMGR_SendSharedDirectRequestProxy(const Identity * id,const Request * req,const Response * resp,uint32 ** ref,Handler handler)68 static int32 SAMGR_SendSharedDirectRequestProxy(const Identity *id, const Request *req, const Response *resp,
69                                                 uint32 **ref, Handler handler)
70 {
71     return SAMGR_SendSharedDirectRequest(id, req, resp, ref, handler);
72 }
73 
GetName(Service * service)74 static const char *GetName(Service *service)
75 {
76     (void)service;
77     return "S_sendShareReq";
78 }
79 
Initialize(Service * service,Identity identity)80 static BOOL Initialize(Service *service, Identity identity)
81 {
82     ExampleService *example = (ExampleService *)service;
83     example->identity = identity;
84 
85     printf("[hcpptest]serviceName %s Initialize \n", service->GetName(service));
86 
87     return TRUE;
88 }
89 
MessageHandle(Service * service,Request * msg)90 static BOOL MessageHandle(Service *service, Request *msg)
91 {
92     (void)service;
93     return FALSE;
94 }
95 
GetTaskConfig(Service * service)96 static TaskConfig GetTaskConfig(Service *service)
97 {
98     (void)service;
99     TaskConfig config = {LEVEL_HIGH, PRI_ABOVE_NORMAL, 0x800, 20, SHARED_TASK};
100     return config;
101 }
102 
103 static ExampleService g_service = {
104     .GetName = GetName,
105     .Initialize = Initialize,
106     .MessageHandle = MessageHandle,
107     .GetTaskConfig = GetTaskConfig,
108     .ver = 0x20,
109     .ref = 1,
110     .iUnknown = {
111         DEFAULT_IUNKNOWN_IMPL,
112     }
113 };
114 
FEATURE_GetName(Feature * feature)115 static const char *FEATURE_GetName(Feature *feature)
116 {
117     (void)feature;
118     return "feature01";
119 }
120 
FEATURE_GetName02(Feature * feature)121 static const char *FEATURE_GetName02(Feature *feature)
122 {
123     (void)feature;
124     return "feature02";
125 }
126 
FEATURE_GetName03(Feature * feature)127 static const char *FEATURE_GetName03(Feature *feature)
128 {
129     (void)feature;
130     return "feature03";
131 }
132 
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)133 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
134 {
135     DemoFeature *demoFeature = (DemoFeature *)feature;
136     demoFeature->identity = identity;
137 
138     printf("[hcpptest]featureName %s Initialize \n", feature->GetName(feature));
139     (void)parent;
140 }
141 
FEATURE_OnStop(Feature * feature,Identity identity)142 static void FEATURE_OnStop(Feature *feature, Identity identity)
143 {
144     (void)feature;
145     (void)identity;
146 }
147 
FEATURE_OnMessage(Feature * feature,Request * request)148 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
149 {
150     DemoFeature *demoFeature = (DemoFeature *)feature;
151     demoFeature->featureCalledCount++;
152     demoFeature->latestRequest = (char *)request->data;
153 
154     demoFeature->featureStatus = TRUE;
155     return TRUE;
156 }
157 
FEATURE_OnMessage02(Feature * feature,Request * request)158 static BOOL FEATURE_OnMessage02(Feature *feature, Request *request)
159 {
160     int32 returnCode;
161     DemoFeature *demoFeature = (DemoFeature *)feature;
162     demoFeature->featureCalledCount++;
163     demoFeature->latestRequest = (char *)request->data;
164 
165     switch (request->msgId) {
166         case MSG_NO:
167             returnCode = 0;
168             break;
169         case MSG_RT:
170         {
171             Response response = {
172                 .data = (char*)"Yes, you did!",
173                 .len = 0,
174                 };
175             returnCode = SAMGR_SendResponse(request, &response);
176         };
177         break;
178         case MSG_FW:
179         {
180             returnCode = 0;
181         };
182         break;
183         default:
184         {
185             returnCode = 0;
186             printf("[hcpptest]unknown msgId.\n");
187         }
188     }
189     demoFeature->featureStatus = ((returnCode == 0) ? TRUE : FALSE);
190     return demoFeature->featureStatus;
191 }
192 
FEATURE_OnMessage03(Feature * feature,Request * request)193 static BOOL FEATURE_OnMessage03(Feature *feature, Request *request)
194 {
195     int32 returnCode;
196     DemoFeature *demoFeature = (DemoFeature *)feature;
197     demoFeature->featureCalledCount++;
198     demoFeature->latestRequest = (char *)request->data;
199 
200     switch (request->msgId) {
201         case MSG_NO:
202             returnCode = 0;
203             break;
204         case MSG_RT:
205         {
206             Response response = {
207                 .data = (char*)"Yes, you did!",
208                 .len = 0,
209                 };
210             returnCode = SAMGR_SendResponse(request, &response);
211         };
212         break;
213         case MSG_FW:
214         {
215             returnCode = 0;
216         };
217         break;
218         default:
219             returnCode = 0;
220     }
221     demoFeature->featureStatus = ((returnCode == 0) ? TRUE : FALSE);
222     return demoFeature->featureStatus;
223 }
224 
225 static DemoFeature g_feature = {
226     .GetName = FEATURE_GetName,
227     .OnInitialize = FEATURE_OnInitialize,
228     .OnStop = FEATURE_OnStop,
229     .OnMessage = FEATURE_OnMessage,
230     .ver = 0x20,
231     .ref = 1,
232     .iUnknown = {
233         DEFAULT_IUNKNOWN_IMPL,
234         .SAMGR_SendSharedRequestProxy = SAMGR_SendSharedRequestProxy,
235         .SAMGR_SendSharedDirectRequestProxy = SAMGR_SendSharedDirectRequestProxy,
236     },
237     .identity = {-1, -1, nullptr},
238     .featureCalledCount = 0,
239     .featureStatus = TRUE,
240     .latestRequest = nullptr,
241 };
242 static DemoFeature g_feature02 = {
243     .GetName = FEATURE_GetName02,
244     .OnInitialize = FEATURE_OnInitialize,
245     .OnStop = FEATURE_OnStop,
246     .OnMessage = FEATURE_OnMessage02,
247     .ver = 0x20,
248     .ref = 0,
249     .iUnknown = {
250         DEFAULT_IUNKNOWN_IMPL,
251         .SAMGR_SendSharedRequestProxy = SAMGR_SendSharedRequestProxy,
252         .SAMGR_SendSharedDirectRequestProxy = SAMGR_SendSharedDirectRequestProxy,
253     },
254     .identity = {-1, 1, nullptr},
255     .featureCalledCount = 0,
256     .featureStatus = TRUE,
257     .latestRequest = nullptr,
258 };
259 static DemoFeature g_feature03 = {
260     .GetName = FEATURE_GetName03,
261     .OnInitialize = FEATURE_OnInitialize,
262     .OnStop = FEATURE_OnStop,
263     .OnMessage = FEATURE_OnMessage03,
264     .ver = 0x20,
265     .ref = 0,
266     .iUnknown = {
267         DEFAULT_IUNKNOWN_IMPL,
268         .SAMGR_SendSharedRequestProxy = SAMGR_SendSharedRequestProxy,
269         .SAMGR_SendSharedDirectRequestProxy = SAMGR_SendSharedDirectRequestProxy,
270     },
271     .identity = {-1, 2, nullptr},
272     .featureCalledCount = 0,
273     .featureStatus = TRUE,
274     .latestRequest = nullptr,
275 };
276 
GServiceInit(void)277 static void GServiceInit(void)
278 {
279     printf("[hcpptest]GServiceInit start! \n");
280     BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
281     if (result == FALSE) {
282         printf("[hcpptest]E RegisterService failed! \n");
283     }
284 }
285 SYSEX_SERVICE_INIT(GServiceInit);
286 
GFeatureInit(void)287 static void GFeatureInit(void)
288 {
289     printf("[hcpptest]GFeatureInit start! \n");
290 
291     BOOL result1 = SAMGR_GetInstance()->RegisterDefaultFeatureApi("S_sendShareReq", GET_IUNKNOWN(g_feature));
292 
293     BOOL result2 = SAMGR_GetInstance()->RegisterFeature("S_sendShareReq", (Feature *)&g_feature02);
294     BOOL result3 = SAMGR_GetInstance()->RegisterFeatureApi("S_sendShareReq", "feature02",
295                                                            GET_IUNKNOWN(g_feature02));
296 
297     BOOL result4 = SAMGR_GetInstance()->RegisterFeature("S_sendShareReq", (Feature *)&g_feature03);
298     BOOL result5 = SAMGR_GetInstance()->RegisterFeatureApi("S_sendShareReq", "feature03",
299                                                            GET_IUNKNOWN(g_feature03));
300     if (result1 == FALSE || result2 == FALSE || result3 == FALSE || result4 == FALSE || result5 == FALSE) {
301         printf("[hcpptest]E failed to register feature or api! \n");
302     }
303 }
304 SYSEX_FEATURE_INIT(GFeatureInit);
305 
GetIUnknown(const char * serviceName,const char * featureName)306 static DemoApi *GetIUnknown(const char *serviceName, const char *featureName)
307 {
308     DemoApi *demoApi = nullptr;
309     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(serviceName, featureName);
310     if (iUnknown == nullptr) {
311         printf("[hcpptest]failed to GetFeatureApi.\n");
312         return nullptr;
313     }
314     int result = iUnknown->QueryInterface(iUnknown, 0x20, (void **)&demoApi);
315     if (result == 0 && demoApi != nullptr) {
316         return demoApi;
317     } else {
318         printf("[hcpptest]failed to QueryInterface.\n");
319         return nullptr;
320     }
321 }
322 
ReleaseIUnknown(DemoApi * demoApi)323 static void ReleaseIUnknown(DemoApi *demoApi)
324 {
325     demoApi->Release((IUnknown *)demoApi);
326 }
327 
DemoHandlerAndCheck(const Request * request,const Response * response)328 static void DemoHandlerAndCheck(const Request *request, const Response *response)
329 {
330     char *requestData = (char*)"I wanna async call good result!";
331     ASSERT_EQ(strcmp((char *)request->data, requestData), 0);
332 
333     char *responseData = (char*)"Yes, you did!";
334     ASSERT_EQ(strcmp((char *)response->data, responseData), 0);
335 }
336 
337 class SendShareRequestTest : public testing::Test {
338 protected:
339     // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)340     static void SetUpTestCase(void)
341     {
342         printf("[hcpptest]SetUpTestCase! \n");
343         SystemInitProxy();
344     }
345     // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)346     static void TearDownTestCase(void)
347     {
348     }
349     // Testcase setup
SetUp()350     virtual void SetUp()
351     {
352         usleep(OPER_INTERVAL * MS2US);
353     }
354     // Testcase teardown
TearDown()355     virtual void TearDown()
356     {
357     }
358 };
359 
360 /**
361  * @tc.number    : DMSLite_SAMGR_SendSharedRequest_0010
362  * @tc.name      : Feature can send a message by SendSharedRequest
363  * @tc.desc      : [C- SOFTWARE -0200]
364 */
365 HWTEST_F(SendShareRequestTest, testSendSharedRequest0010, Function | MediumTest | Level2) {
366     DemoApi *demoApi = GetIUnknown("S_sendShareReq", "feature02");
367     if (demoApi == nullptr) {
368         ADD_FAILURE();
369     }
370     Request request = {.msgId = MSG_RT,
371                        .len = 0,
372                        .data = nullptr,
373                        .msgValue = 0};
374     char *body = (char*)"I wanna async call good result!";
375     request.len = (int16)(strlen(body) + 1);
376     request.data = malloc(request.len);
377     if (request.data == nullptr) {
378         ADD_FAILURE();
379     }
380     errno_t error = strcpy_s((char *)request.data, request.len, body);
381     if (error != EOK) {
382         printf("[hcpptest]error failed to strcpy_s \n");
383     }
384 
385     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi("S_sendShareReq", "feature03");
386     DemoFeature *feature = GET_OBJECT(iUnknown, DemoFeature, iUnknown);
387 
388     feature->featureCalledCount = 0;
389     uint32 *token = NULL;
390     token = demoApi->SAMGR_SendSharedRequestProxy(&feature->identity, &request, token, DemoHandlerAndCheck);
391 
392     usleep(OPER_INTERVAL * MS2US);
393     ASSERT_EQ(feature->featureCalledCount, 1);
394 
395     ReleaseIUnknown(demoApi);
396 }
397 
398 /**
399  * @tc.number    : DMSLite_SAMGR_SendSharedDirectRequest_0010
400  * @tc.name      : Feature can send a message by SendSharedDirectRequest
401  * @tc.desc      : [C- SOFTWARE -0200]
402 */
403 HWTEST_F(SendShareRequestTest, testSendSharedDirectRequest0010, Function | MediumTest | Level2) {
404     DemoApi *demoApi = GetIUnknown("S_sendShareReq", "feature02");
405     if (demoApi == nullptr) {
406         ADD_FAILURE();
407     }
408     Request request = {.msgId = MSG_RT,
409                        .len = 0,
410                        .data = nullptr,
411                        .msgValue = 0};
412     char *body = (char*)"I wanna async call good result!";
413     request.len = (int16)(strlen(body) + 1);
414     request.data = malloc(request.len);
415     if (request.data == nullptr) {
416         ADD_FAILURE();
417     }
418     errno_t error = strcpy_s((char *)request.data, request.len, body);
419     if (error != EOK) {
420         printf("[hcpptest]error failed to strcpy_s \n");
421     }
422 
423     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi("S_sendShareReq", "feature03");
424     DemoFeature *feature = GET_OBJECT(iUnknown, DemoFeature, iUnknown);
425 
426     feature->featureCalledCount = 0;
427     Response response = {
428         .data = (char*)"Yes, you did!",
429         .len = 0,
430         };
431     uint32 *token = NULL;
432     int32 result = demoApi->SAMGR_SendSharedDirectRequestProxy(&feature->identity, &request, &response,
433                                                 &token, DemoHandlerAndCheck);
434     ASSERT_EQ(result, 0);
435 
436     usleep(OPER_INTERVAL * MS2US);
437     ASSERT_EQ(feature->featureCalledCount, 0);
438 
439     ReleaseIUnknown(demoApi);
440 }