diff --git a/Package.swift b/Package.swift index 4dcdd9a5..f3d140d6 100644 --- a/Package.swift +++ b/Package.swift @@ -495,6 +495,7 @@ let package = Package( "connections/implementation/pcp_manager_test.cc", "connections/implementation/ble_advertisement_test.cc", "connections/implementation/base_endpoint_channel_test.cc", + "connections/v3/connections_device_test.cc", "connections/core_test.cc", "connections/status_test.cc", "connections/payload_test.cc", diff --git a/connections/v3/BUILD b/connections/v3/BUILD index 0c768dda..56ddeba1 100644 --- a/connections/v3/BUILD +++ b/connections/v3/BUILD @@ -4,6 +4,7 @@ cc_library( "bandwidth_info.h", "connection_listening_options.h", "connection_resolution.h", + "connections_device.h", "listeners.h", ], visibility = [ @@ -11,8 +12,25 @@ cc_library( ], deps = [ "//connections:core_types", + "//internal/crypto", "//internal/interop:device", + "//internal/platform:connection_info", "//proto:connections_enums_cc_proto", "@com_google_absl//absl/functional:any_invocable", ], ) + +cc_test( + name = "v3_types_test", + srcs = [ + "connections_device_test.cc", + ], + deps = [ + "//connections/implementation:internal_test", + "//connections/v3:v3_types", + "//internal/platform:connection_info", + "//internal/platform/implementation/g3", # build_cleaner: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/connections/v3/connections_device.h b/connections/v3/connections_device.h new file mode 100644 index 00000000..27e65268 --- /dev/null +++ b/connections/v3/connections_device.h @@ -0,0 +1,75 @@ +// Copyright 2023 Google LLC +// +// 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 +// +// https://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. + +#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_V3_CONNECTIONS_DEVICE_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_V3_CONNECTIONS_DEVICE_H_ + +#include +#include + +#include "internal/crypto/random.h" +#include "internal/interop/device.h" +#include "internal/platform/connection_info.h" + +namespace nearby { +namespace connections { +namespace v3 { + +constexpr int kEndpointIdLength = 4; + +class ConnectionsDevice : public nearby::NearbyDevice { + public: + ConnectionsDevice(absl::string_view endpoint_id, + absl::string_view endpoint_info, + const std::vector connection_infos) + : endpoint_id_(endpoint_id), + endpoint_info_(endpoint_info), + connection_infos_(connection_infos) { + if (endpoint_id.length() != kEndpointIdLength) { + endpoint_id_ = GenerateRandomEndpointId(); + } + } + ConnectionsDevice(absl::string_view endpoint_info, + const std::vector connection_infos) + : endpoint_info_(endpoint_info), connection_infos_(connection_infos) { + endpoint_id_ = GenerateRandomEndpointId(); + } + ~ConnectionsDevice() override = default; + + std::string GetEndpointId() const override { return endpoint_id_; } + std::vector GetConnectionInfos() const override { + return connection_infos_; + } + Type GetType() const override { return Type::kConnectionsDevice; } + + std::string GetEndpointInfo() const { return endpoint_info_; } + + private: + std::string GenerateRandomEndpointId() { + std::string result(kEndpointIdLength, 0); + crypto::RandBytes(const_cast(result.data()), + result.size()); + return result; + } + + std::string endpoint_id_; + std::string endpoint_info_; + std::vector connection_infos_; +}; + +} // namespace v3 +} // namespace connections +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_V3_CONNECTIONS_DEVICE_H_ diff --git a/connections/v3/connections_device_test.cc b/connections/v3/connections_device_test.cc new file mode 100644 index 00000000..4952b577 --- /dev/null +++ b/connections/v3/connections_device_test.cc @@ -0,0 +1,60 @@ +// Copyright 2023 Google LLC +// +// 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 +// +// https://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 "connections/v3/connections_device.h" + +#include + +#include "gmock/gmock.h" +#include "protobuf-matchers/protocol-buffer-matchers.h" +#include "gtest/gtest.h" +#include "internal/platform/ble_connection_info.h" +#include "internal/platform/connection_info.h" +#include "internal/platform/wifi_lan_connection_info.h" + +namespace nearby { +namespace connections { +namespace v3 { +namespace { + +constexpr absl::string_view kMacAddress = "\x34\x56\x67\x78\x89\x90"; + +std::vector CreateDefaultConnectionInfos() { + return {BleConnectionInfo(kMacAddress, "", "", {}), + WifiLanConnectionInfo("\x94\xF4\x56\x13", "\x34\x56", {})}; +} + +TEST(ConnectionsDeviceTest, TestTwoArgumentConstructor) { + auto connection_infos = CreateDefaultConnectionInfos(); + ConnectionsDevice device("connections endpoint", connection_infos); + EXPECT_EQ(device.GetConnectionInfos(), connection_infos); + EXPECT_EQ(device.GetType(), NearbyDevice::Type::kConnectionsDevice); + EXPECT_EQ(device.GetEndpointId().length(), 4); + EXPECT_EQ(device.GetEndpointInfo(), "connections endpoint"); +} + +TEST(ConnectionsDeviceTest, TestThreeArgumentConstructor) { + auto connection_infos = CreateDefaultConnectionInfos(); + ConnectionsDevice device("ABCD", "connections endpoint", connection_infos); + EXPECT_EQ(device.GetConnectionInfos(), connection_infos); + EXPECT_EQ(device.GetType(), NearbyDevice::Type::kConnectionsDevice); + EXPECT_EQ(device.GetEndpointId().length(), 4); + EXPECT_EQ(device.GetEndpointId(), "ABCD"); + EXPECT_EQ(device.GetEndpointInfo(), "connections endpoint"); +} + +} // namespace +} // namespace v3 +} // namespace connections +} // namespace nearby diff --git a/internal/platform/BUILD b/internal/platform/BUILD index b26f4ac8..c29cd3f2 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -151,6 +151,7 @@ cc_library( "wifi_lan_connection_info.h", ], visibility = [ + "//connections/v3:__pkg__", "//internal/interop:__pkg__", "//presence:__subpackages__", ],