1 /*
2 * Copyright (C) 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 <iostream>
17 #include <string>
18 #include <cerrno>
19 #include <csignal>
20 #include <cstdlib>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <thread>
26 #include <nativetoken_kit.h>
27 #include <token_setproc.h>
28
29 #include "log_tags.h"
30 #include "if_system_ability_manager.h"
31 #include "ipc_debug.h"
32 #include "ipc_skeleton.h"
33 #include "test_service_client.h"
34
35 using namespace OHOS;
36 using namespace OHOS::HiviewDFX;
37 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "IPCTestClient" };
38 static std::shared_ptr<TestServiceClient> gTestClient{ nullptr };
InitTokenId(void)39 static void InitTokenId(void)
40 {
41 uint64_t tokenId;
42 NativeTokenInfoParams infoInstance = {
43 .dcapsNum = 0,
44 .permsNum = 0,
45 .aclsNum = 0,
46 .dcaps = NULL,
47 .perms = NULL,
48 .acls = NULL,
49 .processName = "com.ipc.test",
50 .aplStr = "normal",
51 };
52 tokenId = GetAccessTokenId(&infoInstance);
53 SetSelfTokenID(tokenId);
54 }
55
ThreadFunc(std::shared_ptr<TestServiceClient> testClient)56 void ThreadFunc(std::shared_ptr<TestServiceClient> testClient)
57 {
58 testClient->TestEnableSerialInvokeFlag();
59 }
60
SignalHandler(int signum)61 void SignalHandler(int signum)
62 {
63 ZLOGI(LABEL, "Caught signal %{public}d", signum);
64 if (gTestClient != nullptr) {
65 ZLOGE(LABEL, "UnRegister RemoteStub before application exit");
66 gTestClient->TestUnRegisterRemoteStub();
67 gTestClient = nullptr;
68 }
69 if (signum == SIGINT) {
70 ZLOGI(LABEL, "SIGINT");
71 IPCSkeleton::StopWorkThread();
72 }
73 }
74
TestCaseSyncTrans(std::shared_ptr<TestServiceClient> & testClient)75 void TestCaseSyncTrans(std::shared_ptr<TestServiceClient> &testClient)
76 {
77 bool ret = testClient->StartSyncTransaction();
78 if (!ret) {
79 std::cout << "[FAILED] Execution of TestCaseSyncTrans case failed" <<std::endl;
80 } else {
81 std::cout << "[PASS] Execution of TestCaseSyncTrans case Successful" <<std::endl;
82 }
83 }
84
TestCasePingService(std::shared_ptr<TestServiceClient> & testClient)85 void TestCasePingService(std::shared_ptr<TestServiceClient> &testClient)
86 {
87 bool ret = testClient->StartPingService();
88 if (!ret) {
89 std::cout << "[FAILED] Execution of TestCasePingService case failed" <<std::endl;
90 } else {
91 std::cout << "[PASS] Execution of TestCasePingService case Successful" <<std::endl;
92 }
93 }
94
TestCaseGetFooService(std::shared_ptr<TestServiceClient> & testClient)95 void TestCaseGetFooService(std::shared_ptr<TestServiceClient> &testClient)
96 {
97 bool ret = testClient->StartGetFooService();
98 if (!ret) {
99 std::cout << "[FAILED] Execution of TestCaseGetFooService case failed" <<std::endl;
100 } else {
101 std::cout << "[PASS] Execution of TestCaseGetFooService case Successful" <<std::endl;
102 }
103 }
104
TestCaseGetFileDescriptor(std::shared_ptr<TestServiceClient> & testClient)105 void TestCaseGetFileDescriptor(std::shared_ptr<TestServiceClient> &testClient)
106 {
107 bool ret = testClient->StartTestFileDescriptor();
108 if (!ret) {
109 std::cout << "[FAILED] Execution of TestCaseGetFileDescriptor case failed" <<std::endl;
110 } else {
111 std::cout << "[PASS] Execution of TestCaseGetFileDescriptor case Successful" <<std::endl;
112 }
113 }
114
TestCaseLoopTest(std::shared_ptr<TestServiceClient> & testClient)115 void TestCaseLoopTest(std::shared_ptr<TestServiceClient> &testClient)
116 {
117 constexpr int maxTestCount = 10240;
118 bool ret = testClient->StartLoopTest(maxTestCount);
119 if (!ret) {
120 std::cout << "[FAILED] Execution of TestCaseLoopTest case failed" <<std::endl;
121 } else {
122 std::cout << "[PASS] Execution of TestCaseLoopTest case Successful" <<std::endl;
123 }
124 }
125
TestCaseDumpService(std::shared_ptr<TestServiceClient> & testClient)126 void TestCaseDumpService(std::shared_ptr<TestServiceClient> &testClient)
127 {
128 bool ret = testClient->StartDumpService();
129 if (!ret) {
130 std::cout << "[FAILED] Execution of TestCaseDumpService case failed" <<std::endl;
131 } else {
132 std::cout << "[PASS] Execution of TestCaseDumpService case Successful" <<std::endl;
133 }
134 }
TestCaseEnableSerialInvokeFlag(std::shared_ptr<TestServiceClient> & testClient)135 void TestCaseEnableSerialInvokeFlag(std::shared_ptr<TestServiceClient> &testClient)
136 {
137 std::thread temp(ThreadFunc, testClient);
138 bool ret = testClient->TestEnableSerialInvokeFlag();
139 temp.join();
140 if (!ret) {
141 std::cout << "[FAILED] Execution of TestCaseEnableSerialInvokeFlag case failed" <<std::endl;
142 } else {
143 std::cout << "[PASS] Execution of TestCaseEnableSerialInvokeFlag case Successful" <<std::endl;
144 }
145 }
146
TestCaseNativeIPCSendRequests(std::shared_ptr<TestServiceClient> & testClient)147 void TestCaseNativeIPCSendRequests(std::shared_ptr<TestServiceClient> &testClient)
148 {
149 bool ret = testClient->TestRegisterRemoteStub();
150 if (!ret) {
151 std::cout << "[FAILED] Execution of TestRegisterRemoteStub case failed" <<std::endl;
152 return;
153 }
154 ret = testClient->TestNativeIPCSendRequests(1);
155 if (!ret) {
156 std::cout << "[FAILED] Execution of TestCaseNativeIPCSendRequests case failed" <<std::endl;
157 return;
158 }
159 std::cout << "[PASS] Execution of TestCaseNativeIPCSendRequests case Successful" <<std::endl;
160 }
161
TestCaseRegisterRemoteStub(std::shared_ptr<TestServiceClient> & testClient)162 void TestCaseRegisterRemoteStub(std::shared_ptr<TestServiceClient> &testClient)
163 {
164 bool ret = testClient->TestRegisterRemoteStub();
165 if (!ret) {
166 std::cout << "[FAILED] Execution of TestRegisterRemoteStub case failed" <<std::endl;
167 return;
168 }
169 gTestClient = testClient;
170 if (signal(SIGINT, SignalHandler) == SIG_ERR) {
171 ZLOGE(LABEL, "Failed to caught signal");
172 std::cout << "[FAILED] Execution of TestRegisterRemoteStub case failed" <<std::endl;
173 return;
174 }
175 std::cout << "[PASS] Execution of TestRegisterRemoteStub case Successful" <<std::endl;
176 }
177
TestCaseTooManyRequests(std::shared_ptr<TestServiceClient> & testClient)178 void TestCaseTooManyRequests(std::shared_ptr<TestServiceClient> &testClient)
179 {
180 bool ret = testClient->TestSendTooManyRequest();
181 if (!ret) {
182 std::cout << "[FAILED] Execution of TestCaseTooManyRequests case failed" <<std::endl;
183 }
184 ret = testClient->StartSyncTransaction();
185 if (!ret) {
186 std::cout << "[FAILED] Execution of TestCaseTooManyRequests case failed" <<std::endl;
187 return;
188 }
189 std::cout << "[PASS] Execution of TestCaseTooManyRequests case Successful" <<std::endl;
190 }
191
TestCaseMultiThreadSendRequest(std::shared_ptr<TestServiceClient> & testClient)192 void TestCaseMultiThreadSendRequest(std::shared_ptr<TestServiceClient> &testClient)
193 {
194 bool ret = testClient->TestMultiThreadSendRequest();
195 if (!ret) {
196 std::cout << "[FAILED] Execution of TestCaseMultiThreadSendRequest case failed" <<std::endl;
197 } else {
198 std::cout << "[PASS] Execution of TestCaseMultiThreadSendRequest case Successful" <<std::endl;
199 }
200 }
201
TestCaseQueryThreadInvocationState(std::shared_ptr<TestServiceClient> & testClient)202 void TestCaseQueryThreadInvocationState(std::shared_ptr<TestServiceClient> &testClient)
203 {
204 bool ret = testClient->TestQueryThreadInvocationState();
205 if (!ret) {
206 std::cout << "[FAILED] Execution of TestCaseQueryThreadInvocationState case failed" <<std::endl;
207 } else {
208 std::cout << "[PASS] Execution of TestCaseQueryThreadInvocationState case Successful" <<std::endl;
209 }
210 }
211
ExecuteAllTestCase()212 void ExecuteAllTestCase()
213 {
214 std::shared_ptr<TestServiceClient> testClient = std::make_shared<TestServiceClient>();
215 int ret = testClient->ConnectService();
216 if (!ret) {
217 ZLOGE(LABEL, "ConnectService failed");
218 return;
219 }
220 TestCaseQueryThreadInvocationState(testClient);
221 TestCaseSyncTrans(testClient);
222 TestCasePingService(testClient);
223 TestCaseGetFooService(testClient);
224 TestCaseGetFileDescriptor(testClient);
225 TestCaseLoopTest(testClient);
226 TestCaseDumpService(testClient);
227 TestCaseEnableSerialInvokeFlag(testClient);
228 TestCaseNativeIPCSendRequests(testClient);
229 TestCaseRegisterRemoteStub(testClient);
230 TestCaseTooManyRequests(testClient);
231 TestCaseMultiThreadSendRequest(testClient);
232 ZLOGI(LABEL, "All test cases have been executed");
233 }
234
main(int argc,char * argv[])235 int main(int argc, char *argv[])
236 {
237 if (fork() == 0) {
238 system("/system/bin/ipc_server_test");
239 return 0;
240 }
241
242 sleep(1);
243 std::cout << "Start executing the client" <<std::endl;
244 InitTokenId();
245 ExecuteAllTestCase();
246
247 // The non IPC context obtains one's own sid
248 std::string selfSid = IPCSkeleton::GetCallingSid();
249 ZLOGI(LABEL, "Get from service: sid = %{public}s", selfSid.c_str());
250 system("kill -9 $(pidof /system/bin/ipc_server_test)");
251
252 return 0;
253 }
254