Skip to content

Commit 4a059b0

Browse files
hai007copybara-github
authored andcommitted
Add more test cases for BluetoothDeviceNameTest improve test coverage.
PiperOrigin-RevId: 873094091
1 parent fbc906a commit 4a059b0

File tree

3 files changed

+187
-121
lines changed

3 files changed

+187
-121
lines changed

connections/implementation/bluetooth_device_name.cc

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include "absl/strings/escaping.h"
2222
#include "absl/strings/str_cat.h"
2323
#include "absl/strings/string_view.h"
24-
#include "connections/implementation/base_pcp_handler.h"
2524
#include "connections/implementation/pcp.h"
25+
#include "connections/implementation/webrtc_state.h"
2626
#include "internal/platform/base64_utils.h"
2727
#include "internal/platform/byte_array.h"
2828
#include "internal/platform/logging.h"
@@ -58,6 +58,14 @@ BluetoothDeviceName::BluetoothDeviceName(Version version, Pcp pcp,
5858
endpoint_info_ = endpoint_info;
5959
uwb_address_ = uwb_address;
6060
web_rtc_state_ = web_rtc_state;
61+
if (endpoint_info_.size() > kMaxEndpointInfoLength) {
62+
LOG(INFO)
63+
<< "While constructing bluetooth device name, truncating Endpoint Info "
64+
<< absl::BytesToHexString(std::string(endpoint_info_)) << " ("
65+
<< endpoint_info_.size() << " bytes) down to " << kMaxEndpointInfoLength
66+
<< " bytes";
67+
endpoint_info_ = ByteArray(endpoint_info_.data(), kMaxEndpointInfoLength);
68+
}
6169
}
6270

6371
BluetoothDeviceName::BluetoothDeviceName(
@@ -154,6 +162,14 @@ BluetoothDeviceName::BluetoothDeviceName(
154162
return;
155163
}
156164
endpoint_info_ = *endpoint_info_bytes;
165+
if (endpoint_info_.size() > kMaxEndpointInfoLength) {
166+
LOG(INFO) << "While deserializing bluetooth device name, truncating "
167+
"Endpoint Info "
168+
<< absl::BytesToHexString(std::string(endpoint_info_)) << " ("
169+
<< endpoint_info_.size() << " bytes) down to "
170+
<< kMaxEndpointInfoLength << " bytes";
171+
endpoint_info_ = ByteArray(endpoint_info_.data(), kMaxEndpointInfoLength);
172+
}
157173

158174
// If the input stream has extra bytes, it's for UWB address. The first byte
159175
// is the address length. It can be 2-byte short address or 8-byte extended
@@ -203,23 +219,14 @@ BluetoothDeviceName::operator std::string() const {
203219

204220
ByteArray reserved_bytes{kReservedLength};
205221

206-
ByteArray usable_endpoint_info(endpoint_info_);
207-
if (endpoint_info_.size() > kMaxEndpointInfoLength) {
208-
LOG(INFO) << "While serializing Advertisement, truncating Endpoint Name "
209-
<< absl::BytesToHexString(endpoint_info_.data()) << " ("
210-
<< endpoint_info_.size() << " bytes) down to "
211-
<< kMaxEndpointInfoLength << " bytes";
212-
usable_endpoint_info.SetData(endpoint_info_.data(), kMaxEndpointInfoLength);
213-
}
214-
215222
// clang-format off
216223
std::string out = absl::StrCat(std::string(1, version_and_pcp_byte),
217224
endpoint_id_,
218225
std::string(service_id_hash_),
219226
std::string(1, field_byte),
220227
std::string(reserved_bytes),
221-
std::string(1, usable_endpoint_info.size()),
222-
std::string(usable_endpoint_info));
228+
std::string(1, endpoint_info_.size()),
229+
std::string(endpoint_info_));
223230
// clang-format on
224231

225232
// If UWB address is available, attach it at the end.

connections/implementation/bluetooth_device_name.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include <string>
1919

2020
#include "absl/strings/string_view.h"
21-
#include "connections/implementation/base_pcp_handler.h"
2221
#include "connections/implementation/pcp.h"
22+
#include "connections/implementation/webrtc_state.h"
2323
#include "internal/platform/byte_array.h"
2424

2525
namespace nearby {
@@ -80,7 +80,6 @@ class BluetoothDeviceName {
8080
std::string endpoint_id_;
8181
ByteArray service_id_hash_;
8282
ByteArray endpoint_info_;
83-
// TODO(b/169550050): Define UWB address field.
8483
ByteArray uwb_address_;
8584
WebRtcState web_rtc_state_{WebRtcState::kUndefined};
8685
};

0 commit comments

Comments
 (0)