Merge pull request !6577 from 37.2/work
This commit is contained in:
openharmony_ci 2024-06-14 09:06:06 +00:00 committed by Gitee
commit 8dd16b56c4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 972 additions and 0 deletions

View File

@ -71,6 +71,7 @@ group("unittest") {
testonly = true
deps = [
":WifiDirectIpManagerTest",
"data:unittest",
"entity:unittest",
]
}

View File

@ -0,0 +1,204 @@
# Copyright (c) 2024 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("../../../../../dsoftbus.gni")
ut_out_path = "dsoftbus/connection/wifi_direct_cpp/data"
wifi_direct_cpp_path = "$dsoftbus_root_path/core/connection/wifi_direct_cpp"
wifi_direct_cpp_unit_path =
"$dsoftbus_root_path/tests/core/connection/wifi_direct_cpp"
config("wifi_direct_include_dirs") {
include_dirs = [
"//third_party/cJSON",
"//third_party/json/include",
"//third_party/googletest/googletest/include",
"//third_party/googletest/googletest/src",
"//third_party/bounds_checking_function/include",
"$dsoftbus_root_path/core/bus_center/utils/include",
"$dsoftbus_root_path/core/authentication/include",
"$dsoftbus_root_path/core/authentication/interface",
"$dsoftbus_root_path/core/bus_center/lnn/net_builder/include",
"$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/include",
"$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/include",
"$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/include",
"$dsoftbus_root_path/core/bus_center/interface",
"$dsoftbus_root_path/core/common/dfx/hisysevent_adapter/include",
"$dsoftbus_root_path/core/common/dfx/interface/include",
"$dsoftbus_root_path/core/connection/interface",
"$dsoftbus_root_path/core/common/include",
"$dsoftbus_root_path/core/discovery/manager/include",
"$dsoftbus_root_path/core/discovery/interface",
"$dsoftbus_root_path/adapter/common/include",
"$dsoftbus_root_path/interfaces/kits/common",
"$dsoftbus_root_path/interfaces/kits/discovery",
"$dsoftbus_root_path/interfaces/kits/bus_center",
"$dsoftbus_root_path/interfaces/inner_kits/lnn",
"$dsoftbus_root_path/core/transmission/trans_channel/proxy/include",
"$wifi_direct_cpp_path",
"$wifi_direct_cpp_unit_path",
]
}
ohos_unittest("LinkInfoTest") {
module_out_path = ut_out_path
configs = [ ":wifi_direct_include_dirs" ]
sources = [
"$wifi_direct_cpp_path/data/ipv4_info.cpp",
"$wifi_direct_cpp_path/data/link_info.cpp",
"$wifi_direct_cpp_path/protocol/tlv_protocol.cpp",
"$wifi_direct_cpp_path/utils/wifi_direct_utils.cpp",
"link_info_test.cpp",
]
deps = [
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
"wifi:wifi_sdk",
]
}
ohos_unittest("NegotiateMessageTest") {
module_out_path = ut_out_path
configs = [ ":wifi_direct_include_dirs" ]
sources = [
"$wifi_direct_cpp_path/data/interface_info.cpp",
"$wifi_direct_cpp_path/data/ipv4_info.cpp",
"$wifi_direct_cpp_path/data/link_info.cpp",
"$wifi_direct_cpp_path/data/negotiate_message.cpp",
"$wifi_direct_cpp_path/protocol/json_protocol.cpp",
"$wifi_direct_cpp_path/protocol/tlv_protocol.cpp",
"$wifi_direct_cpp_path/utils/wifi_direct_utils.cpp",
"negotiate_message_test.cpp",
]
deps = [
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
"wifi:wifi_sdk",
]
}
ohos_unittest("InnerLinkTest") {
module_out_path = ut_out_path
configs = [ ":wifi_direct_include_dirs" ]
sources = [
"$wifi_direct_cpp_path/data/inner_link.cpp",
"$wifi_direct_cpp_path/data/link_manager.cpp",
"$wifi_direct_cpp_unit_path/wifi_direct_mock.cpp",
"inner_link_test.cpp",
]
deps = [
"$dsoftbus_root_path/adapter:softbus_adapter",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
]
}
ohos_unittest("LinkManagerTest") {
module_out_path = ut_out_path
configs = [ ":wifi_direct_include_dirs" ]
sources = [
"$wifi_direct_cpp_path/data/inner_link.cpp",
"$wifi_direct_cpp_path/data/link_manager.cpp",
"$wifi_direct_cpp_path/utils/wifi_direct_anonymous.cpp",
"$wifi_direct_cpp_unit_path/wifi_direct_mock.cpp",
"link_manager_test.cpp",
]
deps = [
"$dsoftbus_root_path/adapter:softbus_adapter",
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
]
}
ohos_unittest("InterfaceInfoTest") {
module_out_path = ut_out_path
configs = [ ":wifi_direct_include_dirs" ]
sources = [
"$wifi_direct_cpp_path/data/interface_info.cpp",
"$wifi_direct_cpp_path/data/ipv4_info.cpp",
"$wifi_direct_cpp_path/protocol/tlv_protocol.cpp",
"$wifi_direct_cpp_path/utils/wifi_direct_utils.cpp",
"interface_info_test.cpp",
]
deps = [
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
"wifi:wifi_sdk",
]
}
ohos_unittest("InterfaceManagerTest") {
module_out_path = ut_out_path
configs = [ ":wifi_direct_include_dirs" ]
sources = [
"$wifi_direct_cpp_path/adapter/p2p_adapter.cpp",
"$wifi_direct_cpp_path/data/interface_info.cpp",
"$wifi_direct_cpp_path/data/interface_manager.cpp",
"$wifi_direct_cpp_path/data/ipv4_info.cpp",
"$wifi_direct_cpp_path/protocol/tlv_protocol.cpp",
"$wifi_direct_cpp_path/utils/wifi_direct_utils.cpp",
"$wifi_direct_cpp_path/wifi_direct_initiator.cpp",
"$wifi_direct_cpp_unit_path/wifi_direct_mock.cpp",
"interface_manager_test.cpp",
]
deps = [
"//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
"wifi:wifi_sdk",
]
}
group("unittest") {
testonly = true
deps = [
":InterfaceInfoTest",
":InterfaceManagerTest",
":LinkInfoTest",
":NegotiateMessageTest",
]
}

View File

@ -0,0 +1,167 @@
/*
* Copyright (c) 2024 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 "data/info_container.h"
#include "data/interface_info.h"
#include "protocol/wifi_direct_protocol_factory.h"
#include <gtest/gtest.h>
using namespace testing::ext;
namespace OHOS::SoftBus {
class InterfaceInfoTest : public testing::Test {
public:
static void SetUpTestCase() { }
static void TearDownTestCase() { }
void SetUp() override { }
void TearDown() override { }
};
/*
* @tc.name: MarshallingTest
* @tc.desc: Test Marshalling and Unmarshalling
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(InterfaceInfoTest, MarshallingTest, TestSize.Level1)
{
InterfaceInfo info1;
info1.SetName("TestName");
Ipv4Info ipv4Info("172.30.1.1");
info1.SetIpString(ipv4Info);
info1.SetSsid("TestSsid");
info1.SetDynamicMac("00:00:00:00:00:00");
info1.SetPsk("TestPsk");
info1.SetCenter20M(10);
info1.SetIsEnable(true);
info1.SetRole(LinkInfo::LinkMode::AP);
info1.SetP2pListenPort(123);
info1.SetBandWidth(20);
info1.SetReuseCount(1);
info1.SetIsAvailable(true);
info1.SetPhysicalRate(1);
auto protocol1 = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::TLV);
protocol1->SetFormat({ TlvProtocol::TLV_TAG_SIZE, TlvProtocol::TLV_LENGTH_SIZE2 });
std::vector<uint8_t> output;
info1.Marshalling(*protocol1, output);
auto protocol2 = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::TLV);
protocol2->SetFormat({ TlvProtocol::TLV_TAG_SIZE, TlvProtocol::TLV_LENGTH_SIZE2 });
InterfaceInfo info2;
info2.Unmarshalling(*protocol2, output);
EXPECT_EQ(info2.GetName(), "TestName");
EXPECT_EQ(info2.GetIpString(), ipv4Info);
EXPECT_EQ(info2.GetSsid(), "TestSsid");
EXPECT_EQ(info2.GetRole(), LinkInfo::LinkMode::AP);
EXPECT_EQ(info2.GetDynamicMac(), "00:00:00:00:00:00");
EXPECT_EQ(info2.GetPsk(), "TestPsk");
EXPECT_EQ(info2.GetCenter20M(), 10);
EXPECT_EQ(info2.IsEnable(), true);
EXPECT_EQ(info2.GetP2pListenPort(), 123);
EXPECT_EQ(info2.GetBandWidth(), 20);
EXPECT_EQ(info2.GetReuseCount(), 1);
EXPECT_EQ(info2.IsAvailable(), true);
EXPECT_EQ(info2.GetPhysicalRate(), 1);
}
/*
* @tc.name: GetAndSetTest01
* @tc.desc: Test GetAndSetTest
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(InterfaceInfoTest, GetAndSetTest01, TestSize.Level1)
{
InterfaceInfo info;
EXPECT_EQ(info.GetName(), "");
info.SetName("TestName");
EXPECT_EQ(info.GetName(), "TestName");
Ipv4Info ipv4Info1("192.168.1.1");
info.SetIpString(ipv4Info1);
auto ipv4Info2 = info.GetIpString();
EXPECT_EQ(ipv4Info2, ipv4Info1);
EXPECT_EQ(info.GetSsid(), "");
info.SetSsid("TestSsid");
EXPECT_EQ(info.GetSsid(), "TestSsid");
EXPECT_EQ(info.GetDynamicMac(), "");
info.SetDynamicMac("00:00:00:00:00:00");
EXPECT_EQ(info.GetDynamicMac(), "00:00:00:00:00:00");
EXPECT_EQ(info.GetRole(), LinkInfo::LinkMode::NONE);
info.SetRole(LinkInfo::LinkMode::AP);
EXPECT_EQ(info.GetRole(), LinkInfo::LinkMode::AP);
EXPECT_EQ(info.GetPsk(), "");
info.SetPsk("TestPsk");
EXPECT_EQ(info.GetPsk(), "TestPsk");
EXPECT_EQ(info.GetCenter20M(), 0);
info.SetCenter20M(10);
EXPECT_EQ(info.GetCenter20M(), 10);
}
/*
* @tc.name: GetAndSetTest02
* @tc.desc: Test GetAndSetTest
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(InterfaceInfoTest, GetAndSetTest02, TestSize.Level1)
{
InterfaceInfo info;
EXPECT_EQ(info.IsEnable(), false);
info.SetIsEnable(true);
EXPECT_EQ(info.IsEnable(), true);
EXPECT_EQ(info.GetBaseMac(), "");
info.SetBaseMac("00:00:00:00:00:00");
EXPECT_EQ(info.GetBaseMac(), "00:00:00:00:00:00");
EXPECT_EQ(info.GetCapability(), 0u);
info.SetCapability(20);
EXPECT_EQ(info.GetCapability(), 20);
std::vector<int> vec = { 65, 66, 67, 68, 69 };
std::vector<int> ret;
EXPECT_EQ(info.GetChannel5GList(), ret);
info.SetChannel5GList(vec);
EXPECT_EQ(info.GetChannel5GList(), vec);
EXPECT_EQ(info.GetReuseCount(), 0);
info.IncreaseRefCount();
EXPECT_EQ(info.GetReuseCount(), 1);
info.SetReuseCount(2);
EXPECT_EQ(info.GetReuseCount(), 2);
info.DecreaseRefCount();
EXPECT_EQ(info.GetReuseCount(), 1);
EXPECT_EQ(info.GetP2pListenPort(), 0);
info.SetP2pListenPort(1);
EXPECT_EQ(info.GetP2pListenPort(), 1);
EXPECT_EQ(info.GetBandWidth(), 0);
info.SetBandWidth(1);
EXPECT_EQ(info.GetBandWidth(), 1);
EXPECT_EQ(info.IsAvailable(), true);
info.SetIsAvailable(false);
EXPECT_EQ(info.IsAvailable(), false);
EXPECT_EQ(info.GetPhysicalRate(), 0);
info.SetPhysicalRate(1);
EXPECT_EQ(info.GetPhysicalRate(), 1);
}
} // namespace OHOS::SoftBus

View File

@ -0,0 +1,81 @@
/*
* Copyright (c) 2024 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 "../wifi_direct_mock.h"
#include "data/interface_info.h"
#include "data/interface_manager.h"
#include <gtest/gtest.h>
using namespace testing::ext;
using namespace testing;
namespace OHOS::SoftBus {
class InterfaceManagerTest : public testing::Test {
public:
static void SetUpTestCase() { }
static void TearDownTestCase() { }
void SetUp() override { }
void TearDown() override { }
};
static bool g_enabledFlag = false;
static int updateInterfaceInfoTrue(InterfaceInfo &info)
{
info.SetIsEnable(true);
return SOFTBUS_OK;
}
static int updateInterfaceInfoFalse(InterfaceInfo &info)
{
info.SetIsEnable(false);
return SOFTBUS_OK;
}
static int readInterfaceInfo(InterfaceInfo &info)
{
g_enabledFlag = info.IsEnable();
return SOFTBUS_OK;
}
/*
* @tc.name: InitInterfaceManagerTest
* @tc.desc: Test manager
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(InterfaceManagerTest, InitInterfaceManagerTest, TestSize.Level1)
{
WifiDirectInterfaceMock wifiDirectInterfaceMock;
EXPECT_CALL(wifiDirectInterfaceMock, Hid2dGetChannelListFor5G).WillRepeatedly(Return(WIFI_SUCCESS));
EXPECT_CALL(wifiDirectInterfaceMock, GetP2pEnableStatus).WillRepeatedly(Return(WIFI_SUCCESS));
InterfaceManager interfaceManager;
interfaceManager.InitInterface(InterfaceInfo::InterfaceType::HML);
interfaceManager.InitInterface(InterfaceInfo::InterfaceType::P2P);
interfaceManager.UpdateInterface(InterfaceInfo::InterfaceType::HML, updateInterfaceInfoTrue);
int hmlResult = interfaceManager.UpdateInterface(InterfaceInfo::InterfaceType::HML, readInterfaceInfo);
EXPECT_EQ(hmlResult, SOFTBUS_OK);
EXPECT_EQ(g_enabledFlag, true);
interfaceManager.UpdateInterface(InterfaceInfo::InterfaceType::P2P, updateInterfaceInfoFalse);
int p2pResult = interfaceManager.UpdateInterface(InterfaceInfo::InterfaceType::P2P, readInterfaceInfo);
EXPECT_EQ(p2pResult, SOFTBUS_OK);
EXPECT_EQ(g_enabledFlag, false);
}
} // namespace OHOS::SoftBus

View File

@ -0,0 +1,190 @@
/*
* Copyright (c) 2024 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 "data/link_info.h"
#include "protocol/wifi_direct_protocol_factory.h"
#include <gtest/gtest.h>
using namespace testing::ext;
namespace OHOS::SoftBus {
class LinkInfoTest : public testing::Test {
public:
static void SetUpTestCase() { }
static void TearDownTestCase() { }
void SetUp() override { }
void TearDown() override { }
};
/*
* @tc.name: SetAndGetBool
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(LinkInfoTest, SetAndGetBool, TestSize.Level1)
{
LinkInfo info;
EXPECT_EQ(info.GetIsDhcp(), false);
info.SetIsDhcp(true);
EXPECT_EQ(info.GetIsDhcp(), true);
EXPECT_EQ(info.GetIsClient(), false);
info.SetIsClient(true);
EXPECT_EQ(info.GetIsClient(), true);
}
/*
* @tc.name: SetAndGetInt
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(LinkInfoTest, SetAndGetInt, TestSize.Level1)
{
LinkInfo info;
EXPECT_EQ(info.GetLocalLinkMode(), LinkInfo::LinkMode::INVALID);
info.SetLocalLinkMode(LinkInfo::LinkMode::HML);
EXPECT_EQ(info.GetLocalLinkMode(), LinkInfo::LinkMode::HML);
EXPECT_EQ(info.GetRemoteLinkMode(), LinkInfo::LinkMode::INVALID);
info.SetRemoteLinkMode(LinkInfo::LinkMode::HML);
EXPECT_EQ(info.GetRemoteLinkMode(), LinkInfo::LinkMode::HML);
EXPECT_EQ(info.GetCenter20M(), 0);
info.SetCenter20M(5180);
EXPECT_EQ(info.GetCenter20M(), 5180);
EXPECT_EQ(info.GetCenterFrequency1(), 0);
info.SetCenterFrequency1(5240);
EXPECT_EQ(info.GetCenterFrequency1(), 5240);
EXPECT_EQ(info.GetCenterFrequency2(), 0);
info.SetCenterFrequency2(5280);
EXPECT_EQ(info.GetCenterFrequency2(), 5280);
EXPECT_EQ(info.GetBandWidth(), 0);
info.SetBandWidth(80);
EXPECT_EQ(info.GetBandWidth(), 80);
EXPECT_EQ(info.GetAuthPort(), 0);
info.SetAuthPort(999);
EXPECT_EQ(info.GetAuthPort(), 999);
EXPECT_EQ(info.GetMaxPhysicalRate(), 0);
info.SetMaxPhysicalRate(866);
EXPECT_EQ(info.GetMaxPhysicalRate(), 866);
EXPECT_EQ(info.GetStatus(), 0);
info.SetStatus(5);
EXPECT_EQ(info.GetStatus(), 5);
}
/*
* @tc.name: SetAndGetString
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(LinkInfoTest, SetAndGetString, TestSize.Level1)
{
LinkInfo info;
EXPECT_EQ(info.GetLocalInterface(), "");
info.SetLocalInterface("chba0");
EXPECT_EQ(info.GetLocalInterface(), "chba0");
EXPECT_EQ(info.GetRemoteInterface(), "");
info.SetRemoteInterface("chba0");
EXPECT_EQ(info.GetRemoteInterface(), "chba0");
EXPECT_EQ(info.GetSsid(), "");
info.SetSsid("OHOS-1234");
EXPECT_EQ(info.GetSsid(), "OHOS-1234");
EXPECT_EQ(info.GetBssid(), "");
info.SetBssid("01:02:03:04:05:06");
EXPECT_EQ(info.GetBssid(), "01:02:03:04:05:06");
EXPECT_EQ(info.GetPsk(), "");
info.SetPsk("12345678");
EXPECT_EQ(info.GetPsk(), "12345678");
EXPECT_EQ(info.GetRemoteDevice(), "");
info.SetRemoteDevice("abcdef");
EXPECT_EQ(info.GetRemoteDevice(), "abcdef");
EXPECT_EQ(info.GetLocalBaseMac(), "");
info.SetLocalBaseMac("01:02:03:04:05:06");
EXPECT_EQ(info.GetLocalBaseMac(), "01:02:03:04:05:06");
EXPECT_EQ(info.GetRemoteBaseMac(), "");
info.SetRemoteBaseMac("01:02:03:04:05:ab");
EXPECT_EQ(info.GetRemoteBaseMac(), "01:02:03:04:05:ab");
}
/*
* @tc.name: SetAndGetIpv4Info
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(LinkInfoTest, SetAndGetIpv4Info, TestSize.Level1)
{
LinkInfo info;
Ipv4Info ipv4Info1("192.168.1.1");
info.SetLocalIpv4Info(ipv4Info1);
auto ipv4Info2 = info.GetLocalIpv4Info();
EXPECT_EQ(ipv4Info2, ipv4Info1);
}
/*
* @tc.name: MarshallingAndUnmarshalling
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(LinkInfoTest, MarshallingAndUnmarshalling, TestSize.Level1)
{
LinkInfo info1;
info1.SetLocalInterface("chba0");
info1.SetRemoteInterface("chba0");
info1.SetLocalBaseMac("01:02:03:04:05:06");
info1.SetRemoteBaseMac("06:05:04:03:02:01");
info1.SetCenter20M(5180);
Ipv4Info localIpv4Info("172.30.1.1");
info1.SetLocalIpv4Info(localIpv4Info);
Ipv4Info remoteIpv4Info("172.30.1.2");
info1.SetRemoteIpv4Info(remoteIpv4Info);
info1.SetIsDhcp(true);
auto protocol1 = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::TLV);
protocol1->SetFormat({ TlvProtocol::TLV_TAG_SIZE, TlvProtocol::TLV_LENGTH_SIZE2 });
std::vector<uint8_t> output;
info1.Marshalling(*protocol1, output);
auto protocol2 = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::TLV);
protocol2->SetFormat({ TlvProtocol::TLV_TAG_SIZE, TlvProtocol::TLV_LENGTH_SIZE2 });
LinkInfo info2;
info2.Unmarshalling(*protocol2, output);
EXPECT_EQ(info2.GetLocalInterface(), "chba0");
EXPECT_EQ(info2.GetRemoteInterface(), "chba0");
EXPECT_EQ(info2.GetLocalBaseMac(), "01:02:03:04:05:06");
EXPECT_EQ(info2.GetRemoteBaseMac(), "06:05:04:03:02:01");
EXPECT_EQ(info2.GetCenter20M(), 5180);
EXPECT_EQ(info2.GetLocalIpv4Info(), localIpv4Info);
EXPECT_EQ(info2.GetRemoteIpv4Info(), remoteIpv4Info);
EXPECT_EQ(info2.GetIsDhcp(), true);
}
} // namespace OHOS::SoftBus

View File

@ -0,0 +1,329 @@
/*
* Copyright (c) 2024 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 "data/link_info.h"
#include "data/negotiate_message.h"
#include "protocol/wifi_direct_protocol_factory.h"
#include <gtest/gtest.h>
using namespace testing::ext;
namespace OHOS::SoftBus {
class NegotiateMessageTest : public testing::Test {
public:
static void SetUpTestCase() { }
static void TearDownTestCase() { }
void SetUp() override { }
void TearDown() override { }
};
/*
* @tc.name: SetAndGetBool
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetBool, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetIsModeStrict(), false);
msg.SetIsModeStrict(true);
EXPECT_EQ(msg.GetIsModeStrict(), true);
EXPECT_EQ(msg.GetIsBridgeSupported(), false);
msg.SetIsBridgeSupported(true);
EXPECT_EQ(msg.GetIsBridgeSupported(), true);
EXPECT_EQ(msg.GetIsProxyEnable(), false);
msg.SetIsProxyEnable(true);
EXPECT_EQ(msg.GetIsProxyEnable(), true);
}
/*
* @tc.name: SetAndGetInt
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetInt, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetMessageType(), NegotiateMessageType::CMD_INVALID);
msg.SetMessageType(NegotiateMessageType::CMD_CONN_V2_REQ_1);
EXPECT_EQ(msg.GetMessageType(), NegotiateMessageType::CMD_CONN_V2_REQ_1);
EXPECT_EQ(msg.GetSessionId(), NegotiateMessage::SESSION_ID_INVALID);
msg.SetSessionId(100);
EXPECT_EQ(msg.GetSessionId(), 100);
EXPECT_EQ(msg.GetPreferLinkMode(), LinkInfo::LinkMode::INVALID);
msg.SetPreferLinkMode(LinkInfo::LinkMode::HML);
EXPECT_EQ(msg.GetPreferLinkMode(), LinkInfo::LinkMode::HML);
EXPECT_EQ(msg.GetPreferLinkBandWidth(), 0);
msg.SetPreferLinkBandWidth(80);
EXPECT_EQ(msg.GetPreferLinkBandWidth(), 80);
EXPECT_EQ(msg.GetResultCode(), NegotiateMessage::RESULT_CODE_INVALID);
msg.SetResultCode(204010);
EXPECT_EQ(msg.GetResultCode(), 204010);
}
/*
* @tc.name: SetAndGetString
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetString, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetRemoteDeviceId(), "");
msg.SetRemoteDeviceId("abcdef");
EXPECT_EQ(msg.GetRemoteDeviceId(), "abcdef");
EXPECT_EQ(msg.Get5GChannelList(), "");
msg.Set5GChannelList("36#40#60");
EXPECT_EQ(msg.Get5GChannelList(), "36#40#60");
EXPECT_EQ(msg.Get5GChannelScore(), "");
msg.Set5GChannelScore("90#10#0");
EXPECT_EQ(msg.Get5GChannelScore(), "90#10#0");
}
/*
* @tc.name: SetAndGetByteArray
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetByteArray, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetWifiConfigInfo().empty(), true);
std::vector<uint8_t> wifiConfig { 0x01, 0x02, 0x03, 0x04 };
msg.SetWifiConfigInfo(wifiConfig);
EXPECT_EQ(msg.GetWifiConfigInfo(), wifiConfig);
}
/*
* @tc.name: SetAndGetIpv4InfoArray
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetIpv4InfoArray, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetIpv4InfoArray().empty(), true);
std::vector<Ipv4Info> ipv4Array { Ipv4Info("172.30.1.1"), Ipv4Info("172.30.1.2") };
msg.SetIpv4InfoArray(ipv4Array);
EXPECT_EQ(msg.GetIpv4InfoArray(), ipv4Array);
}
/*
* @tc.name: SetAndGetInterfaceInfoArray
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetInterfaceInfoArray, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetInterfaceInfoArray().empty(), true);
InterfaceInfo info1;
InterfaceInfo info2;
std::vector<InterfaceInfo> infoArray { InterfaceInfo(), InterfaceInfo() };
msg.SetInterfaceInfoArray(infoArray);
EXPECT_EQ(msg.GetInterfaceInfoArray().size(), infoArray.size());
}
/*
* @tc.name: SetAndGetByteArray
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetLinkInfo, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetLinkInfo().GetCenter20M(), 0);
LinkInfo linkInfo;
linkInfo.SetCenter20M(5180);
linkInfo.SetLocalBaseMac("01:02:03:04:05:06");
msg.SetLinkInfo(linkInfo);
EXPECT_EQ(msg.GetLinkInfo().GetCenter20M(), 5180);
EXPECT_EQ(msg.GetLinkInfo().GetLocalBaseMac(), "01:02:03:04:05:06");
}
/*
* @tc.name: MarshallingAndUnmarshalling
* @tc.desc: marshalling and unmarshalling of tlv
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, MarshallingAndUnmarshallingOfTlv, TestSize.Level1)
{
NegotiateMessage msg1;
msg1.SetSessionId(1);
msg1.SetMessageType(NegotiateMessageType::CMD_CONN_V2_REQ_1);
msg1.SetIpv4InfoArray({ Ipv4Info("172.30.1.1"), Ipv4Info("172.30.2.1") });
LinkInfo linkInfo1;
linkInfo1.SetCenter20M(5180);
linkInfo1.SetLocalBaseMac("01:02:03:04:05:06");
msg1.SetLinkInfo(linkInfo1);
auto protocol1 = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::TLV);
protocol1->SetFormat({ TlvProtocol::TLV_TAG_SIZE, TlvProtocol::TLV_LENGTH_SIZE2 });
std::vector<uint8_t> output;
msg1.Marshalling(*protocol1, output);
std::cout << output.size() << std::endl;
NegotiateMessage msg2;
auto protocol2 = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::TLV);
protocol2->SetFormat({ TlvProtocol::TLV_TAG_SIZE, TlvProtocol::TLV_LENGTH_SIZE2 });
msg2.Unmarshalling(*protocol2, output);
LinkInfo linkInfo2 = msg2.GetLinkInfo();
EXPECT_EQ(msg1.GetSessionId(), msg2.GetSessionId());
EXPECT_EQ(msg1.GetMessageType(), msg2.GetMessageType());
EXPECT_EQ(linkInfo1.GetCenter20M(), linkInfo2.GetCenter20M());
EXPECT_EQ(linkInfo1.GetLocalBaseMac(), linkInfo2.GetLocalBaseMac());
std::vector<Ipv4Info> ipv4Array1 = msg1.GetIpv4InfoArray();
std::vector<Ipv4Info> ipv4Array2 = msg2.GetIpv4InfoArray();
EXPECT_EQ(linkInfo1.GetCenter20M(), linkInfo2.GetCenter20M());
EXPECT_EQ(linkInfo1.GetLocalBaseMac(), linkInfo2.GetLocalBaseMac());
}
/*
* @tc.name: SetAndGetLegacyP2p01
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetLegacyP2p01, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetLegacyP2pGcChannelList(), "");
msg.SetLegacyP2pGcChannelList("1#2#3");
EXPECT_EQ(msg.GetLegacyP2pGcChannelList(), "1#2#3");
EXPECT_EQ(msg.GetLegacyP2pStationFrequency(), 0);
msg.SetLegacyP2pStationFrequency(5180);
EXPECT_EQ(msg.GetLegacyP2pStationFrequency(), 5180);
EXPECT_EQ(msg.GetLegacyP2pRole(), static_cast<int>(WifiDirectRole::WIFI_DIRECT_ROLE_INVALID));
msg.SetLegacyP2pRole(WifiDirectRole::WIFI_DIRECT_ROLE_HML);
EXPECT_EQ(msg.GetLegacyP2pRole(), static_cast<int>(WifiDirectRole::WIFI_DIRECT_ROLE_HML));
EXPECT_EQ(msg.GetLegacyP2pExpectedRole(), static_cast<int>(WifiDirectRole::WIFI_DIRECT_ROLE_INVALID));
msg.SetLegacyP2pExpectedRole(WifiDirectRole::WIFI_DIRECT_ROLE_HML);
EXPECT_EQ(msg.GetLegacyP2pExpectedRole(), static_cast<int>(WifiDirectRole::WIFI_DIRECT_ROLE_HML));
EXPECT_EQ(msg.GetLegacyP2pVersion(), 0);
msg.SetLegacyP2pVersion(2);
EXPECT_EQ(msg.GetLegacyP2pVersion(), 2);
EXPECT_EQ(msg.GetLegacyP2pGcIp(), "");
msg.SetLegacyP2pGcIp("192.168.43.2");
EXPECT_EQ(msg.GetLegacyP2pGcIp(), "192.168.43.2");
EXPECT_EQ(msg.GetLegacyP2pWideBandSupported(), false);
msg.SetLegacyP2pWideBandSupported(true);
EXPECT_EQ(msg.GetLegacyP2pWideBandSupported(), true);
EXPECT_EQ(msg.GetLegacyP2pGroupConfig(), "");
msg.SetLegacyP2pGroupConfig("OHOS-1234\n00:01:02:03:04:05\n00001111\n5180");
EXPECT_EQ(msg.GetLegacyP2pGroupConfig(), "OHOS-1234\n00:01:02:03:04:05\n00001111\n5180");
}
/*
* @tc.name: SetAndGetLegacyP2p02
* @tc.desc: check set and get methods
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, SetAndGetLegacyP2p02, TestSize.Level1)
{
NegotiateMessage msg;
EXPECT_EQ(msg.GetLegacyP2pMac(), "");
msg.SetLegacyP2pMac("01:02:03:04:05:06");
EXPECT_EQ(msg.GetLegacyP2pMac(), "01:02:03:04:05:06");
EXPECT_EQ(msg.GetLegacyP2pGoIp(), "");
msg.SetLegacyP2pGoIp("192.168.43.1");
EXPECT_EQ(msg.GetLegacyP2pGoIp(), "192.168.43.1");
EXPECT_EQ(msg.GetLegacyP2pGoMac(), "");
msg.SetLegacyP2pGoMac("01:02:03:04:05:06");
EXPECT_EQ(msg.GetLegacyP2pGoMac(), "01:02:03:04:05:06");
EXPECT_EQ(msg.GetLegacyP2pGoPort(), 0);
msg.SetLegacyP2pGoPort(4321);
EXPECT_EQ(msg.GetLegacyP2pGoPort(), 4321);
EXPECT_EQ(msg.GetLegacyP2pIp(), "");
msg.SetLegacyP2pIp("192.168.43.4");
EXPECT_EQ(msg.GetLegacyP2pIp(), "192.168.43.4");
EXPECT_EQ(msg.GetLegacyP2pResult(), LegacyResult::OK);
msg.SetLegacyP2pResult(LegacyResult::V1_ERROR_IF_NOT_AVAILABLE);
EXPECT_EQ(msg.GetLegacyP2pResult(), LegacyResult::V1_ERROR_IF_NOT_AVAILABLE);
EXPECT_EQ(msg.GetLegacyP2pContentType(), LegacyContentType::INVALID);
msg.SetLegacyP2pContentType(LegacyContentType::GC_INFO);
EXPECT_EQ(msg.GetLegacyP2pContentType(), LegacyContentType::GC_INFO);
EXPECT_EQ(msg.GetLegacyP2pGcMac(), "");
msg.SetLegacyP2pGcMac("01:02:03:04:05:06");
EXPECT_EQ(msg.GetLegacyP2pGcMac(), "01:02:03:04:05:06");
EXPECT_EQ(msg.GetLegacyP2pCommandType(), LegacyCommandType::CMD_INVALID);
msg.SetLegacyP2pCommandType(LegacyCommandType::CMD_DISCONNECT_V1_REQ);
EXPECT_EQ(msg.GetLegacyP2pCommandType(), LegacyCommandType::CMD_DISCONNECT_V1_REQ);
}
/*
* @tc.name: MarshallingAndUnmarshallingOfJson
* @tc.desc: marshalling and unmarshalling of json
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(NegotiateMessageTest, MarshallingAndUnmarshallingOfJson, TestSize.Level1)
{
NegotiateMessage msg1;
msg1.SetLegacyP2pCommandType(LegacyCommandType::CMD_CONN_V1_REQ);
msg1.SetLegacyP2pContentType(LegacyContentType::GC_INFO);
msg1.SetLegacyP2pRole(WifiDirectRole::WIFI_DIRECT_ROLE_HML);
msg1.SetLegacyP2pGroupConfig("OHOS-1234\n00:01:02:03:04:05\n00001111\n5180");
auto protocol1 = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::JSON);
std::vector<uint8_t> output;
msg1.Marshalling(*protocol1, output);
std::string outJson;
outJson.insert(outJson.end(), output.begin(), output.end());
std::cout << outJson << std::endl;
NegotiateMessage msg2;
auto protocol2 = WifiDirectProtocolFactory::CreateProtocol(ProtocolType::JSON);
msg2.Unmarshalling(*protocol2, output);
EXPECT_EQ(msg1.GetLegacyP2pCommandType(), msg2.GetLegacyP2pCommandType());
EXPECT_EQ(msg1.GetLegacyP2pContentType(), msg2.GetLegacyP2pContentType());
EXPECT_EQ(msg1.GetLegacyP2pRole(), msg2.GetLegacyP2pRole());
EXPECT_EQ(msg1.GetLegacyP2pGroupConfig(), msg2.GetLegacyP2pGroupConfig());
}
} // namespace OHOS::SoftBus