1 /*
2 * Copyright (c) 2022 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 <ctime>
17 #include "test.h"
18
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21
22 #include "securec.h"
23 #include "js_childprocess.h"
24 #include "js_process.h"
25 #include "tools/log.h"
26
27 #define ASSERT_CHECK_CALL(call) \
28 { \
29 ASSERT_EQ(call, napi_ok); \
30 }
31
32 #define ASSERT_CHECK_VALUE_TYPE(env, value, type) \
33 { \
34 napi_valuetype valueType = napi_undefined; \
35 ASSERT_TRUE(value != nullptr); \
36 ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \
37 ASSERT_EQ(valueType, type); \
38 }
RunCommand(napi_env env,napi_value command,napi_value options)39 OHOS::JsSysModule::Process::ChildProcess* RunCommand(napi_env env, napi_value command, napi_value options)
40 {
41 OHOS::JsSysModule::Process::ChildProcess* objectInfo = nullptr;
42 objectInfo = new OHOS::JsSysModule::Process::ChildProcess();
43 objectInfo->InitOptionsInfo(env, options);
44
45 objectInfo->Spawn(env, command);
46 return objectInfo;
47 }
48 static std::string testStr = "";
Method(napi_env env,napi_callback_info info)49 napi_value Method(napi_env env, napi_callback_info info)
50 {
51 napi_value thisVar = nullptr;
52 size_t argc = 0;
53 napi_value args[6] = { 0 }; // 6:six args
54 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr));
55 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
56
57 napi_value name = args[0];
58 napi_value value = args[1];
59
60 std::string buffer1 = "";
61 size_t bufferSize1 = 0;
62 napi_get_value_string_utf8(env, name, nullptr, 0, &bufferSize1);
63 buffer1.reserve(bufferSize1 + 1);
64 buffer1.resize(bufferSize1);
65 napi_get_value_string_utf8(env, name, buffer1.data(), bufferSize1 + 1, &bufferSize1);
66
67 std::string buffer2 = "";
68 size_t bufferSize2 = 0;
69 napi_get_value_string_utf8(env, value, nullptr, 0, &bufferSize2);
70 buffer2.reserve(bufferSize2 + 1);
71 buffer2.resize(bufferSize2);
72 napi_get_value_string_utf8(env, value, buffer2.data(), bufferSize2 + 1, &bufferSize2);
73 testStr += buffer1 + buffer2;
74 napi_value result = nullptr;
75 napi_get_boolean(env, true, &result);
76 return result;
77 }
78 /**
79 * @tc.name: ProcessUptimeTest001
80 * @tc.desc: Test process Uptime.
81 * @tc.type: FUNC
82 */
83 HWTEST_F(NativeEngineTest, ProcessUptimeTest001, testing::ext::TestSize.Level0)
84 {
85 napi_env env = (napi_env)engine_;
86 OHOS::JsSysModule::Process::Process process;
87 napi_value timeStart = process.Uptime(env);
88 sleep(1);
89 napi_value timeEnd = process.Uptime(env);
90 double start = 0;
91 double end = 0;
92 napi_get_value_double(env, timeStart, &start);
93 napi_get_value_double(env, timeEnd, &end);
94 ASSERT_EQ(end - start, 1);
95 }
96
97 /**
98 * @tc.name: ProcessKillTest001
99 * @tc.desc: Test process kill signal.
100 * @tc.type: FUNC
101 */
102 HWTEST_F(NativeEngineTest, ProcessKillTest001, testing::ext::TestSize.Level0)
103 {
104 napi_env env = (napi_env)engine_;
105 OHOS::JsSysModule::Process::Process process;
106
107 std::string command("ls; sleep 1");
108 napi_value temp = nullptr;
109 napi_create_string_utf8(env, command.c_str(), command.length(), &temp);
110
111 OHOS::JsSysModule::Process::ChildProcess* childprocess = RunCommand(env, temp, nullptr);
112
113 napi_value pid = childprocess->Getpid(env);
114 childprocess->Close();
115 napi_value signal = nullptr;
116 napi_create_int32(env, 9, &signal);
117 napi_value result = process.Kill(env, pid, signal);
118 bool res = false;
119 napi_get_value_bool(env, result, &res);
120 ASSERT_FALSE(res);
121 }
122
123 /**
124 * @tc.name: ProcessKillTest002
125 * @tc.desc: Test process kill signal.
126 * @tc.type: FUNC
127 */
128 HWTEST_F(NativeEngineTest, ProcessKillTest002, testing::ext::TestSize.Level0)
129 {
130 napi_env env = (napi_env)engine_;
131 OHOS::JsSysModule::Process::Process process;
132
133 std::string command("ls; sleep 1");
134 napi_value temp = nullptr;
135 napi_create_string_utf8(env, command.c_str(), command.length(), &temp);
136
137 OHOS::JsSysModule::Process::ChildProcess* childprocess = RunCommand(env, temp, nullptr);
138
139 napi_value pid = childprocess->Getpid(env);
140 childprocess->Close();
141 napi_value signal = nullptr;
142 napi_create_int32(env, 999, &signal);
143 napi_value result = process.Kill(env, pid, signal);
144 bool res = false;
145 napi_get_value_bool(env, result, &res);
146 ASSERT_FALSE(res);
147 }
148
149 /**
150 * @tc.name: ProcessRunCmdTest001
151 * @tc.desc: Test process RunCmd fork process.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(NativeEngineTest, ProcessRunCmdTest001, testing::ext::TestSize.Level0)
155 {
156 napi_env env = (napi_env)engine_;
157 OHOS::JsSysModule::Process::Process process;
158
159 std::string command("each abc");
160 napi_value temp = nullptr;
161 napi_create_string_utf8(env, command.c_str(), command.length(), &temp);
162
163 OHOS::JsSysModule::Process::ChildProcess* childprocess = RunCommand(env, temp, nullptr);
164
165 napi_value output = childprocess->GetOutput(env);
166 childprocess->Close();
167 bool res = false;
168 napi_is_promise(env, output, &res);
169 ASSERT_TRUE(res);
170 }
171
172 /**
173 * @tc.name: ProcessRunCmdTest002
174 * @tc.desc: Test process RunCmd fork process.
175 * @tc.type: FUNC
176 */
177 HWTEST_F(NativeEngineTest, ProcessRunCmdTest002, testing::ext::TestSize.Level0)
178 {
179 napi_env env = (napi_env)engine_;
180 OHOS::JsSysModule::Process::Process process;
181
182 std::string command("mkdir test.txt");
183 napi_value temp = nullptr;
184 napi_create_string_utf8(env, command.c_str(), command.length(), &temp);
185
186 OHOS::JsSysModule::Process::ChildProcess* childprocess = RunCommand(env, temp, nullptr);
187
188 napi_value errorOutput = childprocess->GetErrorOutput(env);
189 childprocess->Close();
190 bool res = false;
191 napi_is_promise(env, errorOutput, &res);
192 ASSERT_TRUE(res);
193 }
194
195 /**
196 * @tc.name: ProcessGetUidTest001
197 * @tc.desc: Test process uid.
198 * @tc.type: FUNC
199 */
200 HWTEST_F(NativeEngineTest, ProcessGetUidTest001, testing::ext::TestSize.Level0)
201 {
202 napi_env env = (napi_env)engine_;
203 OHOS::JsSysModule::Process::Process process;
204 napi_value napiUid = process.GetUid(env);
205 int32_t uid = 0;
206 napi_get_value_int32(env, napiUid, &uid);
207 bool res = false;
208 if (uid >= 0) {
209 res = true;
210 }
211 ASSERT_TRUE(res);
212 }
213
214 /**
215 * @tc.name: ProcessGetGidTest001
216 * @tc.desc: Test process gid.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(NativeEngineTest, ProcessGetGidTest001, testing::ext::TestSize.Level0)
220 {
221 napi_env env = (napi_env)engine_;
222 OHOS::JsSysModule::Process::Process process;
223 napi_value napiGid = process.GetGid(env);
224 int32_t gid = 0;
225 napi_get_value_int32(env, napiGid, &gid);
226 bool res = false;
227 if (gid >= 0) {
228 res = true;
229 }
230 ASSERT_TRUE(res);
231 }
232
233 /**
234 * @tc.name: ProcessGetEUidTest001
235 * @tc.desc: Test process euid.
236 * @tc.type: FUNC
237 */
238 HWTEST_F(NativeEngineTest, ProcessGetEUidTest001, testing::ext::TestSize.Level0)
239 {
240 napi_env env = (napi_env)engine_;
241 OHOS::JsSysModule::Process::Process process;
242 napi_value napiEuid = process.GetEUid(env);
243 int32_t euid = 0;
244 napi_get_value_int32(env, napiEuid, &euid);
245 bool res = false;
246 if (euid >= 0) {
247 res = true;
248 }
249 ASSERT_TRUE(res);
250 }
251
252 /**
253 * @tc.name: ProcessGetEGidTest001
254 * @tc.desc: Test process egid.
255 * @tc.type: FUNC
256 */
257 HWTEST_F(NativeEngineTest, ProcessGetEGidTest001, testing::ext::TestSize.Level0)
258 {
259 napi_env env = (napi_env)engine_;
260 OHOS::JsSysModule::Process::Process process;
261 napi_value napiEgid = process.GetEGid(env);
262 int32_t egid = 0;
263 napi_get_value_int32(env, napiEgid, &egid);
264 bool res = false;
265 if (egid >= 0) {
266 res = true;
267 }
268 ASSERT_TRUE(res);
269 }
270
271 /**
272 * @tc.name: ProcessGetPidTest001
273 * @tc.desc: Test process pid.
274 * @tc.type: FUNC
275 */
276 HWTEST_F(NativeEngineTest, ProcessGetPidTest001, testing::ext::TestSize.Level0)
277 {
278 napi_env env = (napi_env)engine_;
279 OHOS::JsSysModule::Process::Process process;
280 napi_value napiPid = process.GetPid(env);
281 int32_t pid = 0;
282 napi_get_value_int32(env, napiPid, &pid);
283 bool res = false;
284 if (pid > 0) {
285 res = true;
286 }
287 ASSERT_TRUE(res);
288 }
289
290 /**
291 * @tc.name: ProcessGetPidTest001
292 * @tc.desc: Test process ppid.
293 * @tc.type: FUNC
294 */
295 HWTEST_F(NativeEngineTest, ProcessGetPpidTest001, testing::ext::TestSize.Level0)
296 {
297 napi_env env = (napi_env)engine_;
298 OHOS::JsSysModule::Process::Process process;
299 napi_value napiPpid = process.GetPpid(env);
300 int32_t ppid = 0;
301 napi_get_value_int32(env, napiPpid, &ppid);
302 bool res = false;
303 if (ppid > 0) {
304 res = true;
305 }
306 ASSERT_TRUE(res);
307 }
308
309 /**
310 * @tc.name:childProcessPpidTest001
311 * @tc.desc: test get the parent process ID.
312 * @tc.type: FUNC
313 */
314 HWTEST_F(NativeEngineTest, childProcessPpidTest001, testing::ext::TestSize.Level0)
315 {
316 napi_env env = (napi_env)engine_;
317 OHOS::JsSysModule::Process::Process process;
318
319 std::string command("ls; sleep 1s;");
320 napi_value temp = nullptr;
321 napi_create_string_utf8(env, command.c_str(), command.length(), &temp);
322
323 OHOS::JsSysModule::Process::ChildProcess* childprocess = RunCommand(env, temp, nullptr);
324 napi_value result = childprocess->Getppid(env);
325 childprocess->Close();
326 int32_t ppid = 0;
327 napi_get_value_int32(env, result, &ppid);
328 bool res = false;
329 if (ppid >= 0) {
330 res = true;
331 }
332 ASSERT_TRUE(res);
333 }
334
335 /**
336 * @tc.name:childProcesspidTest001
337 * @tc.desc: test get the specific pid value.
338 * @tc.type: FUNC
339 */
340 HWTEST_F(NativeEngineTest, childProcesspidTest001, testing::ext::TestSize.Level0)
341 {
342 napi_env env = (napi_env)engine_;
343 OHOS::JsSysModule::Process::Process process;
344
345 std::string command("ls; sleep 1s;");
346 napi_value temp = nullptr;
347 napi_create_string_utf8(env, command.c_str(), command.length(), &temp);
348
349 OHOS::JsSysModule::Process::ChildProcess* childprocess = RunCommand(env, temp, nullptr);
350 napi_value result = childprocess->Getpid(env);
351 childprocess->Close();
352 int32_t pid = 0;
353 napi_get_value_int32(env, result, &pid);
354 bool res = false;
355 if (pid >= 0) {
356 res = true;
357 }
358 ASSERT_TRUE(res);
359 }
360
361 /**
362 * @tc.name: ProcessGetGroupsTest001
363 * @tc.desc: Test process groups.
364 * @tc.type: FUNC
365 */
366 HWTEST_F(NativeEngineTest, ProcessGetGroupsTest001, testing::ext::TestSize.Level0)
367 {
368 napi_env env = (napi_env)engine_;
369 OHOS::JsSysModule::Process::Process process;
370 napi_value element = nullptr;
371 napi_value groups = process.GetGroups(env);
372 napi_get_element(env, groups, 1, &element);
373 int32_t indexOne = 0;
374 napi_get_value_int32(env, element, &indexOne);
375 bool res = false;
376 if (indexOne >= 0) {
377 res = true;
378 }
379 ASSERT_TRUE(res);
380 }
381
382 /**
383 * @tc.name: ProcessChdirTest001
384 * @tc.desc: Test process gid.
385 * @tc.type: FUNC
386 */
387 HWTEST_F(NativeEngineTest, ProcessChdirTest001, testing::ext::TestSize.Level0)
388 {
389 napi_env env = (napi_env)engine_;
390 OHOS::JsSysModule::Process::Process process;
391 std::string catalogue = "/system/lib";
392 napi_value temp = nullptr;
393 napi_create_string_utf8(env, catalogue.c_str(), catalogue.length(), &temp);
394 process.Chdir(env, temp);
395 napi_value cwd = process.Cwd(env);
396 size_t bufferSize = 0;
397 if (napi_get_value_string_utf8(env, cwd, nullptr, 0, &bufferSize) != napi_ok) {
398 HILOG_ERROR("can not get str size");
399 }
400 std::string result = "";
401 result.reserve(bufferSize + 1);
402 result.resize(bufferSize);
403 if (napi_get_value_string_utf8(env, cwd, result.data(), bufferSize + 1, &bufferSize) != napi_ok) {
404 HILOG_ERROR("can not get str value");
405 }
406 std::string tag = "";
407 tag = result;
408 bool res = false;
409 if (tag == catalogue) {
410 res = true;
411 }
412 ASSERT_TRUE(res);
413 }
414
415 /**
416 * @tc.name: ProcessOn001
417 * @tc.desc: Test process gid.
418 * @tc.type: FUNC
419 */
420 HWTEST_F(NativeEngineTest, ProcessOn001, testing::ext::TestSize.Level0)
421 {
422 napi_env env = (napi_env)engine_;
423 OHOS::JsSysModule::Process::Process process;
424 napi_value temp = nullptr;
425 std::string cbNameEvent = "add";
426 napi_create_string_utf8(env, cbNameEvent.c_str(), cbNameEvent.length(), &temp);
427 std::string cbName = "cbMethod";
428 napi_value cb = nullptr;
429 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &cb);
430 process.On(env, temp, cb);
431 napi_value convertResult = nullptr;
432 convertResult = process.Off(env, temp);
433 bool res = false;
434 napi_get_value_bool(env, convertResult, &res);
435 ASSERT_FALSE(res);
436 }
437
438 /**
439 * @tc.name: ProcessOn002
440 * @tc.desc: Test process gid.
441 * @tc.type: FUNC
442 */
443 HWTEST_F(NativeEngineTest, ProcessOn002, testing::ext::TestSize.Level0)
444 {
445 napi_env env = (napi_env)engine_;
446 OHOS::JsSysModule::Process::Process process;
447 napi_value temp = nullptr;
448 std::string cbNameEvent = "UnHandleRejection";
449 napi_create_string_utf8(env, cbNameEvent.c_str(), cbNameEvent.length(), &temp);
450 std::string cbName = "cbMethod";
451 napi_value cb = nullptr;
452 napi_create_function(env, cbName.c_str(), cbName.size(), Method, nullptr, &cb);
453 process.On(env, temp, cb);
454 napi_value convertResult = nullptr;
455 convertResult = process.Off(env, temp);
456 bool res = false;
457 napi_get_value_bool(env, convertResult, &res);
458 ASSERT_TRUE(res);
459 }
460
461 /**
462 * @tc.name: ProcessGetTid001
463 * @tc.desc: Test process gid.
464 * @tc.type: FUNC
465 */
466 HWTEST_F(NativeEngineTest, ProcessGetTid001, testing::ext::TestSize.Level0)
467 {
468 napi_env env = (napi_env)engine_;
469 OHOS::JsSysModule::Process::Process process;
470 napi_value napiTid = process.GetTid(env);
471 int32_t tid = 0;
472 napi_get_value_int32(env, napiTid, &tid);
473 bool res = false;
474 if (tid != 0) {
475 res = true;
476 }
477 ASSERT_TRUE(res);
478 }
479
480 /**
481 * @tc.name: ProcessIsIsolatedProcess001
482 * @tc.desc: test whether the process is isolated.
483 * @tc.type: FUNC
484 */
485 HWTEST_F(NativeEngineTest, ProcessIsolatedProcess001, testing::ext::TestSize.Level0)
486 {
487 napi_env env = (napi_env)engine_;
488 OHOS::JsSysModule::Process::Process process;
489 napi_value result = process.IsIsolatedProcess(env);
490 bool res = false;
491 napi_get_value_bool(env, result, &res);
492 if (res) {
493 ASSERT_TRUE(res);
494 } else {
495 ASSERT_FALSE(res);
496 }
497 }
498
499 /**
500 * @tc.name: ProcessIsAppUid001
501 * @tc.desc: test whether the process is AppUid.
502 * @tc.type: FUNC
503 */
504 HWTEST_F(NativeEngineTest, ProcessIsAppUid001, testing::ext::TestSize.Level0)
505 {
506 napi_env env = (napi_env)engine_;
507 OHOS::JsSysModule::Process::Process process;
508 napi_value uid = nullptr;
509 napi_create_int32(env, 9, &uid);
510 napi_value result = process.IsAppUid(env, uid);
511 bool res = false;
512 napi_get_value_bool(env, result, &res);
513 ASSERT_FALSE(res);
514 }
515
516 /**
517 * @tc.name: ProcessIs64Bit001
518 * @tc.desc: test the operating environment is 64-bit.
519 * @tc.type: FUNC
520 */
521 HWTEST_F(NativeEngineTest, ProcessIs64Bit001, testing::ext::TestSize.Level0)
522 {
523 napi_env env = (napi_env)engine_;
524 OHOS::JsSysModule::Process::Process process;
525 napi_value result = process.Is64Bit(env);
526 bool res = false;
527 napi_get_value_bool(env, result, &res);
528 if (res) {
529 ASSERT_TRUE(res);
530 } else {
531 ASSERT_FALSE(res);
532 }
533 }
534
535 /**
536 * @tc.name: ProcessGetEnvironmentVar001
537 * @tc.desc: test get the value corresponding to the environment variable.
538 * @tc.type: FUNC
539 */
540 HWTEST_F(NativeEngineTest, ProcessGetEnvironmentVar001, testing::ext::TestSize.Level0)
541 {
542 napi_env env = (napi_env)engine_;
543 OHOS::JsSysModule::Process::Process process;
544 napi_value temp = nullptr;
545 std::string envVar = "PATH";
546 napi_create_string_utf8(env, envVar.c_str(), envVar.length(), &temp);
547 napi_value result = process.GetEnvironmentVar(env, temp);
548 napi_valuetype valuetype;
549 napi_typeof(env, result, &valuetype);
550 bool res = false;
551 if (valuetype == napi_string) {
552 res = true;
553 }
554 napi_get_value_bool(env, result, &res);
555 ASSERT_TRUE(res);
556 }
557
558 /**
559 * @tc.name: ProcesGetUidForName001
560 * @tc.desc: test Get process uid by process name.
561 * @tc.type: FUNC
562 */
563 HWTEST_F(NativeEngineTest, ProcesGetUidForName001, testing::ext::TestSize.Level0)
564 {
565 napi_env env = (napi_env)engine_;
566 OHOS::JsSysModule::Process::Process process;
567 napi_value temp = nullptr;
568 std::string user = "root";
569 napi_create_string_utf8(env, user.c_str(), user.length(), &temp);
570 napi_value result = process.GetUidForName(env, temp);
571 int32_t num = 0;
572 napi_get_value_int32(env, result, &num);
573 bool res = false;
574 if (num >= 0) {
575 res = true;
576 }
577 napi_get_value_bool(env, result, &res);
578 ASSERT_TRUE(res);
579 }
580
581 /**
582 * @tc.name: ProcesGetUidForName002
583 * @tc.desc: test Get process uid by process name.
584 * @tc.type: FUNC
585 */
586 HWTEST_F(NativeEngineTest, ProcesGetUidForName002, testing::ext::TestSize.Level0)
587 {
588 napi_env env = (napi_env)engine_;
589 OHOS::JsSysModule::Process::Process process;
590 napi_value temp = nullptr;
591 std::string user = "1234";
592 napi_create_string_utf8(env, user.c_str(), user.length(), &temp);
593 napi_value result = process.GetUidForName(env, temp);
594 int32_t num = 0;
595 napi_get_value_int32(env, result, &num);
596 bool res = false;
597 if (num == -1) {
598 res = true;
599 }
600 napi_get_value_bool(env, result, &res);
601 ASSERT_TRUE(res);
602 }
603
604 /**
605 * @tc.name: ProcesGetThreadPriority001
606 * @tc.desc: test Get thread priority based on specified tid.
607 * @tc.type: FUNC
608 */
609 HWTEST_F(NativeEngineTest, ProcesGetThreadPriority001, testing::ext::TestSize.Level0)
610 {
611 napi_env env = (napi_env)engine_;
612 OHOS::JsSysModule::Process::Process process;
613 napi_value napiTid = process.GetTid(env);
614 napi_value result = process.GetThreadPriority(env, napiTid);
615 napi_valuetype valuetype;
616 napi_typeof(env, result, &valuetype);
617 bool res = false;
618 if (valuetype == napi_number) {
619 res = true;
620 }
621 ASSERT_TRUE(res);
622 }
623
624 /**
625 * @tc.name: ProcesGetGetStartRealtime001
626 * @tc.desc: test Get the real-time elapsed time from system startup to process startup.
627 * @tc.type: FUNC
628 */
629 HWTEST_F(NativeEngineTest, ProcesGetGetStartRealtime001, testing::ext::TestSize.Level0)
630 {
631 napi_env env = (napi_env)engine_;
632 OHOS::JsSysModule::Process::Process process;
633 napi_value result = process.GetStartRealtime(env);
634 double num = 0;
635 napi_get_value_double(env, result, &num);
636 bool res = false;
637 if (num == 0) {
638 res = true;
639 }
640 ASSERT_TRUE(res);
641 }
642
643 /**
644 * @tc.name: ProcesGetPastCputime001
645 * @tc.desc: test Get the CPU time from the process startup to the current time.
646 * @tc.type: FUNC
647 */
648 HWTEST_F(NativeEngineTest, ProcesGetPastCputime001, testing::ext::TestSize.Level0)
649 {
650 napi_env env = (napi_env)engine_;
651 OHOS::JsSysModule::Process::Process process;
652 napi_value result = process.GetPastCputime(env);
653 int32_t num = 0;
654 napi_get_value_int32(env, result, &num);
655 bool res = false;
656 if (num != 0) {
657 res = true;
658 }
659 napi_get_value_bool(env, result, &res);
660 ASSERT_TRUE(res);
661 }
662
663 /**
664 * @tc.name: ProcesGetSystemConfig001
665 * @tc.desc: test Get system configuration information.
666 * @tc.type: FUNC
667 */
668 HWTEST_F(NativeEngineTest, ProcesGetSystemConfig001, testing::ext::TestSize.Level0)
669 {
670 napi_env env = (napi_env)engine_;
671 OHOS::JsSysModule::Process::Process process;
672 napi_value config = nullptr;
673 napi_create_int32(env, _SC_NPROCESSORS_CONF, &config);
674 napi_value result = process.GetSystemConfig(env, config);
675 int32_t num = 0;
676 napi_get_value_int32(env, result, &num);
677 bool res = false;
678 if (num != 0) {
679 res = true;
680 }
681 napi_get_value_bool(env, result, &res);
682 ASSERT_TRUE(res);
683 }
684
685 /**
686 * @tc.name: ProcessCloseTest001
687 * @tc.desc: Close the target process.
688 * @tc.type: FUNC
689 */
690 HWTEST_F(NativeEngineTest, ProcessCloseTest001, testing::ext::TestSize.Level0)
691 {
692 napi_env env = (napi_env)engine_;
693 OHOS::JsSysModule::Process::Process process;
694
695 std::string command("ls; sleep 1");
696 napi_value temp = nullptr;
697 napi_create_string_utf8(env, command.c_str(), command.length(), &temp);
698
699 OHOS::JsSysModule::Process::ChildProcess* childprocess = RunCommand(env, temp, nullptr);
700 childprocess->Wait(env);
701 childprocess->Close();
702 napi_value exitCode = childprocess->GetExitCode(env);
703 int32_t num = 0;
704 napi_value result = nullptr;
705 napi_get_value_int32(env, exitCode, &num);
706 bool res = false;
707 if (num == 0) {
708 res = true;
709 }
710 napi_get_value_bool(env, result, &res);
711 ASSERT_TRUE(res);
712 }
713
714 /**
715 * @tc.name:childProcessKillTest001
716 * @tc.desc: Send a signal to process.
717 * @tc.type: FUNC
718 */
719 HWTEST_F(NativeEngineTest, childProcessKillTest001, testing::ext::TestSize.Level0)
720 {
721 napi_env env = (napi_env)engine_;
722 OHOS::JsSysModule::Process::Process process;
723
724 std::string command("ls; sleep 1s;");
725 napi_value temp = nullptr;
726 napi_create_string_utf8(env, command.c_str(), command.length(), &temp);
727
728 OHOS::JsSysModule::Process::ChildProcess* childprocess = RunCommand(env, temp, nullptr);
729 childprocess->Wait(env);
730 napi_value signo = nullptr;
731 napi_create_int32(env, 9, &signo);
732 childprocess->Kill(env, signo);
733 napi_value result = childprocess->GetKilled(env);
734 childprocess->Close();
735 bool res = false;
736 napi_get_value_bool(env, result, &res);
737 ASSERT_FALSE(res);
738 }