summaryrefslogtreecommitdiff
path: root/chromium/fuchsia/base/inspect_unittest.cc
blob: 7a2bff50c81ebfd27afd73a17409d7b4dab43397 (plain)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "fuchsia/base/inspect.h"

#include <lib/fdio/directory.h>
#include <lib/inspect/cpp/hierarchy.h>
#include <lib/inspect/cpp/reader.h>
#include <lib/inspect/service/cpp/reader.h>
#include <lib/sys/cpp/component_context.h>
#include <lib/sys/inspect/cpp/component.h>
#include <cstdint>
#include <memory>

#include "base/task/single_thread_task_executor.h"
#include "base/test/task_environment.h"
#include "components/version_info/version_info.h"
#include "fuchsia/base/mem_buffer_util.h"
#include "fuchsia/base/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cr_fuchsia {

namespace {

const char kVersion[] = "version";
const char kLastChange[] = "last_change_revision";

class InspectTest : public ::testing::Test {
 public:
  InspectTest() {
    fidl::InterfaceHandle<fuchsia::io::Directory> incoming_directory;
    auto incoming_services =
        std::make_shared<sys::ServiceDirectory>(std::move(incoming_directory));
    context_ = std::make_unique<sys::ComponentContext>(
        std::move(incoming_services),
        published_root_directory_.NewRequest().TakeChannel());
    inspector_ = std::make_unique<sys::ComponentInspector>(context_.get());
    base::RunLoop().RunUntilIdle();
  }

  InspectTest(const InspectTest&) = delete;
  InspectTest& operator=(const InspectTest&) = delete;

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  std::unique_ptr<sys::ComponentContext> context_;
  fidl::InterfaceHandle<fuchsia::io::Directory> published_root_directory_;
  std::unique_ptr<sys::ComponentInspector> inspector_;
};

}  // namespace

TEST_F(InspectTest, PublishVersionInfoToInspect) {
  cr_fuchsia::PublishVersionInfoToInspect(inspector_.get());
  fidl::InterfaceHandle<fuchsia::io::Directory> directory;
  zx_status_t status = fdio_service_connect_at(
      published_root_directory_.channel().get(), "diagnostics",
      directory.NewRequest().TakeChannel().release());
  ASSERT_EQ(ZX_OK, status);
  std::unique_ptr<sys::ServiceDirectory> diagnostics =
      std::make_unique<sys::ServiceDirectory>(std::move(directory));

  // Access the inspect::Tree where the data is served. |tree| is in the
  // directory created for the test, not the diagnostics directory for the test
  // component.
  fuchsia::inspect::TreePtr tree;
  diagnostics->Connect(tree.NewRequest());
  fuchsia::inspect::TreeContent content;
  base::RunLoop run_loop;
  tree->GetContent([&content, &run_loop](fuchsia::inspect::TreeContent c) {
    content = std::move(c);
    run_loop.Quit();
  });
  run_loop.Run();

  // Parse the data as an inspect::Hierarchy.
  ASSERT_TRUE(content.has_buffer());
  std::string buffer_data;
  cr_fuchsia::StringFromMemBuffer(content.buffer(), &buffer_data);
  inspect::Hierarchy hierarchy =
      inspect::ReadFromBuffer(cr_fuchsia::StringToBytes(buffer_data))
          .take_value();

  auto* property =
      hierarchy.node().get_property<inspect::StringPropertyValue>(kVersion);
  EXPECT_EQ(property->value(), version_info::GetVersionNumber());
  property =
      hierarchy.node().get_property<inspect::StringPropertyValue>(kLastChange);
  EXPECT_EQ(property->value(), version_info::GetLastChange());
}

}  // namespace cr_fuchsia