• 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"); you may not use this file except in
4  * 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 distributed under the License is
10  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11  * either express or implied.
12  * See the License for the specific language governing permissions and limitations under the License.
13  */
14 
15 #include <securec.h>
16 #include <ohos_init.h>
17 #include <ohos_errno.h>
18 #include "gtest/gtest.h"
19 #include "utils/SamgrTestBase.h"
20 #include "iproxy_client.h"
21 #include "iproxy_server.h"
22 
23 using namespace testing::ext;
24 
25 static const char *SERVICE_NAME = {"ipcService02"};
26 static const char *FEATURE_NAME = {"ipcFeature"};
27 
28 struct DemoService {
29     INHERIT_SERVICE;
30     Identity identity;
31     bool serviceStatus;
32 };
33 
GetName(Service * service)34 static const char *GetName(Service *service)
35 {
36     (void)service;
37     return SERVICE_NAME;
38 }
39 
Initialize(Service * service,Identity identity)40 static BOOL Initialize(Service *service, Identity identity)
41 {
42     DemoService *demoService = (DemoService *)service;
43     demoService->identity = identity;
44     demoService->serviceStatus = TRUE;
45     return TRUE;
46 }
47 
MessageHandle(Service * service,Request * msg)48 static BOOL MessageHandle(Service *service, Request *msg)
49 {
50     (void)service;
51     (void)msg;
52     return FALSE;
53 }
54 
GetTaskConfig(Service * service)55 static TaskConfig GetTaskConfig(Service *service)
56 {
57     (void)service;
58     TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, 0x800, 20, SHARED_TASK};
59     return config;
60 }
61 
62 static DemoService g_service = {
63     .GetName = GetName,
64     .Initialize = Initialize,
65     .MessageHandle = MessageHandle,
66     .GetTaskConfig = GetTaskConfig,
67     .identity = {-1, -1, nullptr},
68     .serviceStatus = FALSE,
69 };
70 
71 static int calledCount = 0;
72 struct DemoFeatureApi {
73     INHERIT_SERVER_IPROXY;
74     void (*SyncCall)(IUnknown *iUnknown);
75 };
76 
77 struct DemoFeature {
78     INHERIT_FEATURE;
79     INHERIT_IUNKNOWNENTRY(DemoFeatureApi);
80     Identity identity;
81     bool featureStatus;
82 };
83 
84 static const char *FEATURE_GetName(Feature *feature);
85 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity);
86 static void FEATURE_OnStop(Feature *feature, Identity identity);
87 static BOOL FEATURE_OnMessage(Feature *feature, Request *request);
88 static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply);
89 static void SyncCall(IUnknown *iUnknown);
90 
91 static DemoFeature g_feature = {
92     .GetName = FEATURE_GetName,
93     .OnInitialize = FEATURE_OnInitialize,
94     .OnStop = FEATURE_OnStop,
95     .OnMessage = FEATURE_OnMessage,
96     SERVER_IPROXY_IMPL_BEGIN,
97     .Invoke = Invoke,
98     .SyncCall = SyncCall,
99     IPROXY_END,
100     .identity = {-1, -1, NULL},
101     .featureStatus = FALSE,
102 };
FEATURE_GetName(Feature * feature)103 static const char *FEATURE_GetName(Feature *feature)
104 {
105     (void)feature;
106     return FEATURE_NAME;
107 }
108 
FEATURE_OnInitialize(Feature * feature,Service * parent,Identity identity)109 static void FEATURE_OnInitialize(Feature *feature, Service *parent, Identity identity)
110 {
111     DemoFeature *demoFeature = (DemoFeature *)feature;
112     demoFeature->identity = identity;
113     demoFeature->featureStatus = TRUE;
114 }
115 
FEATURE_OnStop(Feature * feature,Identity identity)116 static void FEATURE_OnStop(Feature *feature, Identity identity)
117 {
118     (void)feature;
119     (void)identity;
120 }
121 
FEATURE_OnMessage(Feature * feature,Request * msg)122 static BOOL FEATURE_OnMessage(Feature *feature, Request *msg)
123 {
124     printf("[hcpptest][TID:0x%lx]OnMessage(%s)! Request<%d, %d, %p>",
125            pthread_self(), feature->GetName(feature), msg->msgId, msg->msgValue, msg->data);
126     return TRUE;
127 }
128 
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)129 static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
130 {
131     printf("[hcpptest][TID:0x%lx]Feature Remote Invoke is called! <%p, %d, %p, %p, %p>",
132            pthread_self(), iProxy, funcId, origin, req, reply);
133     WriteBool(reply, TRUE);
134     return EC_SUCCESS;
135 }
136 
SyncCall(IUnknown * iUnknown)137 static void SyncCall(IUnknown *iUnknown)
138 {
139     calledCount++;
140 }
141 
ServiceInit(void)142 static void ServiceInit(void)
143 {
144     BOOL result = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
145     if (result == FALSE) {
146         printf("[hcpptest][error]RegisterService fail \n");
147         (&g_service)->serviceStatus = FALSE;
148     }
149 }
150 SYSEX_SERVICE_INIT(ServiceInit);
151 
FeatureInit(void)152 static void FeatureInit(void)
153 {
154     BOOL result = SAMGR_GetInstance()->RegisterFeature(SERVICE_NAME, (Feature *)&g_feature);
155     if (result == FALSE) {
156         printf("[hcpptest][error]RegisterFeature fail \n");
157         (&g_feature)->featureStatus = FALSE;
158     }
159     result = SAMGR_GetInstance()->RegisterFeatureApi(SERVICE_NAME, FEATURE_NAME, GET_IUNKNOWN(g_feature));
160     if (result == FALSE) {
161         printf("[hcpptest][error]RegisterFeatureApi fail \n");
162         (&g_feature)->featureStatus = FALSE;
163     }
164 }
165 SYSEX_FEATURE_INIT(FeatureInit);
166 
167 class LiteIPCFeatureTest : public testing::Test {
168 protected:
169     // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)170     static void SetUpTestCase(void)
171     {
172         SystemInitProxy();
173     }
174     // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)175     static void TearDownTestCase(void)
176     {
177     }
178     // Testcase setup
SetUp()179     virtual void SetUp()
180     {
181         usleep(OPER_INTERVAL * MS2US);
182     }
183     // Testcase teardown
TearDown()184     virtual void TearDown()
185     {
186     }
187 };
188 
189 /**
190  * @tc.number    : DMSLite_SAMGR_IPCFeature_0010
191  * @tc.name      : Feature which cross process should be inited ok
192  * @tc.desc      : [C- SOFTWARE -0200]
193 */
194 HWTEST_F(LiteIPCFeatureTest, testIPCFeature0010, Function | MediumTest | Level2)
195 {
196     ASSERT_EQ((&g_service)->serviceStatus, TRUE);
197     ASSERT_EQ((&g_feature)->featureStatus, TRUE);
198 };
199 
200 /**
201  * @tc.number    : DMSLite_SAMGR_IPCFeature_0020
202  * @tc.name      : Feature which cross process also can be accessed as LPC
203  * @tc.desc      : [C- SOFTWARE -0200]
204 */
205 HWTEST_F(LiteIPCFeatureTest, testIPCFeature0020, Function | MediumTest | Level2)
206 {
207     DemoFeatureApi *demoApi = nullptr;
208     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(SERVICE_NAME, FEATURE_NAME);
209     ASSERT_EQ(iUnknown != nullptr, TRUE);
210 
211     // DEFAULT_VERSION: means access inside service
212     int resultCode = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&demoApi);
213     ASSERT_EQ(resultCode == EC_SUCCESS, TRUE);
214 
215     int countBefore = calledCount;
216     demoApi->SyncCall((IUnknown *)demoApi);
217     int countAfter = calledCount;
218 
219     ASSERT_EQ(countAfter - countBefore, 1);
220 };
221 
222 /**
223  * @tc.number    : DMSLite_SAMGR_IPCFeature_0030
224  * @tc.name      : For inside caller, Feature which cross process can not be accessed as IPC
225  * @tc.desc      : [C- SOFTWARE -0200]
226 */
227 HWTEST_F(LiteIPCFeatureTest, testIPCFeature0030, Function | MediumTest | Level2)
228 {
229     IClientProxy *demoApi = nullptr;
230     IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi(SERVICE_NAME, FEATURE_NAME);
231     ASSERT_EQ(iUnknown != nullptr, TRUE);
232 
233     // CLIENT_PROXY_VER: means access cross process
234     int resultCode = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&demoApi);
235     ASSERT_EQ(resultCode != EC_SUCCESS, TRUE);
236 };
237 
238 /**
239  * @tc.number    : DMSLite_SAMGR_IPCFeature_0040
240  * @tc.name      : Feature which cross process can be unregistered
241  * @tc.desc      : [C- SOFTWARE -0200]
242 */
243 HWTEST_F(LiteIPCFeatureTest, testIPCFeature0040, Function | MediumTest | Level2)
244 {
245     IUnknown *iUnknown = SAMGR_GetInstance()->UnregisterFeatureApi(SERVICE_NAME, FEATURE_NAME);
246     ASSERT_EQ(iUnknown != nullptr, TRUE);
247 
248     Feature *feature = SAMGR_GetInstance()->UnregisterFeature(SERVICE_NAME, FEATURE_NAME);
249     ASSERT_EQ(feature != nullptr, TRUE);
250 
251     Service *service = SAMGR_GetInstance()->UnregisterService(SERVICE_NAME);
252     ASSERT_EQ(service != nullptr, TRUE);
253 };