diff --git a/bundle.json b/bundle.json index 054a11c5a..7bfa0b17a 100644 --- a/bundle.json +++ b/bundle.json @@ -110,7 +110,8 @@ "//foundation/communication/dsoftbus/tests/sdk/bus_center:benchmarktest", "//foundation/communication/dsoftbus/tests/sdk/discovery:benchmarktest", "//foundation/communication/dsoftbus/tests/sdk/transmission:benchmarktest", - "//foundation/communication/dsoftbus/tests/sdk/transmission:integration_test" + "//foundation/communication/dsoftbus/tests/sdk/transmission:integration_test", + "//foundation/communication/dsoftbus/tests/sdk/frame/common:unittest" ] } } diff --git a/tests/sdk/frame/common/BUILD.gn b/tests/sdk/frame/common/BUILD.gn new file mode 100644 index 000000000..fe7a41ed1 --- /dev/null +++ b/tests/sdk/frame/common/BUILD.gn @@ -0,0 +1,73 @@ +# Copyright (c) 2022 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. + +import("//build/test.gni") +import("//foundation/communication/dsoftbus/dsoftbus.gni") + +module_output_path = "dsoftbus/transmission" + +## UnitTest SoftbusClientEventManagerTest {{{ +ohos_unittest("SoftbusClientEventManagerTest") { + module_out_path = module_output_path + sources = [ "softbus_client_event_manager_test.cpp" ] + + include_dirs = [ + "$dsoftbus_root_path/core/common/include", + "$dsoftbus_root_path/core/frame/common/include", + "$dsoftbus_root_path/core/transmission/common/include", + "$dsoftbus_root_path/sdk/transmission/session/include", + "$dsoftbus_root_path/sdk/transmission/trans_channel/udp/stream/libsoftbus_stream", + "$dsoftbus_root_path/sdk/transmission/trans_channel/udp/stream/libsoftbus_stream/include", + "$dsoftbus_root_path/adapter/common/include", + "$dsoftbus_root_path/components/nstackx/fillp/include", + "$dsoftbus_root_path/sdk/transmission/trans_channel/udp/stream/include", + "$dsoftbus_root_path/sdk/transmission/trans_channel/udp/common/include", + "$dsoftbus_root_path/sdk/transmission/trans_channel/udp/stream/adaptor/include", + "$dsoftbus_root_path/sdk/frame/common/src", + "$dsoftbus_root_path/sdk/frame/common/include", + "$dsoftbus_root_path/interfaces/kits/common", + "$dsoftbus_root_path/sdk/bus_center/manager/include", + ] + + deps = [ + "$dsoftbus_root_path/core/common:softbus_utils", + "$dsoftbus_root_path/core/frame:softbus_server", + "$dsoftbus_root_path/sdk:softbus_client", + "$dsoftbus_root_path/tests/sdk/common:softbus_access_token_test", + "//third_party/bounds_checking_function:libsec_shared", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//third_party/googletest:gtest_main", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + } else { + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + } +} + +group("unittest") { + testonly = true + deps = [] + deps += [ + # deps file + ":SoftbusClientEventManagerTest", + ] +} diff --git a/tests/sdk/frame/common/softbus_client_event_manager_test.cpp b/tests/sdk/frame/common/softbus_client_event_manager_test.cpp new file mode 100644 index 000000000..581337e22 --- /dev/null +++ b/tests/sdk/frame/common/softbus_client_event_manager_test.cpp @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2022 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 "softbus_client_event_manager.h" +#include "softbus_client_frame_manager.h" +#include "softbus_error_code.h" +#include "softbus_adapter_mem.h" + +using namespace std; +using namespace testing::ext; + +namespace OHOS { +#define FRAME_HEADER_LEN 4 + +class SoftbusClientEventManagerTest : public testing::Test { +public: + SoftbusClientEventManagerTest() + {} + ~SoftbusClientEventManagerTest() + {} + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() override + {} + void TearDown() override + {} +}; + +void SoftbusClientEventManagerTest::SetUpTestCase(void) +{} + +void SoftbusClientEventManagerTest::TearDownTestCase(void) +{} + +/** + * @tc.name: EventClientInit001 + * @tc.desc: EventClientInit, use the wrong parameter. + * @tc.desc: EventClientDeinit, use the wrong parameter. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftbusClientEventManagerTest, EventClientInit001, TestSize.Level1) +{ + EventClientDeinit(); + int ret = EventClientInit(); + EXPECT_EQ(SOFTBUS_OK, ret); + + EventClientDeinit(); +} + +/** + * @tc.name: RegisterEventCallback001 + * @tc.desc: RegisterEventCallback, use the wrong parameter. + * @tc.desc: CLIENT_NotifyObserver, use the wrong parameter. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftbusClientEventManagerTest, RegisterEventCallback001, TestSize.Level1) +{ + EventCallback *cb = + (EventCallback *)SoftBusCalloc(sizeof(EventCallback)); + ASSERT_TRUE(cb != nullptr); + + std::unique_ptr data = nullptr; + ssize_t len = 2; + data = std::make_unique(len + FRAME_HEADER_LEN); + + int res = -1; + int ret = RegisterEventCallback((enum SoftBusEvent)res, *cb, data.get() + FRAME_HEADER_LEN); + EXPECT_EQ(SOFTBUS_ERR, ret); + res = 4; + ret = RegisterEventCallback((enum SoftBusEvent)res, *cb, data.get() + FRAME_HEADER_LEN); + EXPECT_EQ(SOFTBUS_ERR, ret); + res = 1; + ret = RegisterEventCallback(EVENT_SERVER_DEATH, NULL, data.get() + FRAME_HEADER_LEN); + EXPECT_EQ(SOFTBUS_ERR, ret); + + + ret = RegisterEventCallback((enum SoftBusEvent)res, *cb, data.get() + FRAME_HEADER_LEN); + EXPECT_EQ(SOFTBUS_ERR, ret); + + unsigned int argLen = 2; + CLIENT_NotifyObserver((enum SoftBusEvent)res, data.get() + FRAME_HEADER_LEN, argLen); + + res = 4; + CLIENT_NotifyObserver(EVENT_SERVER_DEATH, data.get() + FRAME_HEADER_LEN, argLen); + + if (cb != nullptr) { + SoftBusFree(cb); + } +} + +/** + * @tc.name: DelClientPkgName001 + * @tc.desc: DelClientPkgName, use the wrong parameter. + * @tc.desc: EventClientDeinit, use the wrong parameter. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftbusClientEventManagerTest, DelClientPkgName001, TestSize.Level1) +{ + const char *pkgName = "000"; + int32_t ret = InitSoftBus(NULL); + EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret); + + ret = InitSoftBus(pkgName); + EXPECT_EQ(SOFTBUS_OK, ret); + + char *clientName[SOFTBUS_PKGNAME_MAX_NUM] = {0}; + uint32_t clientNameNum = GetSoftBusClientNameList(NULL, SOFTBUS_PKGNAME_MAX_NUM); + EXPECT_EQ(0, clientNameNum); + clientNameNum = GetSoftBusClientNameList(clientName, 0); + EXPECT_EQ(0, clientNameNum); + + clientNameNum = GetSoftBusClientNameList(clientName, SOFTBUS_PKGNAME_MAX_NUM); + EXPECT_NE(0, clientNameNum); +} + +/** + * @tc.name: CheckPackageName001 + * @tc.desc: CheckPackageName, use the wrong parameter. + * @tc.desc: EventClientDeinit, use the wrong parameter. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SoftbusClientEventManagerTest, CheckPackageName001, TestSize.Level1) +{ + const char *pkgName = "000"; + int ret = CheckPackageName(pkgName); + EXPECT_EQ(SOFTBUS_OK, ret); + + const char *tmpPkgName = "000111"; + ret = CheckPackageName(tmpPkgName); + EXPECT_EQ(SOFTBUS_INVALID_PKGNAME, ret); + + ret = CheckPackageName(pkgName); + EXPECT_EQ(SOFTBUS_OK, ret); +} +} // OHOS