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 = {"ipcService01"};
26
27 static int calledCount = 0;
28 struct DefaultFeatureApi {
29 INHERIT_SERVER_IPROXY;
30 void (*SyncCall)(IUnknown *iUnknown);
31 };
32
33 struct DemoService {
34 INHERIT_SERVICE;
35 INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
36 Identity identity;
37 bool serviceStatus;
38 };
39
GetName(Service * service)40 static const char *GetName(Service *service)
41 {
42 (void)service;
43 return SERVICE_NAME;
44 }
GetName2(Service * service)45 static const char *GetName2(Service *service)
46 {
47 (void)service;
48 return "ipcService01x";
49 }
Initialize(Service * service,Identity identity)50 static BOOL Initialize(Service *service, Identity identity)
51 {
52 DemoService *demoService = (DemoService *)service;
53 demoService->identity = identity;
54
55 demoService->serviceStatus = TRUE;
56 return TRUE;
57 }
58
MessageHandle(Service * service,Request * msg)59 static BOOL MessageHandle(Service *service, Request *msg)
60 {
61 printf("[hcpptest][TID:0x%lx]MessageHandle(%s)! Request<%d, %d, %p>",
62 pthread_self(), service->GetName(service), msg->msgId, msg->msgValue, msg->data);
63 return FALSE;
64 }
65
GetTaskConfig(Service * service)66 static TaskConfig GetTaskConfig(Service *service)
67 {
68 (void)service;
69 TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, 0x800, 20, SHARED_TASK};
70 return config;
71 }
SyncCall(IUnknown * iUnknown)72 static void SyncCall(IUnknown *iUnknown)
73 {
74 calledCount++;
75 }
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)76 static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
77 {
78 printf("[hcpptest][TID:0x%lx]Service Remote Invoke is called! <%p, %d, %p, %p, %p>",
79 pthread_self(), iProxy, funcId, origin, req, reply);
80
81 size_t len = 0;
82 char *requestStr = (char *)ReadString(req, &len);
83 if (requestStr != nullptr) {
84 printf("[hcpptest]requestStr is %s", requestStr);
85 } else {
86 printf("[hcpptest]requestStr is NULL");
87 }
88
89 DefaultFeatureApi *defaultApi = (DefaultFeatureApi *)iProxy;
90 if (funcId == 0) {
91 defaultApi->SyncCall((IUnknown *)iProxy);
92 }
93
94 WriteString(reply, "Yes, you did!");
95 return EC_SUCCESS;
96 }
97 static DemoService g_service = {
98 .GetName = GetName,
99 .Initialize = Initialize,
100 .MessageHandle = MessageHandle,
101 .GetTaskConfig = GetTaskConfig,
102 SERVER_IPROXY_IMPL_BEGIN,
103 .Invoke = Invoke,
104 .SyncCall = SyncCall,
105 IPROXY_END,
106 .identity = {-1, -1, nullptr},
107 .serviceStatus = FALSE,
108 };
109
110 static DemoService g_service2 = {
111 .GetName = GetName2,
112 .Initialize = Initialize,
113 .MessageHandle = MessageHandle,
114 .GetTaskConfig = GetTaskConfig,
115 SERVER_IPROXY_IMPL_BEGIN,
116 .Invoke = Invoke,
117 .SyncCall = SyncCall,
118 IPROXY_END,
119 .identity = {-1, -1, nullptr},
120 .serviceStatus = FALSE,
121 };
122
ServiceInit(void)123 static void ServiceInit(void)
124 {
125 BOOL result1 = SAMGR_GetInstance()->RegisterService((Service *)&g_service);
126 BOOL result2 = SAMGR_GetInstance()->RegisterService((Service *)&g_service2);
127 if (result1 == FALSE) {
128 (&g_service)->serviceStatus = FALSE;
129 }
130 if (result2 == FALSE) {
131 (&g_service2)->serviceStatus = FALSE;
132 }
133 }
134 SYSEX_SERVICE_INIT(ServiceInit);
135
FeatureInit(void)136 static void FeatureInit(void)
137 {
138 BOOL result1 = SAMGR_GetInstance()->RegisterDefaultFeatureApi(SERVICE_NAME, GET_IUNKNOWN(g_service));
139 if (result1 == FALSE) {
140 (&g_service)->serviceStatus = FALSE;
141 }
142 BOOL result2 = SAMGR_GetInstance()->RegisterDefaultFeatureApi(SERVICE_NAME, GET_IUNKNOWN(g_service2));
143 if (result2 == FALSE) {
144 (&g_service2)->serviceStatus = FALSE;
145 }
146 }
147 SYSEX_FEATURE_INIT(FeatureInit);
148
149 class LiteIPCServiceTest : public testing::Test {
150 protected:
151 // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)152 static void SetUpTestCase(void)
153 {
154 SystemInitProxy();
155 }
156 // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)157 static void TearDownTestCase(void)
158 {
159 }
160 // Testcase setup
SetUp()161 virtual void SetUp()
162 {
163 usleep(OPER_INTERVAL * MS2US);
164 }
165 // Testcase teardown
TearDown()166 virtual void TearDown()
167 {
168 }
169 };
170
171 /**
172 * @tc.number : DMSLite_SAMGR_IPCService_0010
173 * @tc.name : Service which cross processes is registered ok
174 * @tc.desc : [C- SOFTWARE -0200]
175 */
176 HWTEST_F(LiteIPCServiceTest, testIPCService0010, Function | MediumTest | Level1)
177 {
178 ASSERT_EQ((&g_service)->serviceStatus, TRUE);
179 ASSERT_EQ((&g_service)->identity.serviceId != -1, TRUE);
180 ASSERT_EQ((&g_service2)->serviceStatus, TRUE);
181 };
182
183 /**
184 * @tc.number : DMSLite_SAMGR_IPCService_0020
185 * @tc.name : Service which cross process also can be accessed as LPC
186 * @tc.desc : [C- SOFTWARE -0200]
187 */
188 HWTEST_F(LiteIPCServiceTest, testIPCService0020, Function | MediumTest | Level2)
189 {
190 DefaultFeatureApi *demoApi = nullptr;
191 IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SERVICE_NAME);
192 ASSERT_EQ(iUnknown != nullptr, TRUE);
193
194 // DEFAULT_VERSION: means access inside service
195 int resultCode = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&demoApi);
196 ASSERT_EQ(resultCode == EC_SUCCESS, TRUE);
197
198 int countBefore = calledCount;
199 demoApi->SyncCall((IUnknown *)demoApi);
200 int countAfter = calledCount;
201
202 ASSERT_EQ(countAfter - countBefore, 1);
203 };
204
205 /**
206 * @tc.number : DMSLite_SAMGR_IPCService_0030
207 * @tc.name : For inside caller, Service which cross process can not be accessed as IPC
208 * @tc.desc : [C- SOFTWARE -0200]
209 */
210 HWTEST_F(LiteIPCServiceTest, testIPCService0030, Function | MediumTest | Level2)
211 {
212 IClientProxy *demoApi = nullptr;
213 IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SERVICE_NAME);
214 ASSERT_EQ(iUnknown != nullptr, TRUE);
215
216 // CLIENT_PROXY_VER: means access other process
217 int resultCode = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&demoApi);
218 ASSERT_EQ(resultCode != EC_SUCCESS, TRUE);
219 };
220
221 /**
222 * @tc.number : DMSLite_SAMGR_IPCService_0040
223 * @tc.name : Service which cross process can be unregistered
224 * @tc.desc : [C- SOFTWARE -0200]
225 */
226 HWTEST_F(LiteIPCServiceTest, testIPCService0040, Function | MediumTest | Level2)
227 {
228 IUnknown *iUnknown = SAMGR_GetInstance()->UnregisterDefaultFeatureApi(SERVICE_NAME);
229 ASSERT_EQ(iUnknown != nullptr, TRUE);
230
231 Service *service = SAMGR_GetInstance()->UnregisterService(SERVICE_NAME);
232 ASSERT_EQ(service != nullptr, TRUE);
233 };
234
235 /**
236 * @tc.number : DMSLite_SAMGR_IPCService_0050
237 * @tc.name : User can not unregister service belong to other process
238 * @tc.desc : [C- SOFTWARE -0200]
239 */
240 HWTEST_F(LiteIPCServiceTest, testIPCService0050, Function | MediumTest | Level2)
241 {
242 // samgr belong foundation process
243 IUnknown *iUnknown = SAMGR_GetInstance()->UnregisterDefaultFeatureApi("samgr");
244 ASSERT_EQ(iUnknown == nullptr, TRUE);
245 };