diff --git a/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn b/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn index ea20a1cf9f7b571e107d8d534852829741ebf3bb..b894334a891a3f59706a854a17955c8e9be7e37c 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,20 +14,23 @@ 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/n_error_test.cpp", + "./src/napi_mock.cpp", + ] deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", "${utils_path}/filemgmt_libn:filemgmt_libn", + "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] @@ -39,4 +42,9 @@ ohos_unittest("filemgmt_libn_test") { "ipc:ipc_core", "napi:ace_napi", ] + + defines = [ + "ENABLE_NAPI_MOCK", + "private=public", + ] } 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..d57eb03123d47b4c8035d8af308cb161a7ded319 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/include/napi_mock.h @@ -0,0 +1,147 @@ +/* + * 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_create_int32(napi_env env, int32_t value, napi_value* result) = 0; + + virtual napi_status napi_create_string_utf8(napi_env env, const char* str, size_t length, napi_value* result) = 0; + + virtual napi_status napi_create_error(napi_env env, napi_value code, napi_value msg, napi_value* result) = 0; + + virtual napi_status napi_set_named_property(napi_env env, napi_value object, const char* utf8name, + napi_value value) = 0; + + virtual napi_status napi_get_and_clear_last_exception(napi_env env, napi_value* result) = 0; + + virtual napi_status napi_throw_error(napi_env env, const char* code, const char* msg) = 0; + + virtual napi_status napi_throw(napi_env env, napi_value error) = 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_create_int32, (napi_env env, int32_t value, napi_value* result), (override)); + + MOCK_METHOD(napi_status, napi_create_string_utf8, + (napi_env env, const char* str, size_t length, napi_value* result), + (override)); + + MOCK_METHOD(napi_status, napi_create_error, (napi_env env, napi_value code, napi_value msg, napi_value* result), + (override)); + + MOCK_METHOD(napi_status, napi_set_named_property, (napi_env env, napi_value object, const char* utf8name, + napi_value value), (override)); + + MOCK_METHOD(napi_status, napi_get_and_clear_last_exception, (napi_env env, napi_value* result), (override)); + + MOCK_METHOD(napi_status, napi_throw_error, (napi_env env, const char* code, const char* msg), (override)); + + MOCK_METHOD(napi_status, napi_throw, (napi_env env, napi_value error), (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_error_test.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/n_error_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0224a3d557dc76efbb769f10a79762ec23ff0c3f --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/n_error_test.cpp @@ -0,0 +1,592 @@ +/* + * 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_error.h" +#include "napi_mock.h" + +namespace OHOS { +namespace FileManagement { +namespace LibN { +namespace Test { +using namespace std; + +class NErrorTest : 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_; +}; + +// NError(int errCode) +/** + * @tc.name: NErrorTest_NError_001 + * @tc.desc: Test function of NError::NError interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_NError_001, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NClassTest_DefineClass_001"; + + int errCode = -ENOENT; + NError error(errCode); + auto& expectedEntry = errCodeTable.at(ENOENT); + + EXPECT_EQ(error.errno_, expectedEntry.first); + EXPECT_EQ(error.errMsg_, expectedEntry.second); + + GTEST_LOG_(INFO) << "NClassTest-end NClassTest_DefineClass_001"; +} + +/** + * @tc.name: NErrorTest_NError_002 + * @tc.desc: Test function of NError::NError interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_NError_002, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_NError_002"; + + int inputCode = 5; + NError error(inputCode); + auto& defaultEntry = errCodeTable.at(EIO); + + EXPECT_EQ(error.errno_, defaultEntry.first); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_NError_002"; +} + +/** + * @tc.name: NErrorTest_NError_003 + * @tc.desc: Test function of NError::NError interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_NError_003, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_NError_003"; + + int uvCode = -999; + NError error(uvCode); + auto& defaultEntry = errCodeTable.at(UNKROWN_ERR); + + EXPECT_EQ(error.errno_, defaultEntry.first); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_NError_003"; +} + +// NError(std::function()> errGen) +/** + * @tc.name: NErrorTest_NError_004 + * @tc.desc: Test function of NError::NError interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_NError_004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_NError_004"; + + auto generator = []() { return make_tuple(404, "Not Found"); }; + NError error(generator); + + EXPECT_EQ(error.errno_, 404); + EXPECT_EQ(error.errMsg_, "Not Found"); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_NError_004"; +} + +// operator +/** + * @tc.name: NErrorTest_operator_005 + * @tc.desc: Test function of NError::operator interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_operator_005, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_operator_005"; + + NError error; + error.errno_ = ERRNO_NOERR; + EXPECT_FALSE(static_cast(error)); // 覆盖无错误分支 + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_operator_005"; +} + +/** + * @tc.name: NErrorTest_operator_006 + * @tc.desc: Test function of NError::operator interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_operator_006, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_operator_006"; + + NError error(E_NOENT); + EXPECT_TRUE(static_cast(error)); // 覆盖有错误分支 + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_operator_006"; +} + +// GetNapiErr(napi_env env) +/** + * @tc.name: NErrorTest_GetNapiErr_007 + * @tc.desc: Test function of NError::GetNapiErr interface for FAILURE when nerrno_ == ERRNO_NOERR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErr_007, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErr_007"; + + napi_env env = reinterpret_cast(0x1000); + NError error; + + napi_value object = error.GetNapiErr(env); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErr_007"; +} + +/** + * @tc.name: NErrorTest_GetNapiErr_008 + * @tc.desc: Test function of NError::GetNapiErr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErr_008, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErr_008"; + + napi_env env = reinterpret_cast(0x1000); + NError error(E_NOENT); + napi_value expectedBusinessError = reinterpret_cast(0x1234); + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(expectedBusinessError), testing::Return(napi_ok))); + EXPECT_CALL(GetMock(), napi_set_named_property(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + napi_value object = error.GetNapiErr(env); + EXPECT_EQ(object, expectedBusinessError); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErr_008"; +} + +/** + * @tc.name: NErrorTest_GetNapiErr_009 + * @tc.desc: Test function of NError::GetNapiErr interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErr_009, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErr_009"; + + napi_env env = reinterpret_cast(0x1000); + NError error(E_NOENT); + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + napi_value object = error.GetNapiErr(env); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErr_009"; +} + +/** + * @tc.name: NErrorTest_GetNapiErr_010 + * @tc.desc: Test function of NError::GetNapiErr interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErr_010, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErr_010"; + + napi_env env = reinterpret_cast(0x1000); + NError error(E_NOENT); + napi_value expectedInstance = reinterpret_cast(0x3456); + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::DoAll(testing::SetArgPointee<3>(expectedInstance), testing::Return(napi_ok))); + EXPECT_CALL(GetMock(), napi_set_named_property(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + napi_value object = error.GetNapiErr(env); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErr_010"; +} + +// GetNapiErr(napi_env env, int errCode) +/** + * @tc.name: NErrorTest_GetNapiErr_011 + * @tc.desc: Test function of NError::GetNapiErr interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErr_011, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErr_011"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = ERRNO_NOERR; + NError error; + + napi_value object = error.GetNapiErr(env, errCode); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErr_011"; +} + +/** + * @tc.name: NErrorTest_GetNapiErr_012 + * @tc.desc: Test function of NError::GetNapiErr interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErr_012, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErr_012"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = ENOENT; + NError error; + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + napi_value object = error.GetNapiErr(env, errCode); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErr_012"; +} + +/** + * @tc.name: NErrorTest_GetNapiErr_013 + * @tc.desc: Test function of NError::GetNapiErr interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErr_013, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErr_013"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = UNKROWN_ERR; + NError error; + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + napi_value object = error.GetNapiErr(env, errCode); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErr_013"; +} + +// GetNapiErrAddData(napi_env env, int errCode, napi_value data) +/** + * @tc.name: NErrorTest_GetNapiErrAddData_014 + * @tc.desc: Test function of NError::GetNapiErrAddData interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErrAddData_014, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErrAddData_014"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = ERRNO_NOERR; + napi_value data = reinterpret_cast(0x2345); + NError error; + + napi_value object = error.GetNapiErrAddData(env, errCode, data); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErrAddData_014"; +} + +/** + * @tc.name: NErrorTest_GetNapiErrAddData_015 + * @tc.desc: Test function of NError::GetNapiErrAddData interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErrAddData_015, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErrAddData_015"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = ENOENT; + napi_value data = reinterpret_cast(0x2345); + NError error; + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + EXPECT_CALL(GetMock(), napi_set_named_property(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + napi_value object = error.GetNapiErrAddData(env, errCode, data); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErrAddData_015"; +} + +/** + * @tc.name: NErrorTest_GetNapiErrAddData_016 + * @tc.desc: Test function of NError::GetNapiErrAddData interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_GetNapiErrAddData_016, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_GetNapiErrAddData_016"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = ENOENT; + napi_value data = reinterpret_cast(0x2345); + NError error; + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + EXPECT_CALL(GetMock(), napi_set_named_property(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + napi_value object = error.GetNapiErrAddData(env, errCode, data); + EXPECT_EQ(object, nullptr); + + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_GetNapiErrAddData_016"; +} + +// ThrowErr(napi_env env, string errMsg) +/** + * @tc.name: NErrorTest_ThrowErr_017 + * @tc.desc: Test function of NError::ThrowErr interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_ThrowErr_017, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_ThrowErr_017"; + + napi_env env = reinterpret_cast(0x1000); + NError error(ENOENT); + string errMsg = "No such file or directory"; + + EXPECT_CALL(GetMock(), napi_get_and_clear_last_exception(testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_throw_error(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + error.ThrowErr(env, errMsg); + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_ThrowErr_017"; +} + +/** + * @tc.name: NErrorTest_ThrowErr_018 + * @tc.desc: Test function of NError::ThrowErr interface for ERROR. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_ThrowErr_018, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_ThrowErr_018"; + + napi_env env = reinterpret_cast(0x1000); + NError error(ENOENT); + string errMsg = "No such file or directory"; + + EXPECT_CALL(GetMock(), napi_get_and_clear_last_exception(testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_throw_error(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + error.ThrowErr(env, errMsg); + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_ThrowErr_018"; +} + +// ThrowErrAddData(napi_env env, int errCode, napi_value data) +/** + * @tc.name: NErrorTest_ThrowErrAddData_019 + * @tc.desc: Test function of NError::ThrowErrAddData interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_ThrowErrAddData_019, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_ThrowErrAddData_019"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = ENOENT; + napi_value data = reinterpret_cast(0x2345); + NError error; + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + EXPECT_CALL(GetMock(), napi_set_named_property(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_throw(testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + + error.ThrowErrAddData(env, errCode, data); + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_ThrowErrAddData_019"; +} + +/** + * @tc.name: NErrorTest_ThrowErrAddData_020 + * @tc.desc: Test function of NError::ThrowErrAddData interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_ThrowErrAddData_020, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_ThrowErrAddData_020"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = ENOENT; + napi_value data = reinterpret_cast(0x2345); + NError error; + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + EXPECT_CALL(GetMock(), napi_set_named_property(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + error.ThrowErrAddData(env, errCode, data); + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_ThrowErrAddData_020"; +} + +/** + * @tc.name: NErrorTest_ThrowErrAddData_021 + * @tc.desc: Test function of NError::ThrowErrAddData interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + */ +HWTEST_F(NErrorTest, NErrorTest_ThrowErrAddData_021, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "NClassTest-begin NErrorTest_ThrowErrAddData_021"; + + napi_env env = reinterpret_cast(0x1000); + int errCode = ENOENT; + napi_value data = reinterpret_cast(0x2345); + NError error; + + EXPECT_CALL(GetMock(), napi_create_int32(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_string_utf8(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_create_error(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + EXPECT_CALL(GetMock(), napi_set_named_property(testing::_, testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(napi_ok)); + EXPECT_CALL(GetMock(), napi_throw(testing::_, testing::_)) + .WillOnce(testing::Return(napi_generic_failure)); + + error.ThrowErrAddData(env, errCode, data); + GTEST_LOG_(INFO) << "NClassTest-end NErrorTest_ThrowErrAddData_021"; +} + +} // 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..8cf6215cc1960cd4547f6c3d3b51c51c9ae6a8ac --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/napi_mock.cpp @@ -0,0 +1,141 @@ +/* + * 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_create_int32(napi_env env, int32_t value, napi_value* result) +{ + return GetNapiMock().napi_create_int32(env, value, result); +} + +napi_status napi_create_string_utf8(napi_env env, const char* str, size_t length, napi_value* result) +{ + return GetNapiMock().napi_create_string_utf8(env, str, length, result); +} + +napi_status napi_create_error(napi_env env, napi_value code, napi_value msg, napi_value* result) +{ + return GetNapiMock().napi_create_error(env, code, msg, result); +} + +napi_status napi_set_named_property(napi_env env, napi_value object, const char* utf8name, napi_value value) +{ + return GetNapiMock().napi_set_named_property(env, object, utf8name, value); +} + +napi_status napi_get_and_clear_last_exception(napi_env env, napi_value* result) +{ + return GetNapiMock().napi_get_and_clear_last_exception(env, result); +} + +napi_status napi_throw_error(napi_env env, const char* code, const char* msg) +{ + return GetNapiMock().napi_throw_error(env, code, msg); +} + +napi_status napi_throw(napi_env env, napi_value error) +{ + return GetNapiMock().napi_throw(env, error); +} + +} // extern "C" +#endif \ No newline at end of file