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 "ohos_rpc_test_testhelper.h"
17 #include <codecvt>
18 #include "ipc_debug.h"
19 #include "jni_helper.h"
20 #include "ipc_test_helper.h"
21 #include "log_tags.h"
22
23 using namespace OHOS;
24 using namespace OHOS::HiviewDFX;
25 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCTestHelperJNI" };
26
27 static bool g_isTestHelperMethodRegistered = false;
28 static struct ParcelDesc {
29 jclass klass;
30 jfieldID fieldNativeInstance;
31 } g_jTestHelper;
32
33 /*
34 * Class: ohos_rpc_test_TestHelper
35 * Method: nativeNewInstance
36 * Signature: ()J
37 */
Java_ohos_rpc_test_TestHelper_nativeNewInstance(JNIEnv * env,jclass classz)38 JNIEXPORT jlong JNICALL Java_ohos_rpc_test_TestHelper_nativeNewInstance(
39 JNIEnv *env, jclass classz)
40 {
41 IPCTestHelper *helper = new IPCTestHelper();
42 return (jlong)helper;
43 }
44
45 /*
46 * Class: ohos_rpc_test_TestHelper
47 * Method: nativeFreeInstance
48 * Signature: (J)V
49 */
Java_ohos_rpc_test_TestHelper_nativeFreeInstance(JNIEnv * env,jobject object,jlong instance)50 JNIEXPORT void JNICALL Java_ohos_rpc_test_TestHelper_nativeFreeInstance(
51 JNIEnv *env, jobject object, jlong instance)
52 {
53 IPCTestHelper *nativeHolder = reinterpret_cast<IPCTestHelper *>(instance);
54 delete nativeHolder;
55 }
56
57 /*
58 * Class: ohos_rpc_test_TestHelper
59 * Method: nativePrepareTestSuite
60 * Signature: ()Z
61 */
Java_ohos_rpc_test_TestHelper_nativePrepareTestSuite(JNIEnv * env,jobject object)62 JNIEXPORT jboolean JNICALL Java_ohos_rpc_test_TestHelper_nativePrepareTestSuite(
63 JNIEnv *env, jobject object)
64 {
65 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
66 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
67
68 if (helper != nullptr) {
69 return (jboolean)helper->PrepareTestSuite();
70 }
71
72 return JNI_FALSE;
73 }
74
75 /*
76 * Class: ohos_rpc_test_TestHelper
77 * Method: nativeTearDownTestSuite
78 * Signature: ()Z
79 */
Java_ohos_rpc_test_TestHelper_nativeTearDownTestSuite(JNIEnv * env,jobject object)80 JNIEXPORT jboolean JNICALL Java_ohos_rpc_test_TestHelper_nativeTearDownTestSuite(
81 JNIEnv *env, jobject object)
82 {
83 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
84 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
85
86 if (helper != nullptr) {
87 return (jboolean)helper->TearDownTestSuite();
88 }
89
90 return JNI_FALSE;
91 }
92
93 /*
94 * Class: ohos_rpc_test_TestHelper
95 * Method: nativeStartTestServer
96 * Signature: (I)Z
97 */
Java_ohos_rpc_test_TestHelper_nativeStartTestApp(JNIEnv * env,jobject object,jint appId,jint commandId)98 JNIEXPORT jboolean JNICALL Java_ohos_rpc_test_TestHelper_nativeStartTestApp(
99 JNIEnv *env, jobject object, jint appId, jint commandId)
100 {
101 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
102 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
103
104 if (helper == nullptr) {
105 return JNI_FALSE;
106 }
107
108 return (jboolean)helper->StartTestApp(appId, commandId);
109 }
110
111 /*
112 * Class: ohos_rpc_test_TestHelper
113 * Method: StopTestApp
114 * Signature: (I)Z
115 */
Java_ohos_rpc_test_TestHelper_nativeStopTestApp(JNIEnv * env,jobject object,jlong appId)116 JNIEXPORT jboolean JNICALL Java_ohos_rpc_test_TestHelper_nativeStopTestApp(
117 JNIEnv *env, jobject object, jlong appId)
118 {
119 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
120 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
121
122 if (helper != nullptr) {
123 return (jboolean)helper->StopTestApp(appId);
124 }
125
126 return JNI_FALSE;
127 }
128
129 /*
130 * Class: ohos_rpc_test_TestHelper
131 * Method: nativeGetTestServerPid
132 * Signature: (I)I
133 */
Java_ohos_rpc_test_TestHelper_nativeGetTestAppPid(JNIEnv * env,jobject object,jint appId)134 JNIEXPORT jint JNICALL Java_ohos_rpc_test_TestHelper_nativeGetTestAppPid(
135 JNIEnv *env, jobject object, jint appId)
136 {
137 jint pid = 0;
138 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
139 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
140
141 if (helper != nullptr) {
142 pid = (jint)helper->GetTestAppPid(appId);
143 }
144
145 ZLOGD(LABEL, "nativeGetTestAppPid:%d", pid);
146 return pid;
147 }
148
149 /*
150 * Class: ohos_rpc_test_TestHelper
151 * Method: nativeGetUid
152 * Signature: ()I
153 */
Java_ohos_rpc_test_TestHelper_nativeGetUid(JNIEnv * env,jobject object)154 JNIEXPORT jint JNICALL Java_ohos_rpc_test_TestHelper_nativeGetUid(
155 JNIEnv *env, jobject object)
156 {
157 ZLOGE(LABEL, "Java_ohos_rpc_test_TestHelper_nativeGetUid");
158
159 jint uid = 0;
160 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
161 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
162
163 if (helper != nullptr) {
164 uid = static_cast<jint>(helper->GetUid());
165 }
166
167 return uid;
168 }
169
170 /*
171 * Class: ohos_rpc_test_TestHelper
172 * Method: nativeGetPid
173 * Signature: ()I
174 */
Java_ohos_rpc_test_TestHelper_nativeGetPid(JNIEnv * env,jobject object)175 JNIEXPORT jint JNICALL Java_ohos_rpc_test_TestHelper_nativeGetPid(
176 JNIEnv *env, jobject object)
177 {
178 jint pid = 0;
179 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
180 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
181
182 if (helper != nullptr) {
183 pid = static_cast<jint>(helper->GetPid());
184 }
185
186 return pid;
187 }
188
189 /*
190 * Class: ohos.rpc.test.TestHelper
191 * Method: nativeStartExecutable
192 * Signature: (Ljava/lang/String;I)Z
193 */
Java_ohos_rpc_test_TestHelper_nativeStartExecutable(JNIEnv * env,jobject object,jstring string,jint length)194 JNIEXPORT jboolean JNICALL Java_ohos_rpc_test_TestHelper_nativeStartExecutable(
195 JNIEnv *env, jobject object, jstring string, jint length)
196 {
197 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
198 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
199
200 const char *utfString = env->GetStringUTFChars(string, 0);
201
202 if (utfString != nullptr) {
203 std::string exectubeFile = std::string(utfString, length);
204 ZLOGD(LABEL, "StartExecutable:%s", exectubeFile.c_str());
205 helper->StartExecutable(exectubeFile);
206 env->ReleaseStringUTFChars(string, utfString);
207 return JNI_TRUE;
208 }
209
210 return JNI_FALSE;
211 }
212
213 /*
214 * Class: ohos.rpc.test.TestHelper
215 * Method: nativeStopExecutable
216 * Signature: (Ljava/lang/String;I)Z
217 */
Java_ohos_rpc_test_TestHelper_nativeStopExecutable(JNIEnv * env,jobject object,jstring string,jint length)218 JNIEXPORT jboolean JNICALL Java_ohos_rpc_test_TestHelper_nativeStopExecutable(
219 JNIEnv *env, jobject object, jstring string, jint length)
220 {
221 IPCTestHelper *helper = reinterpret_cast<IPCTestHelper *>(
222 env->GetLongField(object, g_jTestHelper.fieldNativeInstance));
223
224 const char *utfString = env->GetStringUTFChars(string, 0);
225
226 if (utfString != nullptr) {
227 std::string exectubeFile = std::string(utfString, length);
228 ZLOGD(LABEL, "StopExecutable:%s", exectubeFile.c_str());
229 helper->StopExecutable(exectubeFile);
230 env->ReleaseStringUTFChars(string, utfString);
231 return JNI_TRUE;
232 }
233
234 return JNI_FALSE;
235 }
236
237 /*
238 * Class: ohos_rpc_test_TestHelper
239 * Method: nativeRunCommand
240 * Signature: (Ljava/lang/String;I)Z
241 */
Java_ohos_rpc_test_TestHelper_nativeRunCommand(JNIEnv * env,jobject object,jstring string,jint length)242 JNIEXPORT jboolean JNICALL Java_ohos_rpc_test_TestHelper_nativeRunCommand(
243 JNIEnv *env, jobject object, jstring string, jint length)
244 {
245 const char *utfString = env->GetStringUTFChars(string, 0);
246
247 if (utfString != nullptr) {
248 std::string shellCommand = std::string(utfString, length);
249 ZLOGD(LABEL, "StartExecutable:%s", shellCommand.c_str());
250 system(shellCommand.c_str());
251 env->ReleaseStringUTFChars(string, utfString);
252 return JNI_TRUE;
253 }
254
255 return JNI_FALSE;
256 }
257
JTestHelpertRegisterNativeMethods(JNIEnv * env)258 int JTestHelpertRegisterNativeMethods(JNIEnv *env)
259 {
260 jclass clazz;
261 clazz = env->FindClass("ohos/rpc/test/TestHelper");
262 if (clazz == nullptr) {
263 ZLOGD(LABEL, "Could not find class:TestHelper");
264 return -1;
265 }
266
267 g_jTestHelper.klass = (jclass)env->NewGlobalRef(clazz);
268 g_jTestHelper.fieldNativeInstance = env->GetFieldID(clazz, "mNativeInstance", "J");
269
270 if (g_jTestHelper.fieldNativeInstance == nullptr) {
271 ZLOGE(LABEL, "TestHelper get field mNativeInstance failed");
272 return -1;
273 }
274
275 ZLOGD(LABEL, "TestHelper Register Native Methods success\n");
276 return 0;
277 }
278
JNI_OnLoad(JavaVM * vm,void * reserved)279 jint JNI_OnLoad(JavaVM *vm, void *reserved)
280 {
281 if (vm == nullptr) {
282 return -1;
283 }
284 if (g_isTestHelperMethodRegistered) {
285 return JNI_VERSION_1_4;
286 }
287
288 JNIEnv *env = nullptr;
289
290 if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_4) != JNI_OK) {
291 return -1;
292 }
293
294 if (JTestHelpertRegisterNativeMethods(env) < 0) {
295 return -1;
296 }
297
298 ZLOGD(LABEL, "JNI_OnLoad success\n");
299 g_isTestHelperMethodRegistered = true;
300 return JNI_VERSION_1_4;
301 }
302