1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "fuchsia/base/inspect.h"
6 
7 #include <lib/fdio/directory.h>
8 #include <lib/inspect/cpp/hierarchy.h>
9 #include <lib/inspect/cpp/reader.h>
10 #include <lib/inspect/service/cpp/reader.h>
11 #include <lib/sys/cpp/component_context.h>
12 #include <lib/sys/inspect/cpp/component.h>
13 #include <cstdint>
14 #include <memory>
15 
16 #include "base/task/single_thread_task_executor.h"
17 #include "base/test/task_environment.h"
18 #include "components/version_info/version_info.h"
19 #include "fuchsia/base/mem_buffer_util.h"
20 #include "fuchsia/base/string_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 
23 namespace cr_fuchsia {
24 
25 namespace {
26 
27 const char kVersion[] = "version";
28 const char kLastChange[] = "last_change_revision";
29 
30 class InspectTest : public ::testing::Test {
31  public:
InspectTest()32   InspectTest() {
33     fidl::InterfaceHandle<fuchsia::io::Directory> incoming_directory;
34     auto incoming_services =
35         std::make_shared<sys::ServiceDirectory>(std::move(incoming_directory));
36     context_ = std::make_unique<sys::ComponentContext>(
37         std::move(incoming_services),
38         published_root_directory_.NewRequest().TakeChannel());
39     inspector_ = std::make_unique<sys::ComponentInspector>(context_.get());
40     base::RunLoop().RunUntilIdle();
41   }
42 
43   InspectTest(const InspectTest&) = delete;
44   InspectTest& operator=(const InspectTest&) = delete;
45 
46  protected:
47   base::test::SingleThreadTaskEnvironment task_environment_{
48       base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
49   std::unique_ptr<sys::ComponentContext> context_;
50   fidl::InterfaceHandle<fuchsia::io::Directory> published_root_directory_;
51   std::unique_ptr<sys::ComponentInspector> inspector_;
52 };
53 
54 }  // namespace
55 
TEST_F(InspectTest,PublishVersionInfoToInspect)56 TEST_F(InspectTest, PublishVersionInfoToInspect) {
57   cr_fuchsia::PublishVersionInfoToInspect(inspector_.get());
58   fidl::InterfaceHandle<fuchsia::io::Directory> directory;
59   zx_status_t status = fdio_service_connect_at(
60       published_root_directory_.channel().get(), "diagnostics",
61       directory.NewRequest().TakeChannel().release());
62   ASSERT_EQ(ZX_OK, status);
63   std::unique_ptr<sys::ServiceDirectory> diagnostics =
64       std::make_unique<sys::ServiceDirectory>(std::move(directory));
65 
66   // Access the inspect::Tree where the data is served. |tree| is in the
67   // directory created for the test, not the diagnostics directory for the test
68   // component.
69   fuchsia::inspect::TreePtr tree;
70   diagnostics->Connect(tree.NewRequest());
71   fuchsia::inspect::TreeContent content;
72   base::RunLoop run_loop;
73   tree->GetContent([&content, &run_loop](fuchsia::inspect::TreeContent c) {
74     content = std::move(c);
75     run_loop.Quit();
76   });
77   run_loop.Run();
78 
79   // Parse the data as an inspect::Hierarchy.
80   ASSERT_TRUE(content.has_buffer());
81   std::string buffer_data;
82   cr_fuchsia::StringFromMemBuffer(content.buffer(), &buffer_data);
83   inspect::Hierarchy hierarchy =
84       inspect::ReadFromBuffer(cr_fuchsia::StringToBytes(buffer_data))
85           .take_value();
86 
87   auto* property =
88       hierarchy.node().get_property<inspect::StringPropertyValue>(kVersion);
89   EXPECT_EQ(property->value(), version_info::GetVersionNumber());
90   property =
91       hierarchy.node().get_property<inspect::StringPropertyValue>(kLastChange);
92   EXPECT_EQ(property->value(), version_info::GetLastChange());
93 }
94 
95 }  // namespace cr_fuchsia
96