1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/fuchsia_component_support/inspect.h"
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>
17 #include "base/fuchsia/mem_buffer_util.h"
18 #include "base/task/single_thread_task_executor.h"
19 #include "base/test/task_environment.h"
20 #include "components/version_info/version_info.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace fuchsia_component_support {
27 const char kVersion[] = "version";
28 const char kLastChange[] = "last_change_revision";
30 class InspectTest : public ::testing::Test {
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), published_root_directory_.NewRequest());
38 inspector_ = std::make_unique<sys::ComponentInspector>(context_.get());
39 base::RunLoop().RunUntilIdle();
42 InspectTest(const InspectTest&) = delete;
43 InspectTest& operator=(const InspectTest&) = delete;
46 base::test::SingleThreadTaskEnvironment task_environment_{
47 base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
48 std::unique_ptr<sys::ComponentContext> context_;
49 fidl::InterfaceHandle<fuchsia::io::Directory> published_root_directory_;
50 std::unique_ptr<sys::ComponentInspector> inspector_;
55 TEST_F(InspectTest, PublishVersionInfoToInspect) {
56 fuchsia_component_support::PublishVersionInfoToInspect(inspector_.get());
57 fidl::InterfaceHandle<fuchsia::io::Directory> directory;
58 zx_status_t status = fdio_service_connect_at(
59 published_root_directory_.channel().get(), "diagnostics",
60 directory.NewRequest().TakeChannel().release());
61 ASSERT_EQ(ZX_OK, status);
62 std::unique_ptr<sys::ServiceDirectory> diagnostics =
63 std::make_unique<sys::ServiceDirectory>(std::move(directory));
65 // Access the inspect::Tree where the data is served. |tree| is in the
66 // directory created for the test, not the diagnostics directory for the test
68 fuchsia::inspect::TreePtr tree;
69 diagnostics->Connect(tree.NewRequest());
70 fuchsia::inspect::TreeContent content;
71 base::RunLoop run_loop;
72 tree->GetContent([&content, &run_loop](fuchsia::inspect::TreeContent c) {
73 content = std::move(c);
78 // Parse the data as an inspect::Hierarchy.
79 ASSERT_TRUE(content.has_buffer());
80 std::string buffer_data =
81 base::StringFromMemBuffer(content.buffer()).value_or(std::string());
82 const uint8_t* raw_data =
83 reinterpret_cast<const uint8_t*>(buffer_data.data());
84 inspect::Hierarchy hierarchy =
85 inspect::ReadFromBuffer(
86 std::vector<uint8_t>(raw_data, raw_data + buffer_data.length()))
90 hierarchy.node().get_property<inspect::StringPropertyValue>(kVersion);
91 EXPECT_EQ(property->value(), version_info::GetVersionNumber());
93 hierarchy.node().get_property<inspect::StringPropertyValue>(kLastChange);
94 EXPECT_EQ(property->value(), version_info::GetLastChange());
97 } // namespace fuchsia_component_support