File: chromoting_host_services_server_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (168 lines) | stat: -rw-r--r-- 6,472 bytes parent folder | download | duplicates (5)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2023 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/chromoting_host_services_server.h"

#include "base/command_line.h"
#include "base/location.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/mock_callback.h"
#include "base/test/multiprocess_test.h"
#include "base/test/task_environment.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "components/named_mojo_ipc_server/named_mojo_ipc_server_client_util.h"
#include "components/named_mojo_ipc_server/named_mojo_ipc_test_util.h"
#include "mojo/public/c/system/types.h"
#include "mojo/public/cpp/platform/named_platform_channel.h"
#include "mojo/public/cpp/platform/platform_channel_endpoint.h"
#include "mojo/public/cpp/system/invitation.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "mojo/public/cpp/system/simple_watcher.h"
#include "remoting/host/ipc_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"

namespace remoting {
namespace {

using testing::_;
using testing::Return;

static const char kClientProcessName[] = "ClientProcess";
static const char kClientProcessServerNameSwitch[] =
    "chromoting-host-services-server-name";

MULTIPROCESS_TEST_MAIN(ClientProcess) {
  base::test::TaskEnvironment task_environment{
      base::test::TaskEnvironment::MainThreadType::IO};
  base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  mojo::PlatformChannelEndpoint endpoint =
      named_mojo_ipc_server::ConnectToServer(
          cmd_line->GetSwitchValueNative(kClientProcessServerNameSwitch));
  auto invitation = mojo::IncomingInvitation::Accept(std::move(endpoint));
  auto message_pipe =
      invitation.ExtractMessagePipe(kChromotingHostServicesMessagePipeId);
  // |message_pipe| is always valid no matter whether the server sends an
  // invitation or not, so the best we can do is just to wait for the message
  // pipe to be closed by the server, which happens either when the server
  // refuses to send the invitation, or when the scoped message pipe is dropped
  // in BindChromotingHostServicesCallback.
  mojo::SimpleWatcher watcher(FROM_HERE,
                              mojo::SimpleWatcher::ArmingPolicy::AUTOMATIC);
  base::RunLoop run_loop;
  watcher.Watch(
      message_pipe.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED,
      MOJO_WATCH_CONDITION_SATISFIED,
      base::BindLambdaForTesting(
          [&](MojoResult watch_result, const mojo::HandleSignalsState& state) {
            ASSERT_EQ(watch_result, MOJO_RESULT_OK);
            if (state.peer_closed()) {
              run_loop.Quit();
            }
          }));
  run_loop.Run();
  return 0;
}

}  // namespace

class ChromotingHostServicesServerTest : public testing::Test {
 public:
  ChromotingHostServicesServerTest();
  ~ChromotingHostServicesServerTest() override;

 protected:
  base::Process LaunchClientProcess();
  int WaitForProcessExit(base::Process& process);

  mojo::NamedPlatformChannel::ServerName server_name_ =
      named_mojo_ipc_server::test::GenerateRandomServerName();
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::IO};

  // Helper thread to wait for process exit without blocking the main thread.
  base::Thread wait_for_process_exit_thread_{"wait_for_process_exit"};

  base::MockCallback<ChromotingHostServicesServer::Validator> mock_validator_;
  base::MockCallback<
      ChromotingHostServicesServer::BindChromotingHostServicesCallback>
      mock_bind_callback_;
  ChromotingHostServicesServer server_{server_name_, mock_validator_.Get(),
                                       mock_bind_callback_.Get()};
};

ChromotingHostServicesServerTest::ChromotingHostServicesServerTest() {
  wait_for_process_exit_thread_.StartWithOptions(
      base::Thread::Options(base::MessagePumpType::IO, 0));

  server_.StartServer();
}

ChromotingHostServicesServerTest::~ChromotingHostServicesServerTest() {
  server_.StopServer();
  task_environment_.RunUntilIdle();
}

base::Process ChromotingHostServicesServerTest::LaunchClientProcess() {
  base::CommandLine cmd_line = base::GetMultiProcessTestChildBaseCommandLine();
  cmd_line.AppendSwitchNative(kClientProcessServerNameSwitch, server_name_);
  return base::SpawnMultiProcessTestChild(kClientProcessName, cmd_line,
                                          /* options= */ {});
}

int ChromotingHostServicesServerTest::WaitForProcessExit(
    base::Process& process) {
  int exit_code;
  bool process_exited = false;
  base::RunLoop wait_for_process_exit_loop;
  wait_for_process_exit_thread_.task_runner()->PostTaskAndReply(
      FROM_HERE, base::BindLambdaForTesting([&]() {
        process_exited = base::WaitForMultiprocessTestChildExit(
            process, TestTimeouts::action_timeout(), &exit_code);
      }),
      wait_for_process_exit_loop.QuitClosure());
  wait_for_process_exit_loop.Run();
  process.Close();
  EXPECT_TRUE(process_exited);
  return exit_code;
}

TEST_F(ChromotingHostServicesServerTest,
       ValidClientConnects_BindingCallbackIsCalled) {
  EXPECT_CALL(mock_validator_, Run(_)).WillOnce(Return(true));
  EXPECT_CALL(mock_bind_callback_, Run(_, _))
      .WillOnce(
          [&](mojo::PendingReceiver<mojom::ChromotingHostServices> receiver,
              base::ProcessId peer_pid) {
            ASSERT_TRUE(receiver.is_valid());
            ASSERT_NE(peer_pid, base::kNullProcessId);
          });

  base::Process client_process = LaunchClientProcess();
  WaitForProcessExit(client_process);
}

// TODO: crbug.com/409928351 - this test is failing on Mac. Fix this.
#if BUILDFLAG(IS_MAC)
#define MAYBE_InvalidClientConnects_BindingCallbackIsNotCalled \
  DISABLED_InvalidClientConnects_BindingCallbackIsNotCalled
#else
#define MAYBE_InvalidClientConnects_BindingCallbackIsNotCalled \
  InvalidClientConnects_BindingCallbackIsNotCalled
#endif
TEST_F(ChromotingHostServicesServerTest,
       MAYBE_InvalidClientConnects_BindingCallbackIsNotCalled) {
  EXPECT_CALL(mock_validator_, Run(_)).WillOnce(Return(false));
  EXPECT_CALL(mock_bind_callback_, Run(_, _)).Times(0);

  base::Process client_process = LaunchClientProcess();
  WaitForProcessExit(client_process);
}

}  // namespace remoting