mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Roll forward to cl/321106672
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: Iabb20e9c7f7487268fa3ebbbeb58540092e10e0e
This commit is contained in:
@@ -49,6 +49,7 @@ cc_test(
|
||||
deps = [
|
||||
":webrtc",
|
||||
"//platform_v2/base",
|
||||
"//platform_v2/base:test_util",
|
||||
"//platform_v2/impl/g3", # buildcleaner: keep
|
||||
"//platform_v2/public:comm",
|
||||
"//platform_v2/public:types",
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "core_v2/internal/mediums/webrtc/session_description_wrapper.h"
|
||||
#include "platform_v2/base/byte_array.h"
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
#include "platform_v2/public/webrtc.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -20,6 +21,14 @@ namespace connections {
|
||||
namespace mediums {
|
||||
namespace {
|
||||
|
||||
class ConnectionFlowTest : public ::testing::Test {
|
||||
protected:
|
||||
ConnectionFlowTest() {
|
||||
MediumEnvironment::Instance().Stop();
|
||||
MediumEnvironment::Instance().Start({.webrtc_enabled = true});
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<webrtc::IceCandidateInterface> CopyCandidate(
|
||||
const webrtc::IceCandidateInterface* candidate) {
|
||||
return webrtc::CreateIceCandidate(candidate->sdp_mid(),
|
||||
@@ -29,7 +38,7 @@ std::unique_ptr<webrtc::IceCandidateInterface> CopyCandidate(
|
||||
|
||||
// TODO(bfranz) - Add test that deterministically sends answerer_ice_candidates
|
||||
// before answer is sent.
|
||||
TEST(ConnectionFlowTest, SuccessfulOfferAnswerFlow) {
|
||||
TEST_F(ConnectionFlowTest, SuccessfulOfferAnswerFlow) {
|
||||
WebRtcMedium webrtc_medium_offerer, webrtc_medium_answerer;
|
||||
|
||||
Future<ByteArray> message_received_future;
|
||||
@@ -95,7 +104,7 @@ TEST(ConnectionFlowTest, SuccessfulOfferAnswerFlow) {
|
||||
EXPECT_EQ(received_message.result(), ByteArray{message});
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, CreateAnswerBeforeOfferReceived) {
|
||||
TEST_F(ConnectionFlowTest, CreateAnswerBeforeOfferReceived) {
|
||||
WebRtcMedium webrtc_medium;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> answerer = ConnectionFlow::Create(
|
||||
@@ -106,7 +115,7 @@ TEST(ConnectionFlowTest, CreateAnswerBeforeOfferReceived) {
|
||||
EXPECT_FALSE(answer.IsValid());
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, SetAnswerBeforeOffer) {
|
||||
TEST_F(ConnectionFlowTest, SetAnswerBeforeOffer) {
|
||||
WebRtcMedium webrtc_medium_offerer, webrtc_medium_answerer;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> offerer =
|
||||
@@ -128,7 +137,7 @@ TEST(ConnectionFlowTest, SetAnswerBeforeOffer) {
|
||||
EXPECT_FALSE(offerer->OnAnswerReceived(answer));
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, CannotCreateOfferAfterClose) {
|
||||
TEST_F(ConnectionFlowTest, CannotCreateOfferAfterClose) {
|
||||
WebRtcMedium webrtc_medium;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> offerer = ConnectionFlow::Create(
|
||||
@@ -140,7 +149,7 @@ TEST(ConnectionFlowTest, CannotCreateOfferAfterClose) {
|
||||
EXPECT_FALSE(offerer->CreateOffer().IsValid());
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, CannotSetSessionDescriptionAfterClose) {
|
||||
TEST_F(ConnectionFlowTest, CannotSetSessionDescriptionAfterClose) {
|
||||
WebRtcMedium webrtc_medium;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> offerer = ConnectionFlow::Create(
|
||||
@@ -155,7 +164,7 @@ TEST(ConnectionFlowTest, CannotSetSessionDescriptionAfterClose) {
|
||||
EXPECT_FALSE(offerer->SetLocalSessionDescription(offer));
|
||||
}
|
||||
|
||||
TEST(ConnectionFlowTest, CannotReceiveOfferAfterClose) {
|
||||
TEST_F(ConnectionFlowTest, CannotReceiveOfferAfterClose) {
|
||||
WebRtcMedium webrtc_medium_offerer, webrtc_medium_answerer;
|
||||
|
||||
std::unique_ptr<ConnectionFlow> offerer =
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "core_v2/internal/mediums/webrtc/webrtc_socket_wrapper.h"
|
||||
#include "platform_v2/base/listeners.h"
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
#include "platform_v2/public/mutex_lock.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -13,8 +14,16 @@ namespace mediums {
|
||||
|
||||
namespace {
|
||||
|
||||
class WebRtcTest : public ::testing::Test {
|
||||
protected:
|
||||
WebRtcTest() {
|
||||
MediumEnvironment::Instance().Stop();
|
||||
MediumEnvironment::Instance().Start({.webrtc_enabled = true});
|
||||
}
|
||||
};
|
||||
|
||||
// Basic test to check that device is accepting connections when initialized.
|
||||
TEST(WebRtcTest, NotAcceptingConnections) {
|
||||
TEST_F(WebRtcTest, NotAcceptingConnections) {
|
||||
WebRtc webrtc;
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
EXPECT_FALSE(webrtc.IsAcceptingConnections());
|
||||
@@ -22,7 +31,7 @@ TEST(WebRtcTest, NotAcceptingConnections) {
|
||||
|
||||
// Tests the flow when the device tries to accept connections twice. In this
|
||||
// case, only the first call is successful and subsequent calls fail.
|
||||
TEST(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
TEST_F(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
using MockAcceptedCallback =
|
||||
testing::MockFunction<void(WebRtcSocketWrapper socket)>;
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
@@ -40,7 +49,7 @@ TEST(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
|
||||
// Tests the flow when the device tries to connect but the data channel times
|
||||
// out.
|
||||
TEST(WebRtcTest, Connect_DataChannelTimeOut) {
|
||||
TEST_F(WebRtcTest, Connect_DataChannelTimeOut) {
|
||||
WebRtc webrtc;
|
||||
PeerId peer_id("peer_id");
|
||||
|
||||
@@ -54,7 +63,7 @@ TEST(WebRtcTest, Connect_DataChannelTimeOut) {
|
||||
|
||||
// Tests the flow when the device calls Connect() after calling
|
||||
// StartAcceptingConnections() without StopAcceptingConnections().
|
||||
TEST(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
using MockAcceptedCallback =
|
||||
testing::MockFunction<void(WebRtcSocketWrapper socket)>;
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
@@ -74,7 +83,7 @@ TEST(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
|
||||
// Tests the flow when the device calls StartAcceptingConnections but the medium
|
||||
// is closed before a peer device can connect to it.
|
||||
TEST(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
TEST_F(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
using MockAcceptedCallback =
|
||||
testing::MockFunction<void(WebRtcSocketWrapper socket)>;
|
||||
testing::StrictMock<MockAcceptedCallback> mock_accepted_callback_;
|
||||
@@ -91,7 +100,7 @@ TEST(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
|
||||
// Tests the flow when the device tries to connect to two different peers
|
||||
// without disconnecting in between.
|
||||
TEST(WebRtcTest, ConnectTwice) {
|
||||
TEST_F(WebRtcTest, ConnectTwice) {
|
||||
WebRtc receiver, sender, device_c;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const PeerId self_id("self_id"), other_id("other_id");
|
||||
@@ -135,7 +144,7 @@ TEST(WebRtcTest, ConnectTwice) {
|
||||
|
||||
// Tests the flow when the two devices exchange SDP messages and connect to each
|
||||
// other but disconnect before being able to send/receive the actual data.
|
||||
TEST(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const PeerId self_id("self_id");
|
||||
@@ -161,7 +170,7 @@ TEST(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
|
||||
// Tests the flow when the two devices exchange SDP messages and connect to each
|
||||
// other and the actual data is exchanged successfully between the devices.
|
||||
TEST(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const PeerId self_id("self_id");
|
||||
@@ -193,7 +202,7 @@ TEST(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
|
||||
// Tests the flow when the two devices exchange SDP messages and connect to each
|
||||
// other but the signaling channel is closed before sending the data.
|
||||
TEST(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) {
|
||||
TEST_F(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) {
|
||||
WebRtc receiver, sender;
|
||||
WebRtcSocketWrapper receiver_socket, sender_socket;
|
||||
const PeerId self_id("self_id");
|
||||
|
||||
@@ -20,10 +20,10 @@ bool WifiLan::IsAvailable() const {
|
||||
bool WifiLan::IsAvailableLocked() const { return medium_.IsValid(); }
|
||||
|
||||
bool WifiLan::StartAdvertising(const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name) {
|
||||
const std::string& service_info_name) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (wifi_lan_service_info_name.empty()) {
|
||||
if (service_info_name.empty()) {
|
||||
NEARBY_LOG(
|
||||
INFO,
|
||||
"Refusing to turn on WifiLan advertising. Empty service info name.");
|
||||
@@ -36,45 +36,45 @@ bool WifiLan::StartAdvertising(const std::string& service_id,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!medium_.StartAdvertising(service_id, wifi_lan_service_info_name)) {
|
||||
if (!medium_.StartAdvertising(service_id, service_info_name)) {
|
||||
NEARBY_LOG(
|
||||
INFO, "Failed to turn on WifiLan advertising with service info name=%s",
|
||||
wifi_lan_service_info_name.c_str());
|
||||
service_info_name.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
NEARBY_LOGS(INFO) << "Turned on WifiLan advertising with service info name="
|
||||
<< wifi_lan_service_info_name
|
||||
<< service_info_name
|
||||
<< ", service id=" << service_id;
|
||||
advertising_info_.service_id = service_id;
|
||||
advertising_info_.Add(service_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiLan::StopAdvertising(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (!IsAdvertisingLocked()) {
|
||||
if (!IsAdvertisingLocked(service_id)) {
|
||||
NEARBY_LOG(INFO, "Can't turn off WifiLan advertising; it is already off");
|
||||
return false;
|
||||
}
|
||||
|
||||
NEARBY_LOG(INFO, "Turned off WifiLan advertising with service id=%s",
|
||||
service_id.c_str());
|
||||
bool ret = medium_.StopAdvertising(advertising_info_.service_id);
|
||||
bool ret = medium_.StopAdvertising(service_id);
|
||||
// Reset our bundle of advertising state to mark that we're no longer
|
||||
// advertising.
|
||||
advertising_info_.Clear();
|
||||
advertising_info_.Remove(service_id);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool WifiLan::IsAdvertising() {
|
||||
bool WifiLan::IsAdvertising(const std::string& service_id) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
return IsAdvertisingLocked();
|
||||
return IsAdvertisingLocked(service_id);
|
||||
}
|
||||
|
||||
bool WifiLan::IsAdvertisingLocked() {
|
||||
return !advertising_info_.Empty();
|
||||
bool WifiLan::IsAdvertisingLocked(const std::string& service_id) {
|
||||
return advertising_info_.Existed(service_id);
|
||||
}
|
||||
|
||||
bool WifiLan::StartDiscovery(const std::string& service_id,
|
||||
@@ -110,7 +110,7 @@ bool WifiLan::StartDiscovery(const std::string& service_id,
|
||||
NEARBY_LOG(INFO, "Turned on WifiLan discovering with service id=%s",
|
||||
service_id.c_str());
|
||||
// Mark the fact that we're currently performing a WifiLan discovering.
|
||||
discovering_info_.service_id = service_id;
|
||||
discovering_info_.Add(service_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ bool WifiLan::IsDiscovering(const std::string& service_id) {
|
||||
}
|
||||
|
||||
bool WifiLan::IsDiscoveringLocked(const std::string& service_id) {
|
||||
return !discovering_info_.Empty();
|
||||
return discovering_info_.Existed(service_id);
|
||||
}
|
||||
|
||||
bool WifiLan::StartAcceptingConnections(const std::string& service_id,
|
||||
@@ -174,7 +174,7 @@ bool WifiLan::StartAcceptingConnections(const std::string& service_id,
|
||||
return false;
|
||||
}
|
||||
|
||||
accepting_connections_info_.service_id = service_id;
|
||||
accepting_connections_info_.Add(service_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -188,11 +188,10 @@ bool WifiLan::StopAcceptingConnections(const std::string& service_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret =
|
||||
medium_.StopAcceptingConnections(accepting_connections_info_.service_id);
|
||||
bool ret = medium_.StopAcceptingConnections(service_id);
|
||||
// Reset our bundle of accepting connections state to mark that we're no
|
||||
// longer accepting connections.
|
||||
accepting_connections_info_.Clear();
|
||||
accepting_connections_info_.Remove(service_id);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -203,7 +202,7 @@ bool WifiLan::IsAcceptingConnections(const std::string& service_id) {
|
||||
}
|
||||
|
||||
bool WifiLan::IsAcceptingConnectionsLocked(const std::string& service_id) {
|
||||
return !accepting_connections_info_.Empty();
|
||||
return accepting_connections_info_.Existed(service_id);
|
||||
}
|
||||
|
||||
WifiLanSocket WifiLan::Connect(WifiLanService& wifi_lan_service,
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "platform_v2/public/mutex.h"
|
||||
#include "platform_v2/public/wifi_lan.h"
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -25,7 +26,7 @@ class WifiLan {
|
||||
// Sets custom service info name, and then enables WifiLan advertising.
|
||||
// Returns true, if name is successfully set, and false otherwise.
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const std::string& wifi_lan_service_info_name)
|
||||
const std::string& service_info_name)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables WifiLan advertising, and restores service info name to
|
||||
@@ -33,7 +34,7 @@ class WifiLan {
|
||||
bool StopAdvertising(const std::string& service_id)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool IsAdvertising() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool IsAdvertising(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Enables WifiLan discovery mode. Will report any discoverable services in
|
||||
// range through a callback. Returns true, if discovery mode was enabled,
|
||||
@@ -70,31 +71,53 @@ class WifiLan {
|
||||
|
||||
private:
|
||||
struct AdvertisingInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
bool Empty() const { return service_ids.empty(); }
|
||||
void Clear() { service_ids.clear(); }
|
||||
void Add(const std::string& service_id) { service_ids.emplace(service_id); }
|
||||
void Remove(const std::string& service_id) {
|
||||
service_ids.erase(service_id);
|
||||
}
|
||||
bool Existed(const std::string& service_id) const {
|
||||
return service_ids.contains(service_id);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
|
||||
struct DiscoveringInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
bool Empty() const { return service_ids.empty(); }
|
||||
void Clear() { service_ids.clear(); }
|
||||
void Add(const std::string& service_id) { service_ids.emplace(service_id); }
|
||||
void Remove(const std::string& service_id) {
|
||||
service_ids.erase(service_id);
|
||||
}
|
||||
bool Existed(const std::string& service_id) const {
|
||||
return service_ids.contains(service_id);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
|
||||
struct AcceptingConnectionsInfo {
|
||||
bool Empty() const { return service_id.empty(); }
|
||||
void Clear() { service_id.clear(); }
|
||||
bool Empty() const { return service_ids.empty(); }
|
||||
void Clear() { service_ids.clear(); }
|
||||
void Add(const std::string& service_id) { service_ids.emplace(service_id); }
|
||||
void Remove(const std::string& service_id) {
|
||||
service_ids.erase(service_id);
|
||||
}
|
||||
bool Existed(const std::string& service_id) const {
|
||||
return service_ids.contains(service_id);
|
||||
}
|
||||
|
||||
std::string service_id;
|
||||
absl::flat_hash_set<std::string> service_ids;
|
||||
};
|
||||
|
||||
// Same as IsAvailable(), but must be called with mutex_ held.
|
||||
bool IsAvailableLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Same as IsAdvertising(), but must be called with mutex_ held.
|
||||
bool IsAdvertisingLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
bool IsAdvertisingLocked(const std::string& service_id)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
// Same as IsDiscovering(), but must be called with mutex_ held.
|
||||
bool IsDiscoveringLocked(const std::string& service_id)
|
||||
|
||||
Reference in New Issue
Block a user