1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/chromeos/host_event_reporter_impl.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chrome/browser/policy/messaging_layer/proto/synced/crd_event.pb.h"
#include "remoting/protocol/transport.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::Eq;
using ::testing::StrEq;
namespace remoting {
namespace {
constexpr char kHostUser[] = "host@example.com";
constexpr char kHostIp[] = "127.0.0.1:1234";
constexpr char kClientIp[] = "99.88.77.66:4321";
constexpr char kSessionId[] = "0123456789/client@example.com";
constexpr ::ash::reporting::ConnectionType kConnectionType =
::ash::reporting::ConnectionType::CRD_CONNECTION_DIRECT;
class HostEventReporterDelegateStub : public HostEventReporterImpl::Delegate {
public:
HostEventReporterDelegateStub() = default;
void EnqueueEvent(::ash::reporting::CRDRecord record) override {}
};
class TestHostEventReporterDelegate : public HostEventReporterImpl::Delegate {
public:
TestHostEventReporterDelegate() = default;
void EnqueueEvent(::ash::reporting::CRDRecord record) override {
records_.SetValue(std::move(record));
}
bool WaitForEvent() { return records_.Wait(); }
::ash::reporting::CRDRecord TakeEvent() { return records_.Take(); }
bool IsEmpty() const { return !records_.IsReady(); }
private:
base::test::TestFuture<::ash::reporting::CRDRecord> records_;
};
class HostEventReporterTest : public ::testing::Test {
protected:
HostEventReporterTest()
: delegate_(new TestHostEventReporterDelegate()),
monitor_(new HostStatusMonitor()),
reporter_(monitor_, base::WrapUnique(delegate_.get())) {}
base::test::SingleThreadTaskEnvironment task_environment_;
const raw_ptr<TestHostEventReporterDelegate, DanglingUntriaged> delegate_;
scoped_refptr<HostStatusMonitor> monitor_;
HostEventReporterImpl reporter_;
};
TEST_F(HostEventReporterTest, ReportConnectedAndDisconnected) {
{
reporter_.OnHostStarted(kHostUser);
ASSERT_TRUE(delegate_->WaitForEvent());
auto received = delegate_->TakeEvent();
EXPECT_THAT(received.host_user().user_email(), StrEq(kHostUser));
ASSERT_TRUE(received.has_started());
}
{
reporter_.OnClientConnected(kHostIp);
protocol::TransportRoute route;
route.type = protocol::TransportRoute::RouteType::DIRECT;
route.remote_address =
net::IPEndPoint(net::IPAddress(99, 88, 77, 66), 4321);
route.local_address = net::IPEndPoint(net::IPAddress(127, 0, 0, 1), 1234);
reporter_.OnClientRouteChange(kSessionId, "", route);
ASSERT_TRUE(delegate_->WaitForEvent());
auto received = delegate_->TakeEvent();
EXPECT_THAT(received.host_user().user_email(), StrEq(kHostUser));
ASSERT_TRUE(received.has_connected());
EXPECT_THAT(received.connected().host_ip(), StrEq(kHostIp));
EXPECT_THAT(received.connected().client_ip(), StrEq(kClientIp));
EXPECT_THAT(received.connected().session_id(), StrEq(kSessionId));
EXPECT_THAT(received.connected().connection_type(), Eq(kConnectionType));
}
{
reporter_.OnClientDisconnected(kSessionId);
ASSERT_TRUE(delegate_->WaitForEvent());
auto received = delegate_->TakeEvent();
EXPECT_THAT(received.host_user().user_email(), StrEq(kHostUser));
ASSERT_TRUE(received.has_disconnected());
EXPECT_THAT(received.disconnected().host_ip(), StrEq(kHostIp));
EXPECT_THAT(received.disconnected().client_ip(), StrEq(kClientIp));
EXPECT_THAT(received.disconnected().session_id(), StrEq(kSessionId));
}
{
reporter_.OnHostShutdown();
ASSERT_TRUE(delegate_->WaitForEvent());
auto received = delegate_->TakeEvent();
EXPECT_THAT(received.host_user().user_email(), StrEq(kHostUser));
ASSERT_TRUE(received.has_ended());
}
EXPECT_TRUE(delegate_->IsEmpty());
}
} // namespace
// static
std::unique_ptr<HostEventReporter> HostEventReporter::Create(
scoped_refptr<HostStatusMonitor> monitor) {
return std::make_unique<HostEventReporterImpl>(
monitor, std::make_unique<HostEventReporterDelegateStub>());
}
} // namespace remoting
|