Create ConnectionsDevice

PiperOrigin-RevId: 529447365
This commit is contained in:
Anay Wadhera
2023-05-04 10:26:47 -07:00
committed by Copybara-Service
parent d510a8ce3c
commit d91904d79d
5 changed files with 155 additions and 0 deletions
+1
View File
@@ -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",
+18
View File
@@ -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",
],
)
+75
View File
@@ -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 <string>
#include <vector>
#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<ConnectionInfoVariant> 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<ConnectionInfoVariant> 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<ConnectionInfoVariant> 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<std::string::value_type*>(result.data()),
result.size());
return result;
}
std::string endpoint_id_;
std::string endpoint_info_;
std::vector<ConnectionInfoVariant> connection_infos_;
};
} // namespace v3
} // namespace connections
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_CONNECTIONS_V3_CONNECTIONS_DEVICE_H_
+60
View File
@@ -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 <vector>
#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<ConnectionInfoVariant> 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
+1
View File
@@ -151,6 +151,7 @@ cc_library(
"wifi_lan_connection_info.h",
],
visibility = [
"//connections/v3:__pkg__",
"//internal/interop:__pkg__",
"//presence:__subpackages__",
],