From e0b2aeeccc3cea8035a296626035c0598b1fa592 Mon Sep 17 00:00:00 2001 From: maosiping Date: Thu, 20 Jan 2022 15:13:36 +0800 Subject: [PATCH] add unittest Signed-off-by: maosiping --- CMakeLists.txt | 3 +- test/napi/http/test_napi_exec.cpp | 30 +- test/napi/log/test_hilog.cpp | 15 +- test/utils/napi_utils/BUILD.gn | 59 +++ test/utils/napi_utils/CMakeLists.txt | 32 ++ test/utils/napi_utils/test_napi_utils.cpp | 443 ++++++++++++++++++++++ 6 files changed, 559 insertions(+), 23 deletions(-) create mode 100644 test/utils/napi_utils/BUILD.gn create mode 100644 test/utils/napi_utils/CMakeLists.txt create mode 100644 test/utils/napi_utils/test_napi_utils.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 147cca212..f03becaa7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,4 +59,5 @@ link_directories(cmake-build-debug/utils) add_subdirectory(frameworks/js/builtin) add_subdirectory(utils) add_subdirectory(test/napi/http) -add_subdirectory(test/napi/socket) \ No newline at end of file +add_subdirectory(test/napi/socket) +add_subdirectory(test/utils/napi_utils) \ No newline at end of file diff --git a/test/napi/http/test_napi_exec.cpp b/test/napi/http/test_napi_exec.cpp index b90eb9b9b..97f7cfbe0 100644 --- a/test/napi/http/test_napi_exec.cpp +++ b/test/napi/http/test_napi_exec.cpp @@ -287,7 +287,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueUrlHasPara, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -315,7 +315,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueUrlHasParaNoEncode, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -343,7 +343,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataString, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -351,7 +351,7 @@ DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataString, true) NapiUtils::SetStringPropertyUtf8(env, header, "no-use", "no use header"); NapiUtils::SetStringPropertyUtf8(env, header, "just-test", "just test header"); - napi_value extraData = NapiUtils::CreateStringUtf8(env, "this-is-my-get-string=mao&this-test=age"); + napi_value extraData = NapiUtils::CreateStringUtf8(env, "this-is-my-get-string=test&this-test=age"); NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_HEADER), header); NapiUtils::SetNamedProperty(env, options, std::string(HttpConstant::PARAM_KEY_EXTRA_DATA), extraData); @@ -369,7 +369,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetNoExtraData, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -392,7 +392,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNullString, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -418,7 +418,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderByDefaultHasPara, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -441,7 +441,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderByDefault, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value args[2] = {urlValue, callbackTwoParam}; @@ -456,7 +456,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodPostExtraDataString, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -486,7 +486,7 @@ DEFINE_TEST_BEGIN(TestHttpModuleMethodAndHeaderFail, true) { napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP ":7878" - "?name=Mao"); + "?name=test"); napi_value args[2] = {urlValue, callbackTwoParam}; @@ -501,7 +501,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodPostExtraDataArrayBuffer, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -540,7 +540,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetExtraDataNameValueHeaderKeyMultiCase, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value options = NapiUtils::CreateObject(env); @@ -564,7 +564,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOnce, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value args[2] = {urlValue, callbackTwoParam}; @@ -581,7 +581,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOnOn, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value args[2] = {urlValue, callbackTwoParam}; @@ -598,7 +598,7 @@ DEFINE_TEST_END DEFINE_TEST_BEGIN(TestHttpModuleMethodGetOnHeaderReceiveOff, true) { - napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=Mao"); + napi_value urlValue = NapiUtils::CreateStringUtf8(env, "https://" SERVER_IP "?name=test"); napi_value args[2] = {urlValue, callbackTwoParam}; diff --git a/test/napi/log/test_hilog.cpp b/test/napi/log/test_hilog.cpp index 12c8fa3af..83b45ad38 100644 --- a/test/napi/log/test_hilog.cpp +++ b/test/napi/log/test_hilog.cpp @@ -13,10 +13,11 @@ * limitations under the License. */ +#include + #include "hilog/log.h" #include "netstack_log.h" - -#include +#include "securec.h" namespace OHOS::HiviewDFX { static constexpr uint32_t MAX_BUFFER_SIZE = 4096; @@ -53,7 +54,7 @@ int HiLog::Debug(const HiLogLabel &label, const char *fmt, ...) if (label.domain != NETSTACK_LOG_DOMAIN) { return 0; } - (void)PRINT_LOG(Debug); + PRINT_LOG(Debug); return 0; } int HiLog::Info(const HiLogLabel &label, const char *fmt, ...) @@ -61,7 +62,7 @@ int HiLog::Info(const HiLogLabel &label, const char *fmt, ...) if (label.domain != NETSTACK_LOG_DOMAIN) { return 0; } - (void)PRINT_LOG(Info); + PRINT_LOG(Info); return 0; } int HiLog::Warn(const HiLogLabel &label, const char *fmt, ...) @@ -69,7 +70,7 @@ int HiLog::Warn(const HiLogLabel &label, const char *fmt, ...) if (label.domain != NETSTACK_LOG_DOMAIN) { return 0; } - (void)PRINT_LOG(Warn); + PRINT_LOG(Warn); return 0; } int HiLog::Error(const HiLogLabel &label, const char *fmt, ...) @@ -77,7 +78,7 @@ int HiLog::Error(const HiLogLabel &label, const char *fmt, ...) if (label.domain != NETSTACK_LOG_DOMAIN) { return 0; } - (void)PRINT_LOG(Error); + PRINT_LOG(Error); return 0; } int HiLog::Fatal(const HiLogLabel &label, const char *fmt, ...) @@ -85,7 +86,7 @@ int HiLog::Fatal(const HiLogLabel &label, const char *fmt, ...) if (label.domain != NETSTACK_LOG_DOMAIN) { return 0; } - (void)PRINT_LOG(Fatal); + PRINT_LOG(Fatal); return 0; } } // namespace OHOS::HiviewDFX \ No newline at end of file diff --git a/test/utils/napi_utils/BUILD.gn b/test/utils/napi_utils/BUILD.gn new file mode 100644 index 000000000..15d42b785 --- /dev/null +++ b/test/utils/napi_utils/BUILD.gn @@ -0,0 +1,59 @@ +# Copyright (C) 2022 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") + +group("unittest") { + testonly = true + deps = [":netstack_napi_utils_unittest"] +} + +SUBSYSTEM_DIR = "//foundation/communication" + +utils_source = [ "$SUBSYSTEM_DIR/netstack/utils/napi_utils/src/netstack_napi_utils.cpp" ] + +utils_include = [ "$SUBSYSTEM_DIR/netstack/utils/napi_utils/include" ] + +common_include = [ + "//foundation/ace/ace_engine/frameworks/base/utils", + "//foundation/ace/napi/interfaces/kits", + "//foundation/ace/napi", + "//third_party/node/src", +] + +common_deps = [ + "//foundation/ace/napi/:ace_napi", + "//utils/native/base:utils", +] + +common_external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", +] + +ohos_unittest("netstack_napi_utils_unittest") { + install_enable = true + subsystem_name = "communication" + part_name = "netstack" + test_module = "netstack_napi_utils_unittest" + module_out_path = part_name + "/" + test_module + + sources = [ "test_napi_utils.cpp" ] + sources += utils_source + + include_dirs = common_include + + deps = common_deps + + external_deps = common_external_deps +} \ No newline at end of file diff --git a/test/utils/napi_utils/CMakeLists.txt b/test/utils/napi_utils/CMakeLists.txt new file mode 100644 index 000000000..4825e4560 --- /dev/null +++ b/test/utils/napi_utils/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (C) 2022 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. + +add_executable( + napi_utils_unittest + test_napi_utils.cpp + ../../napi/log/test_hilog.cpp +) + +target_link_libraries(napi_utils_unittest ace_napi_quickjs) +target_link_libraries(napi_utils_unittest quickjs) +target_link_libraries(napi_utils_unittest uv) +target_link_libraries(napi_utils_unittest securec) +target_link_libraries(napi_utils_unittest gtestd) +target_link_libraries(napi_utils_unittest gmockd) +target_link_libraries(napi_utils_unittest gtest_maind) +target_link_libraries(napi_utils_unittest gmock_maind) +target_link_libraries(napi_utils_unittest pthread) +target_link_libraries(napi_utils_unittest dl) +target_link_libraries(napi_utils_unittest netstack_utils) + +set(CMAKE_CXX_FLAGS -g) diff --git a/test/utils/napi_utils/test_napi_utils.cpp b/test/utils/napi_utils/test_napi_utils.cpp new file mode 100644 index 000000000..65a16d26f --- /dev/null +++ b/test/utils/napi_utils/test_napi_utils.cpp @@ -0,0 +1,443 @@ +/* + * Copyright (c) 2022 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 "napi/native_node_api.h" +#include "netstack_napi_utils.h" +#include "securec.h" +#include "test_common.h" + +namespace OHOS::NetStack { + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetValueType, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value undefined = NapiUtils::GetUndefined(env); + ASSERT_EQ(NapiUtils::GetValueType(env, undefined), napi_undefined); + + napi_value jsString = NapiUtils::CreateStringUtf8(env, "test-string"); + ASSERT_EQ(NapiUtils::GetValueType(env, jsString), napi_string); + + ASSERT_EQ(NapiUtils::GetValueType(env, nullptr), napi_undefined); + ASSERT_EQ(NapiUtils::GetValueType(env, NapiUtils::CreateObject(env)), napi_object); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestHasNamedProperty, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + ASSERT_FALSE(NapiUtils::HasNamedProperty(env, obj, "test")); + + NapiUtils::SetNamedProperty(env, obj, "test", NapiUtils::CreateObject(env)); + ASSERT_TRUE(NapiUtils::HasNamedProperty(env, obj, "test")); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetNamedProperty, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + NapiUtils::SetNamedProperty(env, obj, "test", NapiUtils::CreateObject(env)); + napi_value innerObj = NapiUtils::GetNamedProperty(env, obj, "test"); + ASSERT_CHECK_VALUE_TYPE(env, innerObj, napi_object); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestSetNamedProperty, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + NapiUtils::SetNamedProperty(env, obj, "test", NapiUtils::CreateObject(env)); + napi_value innerObj = NapiUtils::GetNamedProperty(env, obj, "test"); + ASSERT_CHECK_VALUE_TYPE(env, innerObj, napi_object); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetPropertyNames, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + size_t num = 3; + napi_value obj = NapiUtils::CreateObject(env); + NapiUtils::SetNamedProperty(env, obj, "test1", NapiUtils::CreateObject(env)); + NapiUtils::SetNamedProperty(env, obj, "test2", NapiUtils::CreateObject(env)); + NapiUtils::SetNamedProperty(env, obj, "test3", NapiUtils::CreateObject(env)); + + auto names = NapiUtils::GetPropertyNames(env, obj); + ASSERT_EQ(names.size(), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestCreateUint32, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + int num = 100; + napi_value v = NapiUtils::CreateUint32(env, num); + ASSERT_EQ(NapiUtils::GetUint32FromValue(env, v), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetUint32FromValue, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + int num = 100; + napi_value v = NapiUtils::CreateUint32(env, num); + ASSERT_EQ(NapiUtils::GetUint32FromValue(env, v), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetUint32Property, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + int num = 100; + NapiUtils::SetUint32Property(env, obj, "test", num); + ASSERT_EQ(NapiUtils::GetUint32Property(env, obj, "test"), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestSetUint32Property, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + int num = 100; + NapiUtils::SetUint32Property(env, obj, "test", num); + ASSERT_EQ(NapiUtils::GetUint32Property(env, obj, "test"), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestCreateInt32, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + int num = 100; + napi_value v = NapiUtils::CreateInt32(env, num); + ASSERT_EQ(NapiUtils::GetInt32FromValue(env, v), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetInt32FromValue, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + int num = 100; + napi_value v = NapiUtils::CreateInt32(env, num); + ASSERT_EQ(NapiUtils::GetInt32FromValue(env, v), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetInt32Property, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + int num = 100; + NapiUtils::SetInt32Property(env, obj, "test", num); + ASSERT_EQ(NapiUtils::GetInt32Property(env, obj, "test"), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestSetInt32Property, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + int num = 100; + NapiUtils::SetInt32Property(env, obj, "test", num); + ASSERT_EQ(NapiUtils::GetInt32Property(env, obj, "test"), num); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestCreateStringUtf8, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + std::string s = "test-string"; + napi_value v = NapiUtils::CreateStringUtf8(env, s); + ASSERT_EQ(NapiUtils::GetStringFromValueUtf8(env, v), s); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetStringFromValueUtf8, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + std::string s = "test-string"; + napi_value v = NapiUtils::CreateStringUtf8(env, s); + ASSERT_EQ(NapiUtils::GetStringFromValueUtf8(env, v), s); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetStringPropertyUtf8, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + std::string s = "test-string"; + NapiUtils::SetStringPropertyUtf8(env, obj, "test", s); + ASSERT_EQ(NapiUtils::GetStringPropertyUtf8(env, obj, "test"), s); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestSetStringPropertyUtf8, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + std::string s = "test-string"; + NapiUtils::SetStringPropertyUtf8(env, obj, "test", s); + ASSERT_EQ(NapiUtils::GetStringPropertyUtf8(env, obj, "test"), s); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestValueIsArrayBuffer, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + const char *s = "test-string"; + + void *data = nullptr; + size_t len = strlen(s); + + napi_value v = NapiUtils::CreateArrayBuffer(env, len, &data); + ASSERT_TRUE(NapiUtils::ValueIsArrayBuffer(env, v)); + + (void)memcpy_s(data, len, s, len); + + size_t tempLen = 0; + void *temp = NapiUtils::GetInfoFromArrayBufferValue(env, v, &tempLen); + ASSERT_EQ(tempLen, len); + int ret = memcmp(data, temp, len); + ASSERT_EQ(ret, 0); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetInfoFromArrayBufferValue, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + const char *s = "test-string"; + + void *data = nullptr; + size_t len = strlen(s); + + napi_value v = NapiUtils::CreateArrayBuffer(env, len, &data); + ASSERT_TRUE(NapiUtils::ValueIsArrayBuffer(env, v)); + + (void)memcpy_s(data, len, s, len); + + size_t tempLen = 0; + void *temp = NapiUtils::GetInfoFromArrayBufferValue(env, v, &tempLen); + ASSERT_EQ(tempLen, len); + int ret = memcmp(data, temp, len); + ASSERT_EQ(ret, 0); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestCreateArrayBuffer, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + const char *s = "test-string"; + + void *data = nullptr; + size_t len = strlen(s); + + napi_value v = NapiUtils::CreateArrayBuffer(env, len, &data); + ASSERT_TRUE(NapiUtils::ValueIsArrayBuffer(env, v)); + + (void)memcpy_s(data, len, s, len); + + size_t tempLen = 0; + void *temp = NapiUtils::GetInfoFromArrayBufferValue(env, v, &tempLen); + ASSERT_EQ(tempLen, len); + int ret = memcmp(data, temp, len); + ASSERT_EQ(ret, 0); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestCreateObject, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value undefined = NapiUtils::GetUndefined(env); + ASSERT_EQ(NapiUtils::GetValueType(env, undefined), napi_undefined); + + napi_value jsString = NapiUtils::CreateStringUtf8(env, "test-string"); + ASSERT_EQ(NapiUtils::GetValueType(env, jsString), napi_string); + + ASSERT_EQ(NapiUtils::GetValueType(env, nullptr), napi_undefined); + ASSERT_EQ(NapiUtils::GetValueType(env, NapiUtils::CreateObject(env)), napi_object); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetUndefined, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value undefined = NapiUtils::GetUndefined(env); + ASSERT_EQ(NapiUtils::GetValueType(env, undefined), napi_undefined); + + napi_value jsString = NapiUtils::CreateStringUtf8(env, "test-string"); + ASSERT_EQ(NapiUtils::GetValueType(env, jsString), napi_string); + + ASSERT_EQ(NapiUtils::GetValueType(env, nullptr), napi_undefined); + ASSERT_EQ(NapiUtils::GetValueType(env, NapiUtils::CreateObject(env)), napi_object); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestCallFunction, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + auto func = [](napi_env env, napi_callback_info info) -> napi_value { + return NapiUtils::CreateStringUtf8(env, "test-string"); + }; + + napi_value jsFunc = NapiUtils::CreateFunction(env, "test_func", func, nullptr); + napi_value ret = NapiUtils::CallFunction(env, nullptr, jsFunc, 0, nullptr); + ASSERT_EQ(NapiUtils::GetStringFromValueUtf8(env, ret), "test-string"); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestCreateFunction, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + auto func = [](napi_env env, napi_callback_info info) -> napi_value { + return NapiUtils::CreateStringUtf8(env, "test-string"); + }; + + napi_value jsFunc = NapiUtils::CreateFunction(env, "test_func", func, nullptr); + napi_value ret = NapiUtils::CallFunction(env, nullptr, jsFunc, 0, nullptr); + ASSERT_EQ(NapiUtils::GetStringFromValueUtf8(env, ret), "test-string"); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestCreateReference, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + auto func = [](napi_env env, napi_callback_info info) -> napi_value { + return NapiUtils::CreateStringUtf8(env, "test-string"); + }; + + napi_value jsFunc = NapiUtils::CreateFunction(env, "test_func", func, nullptr); + napi_ref ref = NapiUtils::CreateReference(env, jsFunc); + ASSERT_NE(ref, nullptr); + + napi_value v = NapiUtils::GetReference(env, ref); + ASSERT_NE(v, nullptr); + + NapiUtils::DeleteReference(env, ref); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetReference, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + auto func = [](napi_env env, napi_callback_info info) -> napi_value { + return NapiUtils::CreateStringUtf8(env, "test-string"); + }; + + napi_value jsFunc = NapiUtils::CreateFunction(env, "test_func", func, nullptr); + napi_ref ref = NapiUtils::CreateReference(env, jsFunc); + ASSERT_NE(ref, nullptr); + + napi_value v = NapiUtils::GetReference(env, ref); + ASSERT_NE(v, nullptr); + + NapiUtils::DeleteReference(env, ref); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestDeleteReference, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + auto func = [](napi_env env, napi_callback_info info) -> napi_value { + return NapiUtils::CreateStringUtf8(env, "test-string"); + }; + + napi_value jsFunc = NapiUtils::CreateFunction(env, "test_func", func, nullptr); + napi_ref ref = NapiUtils::CreateReference(env, jsFunc); + ASSERT_NE(ref, nullptr); + + napi_value v = NapiUtils::GetReference(env, ref); + ASSERT_NE(v, nullptr); + + NapiUtils::DeleteReference(env, ref); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestGetBooleanProperty, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + bool s = true; + NapiUtils::SetBooleanProperty(env, obj, "test", s); + ASSERT_EQ(NapiUtils::GetBooleanProperty(env, obj, "test"), s); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestSetBooleanProperty, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + napi_value obj = NapiUtils::CreateObject(env); + + bool s = true; + NapiUtils::SetBooleanProperty(env, obj, "test", s); + ASSERT_EQ(NapiUtils::GetBooleanProperty(env, obj, "test"), s); +} + +[[maybe_unused]] HWTEST_F(NativeEngineTest, TestDefineProperties, testing::ext::TestSize.Level0) /* NOLINT*/ +{ + auto env = (napi_env)engine_; + + int num = 10; + std::initializer_list properties = { + DECLARE_NAPI_STATIC_PROPERTY("test1", NapiUtils::CreateUint32(env, num)), + DECLARE_NAPI_STATIC_PROPERTY("test2", NapiUtils::CreateUint32(env, num)), + DECLARE_NAPI_STATIC_PROPERTY("test3", NapiUtils::CreateUint32(env, num)), + }; + + napi_value obj = NapiUtils::CreateObject(env); + NapiUtils::DefineProperties(env, obj, properties); + + ASSERT_EQ(NapiUtils::GetUint32Property(env, obj, "test1"), num); + ASSERT_EQ(NapiUtils::GetUint32Property(env, obj, "test2"), num); + ASSERT_EQ(NapiUtils::GetUint32Property(env, obj, "test3"), num); +} + +} // namespace OHOS::NetStack + +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, nullptr); // default instance id 0 + + napi_module_register(nullptr); + int ret = RUN_ALL_TESTS(); + (void)ret; + + return 0; +} \ No newline at end of file -- Gitee