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