File: guest_os_wayland_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 (135 lines) | stat: -rw-r--r-- 5,176 bytes parent folder | download | duplicates (6)
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
// 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 "chrome/browser/ash/guest_os/public/guest_os_wayland_server.h"

#include <memory>

#include "base/memory/weak_ptr.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/borealis/borealis_features.h"
#include "chrome/browser/ash/borealis/borealis_service.h"
#include "chrome/browser/ash/borealis/borealis_service_factory.h"
#include "chrome/browser/ash/borealis/testing/callback_factory.h"
#include "chrome/browser/ash/guest_os/guest_os_security_delegate.h"
#include "chrome/test/base/chrome_ash_test_base.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/dbus/concierge/fake_concierge_client.h"
#include "chromeos/ash/components/dbus/vm_wl/wl.pb.h"
#include "components/exo/data_exchange_delegate.h"
#include "components/exo/input_method_surface_manager.h"
#include "components/exo/notification_surface_manager.h"
#include "components/exo/server/wayland_server_controller.h"
#include "components/exo/toast_surface_manager.h"
#include "components/exo/wayland/test/wayland_server_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"

using borealis::NiceCallbackFactory;
using testing::_;
using testing::Invoke;
using testing::IsFalse;

namespace guest_os {

namespace {

class GuestOsWaylandServerTest : public ChromeAshTestBase {
 public:
  void SetUp() override {
    ASSERT_TRUE(root_dir.CreateUniqueTempDir());
    setenv("XDG_RUNTIME_DIR",
           root_dir.GetPath().Append("xdg").MaybeAsASCII().c_str(),
           /*overwrite=*/1);
    ChromeAshTestBase::SetUp();
  }

 protected:
  TestingProfile profile_;

 private:
  base::ScopedTempDir root_dir;
};

}  // namespace

TEST_F(GuestOsWaylandServerTest, BadSocketCausesFailure) {
  auto wsc = std::make_unique<exo::WaylandServerController>(
      nullptr, nullptr, nullptr, nullptr, nullptr);
  GuestOsWaylandServer gows(&profile_);

  base::test::TestFuture<std::optional<std::string>> result_future;
  gows.Listen({}, vm_tools::apps::UNKNOWN, "test", result_future.GetCallback());
  EXPECT_TRUE(result_future.Get().has_value());
}

TEST_F(GuestOsWaylandServerTest, NullDelegateCausesFailure) {
  auto wsc = std::make_unique<exo::WaylandServerController>(
      nullptr, nullptr, nullptr, nullptr, nullptr);
  GuestOsWaylandServer gows(&profile_);
  exo::wayland::test::WaylandServerTestBase::ScopedTempSocket socket;

  // This test relies on the borealis security delegate giving us nullptr
  // when borealis is disallowed.
  base::test::TestFuture<borealis::BorealisFeatures::AllowStatus>
      allowedness_future;
  borealis::BorealisServiceFactory::GetForProfile(&profile_)
      ->Features()
      .IsAllowed(allowedness_future.GetCallback());
  ASSERT_NE(allowedness_future.Get(),
            borealis::BorealisFeatures::AllowStatus::kAllowed);

  base::test::TestFuture<std::optional<std::string>> result_future;
  gows.Listen(socket.TakeFd(), vm_tools::apps::BOREALIS, "borealis",
              result_future.GetCallback());
  EXPECT_TRUE(result_future.Get().has_value());
}

TEST_F(GuestOsWaylandServerTest, DelegateLifetimeManagedCorrectly) {
  auto wsc = std::make_unique<exo::WaylandServerController>(
      nullptr, nullptr, nullptr, nullptr, nullptr);
  GuestOsWaylandServer gows(&profile_);
  exo::wayland::test::WaylandServerTestBase::ScopedTempSocket socket;

  // Initially the server doesn't exist.
  EXPECT_EQ(gows.GetDelegate(vm_tools::apps::UNKNOWN, "test"), nullptr);

  base::test::TestFuture<std::optional<std::string>> listen_future;
  gows.Listen(socket.TakeFd(), vm_tools::apps::UNKNOWN, "test",
              listen_future.GetCallback());
  EXPECT_FALSE(listen_future.Get().has_value());

  // Now the server is valid.
  base::WeakPtr<GuestOsSecurityDelegate> delegate =
      gows.GetDelegate(vm_tools::apps::UNKNOWN, "test");
  EXPECT_TRUE(delegate.MaybeValid());
  EXPECT_NE(delegate.get(), nullptr);

  base::test::TestFuture<std::optional<std::string>> close_future;
  gows.Close(vm_tools::apps::UNKNOWN, "test", close_future.GetCallback());
  EXPECT_FALSE(close_future.Get().has_value());

  EXPECT_EQ(gows.GetDelegate(vm_tools::apps::UNKNOWN, "test"), nullptr);
  EXPECT_TRUE(delegate.WasInvalidated());
}

TEST_F(GuestOsWaylandServerTest, EvictServersOnConciergeCrash) {
  ash::ConciergeClient::InitializeFake();
  auto* concierge_client = ash::FakeConciergeClient::Get();
  auto wsc = std::make_unique<exo::WaylandServerController>(
      nullptr, nullptr, nullptr, nullptr, nullptr);
  GuestOsWaylandServer gows(&profile_);

  exo::wayland::test::WaylandServerTestBase::ScopedTempSocket socket;
  base::test::TestFuture<std::optional<std::string>> listen_future;
  gows.Listen(socket.TakeFd(), vm_tools::apps::UNKNOWN, "test",
              listen_future.GetCallback());
  ASSERT_FALSE(listen_future.Get().has_value());

  EXPECT_NE(gows.GetDelegate(vm_tools::apps::UNKNOWN, "test"), nullptr);
  concierge_client->NotifyConciergeStopped();
  EXPECT_EQ(gows.GetDelegate(vm_tools::apps::UNKNOWN, "test"), nullptr);
}

}  // namespace guest_os