mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Roll forward to cl/338482889
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: Ic2bdb234e89f3c5860d1b483dd4bce689f13d057
This commit is contained in:
@@ -14,89 +14,302 @@
|
||||
|
||||
#include "core/internal/offline_frames.h"
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "proto/connections/offline_wire_formats.pb.h"
|
||||
#include "platform/byte_array.h"
|
||||
#include "platform/base/byte_array.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace location::nearby::connections {
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
namespace parser {
|
||||
namespace {
|
||||
|
||||
using Medium = proto::connections::Medium;
|
||||
using ::testing::EqualsProto;
|
||||
|
||||
std::unique_ptr<OfflineFrame> MakeFrame(V1Frame* sub_frame) {
|
||||
auto frame = std::make_unique<OfflineFrame>();
|
||||
frame->set_version(OfflineFrame::V1);
|
||||
frame->set_allocated_v1(sub_frame);
|
||||
return frame;
|
||||
}
|
||||
constexpr absl::string_view kEndpointId{"ABC"};
|
||||
constexpr absl::string_view kEndpointName{"XYZ"};
|
||||
constexpr int kNonce = 1234;
|
||||
constexpr std::array<Medium, 9> kMediums = {
|
||||
Medium::MDNS, Medium::BLUETOOTH, Medium::WIFI_HOTSPOT,
|
||||
Medium::BLE, Medium::WIFI_LAN, Medium::WIFI_AWARE,
|
||||
Medium::NFC, Medium::WIFI_DIRECT, Medium::WEB_RTC,
|
||||
};
|
||||
|
||||
void SetSubframe(V1Frame* frame, ConnectionRequestFrame* sub_frame) {
|
||||
frame->set_type(V1Frame::CONNECTION_REQUEST);
|
||||
frame->set_allocated_connection_request(sub_frame);
|
||||
}
|
||||
TEST(OfflineFramesTest, CanParseMessageFromBytes) {
|
||||
OfflineFrame tx_message;
|
||||
|
||||
constexpr ConnectionRequestFrame::Medium ToConnectionRequestMedium(
|
||||
proto::connections::Medium medium) {
|
||||
switch (medium) {
|
||||
case proto::connections::MDNS:
|
||||
return ConnectionRequestFrame::MDNS;
|
||||
case proto::connections::BLUETOOTH:
|
||||
return ConnectionRequestFrame::BLUETOOTH;
|
||||
case proto::connections::WIFI_HOTSPOT:
|
||||
return ConnectionRequestFrame::WIFI_HOTSPOT;
|
||||
case proto::connections::BLE:
|
||||
return ConnectionRequestFrame::BLE;
|
||||
case proto::connections::WIFI_LAN:
|
||||
return ConnectionRequestFrame::WIFI_LAN;
|
||||
default:
|
||||
return ConnectionRequestFrame::UNKNOWN_MEDIUM;
|
||||
{
|
||||
tx_message.set_version(OfflineFrame::V1);
|
||||
auto* v1_frame = tx_message.mutable_v1();
|
||||
auto* sub_frame = v1_frame->mutable_connection_request();
|
||||
|
||||
v1_frame->set_type(V1Frame::CONNECTION_REQUEST);
|
||||
sub_frame->set_endpoint_id(kEndpointId);
|
||||
sub_frame->set_endpoint_name(kEndpointName);
|
||||
sub_frame->set_endpoint_info(kEndpointName);
|
||||
sub_frame->set_nonce(kNonce);
|
||||
for (auto& medium : kMediums) {
|
||||
sub_frame->add_mediums(MediumToConnectionRequestMedium(medium));
|
||||
}
|
||||
}
|
||||
auto serialized_bytes = ByteArray(tx_message.SerializeAsString());
|
||||
auto ret_value = FromBytes(serialized_bytes);
|
||||
ASSERT_TRUE(ret_value.ok());
|
||||
const auto& rx_message = ret_value.result();
|
||||
EXPECT_THAT(rx_message, EqualsProto(tx_message));
|
||||
EXPECT_EQ(GetFrameType(rx_message), V1Frame::CONNECTION_REQUEST);
|
||||
EXPECT_EQ(
|
||||
ConnectionRequestMediumsToMediums(rx_message.v1().connection_request()),
|
||||
std::vector(kMediums.begin(), kMediums.end()));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateConnectionRequest) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: CONNECTION_REQUEST
|
||||
connection_request: <
|
||||
endpoint_id: "ABC"
|
||||
endpoint_name: "XYZ"
|
||||
endpoint_info: "XYZ"
|
||||
nonce: 1234
|
||||
mediums: MDNS
|
||||
mediums: BLUETOOTH
|
||||
mediums: WIFI_HOTSPOT
|
||||
mediums: BLE
|
||||
mediums: WIFI_LAN
|
||||
mediums: WIFI_AWARE
|
||||
mediums: NFC
|
||||
mediums: WIFI_DIRECT
|
||||
mediums: WEB_RTC
|
||||
>
|
||||
>)pb";
|
||||
ByteArray bytes = ForConnectionRequest(
|
||||
std::string(kEndpointId), ByteArray{std::string(kEndpointName)}, kNonce,
|
||||
std::vector(kMediums.begin(), kMediums.end()));
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateConnectionResponse) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: CONNECTION_RESPONSE
|
||||
connection_response: <
|
||||
status: 1
|
||||
response: REJECT
|
||||
>
|
||||
>)pb";
|
||||
ByteArray bytes = ForConnectionResponse(1);
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateControlPayloadTransfer) {
|
||||
PayloadTransferFrame::PayloadHeader header;
|
||||
PayloadTransferFrame::ControlMessage control;
|
||||
header.set_id(12345);
|
||||
header.set_type(PayloadTransferFrame::PayloadHeader::BYTES);
|
||||
header.set_total_size(1024);
|
||||
control.set_event(PayloadTransferFrame::ControlMessage::PAYLOAD_CANCELED);
|
||||
control.set_offset(150);
|
||||
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: PAYLOAD_TRANSFER
|
||||
payload_transfer: <
|
||||
packet_type: CONTROL,
|
||||
payload_header: < type: BYTES id: 12345 total_size: 1024 >
|
||||
control_message: < event: PAYLOAD_CANCELED offset: 150 >
|
||||
>
|
||||
>)pb";
|
||||
ByteArray bytes = ForControlPayloadTransfer(header, control);
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateDataPayloadTransfer) {
|
||||
PayloadTransferFrame::PayloadHeader header;
|
||||
PayloadTransferFrame::PayloadChunk chunk;
|
||||
header.set_id(12345);
|
||||
header.set_type(PayloadTransferFrame::PayloadHeader::BYTES);
|
||||
header.set_total_size(1024);
|
||||
chunk.set_body("payload data");
|
||||
chunk.set_offset(150);
|
||||
chunk.set_flags(1);
|
||||
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: PAYLOAD_TRANSFER
|
||||
payload_transfer: <
|
||||
packet_type: DATA,
|
||||
payload_header: < type: BYTES id: 12345 total_size: 1024 >
|
||||
payload_chunk: < flags: 1 offset: 150 body: "payload data" >
|
||||
>
|
||||
>)pb";
|
||||
ByteArray bytes = ForDataPayloadTransfer(header, chunk);
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateBwuWifiHotspotPathAvailable) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: BANDWIDTH_UPGRADE_NEGOTIATION
|
||||
bandwidth_upgrade_negotiation: <
|
||||
event_type: UPGRADE_PATH_AVAILABLE
|
||||
upgrade_path_info: <
|
||||
medium: WIFI_HOTSPOT
|
||||
wifi_hotspot_credentials: <
|
||||
ssid: "ssid"
|
||||
password: "password"
|
||||
port: 1234
|
||||
>
|
||||
>
|
||||
>
|
||||
>)pb";
|
||||
ByteArray bytes = ForBwuWifiHotspotPathAvailable("ssid", "password", 1234);
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateBwuWifiLanPathAvailable) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: BANDWIDTH_UPGRADE_NEGOTIATION
|
||||
bandwidth_upgrade_negotiation: <
|
||||
event_type: UPGRADE_PATH_AVAILABLE
|
||||
upgrade_path_info: <
|
||||
medium: WIFI_LAN
|
||||
wifi_lan_socket: < ip_address: "\x01\x02\x03\x04" wifi_port: 1234 >
|
||||
>
|
||||
>
|
||||
>)pb";
|
||||
ByteArray bytes = ForBwuWifiLanPathAvailable("\x01\x02\x03\x04", 1234);
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateBwuBluetoothPathAvailable) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: BANDWIDTH_UPGRADE_NEGOTIATION
|
||||
bandwidth_upgrade_negotiation: <
|
||||
event_type: UPGRADE_PATH_AVAILABLE
|
||||
upgrade_path_info: <
|
||||
medium: BLUETOOTH
|
||||
bluetooth_credentials: <
|
||||
service_name: "service"
|
||||
mac_address: "\x11\x22\x33\x44\x55\x66"
|
||||
>
|
||||
>
|
||||
>
|
||||
>)pb";
|
||||
ByteArray bytes =
|
||||
ForBwuBluetoothPathAvailable("service", "\x11\x22\x33\x44\x55\x66");
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateBwuLastWrite) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: BANDWIDTH_UPGRADE_NEGOTIATION
|
||||
bandwidth_upgrade_negotiation: < event_type: LAST_WRITE_TO_PRIOR_CHANNEL >
|
||||
>)pb";
|
||||
ByteArray bytes = ForBwuLastWrite();
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateBwuSafeToClose) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: BANDWIDTH_UPGRADE_NEGOTIATION
|
||||
bandwidth_upgrade_negotiation: < event_type: SAFE_TO_CLOSE_PRIOR_CHANNEL >
|
||||
>)pb";
|
||||
ByteArray bytes = ForBwuSafeToClose();
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateBwuIntroduction) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: BANDWIDTH_UPGRADE_NEGOTIATION
|
||||
bandwidth_upgrade_negotiation: <
|
||||
event_type: CLIENT_INTRODUCTION
|
||||
client_introduction: < endpoint_id: "ABC" >
|
||||
>
|
||||
>)pb";
|
||||
ByteArray bytes = ForBwuIntroduction(std::string(kEndpointId));
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
TEST(OfflineFramesTest, CanGenerateKeepAlive) {
|
||||
constexpr char kExpected[] =
|
||||
R"pb(
|
||||
version: V1
|
||||
v1: <
|
||||
type: KEEP_ALIVE
|
||||
keep_alive: <>
|
||||
>)pb";
|
||||
ByteArray bytes = ForKeepAlive();
|
||||
auto response = FromBytes(bytes);
|
||||
ASSERT_TRUE(response.ok());
|
||||
OfflineFrame message = FromBytes(bytes).result();
|
||||
EXPECT_THAT(message, EqualsProto(kExpected));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(OfflineFramesTest, CanParseMessageFromBytes) {
|
||||
const string endpoint_id{"ABC"};
|
||||
const string endpoint_name{"XYZ"};
|
||||
const int nonce{1234};
|
||||
const std::vector<proto::connections::Medium> mediums{Medium::BLE,
|
||||
Medium::BLUETOOTH};
|
||||
|
||||
auto* v1_frame = new V1Frame{};
|
||||
auto* sub_frame = new ConnectionRequestFrame{};
|
||||
sub_frame->set_endpoint_id(endpoint_id);
|
||||
sub_frame->set_endpoint_name(endpoint_name);
|
||||
sub_frame->set_nonce(nonce);
|
||||
|
||||
for (auto& medium : mediums) {
|
||||
sub_frame->add_mediums(ToConnectionRequestMedium(medium));
|
||||
}
|
||||
|
||||
SetSubframe(v1_frame, sub_frame);
|
||||
auto frame = MakeFrame(v1_frame);
|
||||
|
||||
auto bytes = MakeConstPtr(new ByteArray(frame->SerializeAsString()));
|
||||
|
||||
auto ret_value = OfflineFrames::fromBytes(bytes);
|
||||
ASSERT_TRUE(ret_value.ok());
|
||||
const auto& rx_message = ret_value.result();
|
||||
ASSERT_TRUE(rx_message->has_version());
|
||||
ASSERT_EQ(rx_message->version(), OfflineFrame::V1);
|
||||
ASSERT_TRUE(rx_message->has_v1());
|
||||
const auto& rx_frame = rx_message->v1();
|
||||
ASSERT_EQ(rx_frame.type(), V1Frame::CONNECTION_REQUEST);
|
||||
ASSERT_TRUE(rx_frame.has_connection_request());
|
||||
const auto& req = rx_frame.connection_request();
|
||||
ASSERT_TRUE(req.has_endpoint_id());
|
||||
ASSERT_TRUE(req.has_endpoint_name());
|
||||
ASSERT_TRUE(req.has_nonce());
|
||||
ASSERT_EQ(req.endpoint_id(), endpoint_id);
|
||||
ASSERT_EQ(req.endpoint_name(), endpoint_name);
|
||||
ASSERT_EQ(req.nonce(), nonce);
|
||||
ASSERT_EQ(req.mediums_size(), mediums.size());
|
||||
}
|
||||
|
||||
} // namespace location::nearby::connections
|
||||
} // namespace parser
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
Reference in New Issue
Block a user