diff --git a/ohos.build b/ohos.build index 0c6112bb93b2d2e7ac9db3227e6137ba8a5f5725..be7b4f76852d1200c3daa986a03ec0ccb641888d 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 0000000000000000000000000000000000000000..f20ccbf5e8e6d8672e2e615b6167ef8488980a15 --- /dev/null +++ b/tests/core/common/utils/BUILD.gn @@ -0,0 +1,50 @@ +# 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("softbus_utils_test") { + output_extension = "bin" + output_dir = "$root_out_dir/tests/unittest/dsoftbus" + sources = [ "unittest/softbus_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("softbus_utils_test") { + module_out_path = module_output_path + sources = [ "unittest/softbus_utils_test.cpp" ] + + include_dirs = [ "$dsoftbus_root_path/core/common/include" ] + + deps = [ "$dsoftbus_root_path/core/common:softbus_utils" ] + } + + group("unittest") { + testonly = true + 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 new file mode 100644 index 0000000000000000000000000000000000000000..46712b663f1376e7094cc92c94a1052099de072a --- /dev/null +++ b/tests/core/common/utils/unittest/softbus_utils_test.cpp @@ -0,0 +1,338 @@ +/* + * 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 "softbus_errcode.h" +#include "softbus_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_TRUE(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_TRUE(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_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); + + 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 = outBufArray; + outBufLen = 5; + inBuf = "414243444"; + inLen = 9; + ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBuf = outBufArray; + outBufLen = 5; + inBuf = "414243FG"; + inLen = 8; + ret = ConvertHexStringToBytes(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBuf = outBufArray; + 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[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[5] = "ABCD"; + for (int i = 0; i < 5; i++) { + EXPECT_EQ(expect[i], outBuf[i]); + } +} + +/** + * @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) +{ + char *outBuf = NULL; + uint32_t outBufLen = 0; + const unsigned char inBuf[5] = "ABCD"; + int32_t inLen = 4; + int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + char outBufArray[5] = "\0"; + outBuf = outBufArray; + outBufLen = 4; + inLen = 8; + ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_ERR, ret); + + outBufLen = 9; + const unsigned char *inBuf2 = NULL; + inLen = 0; + ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf2, 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) +{ + char outBuf[9] = "\0"; + uint32_t outBufLen = 9; + unsigned char inBuf[5] = "abcd"; + int32_t inLen = 4; + int32_t ret = ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); + EXPECT_EQ(SOFTBUS_OK, ret); + + const char *expect = "61626364"; + 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); + + char str2[5] = "\0"; + len = 1; + ret = GenerateRandomStr(str2, len); + EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); +} + +/** + * @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 = "ABCD"; + maxLen = 5; + EXPECT_TRUE(IsValidString(input, maxLen)); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertBtMacToBinary_001 + * @tc.desc: Parameter error when 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_EQ(SOFTBUS_INVALID_PARAM, ret); +} + +/** + * @tc.name: SoftBusUtilsTest_ConvertBtMacToStr_001 + * @tc.desc: Parameter error when convert binary to bt mac. + * @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; + 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); + + const char *expect = "65:66:67:68:69:6a"; + EXPECT_STREQ(expect, strMac); +} +} // OHOS \ No newline at end of file