File: harness_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (87 lines) | stat: -rw-r--r-- 2,869 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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/wayland/fuzzer/harness.h"

#include "base/files/scoped_temp_dir.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "components/exo/display.h"
#include "components/exo/test/exo_test_base.h"
#include "components/exo/test/test_security_delegate.h"
#include "components/exo/wayland/fuzzer/actions.pb.h"
#include "components/exo/wayland/server.h"

namespace exo {
namespace wayland_fuzzer {
namespace {

// Use ExoTestBase because Server starts to depends on ash::Shell.
using TestBase = test::ExoTestBase;

class WaylandFuzzerTest : public TestBase {
 protected:
  WaylandFuzzerTest() = default;
  WaylandFuzzerTest(const WaylandFuzzerTest&) = delete;
  WaylandFuzzerTest& operator=(const WaylandFuzzerTest&) = delete;
  ~WaylandFuzzerTest() override = default;

  void SetUp() override {
    ASSERT_TRUE(xdg_temp_dir_.CreateUniqueTempDir());
    setenv("XDG_RUNTIME_DIR", xdg_temp_dir_.GetPath().MaybeAsASCII().c_str(),
           1 /* overwrite */);
    TestBase::SetUp();
    display_ = std::make_unique<exo::Display>();
    server_ = wayland::Server::Create(
        display_.get(), std::make_unique<test::TestSecurityDelegate>());
    server_->StartWithDefaultPath(base::DoNothing());
  }

  void TearDown() override {
    server_.reset();
    display_.reset();
    TestBase::TearDown();
  }

  base::ScopedTempDir xdg_temp_dir_;
  std::unique_ptr<exo::Display> display_;
  std::unique_ptr<wayland::Server> server_;
};

void RunHarness(Harness* harness, base::WaitableEvent* event) {
  actions::actions acts;
  acts.add_acts()->mutable_act_wl_display_get_registry()->set_receiver(
      actions::small_value::ZERO);
  acts.add_acts();
  harness->Run(acts);
  event->Signal();
}

TEST_F(WaylandFuzzerTest, MakeSureItWorks) {
  Harness harness;

  // For this test we are just checking that some globals can be bound.
  EXPECT_TRUE(harness.wl_compositor_globals_.empty());
  EXPECT_TRUE(harness.wl_shm_globals_.empty());

  base::Thread client("client");
  ASSERT_TRUE(client.Start());
  base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
                            base::WaitableEvent::InitialState::NOT_SIGNALED);
  client.task_runner()->PostTask(FROM_HERE,
                                 base::BindOnce(&RunHarness, &harness, &event));
  // For this action sequence we need two dispatches. The first will bind the
  // registry, the second is for the callback.
  server_->Dispatch(base::Seconds(5));
  server_->Dispatch(base::Seconds(5));
  server_->Flush();
  event.Wait();

  EXPECT_FALSE(harness.wl_compositor_globals_.empty());
  EXPECT_FALSE(harness.wl_shm_globals_.empty());
}

}  // namespace
}  // namespace wayland_fuzzer
}  // namespace exo