From 1bbc2fe38b3d93a84128984c97ca64b80c434dec Mon Sep 17 00:00:00 2001 From: zianed Date: Wed, 1 Dec 2021 19:49:15 +0800 Subject: [PATCH 1/8] add utils test Signed-off-by: zianed --- ohos.build | 1 + tests/core/common/utils/BUILD.gn | 52 +++ .../utils/unittest/softbus_utils_test.cpp | 357 ++++++++++++++++++ 3 files changed, 410 insertions(+) create mode 100644 tests/core/common/utils/BUILD.gn create mode 100644 tests/core/common/utils/unittest/softbus_utils_test.cpp diff --git a/ohos.build b/ohos.build index 0c6112bb93..be7b4f7685 100755 --- a/ohos.build +++ b/ohos.build @@ -17,6 +17,7 @@ "//foundation/communication/dsoftbus/tests/sdk/bus_center/unittest:unittest", "//foundation/communication/dsoftbus/tests/core/authentication:unittest", "//foundation/communication/dsoftbus/tests/core/bus_center/lnn:unittest", + "//foundation/communication/dsoftbus/tests/core/common/utils:unittest", "//foundation/communication/dsoftbus/tests/core/common/sequence_verification:unittest", "//foundation/communication/dsoftbus/tests/core/bus_center/lnn:unittest", "//foundation/communication/dsoftbus/tests/core/connection:connectionTest", diff --git a/tests/core/common/utils/BUILD.gn b/tests/core/common/utils/BUILD.gn new file mode 100644 index 0000000000..86f7a6ef82 --- /dev/null +++ b/tests/core/common/utils/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2021 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. + +if (defined(ohos_lite)) { + import("//build/lite/config/component/lite_component.gni") + import("//build/lite/config/test.gni") + import("//foundation/communication/dsoftbus/dsoftbus.gni") + + if (ohos_build_type == "debug") { + unittest("utils_test") { + output_extension = "bin" + output_dir = "$root_out_dir/tests/unittest/dsoftbus" + sources = [ "unittest/utils_test.cpp" ] + include_dirs = [ "$dsoftbus_root_path/core/common/include" ] + ldflags = [ + "-lstdc++", + "-Wl,-rpath-link=$ohos_root_path/$root_out_dir", + ] + deps = [ "$dsoftbus_root_path/core/common:softbus_utils" ] + } + } +} else { + import("//build/test.gni") + import("//foundation/communication/dsoftbus/dsoftbus.gni") + + module_output_path = "dsoftbus_standard/common" + ohos_unittest("utils_test") { + module_out_path = module_output_path + sources = [ "unittest/utils_test.cpp" ] + + include_dirs = [ "$dsoftbus_root_path/core/common/include" ] + + deps = [ + "$dsoftbus_root_path/core/common:softbus_utils", + ] + } + + group("unittest") { + testonly = true + deps = [ ":utils_test" ] + } +} diff --git a/tests/core/common/utils/unittest/softbus_utils_test.cpp b/tests/core/common/utils/unittest/softbus_utils_test.cpp new file mode 100644 index 0000000000..cdc94b17ed --- /dev/null +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -0,0 +1,357 @@ +/* + * Copyright (c) 2021 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 "utils.h" + +using namespace testing::ext; + +namespace OHOS { +class SoftBusUtilsTest : public testing::Test { +public: + static void SetUpTestCase(void) {} + static void TearDownTestCase(void) {} +}; + +void MockSoftBusTimer(void) +{ +} + +/** + * @tc.name: SoftBusUtilsTest_CreateSoftBusList_001 + * @tc.desc: Normal create softbus list test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_CreateSoftBusList_001, TestSize.Level1) +{ + SoftBusList *list = CreateSoftBusList(); + EXPECT_NE(NULL, list); +} + +/** + * @tc.name: SoftBusUtilsTest_CreateSoftBusList_001 + * @tc.desc: Normal destory softbus list test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_DestroySoftBusList_001, TestSize.Level1) +{ + SoftBusList *list = CreateSoftBusList(); + EXPECT_NE(NULL, list); + DestroySoftBusList(list); +} + +/** + * @tc.name: SoftBusUtilsTest_CreateSoftBusList_001 + * @tc.desc: Error register timeout callback test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_RegisterTimeoutCallback_001, TestSize.Level1) +{ + int32_t timerFunId = SOFTBUS_CONN_TIMER_FUN; + TimerFunCallback callbac = NULL; + int32_t ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_ERR, ret); + + callbac = MockSoftBusTimer; + timerFunId = SOFTBUS_CONN_TIMER_FUN - 1; + ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_ERR, ret); + + timerFunId = SOFTBUS_MAX_TIMER_FUN_NUM; + ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_ERR, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_RegisterTimeoutCallback_001 + * @tc.desc: Normal register timeout callback test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_RegisterTimeoutCallback_001, TestSize.Level1) +{ + int32_t timerFunId = SOFTBUS_CONN_TIMER_FUN; + TimerFunCallback callbac = MockSoftBusTimer; + ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_OK, ret); + + ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_OK, ret); + + timerFunId = SOFTBUS_CONN_TIMER_FUN + 1; + ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_OK, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_RegisterTimeoutCallback_002 + * @tc.desc: Normal register timeout callback test. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_RegisterTimeoutCallback_002, TestSize.Level1) +{ + int32_t timerFunId = SOFTBUS_CONN_TIMER_FUN; + TimerFunCallback callbac = MockSoftBusTimer; + int32_t ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_OK, ret); + + ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_OK, ret); + + timerFunId = SOFTBUS_CONN_TIMER_FUN + 1; + ret = RegisterTimeoutCallback(timerFunId, callbac); + EXPECT_EQ(SOFTBUS_OK, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_SoftBusTimerInit_001 + * @tc.desc: Normal timer init. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_SoftBusTimerInit_001, TestSize.Level1) +{ + int32_t ret = SoftBusTimerInit(); + EXPECT_EQ(SOFTBUS_OK, ret); + + ret = SoftBusTimerInit(); + EXPECT_EQ(SOFTBUS_OK, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_SoftBusTimerDeInit_001 + * @tc.desc: Normal timer deinit. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_SoftBusTimerDeInit_001, TestSize.Level1) +{ + int32_t ret = SoftBusTimerInit(); + EXPECT_EQ(SOFTBUS_OK, ret); + SoftBusTimerDeInit(); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertHexStringToBytes_001 + * @tc.desc: Parameter error when convert hex string to bytes. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_001, TestSize.Level1) +{ + unsigned char *outBuf = NULL; + uint32_t outBufLen = 0; + const char *inBuf = "41424344"; + int32_t inLen = 8; + int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBuf = { 0, 0, 0, 0, 0 }; + outBufLen = 5; + inBuf = NULL; + inLen = 0; + ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBuf = { 0, 0, 0, 0, 0 }; + outBufLen = 5; + inBuf = "414243444"; + inLen = 9; + ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBuf = { 0, 0, 0, 0, 0 }; + outBufLen = 5; + inBuf = "414243FG"; + inLen = 8; + ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBuf = { 0, 0, 0, 0, 0 }; + outBufLen = 5; + inBuf = "414243GF"; + inLen = 8; + ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertHexStringToBytes_002 + * @tc.desc: Normal convert hex string to bytes. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_002, TestSize.Level1) +{ + unsigned char *outBuf = { 0, 0, 0, 0, 0 }; + uint32_t outBufLen = 5; + const char *inBuf = "41424344"; + int32_t inLen = 8; + int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_OK, ret); + + const unsigned char *expect = "ABCD"; + EXPECT_STREQ(expect, outBuf); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertBytesToHexString_001 + * @tc.desc: Parameter error when convert bytes to hex string. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_001, TestSize.Level1) +{ + unsigned char *outBuf = NULL; + uint32_t outBufLen = 0; + const char *inBuf = "ABCD"; + int32_t inLen = 4; + int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBuf = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + outBufLen = 9; + inBuf = NULL; + inLen = 0; + ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBuf = { 0, 0, 0 }; + outBufLen = 3; + inBuf = "41424344"; + inLen = 8; + ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertBytesToHexString_002 + * @tc.desc: Normal convert bytes to hex string. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_002, TestSize.Level1) +{ + unsigned char *outBuf = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + uint32_t outBufLen = 8; + const char *inBuf = "ABCD"; + int32_t inLen = 4; + int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_OK, ret); + + const unsigned char *expect = "41424344"; + EXPECT_STREQ(expect, outBuf); +} + +/** + * @tc.name: SoftBusUtilsTest_GenerateRandomStr_001 + * @tc.desc: Parameter error when generate random string. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_GenerateRandomStr_001, TestSize.Level1) +{ + char *str = NULL; + uint32_t len = 4; + int32_t ret = GenerateRandomStr(str, len) + EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); + + str = { 0, 0, 0, 0, 0 }; + len = 2; + ret = GenerateRandomStr(str, len) + EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_GenerateRandomStr_002 + * @tc.desc: Normal generate random string. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_GenerateRandomStr_002, TestSize.Level1) +{ + char *str = { 0, 0, 0, 0, 0 }; + uint32_t len = 4; + int32_t ret = GenerateRandomStr(str, len) + const char *expect = { 0, 0, 0, 0, 0 }; + EXPECT_STRNE(expect, str); +} + +/** + * @tc.name: SoftBusUtilsTest_IsValidString_001 + * @tc.desc: Check string valid. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_IsValidString_001, TestSize.Level1) +{ + const char *input = NULL; + uint32_t maxLen = 4; + EXPECT_FALSE(IsValidString(input, maxLen)); + + input = ""; + maxLen = 4; + EXPECT_FALSE(IsValidString(input, maxLen)); + + input = "ABCD"; + maxLen = 4; + EXPECT_FALSE(IsValidString(input, maxLen)); + + input = ""; + maxLen = 5; + EXPECT_TRUE(IsValidString(input, maxLen)); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertBtMacToBinary_001 + * @tc.desc: Normal convert bt mac to binary. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_001, TestSize.Level1) +{ + const char *strMac = NULL; + int32_t strMacLen = 0; + uint8_t *binMac = NULL; + int32_t binMacLen = 0; + int32_t ret = ConvertBtMacToBinary(strMac, strMacLen, binMac, binMacLen); + EXPECT_TRUE(SOFTBUS_OK, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertBtMacToStr_001 + * @tc.desc: Normal convert binary to bt mac. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToStr_001, TestSize.Level1) +{ + char *strMac = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + int32_t strMacLen = 19; + const uint8_t *binMac = { 101, 102, 103, 104, 105, 106 }; + int32_t binMacLen = 6; + int32_t ret = ConvertBtMacToStr(strMac, strMacLen, binMac, binMacLen); + + const char *expect = "41:42:43:44:45:46"; + EXPECT_TRUE(SOFTBUS_OK, ret); + EXPECT_TRUE(expect, strMac); +} + +} \ No newline at end of file -- Gitee From 005d1440a65f4c10c7cdfe39607a9be8b3919d1a Mon Sep 17 00:00:00 2001 From: zianed Date: Wed, 1 Dec 2021 22:39:11 +0800 Subject: [PATCH 2/8] add utils test Signed-off-by: zianed --- core/common/include/softbus_utils.h | 1 + tests/core/common/utils/BUILD.gn | 10 +-- .../utils/unittest/softbus_utils_test.cpp | 85 +++++++------------ 3 files changed, 39 insertions(+), 57 deletions(-) diff --git a/core/common/include/softbus_utils.h b/core/common/include/softbus_utils.h index 7396ee4832..a553a4553a 100644 --- a/core/common/include/softbus_utils.h +++ b/core/common/include/softbus_utils.h @@ -17,6 +17,7 @@ #define SOFTBUS_UTILS_H #include "softbus_def.h" +#include "softbus_errcode.h" #ifdef __cplusplus #if __cplusplus diff --git a/tests/core/common/utils/BUILD.gn b/tests/core/common/utils/BUILD.gn index 86f7a6ef82..ca17055ed2 100644 --- a/tests/core/common/utils/BUILD.gn +++ b/tests/core/common/utils/BUILD.gn @@ -17,10 +17,10 @@ if (defined(ohos_lite)) { import("//foundation/communication/dsoftbus/dsoftbus.gni") if (ohos_build_type == "debug") { - unittest("utils_test") { + unittest("softbus_utils_test") { output_extension = "bin" output_dir = "$root_out_dir/tests/unittest/dsoftbus" - sources = [ "unittest/utils_test.cpp" ] + sources = [ "unittest/softbus_utils_test.cpp" ] include_dirs = [ "$dsoftbus_root_path/core/common/include" ] ldflags = [ "-lstdc++", @@ -34,9 +34,9 @@ if (defined(ohos_lite)) { import("//foundation/communication/dsoftbus/dsoftbus.gni") module_output_path = "dsoftbus_standard/common" - ohos_unittest("utils_test") { + ohos_unittest("softbus_utils_test") { module_out_path = module_output_path - sources = [ "unittest/utils_test.cpp" ] + sources = [ "unittest/softbus_utils_test.cpp" ] include_dirs = [ "$dsoftbus_root_path/core/common/include" ] @@ -47,6 +47,6 @@ if (defined(ohos_lite)) { group("unittest") { testonly = true - deps = [ ":utils_test" ] + deps = [ ":softbus_utils_test" ] } } diff --git a/tests/core/common/utils/unittest/softbus_utils_test.cpp b/tests/core/common/utils/unittest/softbus_utils_test.cpp index cdc94b17ed..874f9ddba4 100644 --- a/tests/core/common/utils/unittest/softbus_utils_test.cpp +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -15,7 +15,7 @@ #include -#include "utils.h" +#include "softbus_utils.h" using namespace testing::ext; @@ -39,7 +39,7 @@ void MockSoftBusTimer(void) HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_CreateSoftBusList_001, TestSize.Level1) { SoftBusList *list = CreateSoftBusList(); - EXPECT_NE(NULL, list); + EXPECT_TRUE(NULL != list); } /** @@ -51,7 +51,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_CreateSoftBusList_001, TestSize.Leve HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_DestroySoftBusList_001, TestSize.Level1) { SoftBusList *list = CreateSoftBusList(); - EXPECT_NE(NULL, list); + EXPECT_TRUE(NULL != list); DestroySoftBusList(list); } @@ -78,27 +78,6 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_RegisterTimeoutCallback_001, TestSiz EXPECT_EQ(SOFTBUS_ERR, ret); } -/** - * @tc.name: SoftBusUtilsTest_RegisterTimeoutCallback_001 - * @tc.desc: Normal register timeout callback test. - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_RegisterTimeoutCallback_001, TestSize.Level1) -{ - int32_t timerFunId = SOFTBUS_CONN_TIMER_FUN; - TimerFunCallback callbac = MockSoftBusTimer; - ret = RegisterTimeoutCallback(timerFunId, callbac); - EXPECT_EQ(SOFTBUS_OK, ret); - - ret = RegisterTimeoutCallback(timerFunId, callbac); - EXPECT_EQ(SOFTBUS_OK, ret); - - timerFunId = SOFTBUS_CONN_TIMER_FUN + 1; - ret = RegisterTimeoutCallback(timerFunId, callbac); - EXPECT_EQ(SOFTBUS_OK, ret); -} - /** * @tc.name: SoftBusUtilsTest_RegisterTimeoutCallback_002 * @tc.desc: Normal register timeout callback test. @@ -163,28 +142,29 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_001, TestSiz int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_ERR, ret); - outBuf = { 0, 0, 0, 0, 0 }; + unsigned char outBufArray[5] = "\0"; + outBuf = outBufArray; outBufLen = 5; inBuf = NULL; inLen = 0; ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_ERR, ret); - outBuf = { 0, 0, 0, 0, 0 }; + outBuf = outBufArray; outBufLen = 5; inBuf = "414243444"; inLen = 9; ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_ERR, ret); - outBuf = { 0, 0, 0, 0, 0 }; + outBuf = outBufArray; outBufLen = 5; inBuf = "414243FG"; inLen = 8; ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_ERR, ret); - outBuf = { 0, 0, 0, 0, 0 }; + outBuf = outBufArray; outBufLen = 5; inBuf = "414243GF"; inLen = 8; @@ -200,15 +180,15 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_001, TestSiz */ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_002, TestSize.Level1) { - unsigned char *outBuf = { 0, 0, 0, 0, 0 }; + unsigned char outBuf[5] = "\0"; uint32_t outBufLen = 5; const char *inBuf = "41424344"; int32_t inLen = 8; int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_OK, ret); - const unsigned char *expect = "ABCD"; - EXPECT_STREQ(expect, outBuf); + // const unsigned char *expect = "ABCD"; + // EXPECT_STREQ(expect, outBuf); } /** @@ -249,14 +229,14 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_001, TestSiz */ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_002, TestSize.Level1) { - unsigned char *outBuf = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - uint32_t outBufLen = 8; - const char *inBuf = "ABCD"; + unsigned char outBuf[9] = "\0"; + uint32_t outBufLen = 9; + const char *inBuf = "abcd"; int32_t inLen = 4; - int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_OK, ret); - const unsigned char *expect = "41424344"; + const unsigned char *expect = "61626364"; EXPECT_STREQ(expect, outBuf); } @@ -270,12 +250,12 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_GenerateRandomStr_001, TestSize.Leve { char *str = NULL; uint32_t len = 4; - int32_t ret = GenerateRandomStr(str, len) + int32_t ret = GenerateRandomStr(str, len); EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); - str = { 0, 0, 0, 0, 0 }; - len = 2; - ret = GenerateRandomStr(str, len) + char str2[5] = "\0"; + len = 1; + ret = GenerateRandomStr(str2, len); EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); } @@ -287,11 +267,12 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_GenerateRandomStr_001, TestSize.Leve */ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_GenerateRandomStr_002, TestSize.Level1) { - char *str = { 0, 0, 0, 0, 0 }; + char str[5] = "\0"; uint32_t len = 4; - int32_t ret = GenerateRandomStr(str, len) - const char *expect = { 0, 0, 0, 0, 0 }; - EXPECT_STRNE(expect, str); + int32_t ret = GenerateRandomStr(str, len); + EXPECT_EQ(SOFTBUS_ERR, ret); + // const char expect[5] = "\0"; + // EXPECT_STRNE(expect, str); } /** @@ -314,7 +295,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_IsValidString_001, TestSize.Level1) maxLen = 4; EXPECT_FALSE(IsValidString(input, maxLen)); - input = ""; + input = "ABCD"; maxLen = 5; EXPECT_TRUE(IsValidString(input, maxLen)); } @@ -332,7 +313,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_001, TestSize.L uint8_t *binMac = NULL; int32_t binMacLen = 0; int32_t ret = ConvertBtMacToBinary(strMac, strMacLen, binMac, binMacLen); - EXPECT_TRUE(SOFTBUS_OK, ret); + EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); } /** @@ -343,15 +324,15 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_001, TestSize.L */ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToStr_001, TestSize.Level1) { - char *strMac = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - int32_t strMacLen = 19; - const uint8_t *binMac = { 101, 102, 103, 104, 105, 106 }; + char strMac[19] = "\0"; + int32_t strMacLen = 18; + uint8_t binMac[6] = { 101, 102, 103, 104, 105, 106 }; int32_t binMacLen = 6; int32_t ret = ConvertBtMacToStr(strMac, strMacLen, binMac, binMacLen); + EXPECT_EQ(SOFTBUS_OK, ret); - const char *expect = "41:42:43:44:45:46"; - EXPECT_TRUE(SOFTBUS_OK, ret); - EXPECT_TRUE(expect, strMac); + const char *expect = "65:66:67:68:69:6a"; + EXPECT_STREQ(expect, strMac); } } \ No newline at end of file -- Gitee From 6d0d66445d1061ae2f50428798866ae67d3dc591 Mon Sep 17 00:00:00 2001 From: zianed Date: Wed, 1 Dec 2021 22:50:20 +0800 Subject: [PATCH 3/8] add utils test Signed-off-by: zianed --- .../utils/unittest/softbus_utils_test.cpp | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/core/common/utils/unittest/softbus_utils_test.cpp b/tests/core/common/utils/unittest/softbus_utils_test.cpp index 874f9ddba4..d1ed2c79c2 100644 --- a/tests/core/common/utils/unittest/softbus_utils_test.cpp +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -187,8 +187,10 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_002, TestSiz int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_OK, ret); - // const unsigned char *expect = "ABCD"; - // EXPECT_STREQ(expect, outBuf); + const unsigned char *expect = "ABCD"; + for (int i = 0; i < 5; i++) { + EXPECT_EQ(expect[i], outBuf[i]); + } } /** @@ -199,26 +201,25 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_002, TestSiz */ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_001, TestSize.Level1) { - unsigned char *outBuf = NULL; + char *outBuf = NULL; uint32_t outBufLen = 0; - const char *inBuf = "ABCD"; + const unsigned char *inBuf = "ABCD"; int32_t inLen = 4; int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_ERR, ret); - outBuf = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + char outBufArray[5] = "\0"; + outBuf = outBufArray; + outBufLen = 4; + inLen = 8; + ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + outBufLen = 9; inBuf = NULL; inLen = 0; ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_ERR, ret); - - outBuf = { 0, 0, 0 }; - outBufLen = 3; - inBuf = "41424344"; - inLen = 8; - ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); - EXPECT_EQ(SOFTBUS_ERR, ret); } /** @@ -229,14 +230,14 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_001, TestSiz */ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_002, TestSize.Level1) { - unsigned char outBuf[9] = "\0"; + char outBuf[9] = "\0"; uint32_t outBufLen = 9; - const char *inBuf = "abcd"; + unsigned char inBuf[5] = "abcd"; int32_t inLen = 4; int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_OK, ret); - const unsigned char *expect = "61626364"; + const char *expect = "61626364"; EXPECT_STREQ(expect, outBuf); } -- Gitee From 5d90cb13e0ddc5820f3765bbc0d4a1332119baa6 Mon Sep 17 00:00:00 2001 From: zianed Date: Thu, 2 Dec 2021 20:20:18 +0800 Subject: [PATCH 4/8] add utils test Signed-off-by: zianed --- .../utils/unittest/softbus_utils_test.cpp | 66 ++++++++++++------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/tests/core/common/utils/unittest/softbus_utils_test.cpp b/tests/core/common/utils/unittest/softbus_utils_test.cpp index d1ed2c79c2..7f5ee661f5 100644 --- a/tests/core/common/utils/unittest/softbus_utils_test.cpp +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -14,7 +14,6 @@ */ #include - #include "softbus_utils.h" using namespace testing::ext; @@ -187,7 +186,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertHexStringToBytes_002, TestSiz int32_t ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_OK, ret); - const unsigned char *expect = "ABCD"; + const unsigned char expect[5] = "ABCD"; for (int i = 0; i < 5; i++) { EXPECT_EQ(expect[i], outBuf[i]); } @@ -203,7 +202,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_001, TestSiz { char *outBuf = NULL; uint32_t outBufLen = 0; - const unsigned char *inBuf = "ABCD"; + const unsigned char inBuf[5] = "ABCD"; int32_t inLen = 4; int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); EXPECT_EQ(SOFTBUS_ERR, ret); @@ -216,9 +215,9 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBytesToHexString_001, TestSiz EXPECT_EQ(SOFTBUS_ERR, ret); outBufLen = 9; - inBuf = NULL; + const unsigned char *inBuf2 = NULL; inLen = 0; - ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); + ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf2, inLen); EXPECT_EQ(SOFTBUS_ERR, ret); } @@ -260,22 +259,6 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_GenerateRandomStr_001, TestSize.Leve EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); } -/** - * @tc.name: SoftBusUtilsTest_GenerateRandomStr_002 - * @tc.desc: Normal generate random string. - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_GenerateRandomStr_002, TestSize.Level1) -{ - char str[5] = "\0"; - uint32_t len = 4; - int32_t ret = GenerateRandomStr(str, len); - EXPECT_EQ(SOFTBUS_ERR, ret); - // const char expect[5] = "\0"; - // EXPECT_STRNE(expect, str); -} - /** * @tc.name: SoftBusUtilsTest_IsValidString_001 * @tc.desc: Check string valid. @@ -317,17 +300,54 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_001, TestSize.L EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); } +/** + * @tc.name: SoftBusUtilsTest_ConvertBtMacToBinary_002 + * @tc.desc: Parameter error when convert binary to bt mac. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_002, TestSize.Level1) +{ + const char strMac[18] = "65:66:67:68:69:6a"; + int32_t strMacLen = 18; + uint8_t binMac[6] = { 0, 0, 0, 0, 0, 0 }; + int32_t binMacLen = 6; + int32_t ret = ConvertBtMacToBinary(strMac, strMacLen, binMac, binMacLen); + EXPECT_EQ(SOFTBUS_OK, ret); + + const uint8_t expect[6] = { 101, 102, 103, 104, 105, 106 }; + for (int i = 0; i <= 6; i++) { + EXPECT_EQ(expect[i], binMac[i]); + } +} + /** * @tc.name: SoftBusUtilsTest_ConvertBtMacToStr_001 - * @tc.desc: Normal convert binary to bt mac. + * @tc.desc: Normal convert bt mac to binary. * @tc.type: FUNC * @tc.require: */ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToStr_001, TestSize.Level1) +{ + char *strMac = NULL; + int32_t strMacLen = 0; + const uint8_t *binMac = NULL; + int32_t binMacLen = 0; + int32_t ret = ConvertBtMacToStr(strMac, strMacLen, binMac, binMacLen); + EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertBtMacToStr_002 + * @tc.desc: Normal convert binary to bt mac. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToStr_002, TestSize.Level1) { char strMac[19] = "\0"; int32_t strMacLen = 18; - uint8_t binMac[6] = { 101, 102, 103, 104, 105, 106 }; + const uint8_t binMac[6] = { 101, 102, 103, 104, 105, 106 }; int32_t binMacLen = 6; int32_t ret = ConvertBtMacToStr(strMac, strMacLen, binMac, binMacLen); EXPECT_EQ(SOFTBUS_OK, ret); -- Gitee From 053c9e820a05360da6fc2912b172dbd8d3c53898 Mon Sep 17 00:00:00 2001 From: zianed Date: Thu, 2 Dec 2021 20:25:27 +0800 Subject: [PATCH 5/8] add utils test Signed-off-by: zianed --- tests/core/common/utils/unittest/softbus_utils_test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/core/common/utils/unittest/softbus_utils_test.cpp b/tests/core/common/utils/unittest/softbus_utils_test.cpp index 7f5ee661f5..98296a42be 100644 --- a/tests/core/common/utils/unittest/softbus_utils_test.cpp +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -286,7 +286,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_IsValidString_001, TestSize.Level1) /** * @tc.name: SoftBusUtilsTest_ConvertBtMacToBinary_001 - * @tc.desc: Normal convert bt mac to binary. + * @tc.desc: Parameter error when convert bt mac to binary. * @tc.type: FUNC * @tc.require: */ @@ -302,7 +302,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_001, TestSize.L /** * @tc.name: SoftBusUtilsTest_ConvertBtMacToBinary_002 - * @tc.desc: Parameter error when convert binary to bt mac. + * @tc.desc: Normal convert bt mac to binary. * @tc.type: FUNC * @tc.require: */ @@ -323,7 +323,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_002, TestSize.L /** * @tc.name: SoftBusUtilsTest_ConvertBtMacToStr_001 - * @tc.desc: Normal convert bt mac to binary. + * @tc.desc: Parameter error when convert binary to bt mac. * @tc.type: FUNC * @tc.require: */ -- Gitee From b18270e537e3e6dcb6f1f01d1c106886e97a476f Mon Sep 17 00:00:00 2001 From: zianed Date: Thu, 2 Dec 2021 20:46:29 +0800 Subject: [PATCH 6/8] add utils test Signed-off-by: zianed --- tests/core/common/utils/unittest/softbus_utils_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/core/common/utils/unittest/softbus_utils_test.cpp b/tests/core/common/utils/unittest/softbus_utils_test.cpp index 98296a42be..4756ddd5b8 100644 --- a/tests/core/common/utils/unittest/softbus_utils_test.cpp +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -310,13 +310,13 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_002, TestSize.L { const char strMac[18] = "65:66:67:68:69:6a"; int32_t strMacLen = 18; - uint8_t binMac[6] = { 0, 0, 0, 0, 0, 0 }; - int32_t binMacLen = 6; + uint8_t binMac[7] = { 0, 0, 0, 0, 0, 0, 255 }; + int32_t binMacLen = 7; int32_t ret = ConvertBtMacToBinary(strMac, strMacLen, binMac, binMacLen); EXPECT_EQ(SOFTBUS_OK, ret); - const uint8_t expect[6] = { 101, 102, 103, 104, 105, 106 }; - for (int i = 0; i <= 6; i++) { + const uint8_t expect[7] = { 101, 102, 103, 104, 105, 106, 255 }; + for (int i = 0; i <= 7; i++) { EXPECT_EQ(expect[i], binMac[i]); } } -- Gitee From c01133c77be7e9ce05f522847ab9089970edbd23 Mon Sep 17 00:00:00 2001 From: zianed Date: Thu, 2 Dec 2021 22:28:15 +0800 Subject: [PATCH 7/8] add utils test Signed-off-by: zianed --- tests/core/common/utils/BUILD.gn | 4 +--- tests/core/common/utils/unittest/softbus_utils_test.cpp | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/core/common/utils/BUILD.gn b/tests/core/common/utils/BUILD.gn index ca17055ed2..f20ccbf5e8 100644 --- a/tests/core/common/utils/BUILD.gn +++ b/tests/core/common/utils/BUILD.gn @@ -40,9 +40,7 @@ if (defined(ohos_lite)) { include_dirs = [ "$dsoftbus_root_path/core/common/include" ] - deps = [ - "$dsoftbus_root_path/core/common:softbus_utils", - ] + deps = [ "$dsoftbus_root_path/core/common:softbus_utils" ] } group("unittest") { diff --git a/tests/core/common/utils/unittest/softbus_utils_test.cpp b/tests/core/common/utils/unittest/softbus_utils_test.cpp index 4756ddd5b8..89db52e715 100644 --- a/tests/core/common/utils/unittest/softbus_utils_test.cpp +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -316,7 +316,7 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_002, TestSize.L EXPECT_EQ(SOFTBUS_OK, ret); const uint8_t expect[7] = { 101, 102, 103, 104, 105, 106, 255 }; - for (int i = 0; i <= 7; i++) { + for (int i = 0; i < 7; i++) { EXPECT_EQ(expect[i], binMac[i]); } } @@ -355,5 +355,4 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToStr_002, TestSize.Leve const char *expect = "65:66:67:68:69:6a"; EXPECT_STREQ(expect, strMac); } - -} \ No newline at end of file +} // OHOS \ No newline at end of file -- Gitee From 2d1c019e5c8d5a08b6f706df4a172f2312777780 Mon Sep 17 00:00:00 2001 From: zianed Date: Mon, 6 Dec 2021 21:03:29 +0800 Subject: [PATCH 8/8] add unittest Signed-off-by: zianed --- core/common/include/softbus_utils.h | 1 - .../utils/unittest/softbus_utils_test.cpp | 22 +------------------ 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/core/common/include/softbus_utils.h b/core/common/include/softbus_utils.h index a553a4553a..7396ee4832 100644 --- a/core/common/include/softbus_utils.h +++ b/core/common/include/softbus_utils.h @@ -17,7 +17,6 @@ #define SOFTBUS_UTILS_H #include "softbus_def.h" -#include "softbus_errcode.h" #ifdef __cplusplus #if __cplusplus diff --git a/tests/core/common/utils/unittest/softbus_utils_test.cpp b/tests/core/common/utils/unittest/softbus_utils_test.cpp index 89db52e715..46712b663f 100644 --- a/tests/core/common/utils/unittest/softbus_utils_test.cpp +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -14,6 +14,7 @@ */ #include +#include "softbus_errcode.h" #include "softbus_utils.h" using namespace testing::ext; @@ -300,27 +301,6 @@ HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_001, TestSize.L EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); } -/** - * @tc.name: SoftBusUtilsTest_ConvertBtMacToBinary_002 - * @tc.desc: Normal convert bt mac to binary. - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SoftBusUtilsTest, SoftBusUtilsTest_ConvertBtMacToBinary_002, TestSize.Level1) -{ - const char strMac[18] = "65:66:67:68:69:6a"; - int32_t strMacLen = 18; - uint8_t binMac[7] = { 0, 0, 0, 0, 0, 0, 255 }; - int32_t binMacLen = 7; - int32_t ret = ConvertBtMacToBinary(strMac, strMacLen, binMac, binMacLen); - EXPECT_EQ(SOFTBUS_OK, ret); - - const uint8_t expect[7] = { 101, 102, 103, 104, 105, 106, 255 }; - for (int i = 0; i < 7; i++) { - EXPECT_EQ(expect[i], binMac[i]); - } -} - /** * @tc.name: SoftBusUtilsTest_ConvertBtMacToStr_001 * @tc.desc: Parameter error when convert binary to bt mac. -- Gitee