Prevent logging connection failure when mDNS client connects to test the connection.

PiperOrigin-RevId: 850545490
This commit is contained in:
Francis Tsui
2025-12-30 16:54:14 -08:00
committed by Copybara-Service
parent c86f9f573f
commit 931624a621
12 changed files with 525 additions and 393 deletions
+2 -1
View File
@@ -315,6 +315,7 @@ cc_test(
"//connections/implementation/mediums:webrtc_utils",
"//connections/implementation/proto:offline_wire_formats_cc_proto",
"//connections/v3:v3_types",
"//internal/analytics:mock_event_logger",
"//internal/flags:nearby_flags",
"//internal/interop:authentication_status",
"//internal/interop:authentication_transport_interface",
@@ -324,8 +325,8 @@ cc_test(
"//internal/platform:mac_address",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation:comm",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//internal/proto/analytics:connections_log_cc_proto",
"//proto:connections_enums_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/base:core_headers",
@@ -30,6 +30,7 @@
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/offline_frames.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/base64_utils.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/byte_utils.h"
#include "internal/platform/exception.h"
@@ -50,15 +51,6 @@ using ::location::nearby::proto::connections::Medium::BLE_L2CAP;
using DisconnectionReason =
::location::nearby::proto::connections::DisconnectionReason;
ExceptionOr<std::int32_t> ReadInt(InputStream* reader) {
ExceptionOr<ByteArray> read_bytes = reader->ReadExactly(sizeof(std::int32_t));
if (!read_bytes.ok()) {
return ExceptionOr<std::int32_t>(read_bytes.exception());
}
return ExceptionOr<std::int32_t>(
byte_utils::BytesToInt(std::move(read_bytes.result())));
}
Exception WriteInt(OutputStream* writer, std::int32_t value) {
return writer->Write(byte_utils::IntToBytes(value));
}
@@ -123,10 +115,10 @@ ExceptionOr<ByteArray> BaseEndpointChannel::Read(
return ExceptionOr<ByteArray>(read_control_block_bytes.exception());
}
read_int =
(GetMedium() == BLE_L2CAP) ? ReadPayloadLength() : ReadInt(reader_);
read_int = (GetMedium() == BLE_L2CAP) ? ReadPayloadLength()
: Base64Utils::ReadInt(reader_);
} else {
read_int = ReadInt(reader_);
read_int = Base64Utils::ReadInt(reader_);
}
if (!read_int.ok()) {
return ExceptionOr<ByteArray>(read_int.exception());
@@ -473,11 +473,11 @@ TEST_F(BaseEndpointChannelTest, ReadAfterInputStreamClosed) {
// Close the output stream before trying to read from the input.
output->Close();
// Trying to read should fail gracefully with an IO error.
// Trying to read should fail gracefully with an kNoData error.
ExceptionOr<ByteArray> read_data = test_channel.Read();
ASSERT_FALSE(read_data.ok());
ASSERT_TRUE(read_data.GetException().Raised(Exception::kIo));
EXPECT_EQ(read_data.exception(), Exception::kNoData);
}
TEST_F(BaseEndpointChannelTest, ReadUnencryptedFrameOnEncryptedChannel) {
+29 -18
View File
@@ -701,8 +701,8 @@ void BasePcpHandler::OnEncryptionSuccessRunnableV3(
ProcessPreConnectionInitiationFailure(
pending_connection_info.client, pending_connection_info.medium,
remote_device.GetEndpointId(), pending_connection_info.channel.get(),
pending_connection_info.is_incoming, pending_connection_info.start_time,
{Status::kEndpointIoError},
pending_connection_info.is_incoming, /*log_failure=*/true,
pending_connection_info.start_time, {Status::kEndpointIoError},
OperationResultCode::NEARBY_AUTHENTICATION_FAILURE,
pending_connection_info.result.lock().get());
return;
@@ -763,8 +763,8 @@ void BasePcpHandler::OnEncryptionSuccessRunnable(
ProcessPreConnectionInitiationFailure(
pending_connection_info.client, pending_connection_info.medium,
endpoint_id, pending_connection_info.channel.get(),
pending_connection_info.is_incoming, pending_connection_info.start_time,
{Status::kEndpointIoError},
pending_connection_info.is_incoming, /*log_failure=*/true,
pending_connection_info.start_time, {Status::kEndpointIoError},
OperationResultCode::NEARBY_AUTHENTICATION_FAILURE,
pending_connection_info.result.lock().get());
return;
@@ -848,8 +848,8 @@ void BasePcpHandler::OnEncryptionFailureRunnable(
ProcessPreConnectionInitiationFailure(
pending_connection_info.client, pending_connection_info.medium,
endpoint_id, pending_connection_info.channel.get(),
pending_connection_info.is_incoming, pending_connection_info.start_time,
{Status::kEndpointIoError},
pending_connection_info.is_incoming, /*log_failure=*/true,
pending_connection_info.start_time, {Status::kEndpointIoError},
OperationResultCode::NEARBY_ENCRYPTION_FAILURE,
pending_connection_info.result.lock().get());
}
@@ -971,7 +971,8 @@ Status BasePcpHandler::RequestConnection(
<< endpoint_id;
ProcessPreConnectionInitiationFailure(
client, channel_medium, endpoint_id, channel.get(),
/* is_incoming = */ false, start_time, connect_impl_result.status,
/*is_incoming=*/false, /*log_failure=*/true, start_time,
connect_impl_result.status,
connect_impl_result.operation_result_code, result.get());
return;
}
@@ -995,7 +996,8 @@ Status BasePcpHandler::RequestConnection(
<< endpoint_id;
ProcessPreConnectionInitiationFailure(
client, channel_medium, endpoint_id, channel.get(),
/* is_incoming = */ false, start_time, {Status::kEndpointIoError},
/*is_incoming=*/false, /*log_failure=*/true, start_time,
{Status::kEndpointIoError},
client->GetAnalyticsRecorder()
.GetChannelIoErrorResultCodeFromMedium(channel_medium),
result.get());
@@ -1115,7 +1117,8 @@ Status BasePcpHandler::RequestConnectionV3(
<< endpoint_id;
ProcessPreConnectionInitiationFailure(
client, channel_medium, endpoint_id, channel.get(),
/* is_incoming = */ false, start_time, connect_impl_result.status,
/*is_incoming=*/false, /*log_failure=*/true, start_time,
connect_impl_result.status,
connect_impl_result.operation_result_code, result.get());
return;
}
@@ -1140,7 +1143,8 @@ Status BasePcpHandler::RequestConnectionV3(
<< endpoint_id;
ProcessPreConnectionInitiationFailure(
client, channel_medium, endpoint_id, channel.get(),
/* is_incoming = */ false, start_time, {Status::kEndpointIoError},
/*is_incoming=*/false, /*log_failure=*/true, start_time,
{Status::kEndpointIoError},
client->GetAnalyticsRecorder()
.GetChannelIoErrorResultCodeFromMedium(channel_medium),
result.get());
@@ -1462,9 +1466,9 @@ Exception BasePcpHandler::WriteConnectionRequestFrame(
void BasePcpHandler::ProcessPreConnectionInitiationFailure(
ClientProxy* client, Medium medium, const std::string& endpoint_id,
EndpointChannel* channel, bool is_incoming, absl::Time start_time,
Status status, OperationResultCode operation_result_code,
Future<Status>* result) {
EndpointChannel* channel, bool is_incoming, bool log_failure,
absl::Time start_time, Status status,
OperationResultCode operation_result_code, Future<Status>* result) {
if (channel != nullptr) {
channel->Close();
}
@@ -1474,8 +1478,10 @@ void BasePcpHandler::ProcessPreConnectionInitiationFailure(
result->Set(status);
}
LogConnectionAttemptFailure(client, medium, endpoint_id, is_incoming,
start_time, channel, operation_result_code);
if (log_failure) {
LogConnectionAttemptFailure(client, medium, endpoint_id, is_incoming,
start_time, channel, operation_result_code);
}
// result is hold inside a swapper, and saved in PendingConnectionInfo.
// PendingConnectionInfo destructor will clear the memory of SettableFuture
// shared_ptr for result.
@@ -1978,9 +1984,14 @@ Exception BasePcpHandler::OnIncomingConnection(
<< client->GetClientId() << "; device="
<< absl::BytesToHexString(remote_endpoint_info.data())
<< "with error: " << wrapped_frame.exception();
// Do not log connection failure if no data is received from the channel.
// This prevents logging Wifi connection failure when mDNS client connects
// to test the connection.
ProcessPreConnectionInitiationFailure(
client, medium, /*endpoint_id=*/"", channel.get(),
/*is_incoming=*/true, start_time, {Status::kError},
/*is_incoming=*/true,
/*log_failure=*/wrapped_frame.exception() != Exception::kNoData,
start_time, {Status::kError},
client->GetAnalyticsRecorder().GetChannelIoErrorResultCodeFromMedium(
medium),
nullptr);
@@ -2254,8 +2265,8 @@ void BasePcpHandler::ProcessTieBreakLoss(
ProcessPreConnectionInitiationFailure(
client, pending_connection_info->medium, endpoint_id,
pending_connection_info->channel.get(),
pending_connection_info->is_incoming, pending_connection_info->start_time,
{Status::kEndpointIoError},
pending_connection_info->is_incoming, /*log_failure=*/true,
pending_connection_info->start_time, {Status::kEndpointIoError},
OperationResultCode::CLIENT_PROCESS_TIE_BREAK_LOSS,
pending_connection_info->result.lock().get());
ProcessPreConnectionResultFailure(client, endpoint_id,
@@ -576,8 +576,8 @@ class BasePcpHandler : public PcpHandler,
void ProcessPreConnectionInitiationFailure(
ClientProxy* client, Medium medium, const std::string& endpoint_id,
EndpointChannel* channel, bool is_incoming, absl::Time start_time,
Status status,
EndpointChannel* channel, bool is_incoming, bool log_failure,
absl::Time start_time, Status status,
location::nearby::proto::connections::OperationResultCode
operation_result_code,
Future<Status>* result);
File diff suppressed because it is too large Load Diff
@@ -239,6 +239,10 @@ ExceptionOr<bool> EndpointManager::HandleData(
ExceptionOr<ByteArray> bytes = endpoint_channel->Read(packet_meta_data);
if (!bytes.ok()) {
LOG(INFO) << "Stop reading on read-time exception: " << bytes.exception();
// Treat kNoData as kIo.
if (bytes.exception() == Exception::kNoData) {
return ExceptionOr<bool>(Exception::kIo);
}
return ExceptionOr<bool>(bytes.exception());
}
ExceptionOr<OfflineFrame> wrapped_frame = parser::FromBytes(bytes.result());
+1
View File
@@ -32,6 +32,7 @@ struct Exception {
kTimeout = 5, // Operation did not finish within specified time.
kIllegalCharacters = 6, // File name or parent path contained
// illegal chars
kNoData = 7, // No data available.
};
bool Ok() const { return value == kSuccess; }
explicit operator bool() const { return Ok(); }
@@ -182,9 +182,11 @@ ExceptionOr<ByteArray> NearbyClientSocket::Read(std::int64_t size) {
// Successfully read some bytes.
total_bytes_read += bytes_read;
} else if (bytes_read == 0) {
// The peer has performed a graceful shutdown.
LOG(INFO) << "Socket closed gracefully by peer before all data was read.";
return {Exception::kIo};
// The peer has performed a graceful shutdown. Return any data already
// read.
buffer.resize(total_bytes_read);
LOG(INFO) << "Socket closed by peer, data size: " << buffer.size();
return ExceptionOr(ByteArray(std::move(buffer)));
} else { // bytes_read == SOCKET_ERROR
if (WSAGetLastError() == WSAEINTR) {
VLOG(1) << "Interrupted while reading from socket.";
@@ -210,14 +210,13 @@ ServiceAddress SocketAddress::ToServiceAddress(uint16_t port) const {
ipv4_address()->sin_addr.S_un.S_un_b.s_b4},
.port = port,
};
} else {
return ServiceAddress{
.address =
std::vector<char>(ipv6_address()->sin6_addr.u.Byte,
ipv6_address()->sin6_addr.u.Byte + 16),
.port = port,
};
}
return ServiceAddress{
.address =
std::vector<char>(ipv6_address()->sin6_addr.u.Byte,
ipv6_address()->sin6_addr.u.Byte + 16),
.port = port,
};
}
+1 -2
View File
@@ -16,7 +16,6 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <utility>
#include "internal/platform/byte_array.h"
@@ -56,7 +55,7 @@ ExceptionOr<ByteArray> InputStream::ReadExactly(std::size_t size) {
const ByteArray& result = read_bytes.result();
if (result.Empty()) {
return ExceptionOr<ByteArray>(Exception::kIo);
return ExceptionOr<ByteArray>(Exception::kNoData);
}
if (current_pos == 0) {
if (result.size() == size) {
+1
View File
@@ -73,6 +73,7 @@ cc_library(
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",