diff --git a/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn b/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn index 0203cfe9b0174a6c5f6e03499e4e1c4cbc384b32..d29a9d846ad6b6f4d4fb34307b33c19e2f14acf9 100644 --- a/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn +++ b/interfaces/test/unittest/filemgmt_libn_test/BUILD.gn @@ -23,7 +23,11 @@ ohos_unittest("filemgmt_libn_test") { module_out_path = "filemanagement/file_api" include_dirs = [ "./include" ] - sources = [ "./src/n_class_test.cpp" ] + sources = [ + "./src/n_class_test.cpp", + "./src/n_error_test.cpp", + "./src/n_val_test.cpp", + ] deps = [ "${utils_path}/filemgmt_libhilog:filemgmt_libhilog", diff --git a/interfaces/test/unittest/filemgmt_libn_test/include/n_error_test.h b/interfaces/test/unittest/filemgmt_libn_test/include/n_error_test.h new file mode 100644 index 0000000000000000000000000000000000000000..f138d67c11e5c4623c160d11e8e5518187492dc0 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/include/n_error_test.h @@ -0,0 +1,35 @@ +/* + * 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 FileManagement { +namespace LibN { +class NErrorLibnTest : 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/n_val_test.h b/interfaces/test/unittest/filemgmt_libn_test/include/n_val_test.h new file mode 100644 index 0000000000000000000000000000000000000000..337217353742054f6dd0a749082652c62167eca9 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/include/n_val_test.h @@ -0,0 +1,39 @@ +/* + * 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 FileManagement { +namespace LibN { + +class NValTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS + +#endif + + 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..2b6232a941f4bac110d7fd0a0ba48dc0b0245db4 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/n_error_test.cpp @@ -0,0 +1,199 @@ +/* + * 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. + */ + +#include "n_error_test.h" +#include "/home/shuai/OpenHarmony/master/foundation/filemanagement/file_api/utils/filemgmt_libn/include/n_error.h" + +using namespace std; +using namespace testing::ext; +using namespace OHOS::FileManagement::LibN; + +namespace OHOS { +namespace FileManagement { +namespace LibN { + +void NErrorLibnTest::SetUpTestCase(void) {} + +void NErrorLibnTest::TearDownTestCase(void) {} + +void NErrorLibnTest::SetUp() {} + +void NErrorLibnTest::TearDown() {} + +/** + * @tc.name: NErrorTest_DefaultConstructor + * @tc.desc: Test for NError default constructor. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_DefaultConstructor, TestSize.Level1) +{ + NError error; + EXPECT_EQ(static_cast(error), false); +} + +/** + * @tc.name: NErrorTest_PosixErrorCode + * @tc.desc: Test for NError class with POSIX error code. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_PosixErrorCode, TestSize.Level1) { + int posixErrorCode = EINVAL; + NError error(posixErrorCode); + EXPECT_TRUE(static_cast(error)); +} + +/** + * @tc.name: NErrorTest_CustomErrorCode + * @tc.desc: Test for NError class with custom error code. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_CustomErrorCode, TestSize.Level1) { + std::function()> customError = []() { + return std::make_tuple(100, "Custom error message"); + }; + NError error(customError); + EXPECT_TRUE(static_cast(error)); +} + +/** + * @tc.name: NErrorTest_KnownErrorCode + * @tc.desc: Test for NError class with known error code. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_KnownErrorCode, TestSize.Level1) +{ + int knownErrorCode = ENOENT; + NError error(knownErrorCode); + EXPECT_EQ(static_cast(error), true); +} + +/** + * @tc.name: NErrorTest_UnknownErrorCode + * @tc.desc: Test for NError class with unknown error code failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_UnknownErrorCode, TestSize.Level1) +{ + int unknownErrorCode = 99; + NError error(unknownErrorCode); + EXPECT_EQ(static_cast(error), true); +} + +/** + * @tc.name: NErrorTest_GetNapiErr_001 + * @tc.desc: Test for NError class with GetNapiErr of one valid parameter failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_GetNapiErr_001, TestSize.Level1) +{ + NError error(ENOENT); + napi_env env = nullptr; + napi_value napiErr = error.GetNapiErr(env); + EXPECT_EQ(napiErr, nullptr); +} + +/** + * @tc.name: NErrorTest_GetNapiError_002 + * @tc.desc: Test for NError class GetNapiErr of one invalid parameter failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_GetNapiError_002, TestSize.Level1) { + NError error(99); + napi_env env = nullptr; + napi_value napiErr = error.GetNapiErr(env); + EXPECT_EQ(napiErr, nullptr); +} + +/** + * @tc.name: NErrorTest_GetNapiErr_003 + * @tc.desc: Test for NError class with GetNapiErr of two valid parameter failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_GetNapiErr_003, TestSize.Level1) +{ + NError error; + napi_env env = nullptr; + napi_value napiErr = error.GetNapiErr(env, ENOENT); + EXPECT_EQ(napiErr, nullptr); +} + +/** + * @tc.name: NErrorTest_GetNapiError_004 + * @tc.desc: Test for NError class GetNapiErr of two invalid parameter failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_GetNapiError_004, TestSize.Level1) { + NError error; + napi_env env = nullptr; + napi_value napiErr = error.GetNapiErr(env, 99); + EXPECT_EQ(napiErr, nullptr); +} + +/** + * @tc.name: NErrorTest_GetNapiErrAddData_001 + * @tc.desc: Test for NError class GetNapiErrAddData invalid errcode failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_GetNapiErrAddData_001, TestSize.Level1) { + NError error; + napi_env env = nullptr; + napi_value val = nullptr; + napi_value val2 = error.GetNapiErrAddData(env, 99, val); + EXPECT_EQ(val2, nullptr); +} + +/** + * @tc.name: NErrorTest_GetNapiErrAddData_002 + * @tc.desc: Test for NError class GetNapiErrAddData valid errcode failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + */ +HWTEST_F(NErrorLibnTest, NErrorTest_GetNapiErrAddData_002, TestSize.Level1) { + NError error; + napi_env env = nullptr; + napi_value val = nullptr; + napi_value val2 = error.GetNapiErrAddData(env, ENOENT, val); + EXPECT_EQ(val2, nullptr); +} + +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/test/unittest/filemgmt_libn_test/src/n_val_test.cpp b/interfaces/test/unittest/filemgmt_libn_test/src/n_val_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbf4c4e5890165d7a4ccd3985a77b2fdbdaab5f8 --- /dev/null +++ b/interfaces/test/unittest/filemgmt_libn_test/src/n_val_test.cpp @@ -0,0 +1,170 @@ +/* + * 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. + */ + +#include "n_val_test.h" +#include "/home/shuai/OpenHarmony/master/foundation/filemanagement/file_api/utils/filemgmt_libn/include/n_val.h" + +using namespace std; +using namespace testing::ext; +using namespace OHOS::FileManagement::LibN; + +namespace OHOS { +namespace FileManagement { +namespace LibN { + +void NValTest::SetUpTestCase(void) {} + +void NValTest::TearDownTestCase(void) {} + +void NValTest::SetUp() {} + +void NValTest::TearDown() {} + +/** + * @tc.name: NValTest_DefaultConstructor + * @tc.desc: Test NVal default constructor. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NValTest, NValTest_DefaultConstructor, TestSize.Level1) +{ + NVal nval; + EXPECT_EQ(static_cast(nval), false); +} + + +/** + * @tc.name: NValTest_ParamConstructor + * @tc.desc: Test NVal parameterized constructors. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NValTest, NValTest_ParamConstructor, TestSize.Level1) +{ + napi_env env = nullptr; + napi_value val = nullptr; + NVal nval(env, val); + EXPECT_EQ(static_cast(nval), false); +} + +/** + * @tc.name: NValTest_CopyConstructor + * @tc.desc: Test NVal copy constructor. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NValTest, NValTest_CopyConstructor, TestSize.Level1) +{ + napi_env env = nullptr; + napi_value val = nullptr; + NVal originalNVal(env, val); + NVal copyNVal = originalNVal; + EXPECT_EQ(static_cast(copyNVal), static_cast(originalNVal)); +} + +/** + * @tc.name: NValTest_ToUTF8String + * @tc.desc: Test for converting NVal to UTF8 string failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NValTest, NValTest_ToUTF8String, TestSize.Level1) +{ + napi_env env = nullptr; + NVal nval(env, NVal::CreateUTF8String(env, "test string").val_); + auto [success, str, len] = nval.ToUTF8String(); + EXPECT_TRUE(!success); +} + +/** + * @tc.name: NValTest_ToInt32 + * @tc.desc: Test for converting NVal to Int32 failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NValTest, NValTest_ToInt32, TestSize.Level1) +{ + napi_env env = nullptr; + NVal nval(env, NVal::CreateInt64(env, 1234).val_); + auto [success, value] = nval.ToInt32(); + EXPECT_TRUE(!success); + EXPECT_EQ(value, 0); +} + +/** + * @tc.name: NValTest_ToUTF16String + * @tc.desc: Test the ability of NVal class to convert to UTF16 strings failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NValTest, NValTest_ToUTF16String, TestSize.Level1) +{ + napi_env env = nullptr; + NVal nval(env, NVal::CreateUTF8String(env, "testString").val_); + auto [success, str, len] = nval.ToUTF16String(); + EXPECT_TRUE(!success); +} + +/** + * @tc.name: NValTest_ToBool + * @tc.desc: Test the ability of NVal classes to convert to Boolean values failed. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NValTest, NValTest_ToBool, TestSize.Level1) +{ + napi_env env = nullptr; + NVal nval(env, NVal::CreateBool(env, true).val_); + auto [success, value] = nval.ToBool(); + EXPECT_TRUE(!success); + EXPECT_EQ(value, false); +} + +/** + * @tc.name: NValTest_HasProp + * @tc.desc: Test whether NVal objects have the ability to specify properties. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level: Level 1 + * @tc.require: AR000HG8M4 + */ +HWTEST_F(NValTest, NValTest_HasProp, TestSize.Level1) +{ + napi_env env = nullptr; + NVal val = NVal::CreateObject(env); + std::string propName = "testProp"; + napi_value propValue; + napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &propValue); + val.AddProp(propName, propValue); + EXPECT_TRUE(!val.HasProp(propName)); +} + +} // namespace LibN +} // namespace FileManagement +} // namespace OHOS