mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Refactor WifiLanServiceInfotest to improve test coverage.
PiperOrigin-RevId: 794712543
This commit is contained in:
@@ -20,8 +20,8 @@
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "connections/implementation/base_pcp_handler.h"
|
||||
#include "connections/implementation/pcp.h"
|
||||
#include "connections/implementation/webrtc_state.h"
|
||||
#include "internal/platform/base64_utils.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
@@ -14,11 +14,14 @@
|
||||
|
||||
#include "connections/implementation/wifi_lan_service_info.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "connections/implementation/pcp.h"
|
||||
#include "connections/implementation/webrtc_state.h"
|
||||
#include "internal/platform/base64_utils.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/nsd_service_info.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -31,15 +34,18 @@ constexpr Pcp kPcp = Pcp::kP2pCluster;
|
||||
constexpr absl::string_view kEndPointID{"AB12"};
|
||||
constexpr absl::string_view kServiceIDHashBytes{"\x0a\x0b\x0c"};
|
||||
constexpr absl::string_view kEndPointName{"RAWK + ROWL!"};
|
||||
constexpr absl::string_view kUwbAddressBytes{
|
||||
"\x01\x02\x03\x04\xab\xcd\xef\xac"};
|
||||
constexpr WebRtcState kWebRtcState = WebRtcState::kConnectable;
|
||||
|
||||
// TODO(b/169550050): Implement UWBAddress.
|
||||
TEST(WifiLanServiceInfoTest, ConstructionWorks) {
|
||||
ByteArray service_id_hash{std::string(kServiceIDHashBytes)};
|
||||
ByteArray endpoint_info{std::string(kEndPointName)};
|
||||
ByteArray uwb_address{std::string(kUwbAddressBytes)};
|
||||
WifiLanServiceInfo wifi_lan_service_info{
|
||||
kVersion, kPcp, kEndPointID, service_id_hash,
|
||||
endpoint_info, ByteArray{}, kWebRtcState};
|
||||
endpoint_info, {uwb_address}, kWebRtcState};
|
||||
|
||||
EXPECT_TRUE(wifi_lan_service_info.IsValid());
|
||||
EXPECT_EQ(kPcp, wifi_lan_service_info.GetPcp());
|
||||
@@ -47,14 +53,38 @@ TEST(WifiLanServiceInfoTest, ConstructionWorks) {
|
||||
EXPECT_EQ(kEndPointID, wifi_lan_service_info.GetEndpointId());
|
||||
EXPECT_EQ(service_id_hash, wifi_lan_service_info.GetServiceIdHash());
|
||||
EXPECT_EQ(endpoint_info, wifi_lan_service_info.GetEndpointInfo());
|
||||
EXPECT_EQ(uwb_address, wifi_lan_service_info.GetUwbAddress());
|
||||
EXPECT_EQ(kWebRtcState, wifi_lan_service_info.GetWebRtcState());
|
||||
}
|
||||
|
||||
TEST(WifiLanServiceInfoTest, ConstructionFromSerializedStringWorks) {
|
||||
ByteArray service_id_hash{std::string(kServiceIDHashBytes)};
|
||||
ByteArray endpoint_info{std::string(kEndPointName)};
|
||||
ByteArray uwb_address{std::string(kUwbAddressBytes)};
|
||||
WifiLanServiceInfo org_wifi_lan_service_info{
|
||||
kVersion, kPcp, kEndPointID, service_id_hash,
|
||||
endpoint_info, ByteArray{}, kWebRtcState};
|
||||
endpoint_info, uwb_address, kWebRtcState};
|
||||
NsdServiceInfo nsd_service_info{org_wifi_lan_service_info};
|
||||
|
||||
WifiLanServiceInfo wifi_lan_service_info{nsd_service_info};
|
||||
|
||||
EXPECT_TRUE(wifi_lan_service_info.IsValid());
|
||||
EXPECT_EQ(kPcp, wifi_lan_service_info.GetPcp());
|
||||
EXPECT_EQ(kVersion, wifi_lan_service_info.GetVersion());
|
||||
EXPECT_EQ(kEndPointID, wifi_lan_service_info.GetEndpointId());
|
||||
EXPECT_EQ(service_id_hash, wifi_lan_service_info.GetServiceIdHash());
|
||||
EXPECT_EQ(endpoint_info, wifi_lan_service_info.GetEndpointInfo());
|
||||
EXPECT_EQ(uwb_address, wifi_lan_service_info.GetUwbAddress());
|
||||
EXPECT_EQ(kWebRtcState, wifi_lan_service_info.GetWebRtcState());
|
||||
}
|
||||
|
||||
TEST(WifiLanServiceInfoTest,
|
||||
ConstructionFromSerializedStringWorksNoUwbAddress) {
|
||||
ByteArray service_id_hash{std::string(kServiceIDHashBytes)};
|
||||
ByteArray endpoint_info{std::string(kEndPointName)};
|
||||
WifiLanServiceInfo org_wifi_lan_service_info{
|
||||
kVersion, kPcp, kEndPointID, service_id_hash,
|
||||
endpoint_info, {}, kWebRtcState};
|
||||
NsdServiceInfo nsd_service_info{org_wifi_lan_service_info};
|
||||
|
||||
WifiLanServiceInfo wifi_lan_service_info{nsd_service_info};
|
||||
@@ -164,6 +194,94 @@ TEST(WifiLanServiceInfoTest, ConstructionFailsWithLongEndpointInfoLength) {
|
||||
EXPECT_FALSE(wifi_lan_service_info.IsValid());
|
||||
}
|
||||
|
||||
|
||||
TEST(WifiLanServiceInfoTest, ConstructionBadEndPointInfo) {
|
||||
ByteArray service_id_hash{std::string(kServiceIDHashBytes)};
|
||||
ByteArray endpoint_info{std::string(kEndPointName)};
|
||||
ByteArray uwb_address{std::string(kUwbAddressBytes)};
|
||||
WifiLanServiceInfo org_wifi_lan_service_info{
|
||||
kVersion, kPcp, kEndPointID, service_id_hash,
|
||||
endpoint_info, uwb_address, kWebRtcState};
|
||||
NsdServiceInfo nsd_service_info{org_wifi_lan_service_info};
|
||||
|
||||
// Create a ByteArray larger than the allowed size.
|
||||
ByteArray bad_endpoint_info(WifiLanServiceInfo::kMaxEndpointInfoLength + 2);
|
||||
// Base64 encode the oversized ByteArray.
|
||||
std::string encoded_bad_endpoint_info =
|
||||
Base64Utils::Encode(bad_endpoint_info);
|
||||
// Set the TXT record with the Base64 encoded bad endpoint info.
|
||||
nsd_service_info.SetTxtRecord(
|
||||
std::string(WifiLanServiceInfo::kKeyEndpointInfo),
|
||||
encoded_bad_endpoint_info);
|
||||
|
||||
WifiLanServiceInfo wifi_lan_service_info{nsd_service_info};
|
||||
|
||||
EXPECT_FALSE(wifi_lan_service_info.IsValid());
|
||||
}
|
||||
|
||||
|
||||
TEST(WifiLanServiceInfoTest, ConstructionBadServiceName) {
|
||||
ByteArray service_id_hash{std::string(kServiceIDHashBytes)};
|
||||
ByteArray endpoint_info{std::string(kEndPointName)};
|
||||
ByteArray uwb_address{std::string(kUwbAddressBytes)};
|
||||
WifiLanServiceInfo org_wifi_lan_service_info{
|
||||
kVersion, kPcp, kEndPointID, service_id_hash,
|
||||
endpoint_info, uwb_address, kWebRtcState};
|
||||
|
||||
NsdServiceInfo nsd_service_info_1{org_wifi_lan_service_info};
|
||||
nsd_service_info_1.SetServiceName("");
|
||||
WifiLanServiceInfo wifi_lan_service_info_1{nsd_service_info_1};
|
||||
EXPECT_FALSE(wifi_lan_service_info_1.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info_2{org_wifi_lan_service_info};
|
||||
// ServiceName must be at least 9 bytes long, purposely set it to 4 bytes.
|
||||
nsd_service_info_2.SetServiceName("AB12");
|
||||
WifiLanServiceInfo wifi_lan_service_info_2{nsd_service_info_2};
|
||||
EXPECT_FALSE(wifi_lan_service_info_2.IsValid());
|
||||
}
|
||||
|
||||
|
||||
TEST(WifiLanServiceInfoTest, ConstructionBadFields) {
|
||||
ByteArray service_id_hash{std::string(kServiceIDHashBytes)};
|
||||
ByteArray endpoint_info{std::string(kEndPointName)};
|
||||
ByteArray uwb_address{std::string(kUwbAddressBytes)};
|
||||
WifiLanServiceInfo org_wifi_lan_service_info{
|
||||
kVersion, kPcp, kEndPointID, service_id_hash,
|
||||
endpoint_info, uwb_address, kWebRtcState};
|
||||
|
||||
NsdServiceInfo nsd_service_info_1{org_wifi_lan_service_info};
|
||||
auto service_name = Base64Utils::Decode(nsd_service_info_1.GetServiceName());
|
||||
// Set Version to wrong value.
|
||||
if (!service_name.Empty()) {
|
||||
char* service_name_ptr = service_name.data();
|
||||
service_name_ptr[0] = 0x42;
|
||||
}
|
||||
nsd_service_info_1.SetServiceName(Base64Utils::Encode(service_name));
|
||||
WifiLanServiceInfo wifi_lan_service_info_1{nsd_service_info_1};
|
||||
EXPECT_FALSE(wifi_lan_service_info_1.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info_2{org_wifi_lan_service_info};
|
||||
service_name = Base64Utils::Decode(nsd_service_info_2.GetServiceName());
|
||||
// Set Pcp to wrong value.
|
||||
if (!service_name.Empty()) {
|
||||
char* service_name_ptr = service_name.data();
|
||||
service_name_ptr[0] = 0x26;
|
||||
}
|
||||
nsd_service_info_2 .SetServiceName(Base64Utils::Encode(service_name));
|
||||
WifiLanServiceInfo wifi_lan_service_info_2{nsd_service_info_2};
|
||||
EXPECT_TRUE(wifi_lan_service_info_2.IsValid());
|
||||
|
||||
NsdServiceInfo nsd_service_info_3{org_wifi_lan_service_info};
|
||||
service_name = Base64Utils::Decode(nsd_service_info_3.GetServiceName());
|
||||
// Set service name to shorter value to remove the UWB address field.
|
||||
if (!service_name.Empty()) {
|
||||
service_name.resize(9);
|
||||
}
|
||||
nsd_service_info_3.SetServiceName(Base64Utils::Encode(service_name));
|
||||
WifiLanServiceInfo wifi_lan_service_info_3{nsd_service_info_3};
|
||||
EXPECT_FALSE(wifi_lan_service_info_3.IsValid());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
|
||||
@@ -55,7 +55,7 @@ class NsdServiceInfo {
|
||||
// Adds the TXTRecord with a pair of key and value.
|
||||
void SetTxtRecord(const std::string& txt_record_key,
|
||||
const std::string& txt_record_value) {
|
||||
txt_records_.emplace(txt_record_key, txt_record_value);
|
||||
txt_records_.insert_or_assign(txt_record_key, txt_record_value);
|
||||
}
|
||||
|
||||
// Gets all TXTRecord.
|
||||
|
||||
Reference in New Issue
Block a user