From e32104210849260295f96f6dbd7395bc4e65a0cc Mon Sep 17 00:00:00 2001 From: lifansheng Date: Thu, 16 Sep 2021 11:14:16 +0800 Subject: [PATCH] add js_sys_module test Signed-off-by: lifansheng --- ohos.build | 1 + test/unittest/BUILD.gn | 65 ++++++++++++ test/unittest/test.h | 33 ++++++ test/unittest/test_process.cpp | 187 +++++++++++++++++++++++++++++++++ test/unittest/test_quickjs.cpp | 60 +++++++++++ 5 files changed, 346 insertions(+) create mode 100755 test/unittest/BUILD.gn create mode 100755 test/unittest/test.h create mode 100755 test/unittest/test_process.cpp create mode 100755 test/unittest/test_quickjs.cpp diff --git a/ohos.build b/ohos.build index 524bc01..54ab139 100644 --- a/ohos.build +++ b/ohos.build @@ -12,6 +12,7 @@ "inner_kits": [ ], "test_list": [ + "//base/compileruntime/js_sys_module/test/unittest:unittest" ] } } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn new file mode 100755 index 0000000..a0745e9 --- /dev/null +++ b/test/unittest/BUILD.gn @@ -0,0 +1,65 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +if (is_standard_system) { + module_output_path = "ace_engine_standard/napi" +} + +ohos_unittest("test_process_unittest") { + module_out_path = module_output_path + + include_dirs = [ + "//base/compileruntime/js_util_module/util", + "//base/compileruntime/js_sys_module/process", + "//foundation/ace/napi", + "//foundation/ace/napi/interfaces/kits", + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/native_engine/impl/quickjs", + "//third_party/icu/icu4c/source/common", + "//third_party/googletest/include", + "//third_party/node/src", + "//utils/native/base/include", + ] + + cflags = [ "-g3" ] + + sources = [ + "test_process.cpp", + "test_quickjs.cpp", + ] + + deps = [ + "//foundation/ace/napi/:ace_napi", + "//foundation/ace/napi/:ace_napi_quickjs", + "//third_party/googletest:gtest", + "//third_party/googletest:gtest_main", + "//third_party/libuv:uv_static", + "//third_party/quickjs:qjs", + "//third_party/icu/icu4c:static_icuuc", + "//utils/native/base:utils", + "//utils/native/base:utilsecurec", + "//base/compileruntime/js_sys_module/process:process_packages", + "//base/compileruntime/js_util_module/util:util_packages", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } +} + +group("unittest") { + testonly = true + deps = [ ":test_process_unittest" ] +} diff --git a/test/unittest/test.h b/test/unittest/test.h new file mode 100755 index 0000000..af07722 --- /dev/null +++ b/test/unittest/test.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H +#define FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H + +#include "native_engine.h" + +#include "gtest/gtest.h" + +class NativeEngineTest : public testing::Test { +public: + NativeEngineTest(); + virtual ~NativeEngineTest(); + void SetUp() override {} + void TearDown() override {} +protected: + NativeEngine* engine_; +}; + +#endif /* FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H */ \ No newline at end of file diff --git a/test/unittest/test_process.cpp b/test/unittest/test_process.cpp new file mode 100755 index 0000000..8a65b1f --- /dev/null +++ b/test/unittest/test_process.cpp @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "test.h" + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +#include "securec.h" +#include "utils/log.h" +#include "js_childprocess.h" +#include "js_process.h" + +#define ASSERT_CHECK_CALL(call) \ + { \ + ASSERT_EQ(call, napi_ok); \ + } + +#define ASSERT_CHECK_VALUE_TYPE(env, value, type) \ + { \ + napi_valuetype valueType = napi_undefined; \ + ASSERT_TRUE(value != nullptr); \ + ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \ + ASSERT_EQ(valueType, type); \ + } +static OHOS::Js_sys_module::Process::ChildProcess RunCommand(napi_env env, napi_value command, napi_value options) +{ + OHOS::Js_sys_module::Process::ChildProcess objectInfo(env); + + objectInfo.InitOptionsInfo(options); + + objectInfo.Spawn(command); + + return objectInfo; +} +/** + * @tc.name: ProcessUptimeTest001 + * @tc.desc: Test process Uptime. + * @tc.type: FUNC + */ +HWTEST_F(NativeEngineTest, ProcessUptimeTest001, testing::ext::TestSize.Level0) +{ + napi_env env = (napi_env)engine_; + OHOS::Js_sys_module::Process::Process process(env); + napi_value timeStart = process.Uptime(); + sleep(1); + napi_value timeEnd = process.Uptime(); + double start = 0; + double end = 0; + napi_get_value_double(env, timeStart, &start); + napi_get_value_double(env, timeEnd, &end); + ASSERT_EQ(end - start, 1); +} + +/** + * @tc.name: ProcessKillTest001 + * @tc.desc: Test process kill signal. + * @tc.type: FUNC + */ +HWTEST_F(NativeEngineTest, ProcessKillTest001, testing::ext::TestSize.Level0) +{ + napi_env env = (napi_env)engine_; + OHOS::Js_sys_module::Process::Process process(env); + + std::string command("ls; sleep 1"); + napi_value temp = nullptr; + napi_create_string_utf8(env, command.c_str(), command.length(), &temp); + + OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); + + napi_value pid = childprocess.Getpid(); + napi_value signal = nullptr; + napi_create_int32(env, 9, &signal); + napi_value result = process.Kill(pid, signal); + bool res = false; + napi_get_value_bool(env, result, &res); + ASSERT_TRUE(res); +} + +/** + * @tc.name: ProcessKillTest002 + * @tc.desc: Test process kill signal. + * @tc.type: FUNC + */ +HWTEST_F(NativeEngineTest, ProcessKillTest002, testing::ext::TestSize.Level0) +{ + napi_env env = (napi_env)engine_; + OHOS::Js_sys_module::Process::Process process(env); + + std::string command("ls; sleep 1"); + napi_value temp = nullptr; + napi_create_string_utf8(env, command.c_str(), command.length(), &temp); + + OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); + + napi_value pid = childprocess.Getpid(); + napi_value signal = nullptr; + napi_create_int32(env, 999, &signal); + napi_value result = process.Kill(pid, signal); + bool res = false; + napi_get_value_bool(env, result, &res); + ASSERT_FALSE(res); +} + +/** + * @tc.name: ProcessRunCmdTest001 + * @tc.desc: Test process RunCmd fork process. + * @tc.type: FUNC + */ +HWTEST_F(NativeEngineTest, ProcessRunCmdTest001, testing::ext::TestSize.Level0) +{ + napi_env env = (napi_env)engine_; + OHOS::Js_sys_module::Process::Process process(env); + + std::string command("each abc"); + napi_value temp = nullptr; + napi_create_string_utf8(env, command.c_str(), command.length(), &temp); + + OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); + + napi_value output = childprocess.GetOutput(); + bool res = false; + napi_is_promise(env, output, &res); + ASSERT_TRUE(res); +} + +/** + * @tc.name: ProcessRunCmdTest002 + * @tc.desc: Test process RunCmd fork process. + * @tc.type: FUNC + */ +HWTEST_F(NativeEngineTest, ProcessRunCmdTest002, testing::ext::TestSize.Level0) +{ + napi_env env = (napi_env)engine_; + OHOS::Js_sys_module::Process::Process process(env); + + std::string command("mkdir test.txt"); + napi_value temp = nullptr; + napi_create_string_utf8(env, command.c_str(), command.length(), &temp); + + OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); + + napi_value errorOutput = childprocess.GetErrorOutput(); + bool res = false; + napi_is_promise(env, errorOutput, &res); + ASSERT_TRUE(res); +} + +/** + * @tc.name: ChildProcessKillTest001 + * @tc.desc: Test childProces kill signal. + * @tc.type: FUNC + */ +HWTEST_F(NativeEngineTest, ChildProcessKillTest001, testing::ext::TestSize.Level0) +{ + napi_env env = (napi_env)engine_; + OHOS::Js_sys_module::Process::Process process(env); + + std::string command("sleep 1; ls;"); + napi_value temp = nullptr; + napi_create_string_utf8(env, command.c_str(), command.length(), &temp); + + OHOS::Js_sys_module::Process::ChildProcess childprocess = RunCommand(env, temp, nullptr); + + napi_value signal = nullptr; + napi_create_int32(env, 9, &signal); + childprocess.Kill(signal); + childprocess.Wait(); + napi_value result = childprocess.GetExitCode(); + + int32_t res = 0; + napi_get_value_int32(env, result, &res); + ASSERT_EQ(static_cast(res), 9); +} \ No newline at end of file diff --git a/test/unittest/test_quickjs.cpp b/test/unittest/test_quickjs.cpp new file mode 100755 index 0000000..22fca43 --- /dev/null +++ b/test/unittest/test_quickjs.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "test.h" + +#include "quickjs_native_engine.h" + +static NativeEngine* g_nativeEngine = nullptr; + +NativeEngineTest::NativeEngineTest() +{ + engine_ = g_nativeEngine; +} + +NativeEngineTest::~NativeEngineTest() {} + +int main(int argc, char** argv) +{ + testing::GTEST_FLAG(output) = "xml:./"; + testing::InitGoogleTest(&argc, argv); + + JSRuntime* rt = JS_NewRuntime(); + if (rt == nullptr) { + return 0; + } + + JSContext* ctx = JS_NewContext(rt); + if (ctx == nullptr) { + return 0; + } + + js_std_add_helpers(ctx, 0, nullptr); + + g_nativeEngine = new QuickJSNativeEngine(rt, ctx); + + int ret = RUN_ALL_TESTS(); + + g_nativeEngine->Loop(LOOP_DEFAULT); + + delete g_nativeEngine; + g_nativeEngine = nullptr; + + js_std_free_handlers(rt); + JS_FreeContext(ctx); + JS_FreeRuntime(rt); + + return ret; +}