• 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 
22 using namespace testing::ext;
23 
24 struct DemoApi {
25     INHERIT_IUNKNOWN;
26     bool (*FeatureApi001)(IUnknown *iUnknown, const char *para1);
27 };
28 
29 struct DemoFeature {
30     INHERIT_FEATURE;
31     INHERIT_IUNKNOWNENTRY(DemoApi);
32     Identity identity;
33 };
34 
35 struct DefaultFeatureApi {
36     INHERIT_IUNKNOWN;
37     BOOL (*DefaultApi001)(IUnknown *iUnknown, char *para1);
38 };
39 
40 struct DemoService {
41     INHERIT_SERVICE;
42     INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
43     Identity identity;
44 };
45 
DefaultApi001(IUnknown * iUnknown,char * para1)46 static BOOL DefaultApi001(IUnknown *iUnknown,  char *para1)
47 {
48     (void)iUnknown;
49     return TRUE;
50 }
51 
FeatureApi001(IUnknown * iUnknown,const char * para1)52 static bool FeatureApi001(IUnknown *iUnknown,  const char *para1)
53 {
54     (void)iUnknown;
55     return TRUE;
56 }
57 
GetName(Service * service)58 static const char *GetName(Service *service)
59 {
60     (void)service;
61     return "Service_NoTask";
62 }
63 
Initialize(Service * service,Identity identity)64 static BOOL Initialize(Service *service, Identity identity)
65 {
66     DemoService *demoService = (DemoService *)service;
67     demoService->identity = identity;
68     return TRUE;
69 }
70 
MessageHandle(Service * service,Request * msg)71 static BOOL MessageHandle(Service *service, Request *msg)
72 {
73     (void)service;
74     (void)msg;
75     return FALSE;
76 }
77 
GetTaskConfig(Service * service)78 static TaskConfig GetTaskConfig(Service *service)
79 {
80     (void)service;
81     TaskConfig config = {LEVEL_HIGH, PRI_ABOVE_NORMAL, 0x800, 20, NO_TASK};
82     return config;
83 }
84 
85 static DemoService g_createService = {
86     .GetName = GetName,
87     .Initialize = Initialize,
88     .MessageHandle = MessageHandle,
89     .GetTaskConfig = GetTaskConfig,
90     .ver = 0x20,
91     .ref = 1,
92     .iUnknown = {
93         DEFAULT_IUNKNOWN_IMPL,
94         .DefaultApi001 = DefaultApi001,
95     }
96 };
97 
FEATURE_GetName(Feature * feature)98 static const char *FEATURE_GetName(Feature *feature)
99 {
100     (void)feature;
101     return "featureName501";
102 }
103 
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)104 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
105 {
106     DemoFeature *demoFeature = (DemoFeature *)feature;
107     demoFeature->identity = identity;
108     (void)parent;
109 }
110 
FEATURE_OnStop(Feature * feature,Identity identity)111 static void FEATURE_OnStop(Feature *feature, Identity identity)
112 {
113     (void)feature;
114     (void)identity;
115 }
116 
FEATURE_OnMessage(Feature * feature,Request * request)117 static BOOL FEATURE_OnMessage(Feature *feature, Request *request)
118 {
119     (void)feature;
120     char *dataR = (char*)"Yes, you did!";
121     Response response = {
122         .data = dataR,
123         .len = 0,
124     };
125     SAMGR_SendResponse(request, &response);
126     return TRUE;
127 }
128 
129 static DemoFeature g_createFeature = {
130     .GetName = FEATURE_GetName,
131     .OnInitialize = FEATURE_OnInitialize,
132     .OnStop = FEATURE_OnStop,
133     .OnMessage = FEATURE_OnMessage,
134     .ver = 0x20,
135     .ref = 1,
136     .iUnknown = {
137         DEFAULT_IUNKNOWN_IMPL,
138         .FeatureApi001 = FeatureApi001,
139     },
140     .identity = {-1, -1, nullptr},
141 };
142 
GServiceInit(void)143 static void GServiceInit(void)
144 {
145     BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_createService);
146     if (result != TRUE) {
147         printf("[hcpptest]error RegisterService failed");
148     }
149 }
150 SYSEX_SERVICE_INIT(GServiceInit);
151 
GFeatureInit(void)152 static void GFeatureInit(void)
153 {
154     BOOL result1 = SAMGR_GetInstance()->RegisterDefaultFeatureApi("Service_NoTask",
155                                                                   GET_IUNKNOWN(g_createService));
156     BOOL result2 = SAMGR_GetInstance()->RegisterFeature("Service_NoTask", (Feature *)&g_createFeature);
157     BOOL result3 = SAMGR_GetInstance()->RegisterFeatureApi("Service_NoTask", "featureName501",
158                                                            GET_IUNKNOWN(g_createFeature));
159     if (result1 != TRUE || result2 != TRUE || result3 != TRUE) {
160         printf("[hcpptest]error register feature or api fail <%d, %d, %d> \n", result1, result2, result3);
161     }
162 }
163 SYSEX_FEATURE_INIT(GFeatureInit);
164 
GetIUnknown(const char * serviceName,const char * featureName)165 static DemoApi *GetIUnknown(const char *serviceName, const char *featureName)
166 {
167     DemoApi *demoApi = nullptr;
168     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(serviceName, featureName);
169     if (iUnknown == nullptr) {
170         return nullptr;
171     }
172     int result = iUnknown->QueryInterface(iUnknown, 0x20, (void **)&demoApi);
173     if (result == 0 && demoApi != nullptr) {
174         return demoApi;
175     } else {
176         return nullptr;
177     }
178 }
179 
GetDefaultIUnknown(const char * serviceName)180 static DefaultFeatureApi *GetDefaultIUnknown(const char *serviceName)
181 {
182     DefaultFeatureApi *defaultApi = nullptr;
183     IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(serviceName);
184     if (iUnknown == nullptr) {
185         return nullptr;
186     }
187     int result = iUnknown->QueryInterface(iUnknown, 0x20, (void **)&defaultApi);
188     if (result == 0 && defaultApi != nullptr) {
189         return defaultApi;
190     } else {
191         return nullptr;
192     }
193 }
194 
ReleaseIUnknown(DemoApi * demoApi)195 static void ReleaseIUnknown(DemoApi *demoApi)
196 {
197     demoApi->Release((IUnknown *)demoApi);
198 }
199 
200 class TaskpoolNoTaskTest : public testing::Test {
201 protected:
202     // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)203     static void SetUpTestCase(void)
204     {
205         printf("[hcpptest]SetUpTestCase ! \n");
206         SystemInitProxy();
207     }
208     // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)209     static void TearDownTestCase(void)
210     {
211     }
212     // Testcase setup
SetUp()213     virtual void SetUp()
214     {
215         usleep(OPER_INTERVAL * MS2US);
216     }
217     // Testcase teardown
TearDown()218     virtual void TearDown()
219     {
220     }
221 };
222 
223 /**
224  * @tc.number    : DMSLite_SAMGR_Taskpool_NoTask_0010
225  * @tc.name      : Service without task can handle direct calls
226  * @tc.desc      : [C- SOFTWARE -0200]
227 */
228 HWTEST_F(TaskpoolNoTaskTest, testNoTask0010, Function | MediumTest | Level2)
229 {
230     DemoApi *demoApi = GetIUnknown("Service_NoTask", "featureName501");
231     if (demoApi == nullptr) {
232         ADD_FAILURE();
233     }
234 
235     bool result = demoApi->FeatureApi001((IUnknown *)demoApi, (char*)"xxxx");
236     ASSERT_EQ(result, TRUE);
237     ReleaseIUnknown(demoApi);
238 
239     DefaultFeatureApi *defaultApi = GetDefaultIUnknown("Service_NoTask");
240     if (defaultApi == nullptr) {
241         ADD_FAILURE();
242     }
243     result = defaultApi->DefaultApi001((IUnknown *)defaultApi, (char*)"yyyy");
244     ASSERT_EQ(result, TRUE);
245 };