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..58a86cce6029d438b77578d12ae5c680063f6fba 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 @@ -14,29 +14,33 @@ import("//build/test.gni") import("//foundation/filemanagement/file_api/file_api.gni") -group("unittest") { +ohos_unittest("filemgmt_libn_test") { + branch_protector_ret = "pac_ret" testonly = true - deps = [ ":filemgmt_libn_test" ] -} -ohos_unittest("filemgmt_libn_test") { 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", + ] deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", - "//third_party/googletest:gtest_main", ] external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", "c_utils:utilsbase", + "googletest:gmock_main", + "googletest:gtest_main", "hilog:libhilog", "ipc:ipc_core", "napi:ace_napi", ] + + defines = [ "ENABLE_NAPI_MOCK" ] } 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..30f85120c4cd631b8324c91732ed0ec10c058183 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h @@ -0,0 +1,114 @@ +/* + * 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; +}; + +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)); +}; + +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/napi_mock.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..271603a2a9b7adc6858f38c45a46bf9d848e427a --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp @@ -0,0 +1,106 @@ +/* + * 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); +} + +} // extern "C" +#endif \ No newline at end of file