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 "iproxy_client.h"
20 #include "registry.h"
21 #include "utils/SamgrTestBase.h"
22
23 using namespace testing::ext;
24
25 static const int NORMAL_IO_SIZE = 250;
26
GetRemoteIUnknown(const char * serviceName,const char * featureName)27 static IClientProxy *GetRemoteIUnknown(const char *serviceName, const char *featureName)
28 {
29 IClientProxy *demoApi = NULL;
30 IUnknown *iUnknown = NULL;
31 if (featureName == nullptr) {
32 iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(serviceName);
33 } else {
34 iUnknown = SAMGR_GetInstance()->GetFeatureApi(serviceName, featureName);
35 }
36
37 if (iUnknown == nullptr) {
38 printf("[hcpptest][TID:0x%lx][GetDefaultFeatureApi S:%s]: error is NULL", pthread_self(), serviceName);
39 return nullptr;
40 }
41 (void)iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&demoApi);
42
43 return demoApi;
44 }
45
CurrentCallback(IOwner owner,int code,IpcIo * reply)46 static int CurrentCallback(IOwner owner, int code, IpcIo *reply)
47 {
48 printf("[hcpptest]CurrentCallback run \n");
49 size_t len = 0;
50 char *response = (char *)ReadString(reply, &len);
51 printf("[hcpptest]response %s \n", response);
52 return 0;
53 }
54
ReleaseIUnknown(IUnknown * demoApi)55 static void ReleaseIUnknown(IUnknown *demoApi)
56 {
57 if (demoApi != NULL) {
58 demoApi->Release((IUnknown *)demoApi);
59 }
60 }
61
62 class LiteIPCClientTest : public testing::Test {
63 protected:
64 // SetUpTestCase: Testsuit setup, run before 1st testcase
SetUpTestCase(void)65 static void SetUpTestCase(void)
66 {
67 printf("[hcpptest]SetUpTestCase! \n");
68 SystemInitProxy();
69 }
70 // TearDownTestCase: Testsuit teardown, run after last testcase
TearDownTestCase(void)71 static void TearDownTestCase(void)
72 {
73 }
74 // Testcase setup
SetUp()75 virtual void SetUp()
76 {
77 }
78 // Testcase teardown
TearDown()79 virtual void TearDown()
80 {
81 }
82 };
83
84
85 /**
86 * @tc.number : DMSLite_SAMGR_IPCClient_0010
87 * @tc.name : Client can get api of other process
88 * @tc.desc : [C- SOFTWARE -0200]
89 */
90 HWTEST_F(LiteIPCClientTest, testIPCClient0010, Function | MediumTest | Level1)
91 {
92 IClientProxy *demoApi = NULL;
93 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi("abilityms", "AmsFeature");
94 ASSERT_EQ(iUnknown != NULL, TRUE);
95
96 int result = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&demoApi);
97 ASSERT_EQ(result, 0);
98 ASSERT_EQ(demoApi != nullptr, TRUE);
99 };
100
101 /**
102 * @tc.number : DMSLite_SAMGR_IPCClient_0020
103 * @tc.name : Client can not get api if service name or feature name not exist in the system
104 * @tc.desc : [C- SOFTWARE -0200]
105 */
106 HWTEST_F(LiteIPCClientTest, testIPCClient0020, Function | MediumTest | Level2)
107 {
108 IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi("notExistService");
109 ASSERT_EQ(iUnknown == NULL, TRUE);
110
111 iUnknown = SAMGR_GetInstance()->GetFeatureApi("notExistService", "");
112 ASSERT_EQ(iUnknown == NULL, TRUE);
113
114 iUnknown = SAMGR_GetInstance()->GetFeatureApi("abilityms", "notExistFeature");
115 ASSERT_EQ(iUnknown == NULL, TRUE);
116 };
117
118 /**
119 * @tc.number : DMSLite_SAMGR_IPCClient_0040
120 * @tc.name : Client can send request to other process
121 * @tc.desc : [C- SOFTWARE -0200]
122 */
123 HWTEST_F(LiteIPCClientTest, testIPCClient0040, Function | MediumTest | Level1)
124 {
125 IClientProxy *remoteApi = GetRemoteIUnknown("abilityms", "AmsFeature");
126 ASSERT_EQ(remoteApi != nullptr, TRUE);
127
128 IpcIo request;
129 char data[NORMAL_IO_SIZE];
130 IpcIoInit(&request, data, sizeof(data), 0);
131 char *buff = (char*)"test xxxx";
132 WriteString(&request, buff);
133
134 char data2[NORMAL_IO_SIZE];
135 int funcId = 0;
136 int result = remoteApi->Invoke(remoteApi, funcId, &request, data2, CurrentCallback);
137 ASSERT_EQ(result, EC_SUCCESS);
138
139 ReleaseIUnknown((IUnknown *)remoteApi);
140 };
141
142 /**
143 * @tc.number : DMSLite_SAMGR_IPCClient_0050
144 * @tc.name : User can send request which is null to other process
145 * @tc.desc : [C- SOFTWARE -0200]
146 */
147 HWTEST_F(LiteIPCClientTest, testIPCClient0050, Function | MediumTest | Level2)
148 {
149 IClientProxy *remoteApi = GetRemoteIUnknown("abilityms", "AmsFeature");
150 ASSERT_EQ(remoteApi != nullptr, TRUE);
151
152 char data2[NORMAL_IO_SIZE];
153 int funcId = 0;
154 int result = remoteApi->Invoke(remoteApi, funcId, nullptr, data2, CurrentCallback);
155 printf("[hcpptest]result is: %d \n", result);
156 ASSERT_EQ(result, EC_SUCCESS);
157
158 ReleaseIUnknown((IUnknown *)remoteApi);
159 };
160
161 /**
162 * @tc.number : DMSLite_SAMGR_IPCClient_0060
163 * @tc.name : User can send request without processing receipts
164 * @tc.desc : [C- SOFTWARE -0200]
165 */
166 HWTEST_F(LiteIPCClientTest, testIPCClient0060, Function | MediumTest | Level2)
167 {
168 IClientProxy *remoteApi = GetRemoteIUnknown("abilityms", "AmsInnerFeature");
169 ASSERT_EQ(remoteApi != nullptr, TRUE);
170
171 IpcIo request;
172 char data[NORMAL_IO_SIZE];
173 IpcIoInit(&request, data, sizeof(data), 0);
174 char *buff = (char*)"test xxxx";
175 WriteString(&request, buff);
176
177 int funcId = 0;
178 int result = remoteApi->Invoke(remoteApi, funcId, &request, nullptr, nullptr);
179 printf("[hcpptest]result is: %d \n", result);
180 ASSERT_EQ(result, EC_SUCCESS);
181
182 ReleaseIUnknown((IUnknown *)remoteApi);
183 };
184
185 /**
186 * @tc.number : DMSLite_SAMGR_IPCClient_0070
187 * @tc.name : User can not send request which longer than 2048 bytes
188 * @tc.desc : [C- SOFTWARE -0200]
189 */
190 HWTEST_F(LiteIPCClientTest, testIPCClient0070, Function | MediumTest | Level2)
191 {
192 IClientProxy *remoteApi = GetRemoteIUnknown("abilityms", "AmsInnerFeature");
193 ASSERT_EQ(remoteApi != nullptr, TRUE);
194
195 IpcIo request;
196 char data[MAX_IO_SIZE + 1];
197 IpcIoInit(&request, data, sizeof(data), 0);
198 char *buff = (char*)"test xxxx";
199 WriteString(&request, buff);
200
201 char data2[NORMAL_IO_SIZE];
202 int funcId = 0;
203 int result = remoteApi->Invoke(remoteApi, funcId, &request, data2, CurrentCallback);
204 printf("[hcpptest]result is: %d \n", result);
205 ASSERT_EQ(result, EC_INVALID);
206
207 ReleaseIUnknown((IUnknown *)remoteApi);
208 };
209
210 /**
211 * @tc.number : DMSLite_SAMGR_IPCClient_0100
212 * @tc.name : User can get IPC address of remote service
213 * @tc.desc : [C- SOFTWARE -0200]
214 */
215 HWTEST_F(LiteIPCClientTest, testIPCClient0100, Function | MediumTest | Level2)
216 {
217 IUnknown *iUnknown = SAMGR_GetInstance()->GetFeatureApi("abilityms", "AmsFeature");
218 ASSERT_EQ(iUnknown != nullptr, TRUE);
219
220 SvcIdentity svcIdentity = SAMGR_GetRemoteIdentity("abilityms", "AmsFeature");
221 ASSERT_EQ(svcIdentity.handle != 0xffffffff, TRUE);
222 };
223
224 /**
225 * @tc.number : DMSLite_SAMGR_IPCClient_0110
226 * @tc.name : User can get an invalid IPC address if input para is nullptr
227 * @tc.desc : [C- SOFTWARE -0200]
228 */
229 HWTEST_F(LiteIPCClientTest, testIPCClient0110, Function | MediumTest | Level2)
230 {
231 SvcIdentity svcIdentity = SAMGR_GetRemoteIdentity(nullptr, "AmsFeature");
232 ASSERT_EQ(svcIdentity.handle == 0xffffffff, TRUE);
233
234 svcIdentity = SAMGR_GetRemoteIdentity("abilityms", nullptr);
235 ASSERT_EQ(svcIdentity.handle == 0xffffffff, TRUE);
236 };
237
238 /**
239 * @tc.number : DMSLite_SAMGR_IPCClient_0120
240 * @tc.name : User can get an invalid IPC address if has not called GetFeatureApi first
241 * @tc.desc : [C- SOFTWARE -0200]
242 */
243 HWTEST_F(LiteIPCClientTest, testIPCClient0120, Function | MediumTest | Level2)
244 {
245 SvcIdentity svcIdentity = SAMGR_GetRemoteIdentity("samgr", nullptr);
246 ASSERT_EQ(svcIdentity.handle == 0xffffffff, TRUE);
247 };
248
249 /**
250 * @tc.number : DMSLite_SAMGR_IPCClient_0130
251 * @tc.name : User get an invalid IPC address if service not exist
252 * @tc.desc : [C- SOFTWARE -0200]
253 */
254 HWTEST_F(LiteIPCClientTest, testIPCClient0130, Function | MediumTest | Level2)
255 {
256 SvcIdentity svcIdentity = SAMGR_GetRemoteIdentity("noExistService", "noExistFeature");
257 ASSERT_EQ(svcIdentity.handle == 0xffffffff, TRUE);
258 ASSERT_EQ(svcIdentity.token == 0xffffffff, TRUE);
259 };
260
261 /**
262 * @tc.number : DMSLite_SAMGR_IPCClient_0140
263 * @tc.name : User can register a proxy for remote api
264 * @tc.desc : [C- SOFTWARE -0200]
265 */
266 HWTEST_F(LiteIPCClientTest, testIPCClient0140, Function | MediumTest | Level2)
267 {
__anoncfa1d40a0102(const char *service, const char *feature, uint32 size) 268 auto creatClient = [] (const char *service, const char *feature, uint32 size) -> void * {
269 printf("[hcpptest]creatClient run \n");
270 return nullptr;
271 };
272
__anoncfa1d40a0202(const char *service, const char *feature, void *iproxy) 273 auto destroyClient = [] (const char *service, const char *feature, void *iproxy) {
274 printf("[hcpptest]destroyClient run \n");
275 };
276
277 // this method createClient will return a proxy.
278 int result = SAMGR_RegisterFactory("abilityms", "AmsFeature", creatClient, destroyClient);
279 ASSERT_EQ(result == 0, TRUE);
280 };