diff --git a/bundle.json b/bundle.json index 6c5ac79272ef08438e040807365d4fbf540a20a0..4b6baf440a71dd3e6847a877fca9602526306c61 100644 --- a/bundle.json +++ b/bundle.json @@ -199,7 +199,7 @@ } ], "test": [ - "//foundation/filemanagement/file_api/interfaces/test/unittest:unittest" + "//foundation/filemanagement/file_api/interfaces/test/unittest:file_api_unittest" ] } } diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 2901748d0a0b362cb12a791cb6acbfe9d22440b1..a295ea7879f51339123dcd9dda57130be15705e4 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -645,7 +645,6 @@ config("ani_config") { ] cflags = [ - "-fvisibility=hidden", "-fdata-sections", "-ffunction-sections", "-Oz", @@ -654,7 +653,6 @@ config("ani_config") { "-std=c++17", "-fvisibility-inlines-hidden", "-Oz", - "-Wno-unused-function", ] } @@ -800,18 +798,6 @@ ohos_shared_library("ani_fs_class") { "samgr:samgr_proxy", ] - cflags = [ - "-fvisibility=hidden", - "-fdata-sections", - "-ffunction-sections", - "-Oz", - ] - cflags_cc = [ - "-std=c++17", - "-fvisibility-inlines-hidden", - "-Oz", - "-Wno-unused-function", - ] branch_protector_ret = "pac_ret" sanitize = { integer_overflow = true @@ -872,16 +858,7 @@ ohos_shared_library("ani_hash_class") { "runtime_core:libarkruntime", ] use_exceptions = true - cflags = [ - "-fvisibility=hidden", - "-fdata-sections", - "-ffunction-sections", - "-Oz", - ] - cflags_cc = [ - "-fvisibility-inlines-hidden", - "-Oz", - ] + branch_protector_ret = "pac_ret" sanitize = { integer_overflow = true @@ -940,16 +917,7 @@ ohos_shared_library("ani_securitylabel_class") { "runtime_core:libarkruntime", ] use_exceptions = true - cflags = [ - "-fvisibility=hidden", - "-fdata-sections", - "-ffunction-sections", - "-Oz", - ] - cflags_cc = [ - "-fvisibility-inlines-hidden", - "-Oz", - ] + branch_protector_ret = "pac_ret" sanitize = { integer_overflow = true diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 95f924b844e04552b66b00271d787063419395ea..ec75efa7209dd0a4850f8c263fb581317bf9076f 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -11,7 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -group("unittest") { +group("file_api_unittest") { testonly = true deps = [ "class_file:class_file_test", diff --git a/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn b/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn index ea20a1cf9f7b571e107d8d534852829741ebf3bb..8d90e442fbd2857b7fe078d0d27d365af177b629 100644 --- a/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn +++ b/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2023 Huawei Device Co., Ltd. +# Copyright (C) 2023-2025 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 @@ -20,15 +20,21 @@ group("unittest") { } ohos_unittest("filemgmt_libn_test") { + testonly = true + module_out_path = "file_api/file_api" include_dirs = [ "./include" ] - sources = [ "./src/n_class_test.cpp" ] + sources = [ + # "./src/n_class_test.cpp", + "./src/napi_mock.cpp", + "./src/n_func_arg_test.cpp", + ] deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", - "//third_party/googletest:gtest_main", + "//third_party/googletest:gmock_main", ] external_deps = [ @@ -39,4 +45,10 @@ ohos_unittest("filemgmt_libn_test") { "ipc:ipc_core", "napi:ace_napi", ] + + cflags_cc = [ "-DENABLE_NAPI_MOCK" ] + + defines = [ + "private=public" + ] } diff --git a/interfaces/test/unittest/filemgmt_libn_test/include/n_class_test.h b/interfaces/test/unittest/filemgmt_libn_test/include/n_class_test.h deleted file mode 100644 index 200927e492b48befaf026e4d7582bff59335a6b6..0000000000000000000000000000000000000000 --- a/interfaces/test/unittest/filemgmt_libn_test/include/n_class_test.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2023 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 FILEMGMT_LIBN_TEST_H -#define FILEMGMT_LIBN_TEST_H - -#include - -namespace OHOS { -namespace Media { -class FilemangementLibnTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; -} -} -#endif \ No newline at end of file diff --git a/interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h b/interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..39eaaa8893fc570fe47e3639d5e7b863814da6aa --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2025 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 INTERFACES_TEST_UNITTEST_FILEMGMT_LIBN_TEST_INCLUDE_NAPI_MOCK_H +#define INTERFACES_TEST_UNITTEST_FILEMGMT_LIBN_TEST_INCLUDE_NAPI_MOCK_H + +#pragma once +#include +#include "n_napi.h" + +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { + +class INapiMock { +public: + virtual ~INapiMock() = default; + + virtual napi_status napi_define_class(napi_env env, const char *name, size_t length, napi_callback constructor, + void *data, size_t property_count, const napi_property_descriptor *properties, napi_value *result) = 0; + + virtual napi_status napi_create_reference( + napi_env env, napi_value value, uint32_t initial_refcount, napi_ref *result) = 0; + + virtual napi_status napi_add_env_cleanup_hook(napi_env env, void (*fun)(void *arg), void *arg) = 0; + + virtual napi_status napi_delete_reference(napi_env env, napi_ref ref) = 0; + + virtual napi_status napi_get_reference_value(napi_env env, napi_ref ref, napi_value *result) = 0; + + virtual napi_status napi_new_instance( + napi_env env, napi_value constructor, size_t argc, const napi_value *argv, napi_value *result) = 0; + + virtual napi_status napi_unwrap(napi_env env, napi_value js_object, void **result) = 0; + + virtual napi_status napi_wrap(napi_env env, napi_value js_object, void *native_object, napi_finalize finalize_cb, + void *finalize_hint, napi_ref *result) = 0; + + virtual napi_status napi_remove_wrap(napi_env env, napi_value js_object, void **result) = 0; + + virtual napi_status napi_get_cb_info(napi_env env, napi_callback_info cbinfo, size_t* argc, napi_value* argv, + napi_value* thisArg, void** data) = 0; +}; + +class NapiMock : public INapiMock { +public: + MOCK_METHOD(napi_status, napi_define_class, + (napi_env, const char *, size_t, napi_callback, void *, size_t, const napi_property_descriptor *, napi_value *), + (override)); + + MOCK_METHOD(napi_status, napi_create_reference, (napi_env, napi_value, uint32_t, napi_ref *), (override)); + + MOCK_METHOD(napi_status, napi_add_env_cleanup_hook, (napi_env env, void (*fun)(void *arg), void *arg), (override)); + + MOCK_METHOD(napi_status, napi_delete_reference, (napi_env env, napi_ref ref), (override)); + + MOCK_METHOD(napi_status, napi_get_reference_value, (napi_env env, napi_ref ref, napi_value *result), (override)); + + MOCK_METHOD(napi_status, napi_new_instance, + (napi_env env, napi_value constructor, size_t argc, const napi_value *argv, napi_value *result), (override)); + + MOCK_METHOD(napi_status, napi_unwrap, (napi_env env, napi_value js_object, void **result), (override)); + + MOCK_METHOD(napi_status, napi_wrap, + (napi_env env, napi_value js_object, void *native_object, napi_finalize finalize_cb, void *finalize_hint, + napi_ref *result), + (override)); + + MOCK_METHOD(napi_status, napi_remove_wrap, (napi_env env, napi_value js_object, void **result), (override)); + + MOCK_METHOD(napi_status, napi_get_cb_info, (napi_env env, napi_callback_info cbinfo, size_t* argc, napi_value* argv, + napi_value* thisArg, void** data), (override)); +}; + +NapiMock &GetNapiMock(); +void SetNapiMock(NapiMock *mock); +void ResetNapiMock(); + +class ScopedNapiMock { +public: + explicit ScopedNapiMock(NapiMock *mock) : mock_(mock) + { + SetNapiMock(mock_); + } + + ~ScopedNapiMock() + { + ResetNapiMock(); + } + + NapiMock &GetMock() + { + return *mock_; + } + + ScopedNapiMock(const ScopedNapiMock &) = delete; + ScopedNapiMock &operator=(const ScopedNapiMock &) = delete; + +private: + NapiMock *mock_; +}; + +} // namespace Test +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS +#endif // INTERFACES_TEST_UNITTEST_FILEMGMT_LIBN_TEST_INCLUDE_NAPI_MOCK_H \ No newline at end of file diff --git a/interfaces/test/unittest/filemgmt_libn_test/src/n_class_test.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/n_class_test.cpp index a39ef98067971eac9c678e2da4f810cdfd5e9556..65047faf96b4cd4058905e2ff8a53ea62cbbf565 100644 --- a/interfaces/test/unittest/filemgmt_libn_test/src/n_class_test.cpp +++ b/interfaces/test/unittest/filemgmt_libn_test/src/n_class_test.cpp @@ -1,10 +1,10 @@ /* - * Copyright (C) 2023 Huawei Device Co., Ltd. + * Copyright (C) 2023-2025 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 + * 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, @@ -13,44 +13,571 @@ * limitations under the License. */ -#include "n_class_test.h" +#include +#include + +#include +#include + #include "n_class.h" +#include "napi_mock.h" +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { using namespace std; -using namespace OHOS; -using namespace testing::ext; -namespace OHOS { -namespace Media { -void FilemangementLibnTest::SetUpTestCase(void) {} +class NClassTest : public testing::Test { +public: + static void SetUpTestSuite(void) {} + static void TearDownTestSuite(void) {} + + void SetUp() override + { + mock_ = std::make_unique(); + scopedMock_ = std::make_unique(mock_.get()); + } + + void TearDown() override + { + scopedMock_.reset(); + mock_.reset(); + } + + NapiMock &GetMock() + { + return *mock_; + } + +private: + std::unique_ptr mock_; + std::unique_ptr scopedMock_; +}; + +/** + * @tc.name: NClassTest_DefineClass_001 + * @tc.desc: Test function of NClass::DefineClass interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_DefineClass_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_DefineClass_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NClassTest_DefineClass_001"; + napi_callback callback = reinterpret_cast(0x1122); + vector props = {}; -void FilemangementLibnTest::TearDownTestCase(void) {} + // Set mock behaviors + napi_value expectedClass = reinterpret_cast(0x1234); + EXPECT_CALL(GetMock(), napi_define_class(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_, + testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<7>(expectedClass), testing::Return(napi_ok))); -void FilemangementLibnTest::SetUp() {} + // Do testing + auto [succ, classValue] = NClass::DefineClass(env, className, callback, std::move(props)); + + // Verify results + EXPECT_TRUE(succ); + EXPECT_EQ(classValue, expectedClass); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_DefineClass_001"; +} -void FilemangementLibnTest::TearDown() {} - -HWTEST_F(FilemangementLibnTest, NClassTest_DefineClass_001, TestSize.Level1) +/** + * @tc.name: NClassTest_DefineClass_002 + * @tc.desc: Test function of NClass::DefineClass interface for FAILURE when napi_define_class fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_DefineClass_002, testing::ext::TestSize.Level1) { - string className = "TestClassAB"; - napi_callback vlaue = nullptr; + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_DefineClass_002"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NClassTest_DefineClass_002"; + napi_callback callback = reinterpret_cast(0x1122); vector props = {}; - bool succ = false; - napi_value classValue = nullptr; - napi_env envTest = nullptr; - tie(succ, classValue) = FileManagement::LibN::NClass::DefineClass(envTest, className, vlaue, std::move(props)); - EXPECT_EQ(succ, false); - EXPECT_EQ((classValue == nullptr), true); + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_define_class(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_, + testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + // Do testing + auto [succ, classValue] = NClass::DefineClass(env, className, callback, std::move(props)); + + // Verify results + EXPECT_FALSE(succ); + EXPECT_EQ(classValue, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_DefineClass_002"; +} + +/** + * @tc.name: NClassTest_SaveClass_001 + * @tc.desc: Test function of NClass::SaveClass interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_SaveClass_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_SaveClass_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NClassTest_SaveClass_001"; + + // Set mock behaviors + napi_value classValue = reinterpret_cast(0x1234); + napi_ref mockReference = reinterpret_cast(0x2345); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(mockReference), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_add_env_cleanup_hook(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + // Do testing + bool succ = NClass::SaveClass(env, className, classValue); + + // Verify results + EXPECT_TRUE(succ); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_SaveClass_001"; +} + +/** + * @tc.name: NClassTest_SaveClass_002 + * @tc.desc: Test function of NClass::SaveClass interface for FAILURE when napi_create_reference fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_SaveClass_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_SaveClass_002"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NClassTest_SaveClass_002"; + napi_value classValue = reinterpret_cast(0x1234); + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + // Do testing + bool succ = NClass::SaveClass(env, className, classValue); + + // Verify results + EXPECT_FALSE(succ); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_SaveClass_002"; +} + +/** + * @tc.name: NClassTest_CleanClass_001 + * @tc.desc: Test function of NClass::CleanClass interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_CleanClass_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_CleanClass_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_delete_reference(testing::_, testing::_)).WillOnce(testing::Return(napi_ok)); + + // Do testing + NClass::CleanClass(env); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_CleanClass_001"; +} + +/** + * @tc.name: NClassTest_InstantiateClass_001 + * @tc.desc: Test function of NClass::InstantiateClass interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_InstantiateClass_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_InstantiateClass_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NClassTest_InstantiateClass_001"; + vector emptyArgs = {}; + + // Set mock behaviors + napi_value classValue = reinterpret_cast(0x1234); + napi_ref mockReference = reinterpret_cast(0x2345); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(mockReference), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_get_reference_value(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + napi_value expectedInstance = reinterpret_cast(0x3456); + EXPECT_CALL(GetMock(), napi_new_instance(testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<4>(expectedInstance), testing::Return(napi_ok))); + + // Prepare instance + bool succ = NClass::SaveClass(env, className, classValue); + EXPECT_TRUE(succ); + + // Do testing + auto instance = NClass::InstantiateClass(env, className, emptyArgs); + + // Verify results + EXPECT_EQ(instance, expectedInstance); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_InstantiateClass_001"; +} + +/** + * @tc.name: NClassTest_InstantiateClass_002 + * @tc.desc: Test function of NClass::InstantiateClass interface for FAILURE when class hasn't been saved yet. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_InstantiateClass_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_InstantiateClass_002"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NClassTest_InstantiateClass_002"; + vector emptyArgs = {}; + + // Do testing + auto instance = NClass::InstantiateClass(env, className, emptyArgs); + + // Verify results + EXPECT_EQ(instance, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_InstantiateClass_002"; +} + +/** + * @tc.name: NClassTest_InstantiateClass_003 + * @tc.desc: Test function of NClass::InstantiateClass interface for FAILURE when napi_get_reference_value fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_InstantiateClass_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_InstantiateClass_003"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NClassTest_InstantiateClass_003"; + vector emptyArgs = {}; + + // Set mock behaviors + napi_value classValue = reinterpret_cast(0x1234); + napi_ref mockReference = reinterpret_cast(0x2345); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(mockReference), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_get_reference_value(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_invalid_arg)); + + // Prepare instance + bool succ = NClass::SaveClass(env, className, classValue); + EXPECT_TRUE(succ); + + // Do testing + auto instance = NClass::InstantiateClass(env, className, emptyArgs); + + // Verify results + EXPECT_EQ(instance, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_InstantiateClass_003"; +} + +/** + * @tc.name: NClassTest_InstantiateClass_004 + * @tc.desc: Test function of NClass::InstantiateClass interface for FAILURE when napi_new_instance fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_InstantiateClass_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_InstantiateClass_004"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NClassTest_InstantiateClass_004"; + vector emptyArgs = {}; + + // Set mock behaviors + napi_value classValue = reinterpret_cast(0x1234); + napi_ref mockReference = reinterpret_cast(0x2345); + EXPECT_CALL(GetMock(), napi_create_reference(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(mockReference), testing::Return(napi_ok))); + + EXPECT_CALL(GetMock(), napi_get_reference_value(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + EXPECT_CALL(GetMock(), napi_new_instance(testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + // Prepare instance + bool succ = NClass::SaveClass(env, className, classValue); + EXPECT_TRUE(succ); + + // Do testing + auto instance = NClass::InstantiateClass(env, className, emptyArgs); + + // Verify results + EXPECT_EQ(instance, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_InstantiateClass_004"; +} + +/** + * @tc.name: NClassTest_GetEntityOf_001 + * @tc.desc: Test function of NClass::GetEntityOf interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_GetEntityOf_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_GetEntityOf_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + napi_value object = reinterpret_cast(0x1234); + void *expectedEntity = reinterpret_cast(0x23456); + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_unwrap(testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<2>(expectedEntity), testing::Return(napi_ok))); + + // Do testing + auto entity = NClass::GetEntityOf(env, object); + + // Verify results + EXPECT_EQ(entity, expectedEntity); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_GetEntityOf_001"; } -HWTEST_F(FilemangementLibnTest, NClassTest_SaveClass_001, TestSize.Level1) +/** + * @tc.name: NClassTest_GetEntityOf_002 + * @tc.desc: Test function of NClass::GetEntityOf interface for FAILURE when arg env nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_GetEntityOf_002, testing::ext::TestSize.Level1) { - string className = "TestClassAB"; - bool succ = false; - napi_value classValue = nullptr; - napi_env envTest = nullptr; - succ = FileManagement::LibN::NClass::SaveClass(envTest, className, classValue); - EXPECT_EQ(succ, false); + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_GetEntityOf_002"; + // Prepare test parameters + napi_value object = reinterpret_cast(0x1234); + + // Do testing + auto entity = NClass::GetEntityOf(nullptr, object); + + // Verify results + EXPECT_EQ(entity, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_GetEntityOf_002"; +} + +/** + * @tc.name: NClassTest_GetEntityOf_003 + * @tc.desc: Test function of NClass::GetEntityOf interface for FAILURE when arg objStat nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_GetEntityOf_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_GetEntityOf_003"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + + // Do testing + auto entity = NClass::GetEntityOf(env, nullptr); + + // Verify results + EXPECT_EQ(entity, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_GetEntityOf_003"; +} + +/** + * @tc.name: NClassTest_GetEntityOf_004 + * @tc.desc: Test function of NClass::GetEntityOf interface for FAILURE when napi_unwrap fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_GetEntityOf_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_GetEntityOf_004"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + napi_value object = reinterpret_cast(0x1234); + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_unwrap(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + // Do testing + auto entity = NClass::GetEntityOf(env, object); + + // Verify results + EXPECT_EQ(entity, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_GetEntityOf_004"; +} + +/** + * @tc.name: NClassTest_SetEntityFor_001 + * @tc.desc: Test function of NClass::SetEntityFor interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_SetEntityFor_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_SetEntityFor_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + napi_value object = reinterpret_cast(0x1234); + string entity = "NClassTest_SetEntityFor_001"; + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_wrap(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + // Do testing + bool result = NClass::SetEntityFor(env, object, make_unique(entity)); + + // Verify results + EXPECT_TRUE(result); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_SetEntityFor_001"; } -} // namespace Media + +/** + * @tc.name: NClassTest_SetEntityFor_002 + * @tc.desc: Test function of NClass::SetEntityFor interface for FAILURE when napi_wrap fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_SetEntityFor_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_SetEntityFor_002"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + napi_value object = reinterpret_cast(0x1234); + string entity = "NClassTest_SetEntityFor_001"; + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_wrap(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + // Do testing + bool result = NClass::SetEntityFor(env, object, make_unique(entity)); + + // Verify results + EXPECT_FALSE(result); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_SetEntityFor_002"; +} + +/** + * @tc.name: NClassTest_RemoveEntityOfFinal_001 + * @tc.desc: Test function of NClass::RemoveEntityOfFinal interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_RemoveEntityOfFinal_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_RemoveEntityOfFinal_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + napi_value object = reinterpret_cast(0x1234); + void *expectedEntity = reinterpret_cast(0x23456); + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_remove_wrap(testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<2>(expectedEntity), testing::Return(napi_ok))); + + // Do testing + auto entity = NClass::RemoveEntityOfFinal(env, object); + + // Verify results + EXPECT_EQ(entity, expectedEntity); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_RemoveEntityOfFinal_001"; +} + +/** + * @tc.name: NClassTest_RemoveEntityOfFinal_002 + * @tc.desc: Test function of NClass::RemoveEntityOfFinal interface for FAILURE when arg env nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_RemoveEntityOfFinal_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_RemoveEntityOfFinal_002"; + // Prepare test parameters + napi_value object = reinterpret_cast(0x1234); + + // Do testing + auto entity = NClass::RemoveEntityOfFinal(nullptr, object); + + // Verify results + EXPECT_EQ(entity, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_RemoveEntityOfFinal_002"; +} + +/** + * @tc.name: NClassTest_RemoveEntityOfFinal_003 + * @tc.desc: Test function of NClass::RemoveEntityOfFinal interface for FAILURE when arg objStat nullptr. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_RemoveEntityOfFinal_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_RemoveEntityOfFinal_003"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + + // Do testing + auto entity = NClass::RemoveEntityOfFinal(env, nullptr); + + // Verify results + EXPECT_EQ(entity, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_RemoveEntityOfFinal_003"; +} + +/** + * @tc.name: NClassTest_RemoveEntityOfFinal_004 + * @tc.desc: Test function of NClass::RemoveEntityOfFinal interface for FAILURE when napi_remove_wrap fails. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NClassTest, NClassTest_RemoveEntityOfFinal_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_RemoveEntityOfFinal_004"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + napi_value object = reinterpret_cast(0x1234); + + // Set mock behaviors + EXPECT_CALL(GetMock(), napi_remove_wrap(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + // Do testing + auto entity = NClass::RemoveEntityOfFinal(env, object); + + // Verify results + EXPECT_EQ(entity, nullptr); + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_RemoveEntityOfFinal_004"; +} + +} // namespace Test +} // namespace LibN +} // namespace FileManagement } // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/filemgmt_libn_test/src/n_func_arg_test.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/n_func_arg_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3629fe2e43d8cf58d9c8ba09ac8b9b1b7b5284dc --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/n_func_arg_test.cpp @@ -0,0 +1,393 @@ +/* + * Copyright (C) 2023-2025 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 + +#include +#include + +#include "n_func_arg.h" +#include "napi_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { +using namespace std; + +class NFuncArgTest : public testing::Test { +public: + static void SetUpTestSuite(void) {} + static void TearDownTestSuite(void) {} + + void SetUp() override + { + mock_ = std::make_unique(); + scopedMock_ = std::make_unique(mock_.get()); + } + + void TearDown() override + { + scopedMock_.reset(); + mock_.reset(); + } + + NapiMock &GetMock() + { + return *mock_; + } + +private: + std::unique_ptr mock_; + std::unique_ptr scopedMock_; +}; + +/** + * @tc.name: NFuncArgTest_Constructor_001 + * @tc.desc: Test function of NFuncArg constructor for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_Constructor_001, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_Constructor_001"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_Constructor_001"; + napi_callback_info callback = reinterpret_cast(0x2000); + // 创建NFuncArg对象 + NFuncArg arg(env, callback); + // Do testing and Verify results + EXPECT_EQ(arg.GetArgc(), 0); + EXPECT_EQ(arg.GetThisVar(), nullptr); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_Constructor_001"; +} + +/** + * @tc.name: NFuncArgTest_SetAndGetArgc_002 + * @tc.desc: Test function of NFuncArg::SetArgc and NFuncArg::GetArgc interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_SetAndGetArgc_002, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_SetAndGetArgc_002"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_SetAndGetArgc_002"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t testArgc = 5; + // Do testing + arg.SetArgc(testArgc); + // Verify results + size_t resultArgc = arg.GetArgc(); + EXPECT_EQ(resultArgc, testArgc); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_SetAndGetArgc_002"; +} + +/** + * @tc.name: NFuncArgTest_SetAndGetThisVar_003 + * @tc.desc: Test function of NFuncArg::SetThisVar and NFuncArg::GetThisVar interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_SetAndGetThisVar_003, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_SetAndGetThisVar_003"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_SetAndGetThisVar_003"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + napi_value testThisVar = reinterpret_cast(0x1234); + // Do testing + arg.SetThisVar(testThisVar); + // Verify results + napi_value resultThisVar = arg.GetThisVar(); + EXPECT_EQ(resultThisVar, testThisVar); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_SetAndGetThisVar_003"; +} + +/** + * @tc.name: NFuncArgTest_GetArg_ValidPosition_004 + * @tc.desc: Test function of NFuncArg::GetArg interface for valid arg position for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_GetArg_ValidPosition_004, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_GetArg_ValidPosition_004"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_GetArg_ValidPosition_004"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t testArgc = 3; + arg.SetArgc(testArgc); + arg.argv_ = std::make_unique(testArgc); + for (size_t i = 0; i < testArgc; ++i) { + arg.argv_[i] = reinterpret_cast(i + 1000); + } + size_t validArgPos = 1; + // Do testing + napi_value resultArg = arg.GetArg(validArgPos); + // Verify results + EXPECT_EQ(resultArg, arg.argv_[validArgPos]); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_GetArg_ValidPosition_004"; +} + +/** + * @tc.name: NFuncArgTest_GetArg_InvalidPosition_005 + * @tc.desc: Test function of NFuncArg::GetArg interface for invalid arg position for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_GetArg_InvalidPosition_005, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_GetArg_InvalidPosition_005"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_GetArg_InvalidPosition_005"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t testArgc = 3; + arg.SetArgc(testArgc); + size_t invalidArgPos = 5; + // Do testing + napi_value resultArg = arg.GetArg(invalidArgPos); + // Verify results + EXPECT_EQ(resultArg, nullptr); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_GetArg_InvalidPosition_005"; +} + +/** + * @tc.name: NFuncArgTest_OperatorSquareBrackets_ValidPosition_006 + * @tc.desc: Test function of NFuncArg::operator[] interface for valid arg position for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_OperatorSquareBrackets_ValidPosition_006, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_OperatorSquareBrackets_ValidPosition_006"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_OperatorSquareBrackets_ValidPosition_006"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t testArgc = 3; + arg.SetArgc(testArgc); + arg.argv_ = std::make_unique(testArgc); + for (size_t i = 0; i < testArgc; ++i) { + arg.argv_[i] = reinterpret_cast(i + 1000); + } + size_t validArgPos = 2; + // Do testing + napi_value resultArg = arg.GetArg(validArgPos); + // Verify results + EXPECT_EQ(resultArg, arg.argv_[validArgPos]); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_OperatorSquareBrackets_ValidPosition_006"; +} + +/** + * @tc.name: NFuncArgTest_OperatorSquareBrackets_InvalidPosition_007 + * @tc.desc: Test function of NFuncArg::operator[] interface for invalid arg position for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_OperatorSquareBrackets_InvalidPosition_007, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_OperatorSquareBrackets_InvalidPosition_007"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_OperatorSquareBrackets_InvalidPosition_007"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t testArgc = 3; + arg.SetArgc(testArgc); + size_t invalidArgPos = 4; + // Do testing + napi_value resultArg = arg[invalidArgPos]; + // Verify results + EXPECT_EQ(resultArg, nullptr); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_OperatorSquareBrackets_InvalidPosition_007"; +} + +/** + * @tc.name: NFuncArgTest_InitArgs_WithChecker_Success_008 + * @tc.desc: Test function of NFuncArg::InitArgs with specific argc checker for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_InitArgs_WithChecker_Success_008, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_InitArgs_WithChecker_Success_008"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t testArgc = 0; + auto checker = [testArgc, &arg]() { + size_t realArgc = arg.GetArgc(); + GTEST_LOG_(INFO) << "Actual argc: " << realArgc << ", Expected argc: " << testArgc; + return realArgc == testArgc; + }; + // 模拟napi_get_cb_info成功返回 + EXPECT_CALL(GetMock(), napi_get_cb_info(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + // Do testing + bool result = arg.InitArgs(checker); + GTEST_LOG_(INFO) << "InitArgs result: " << result; + // Verify results + EXPECT_TRUE(result); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_InitArgs_WithChecker_Success_008"; +} + +/** + * @tc.name: NFuncArgTest_InitArgs_WithSpecifiedArgc_Success_009 + * @tc.desc: Test function of NFuncArg::InitArgs with specified argc for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_InitArgs_WithSpecifiedArgc_Success_009, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_InitArgs_WithSpecifiedArgc_Success_009"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_InitArgs_WithSpecifiedArgc_Success_009"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t testArgc = 0; + // 模拟napi_get_cb_info成功返回 + EXPECT_CALL(GetMock(), napi_get_cb_info(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + // Do testing + bool result = arg.InitArgs(testArgc); + // Verify results + EXPECT_TRUE(result); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_InitArgs_WithSpecifiedArgc_Success_009"; +} + +/** + * @tc.name: NFuncArgTest_InitArgs_WithSpecifiedArgc_Failure_010 + * @tc.desc: Test function of NFuncArg::InitArgs with specified argc for FAILURE. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_InitArgs_WithSpecifiedArgc_Failure_010, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_InitArgs_WithSpecifiedArgc_Failure_010"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_InitArgs_WithSpecifiedArgc_Failure_010"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t testArgc = 3; + // 模拟napi_get_cb_info失败返回 + EXPECT_CALL(GetMock(), napi_get_cb_info(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + // Do testing + bool result = arg.InitArgs(testArgc); + // Verify results + EXPECT_FALSE(result); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_InitArgs_WithSpecifiedArgc_Failure_010"; +} + +/** + * @tc.name: NFuncArgTest_InitArgs_ArgcRange_011 + * @tc.desc: Test function of NFuncArg::InitArgs with specified size_t minArgc and size_t maxArgc for FAIL. + * Verify that when napi_get_cb_info succeeds and the received argc is within the specified range, it returns false. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_InitArgs_ArgcRange_011, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_InitArgs_ArgcRange_011"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_InitArgs_ArgcRange_011"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t minArgc = 2; + size_t maxArgc = 5; + + // 模拟napi_get_cb_info成功返回 + EXPECT_CALL(GetMock(), napi_get_cb_info(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + // Do testing + bool result = arg.InitArgs(minArgc, maxArgc); + + // Verify results + EXPECT_FALSE(result); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_InitArgs_ArgcRange_011"; +} + +/** + * @tc.name: NFuncArgTest_InitArgs_ArgcRange_014 + * @tc.desc: Test function of NFuncArg::InitArgs with specified size_t minArgc and size_t maxArgc for FAILURE. + * Verify that when napi_get_cb_info succeeds but the received argc is outside the specified range, it returns false. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_InitArgs_ArgcRange_014, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_InitArgs_ArgcRange_014"; + // Prepare test parameters + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_InitArgs_ArgcRange_014"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + size_t minArgc = 0; + size_t maxArgc = 0; + EXPECT_CALL(GetMock(), napi_get_cb_info(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<2>(1), testing::Return(napi_generic_failure))); + + // Do testing + bool result = arg.InitArgs(minArgc, maxArgc); + + // Verify results + EXPECT_FALSE(result); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_InitArgs_ArgcRange_014"; +} + +/** + * @tc.name: NFuncArgTest_InitArgs_FirstNapiGetCbInfoFail_015 + * @tc.desc: Test function of NFuncArg::InitArgs, when napi_get_cb_info first return false, it returns false。 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NFuncArgTest, NFuncArgTest_InitArgs_FirstNapiGetCbInfoFail_015, testing::ext::TestSize.Level1) { + GTEST_LOG_(INFO) << "NFuncArgTest-begin NFuncArgTest_InitArgs_FirstNapiGetCbInfoFail_015"; + napi_env env = reinterpret_cast(0x1000); + string className = "NFuncArgTest_InitArgs_FirstNapiGetCbInfoFail_015"; + napi_callback_info callback = reinterpret_cast(0x2000); + NFuncArg arg(env, callback); + auto checker = []() { return true; }; + + EXPECT_CALL(GetMock(), napi_get_cb_info(testing::_, testing::_, testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + bool result = arg.InitArgs(checker); + EXPECT_FALSE(result); + GTEST_LOG_(INFO) << "NFuncArgTest-end NFuncArgTest_InitArgs_FirstNapiGetCbInfoFail_015"; +} + +} // namespace Test +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2eaf75114c7118005859456a6789ad877dd5b94 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2025 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_mock.h" + +#include + +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { + +namespace { +std::mutex g_mockMutex; +NapiMock *g_currentMock = nullptr; +NapiMock g_defaultMock; +} // namespace + +NapiMock &GetNapiMock() +{ + std::lock_guard lock(g_mockMutex); + return g_currentMock ? *g_currentMock : g_defaultMock; +} + +void SetNapiMock(NapiMock *mock) +{ + std::lock_guard lock(g_mockMutex); + g_currentMock = mock; +} + +void ResetNapiMock() +{ + SetNapiMock(nullptr); +} + +} // namespace Test +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS + +#ifdef ENABLE_NAPI_MOCK +extern "C" { +using namespace OHOS::FileManagement::LibN::Test; + +napi_status napi_define_class(napi_env env, const char *name, size_t length, napi_callback constructor, void *data, + size_t property_count, const napi_property_descriptor *properties, napi_value *result) +{ + return GetNapiMock().napi_define_class(env, name, length, constructor, data, property_count, properties, result); +} + +napi_status napi_create_reference(napi_env env, napi_value value, uint32_t initial_refcount, napi_ref *result) +{ + return GetNapiMock().napi_create_reference(env, value, initial_refcount, result); +} + +napi_status napi_add_env_cleanup_hook(napi_env env, void (*fun)(void *arg), void *arg) +{ + return GetNapiMock().napi_add_env_cleanup_hook(env, fun, arg); +} + +napi_status napi_delete_reference(napi_env env, napi_ref ref) +{ + return GetNapiMock().napi_delete_reference(env, ref); +} + +napi_status napi_get_reference_value(napi_env env, napi_ref ref, napi_value *result) +{ + return GetNapiMock().napi_get_reference_value(env, ref, result); +} + +napi_status napi_new_instance( + napi_env env, napi_value constructor, size_t argc, const napi_value *argv, napi_value *result) +{ + return GetNapiMock().napi_new_instance(env, constructor, argc, argv, result); +} + +napi_status napi_unwrap(napi_env env, napi_value js_object, void **result) +{ + return GetNapiMock().napi_unwrap(env, js_object, result); +} + +napi_status napi_wrap(napi_env env, napi_value js_object, void *native_object, napi_finalize finalize_cb, + void *finalize_hint, napi_ref *result) +{ + return GetNapiMock().napi_wrap(env, js_object, native_object, finalize_cb, finalize_hint, result); +} + +napi_status napi_remove_wrap(napi_env env, napi_value js_object, void **result) +{ + return GetNapiMock().napi_remove_wrap(env, js_object, result); +} + +napi_status napi_get_cb_info(napi_env env, napi_callback_info cbinfo, size_t* argc, napi_value* argv, + napi_value* thisArg, void** data) +{ + return GetNapiMock().napi_get_cb_info(env, cbinfo, argc, argv, thisArg, data); +} + +} // extern "C" +#endif \ No newline at end of file