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