inspect_unittest.cc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include "components/fuchsia_component_support/inspect.h"
  5. #include <lib/fdio/directory.h>
  6. #include <lib/inspect/cpp/hierarchy.h>
  7. #include <lib/inspect/cpp/reader.h>
  8. #include <lib/inspect/service/cpp/reader.h>
  9. #include <lib/sys/cpp/component_context.h>
  10. #include <lib/sys/inspect/cpp/component.h>
  11. #include <cstdint>
  12. #include <memory>
  13. #include "base/fuchsia/mem_buffer_util.h"
  14. #include "base/task/single_thread_task_executor.h"
  15. #include "base/test/task_environment.h"
  16. #include "components/version_info/version_info.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. namespace fuchsia_component_support {
  19. namespace {
  20. const char kVersion[] = "version";
  21. const char kLastChange[] = "last_change_revision";
  22. class InspectTest : public ::testing::Test {
  23. public:
  24. InspectTest() {
  25. fidl::InterfaceHandle<fuchsia::io::Directory> incoming_directory;
  26. auto incoming_services =
  27. std::make_shared<sys::ServiceDirectory>(std::move(incoming_directory));
  28. context_ = std::make_unique<sys::ComponentContext>(
  29. std::move(incoming_services),
  30. published_root_directory_.NewRequest().TakeChannel());
  31. inspector_ = std::make_unique<sys::ComponentInspector>(context_.get());
  32. base::RunLoop().RunUntilIdle();
  33. }
  34. InspectTest(const InspectTest&) = delete;
  35. InspectTest& operator=(const InspectTest&) = delete;
  36. protected:
  37. base::test::SingleThreadTaskEnvironment task_environment_{
  38. base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  39. std::unique_ptr<sys::ComponentContext> context_;
  40. fidl::InterfaceHandle<fuchsia::io::Directory> published_root_directory_;
  41. std::unique_ptr<sys::ComponentInspector> inspector_;
  42. };
  43. } // namespace
  44. TEST_F(InspectTest, PublishVersionInfoToInspect) {
  45. fuchsia_component_support::PublishVersionInfoToInspect(inspector_.get());
  46. fidl::InterfaceHandle<fuchsia::io::Directory> directory;
  47. zx_status_t status = fdio_service_connect_at(
  48. published_root_directory_.channel().get(), "diagnostics",
  49. directory.NewRequest().TakeChannel().release());
  50. ASSERT_EQ(ZX_OK, status);
  51. std::unique_ptr<sys::ServiceDirectory> diagnostics =
  52. std::make_unique<sys::ServiceDirectory>(std::move(directory));
  53. // Access the inspect::Tree where the data is served. |tree| is in the
  54. // directory created for the test, not the diagnostics directory for the test
  55. // component.
  56. fuchsia::inspect::TreePtr tree;
  57. diagnostics->Connect(tree.NewRequest());
  58. fuchsia::inspect::TreeContent content;
  59. base::RunLoop run_loop;
  60. tree->GetContent([&content, &run_loop](fuchsia::inspect::TreeContent c) {
  61. content = std::move(c);
  62. run_loop.Quit();
  63. });
  64. run_loop.Run();
  65. // Parse the data as an inspect::Hierarchy.
  66. ASSERT_TRUE(content.has_buffer());
  67. std::string buffer_data =
  68. base::StringFromMemBuffer(content.buffer()).value_or(std::string());
  69. const uint8_t* raw_data =
  70. reinterpret_cast<const uint8_t*>(buffer_data.data());
  71. inspect::Hierarchy hierarchy =
  72. inspect::ReadFromBuffer(
  73. std::vector<uint8_t>(raw_data, raw_data + buffer_data.length()))
  74. .take_value();
  75. auto* property =
  76. hierarchy.node().get_property<inspect::StringPropertyValue>(kVersion);
  77. EXPECT_EQ(property->value(), version_info::GetVersionNumber());
  78. property =
  79. hierarchy.node().get_property<inspect::StringPropertyValue>(kLastChange);
  80. EXPECT_EQ(property->value(), version_info::GetLastChange());
  81. }
  82. } // namespace fuchsia_component_support