Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 10a32521 authored by Jakub Rotkiewicz (xWF)'s avatar Jakub Rotkiewicz (xWF) Committed by Gerrit Code Review
Browse files

Merge changes I960be412,Id377cd0c into main

* changes:
  snoop_logger: recreate log directory if not present
  Revert "HCI: Failing to open the snoop-log is not fatal"
parents 4bb6652a c896099a
Loading
Loading
Loading
Loading
+25 −9
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
#include <algorithm>
#include <bitset>
#include <chrono>
#include <filesystem>
#include <sstream>

#include "common/circular_buffer.h"
@@ -322,6 +323,21 @@ std::string get_btsnoop_log_path(std::string log_dir, bool filtered) {

std::string get_last_log_path(std::string log_file_path) { return log_file_path.append(".last"); }

#ifdef __ANDROID__
static bool create_log_directories() {
  std::filesystem::path default_path = os::ParameterProvider::SnoopLogFilePath();
  std::filesystem::path default_dir_path = default_path.parent_path();

  if (std::filesystem::exists(default_dir_path)) {
    log::info("Directory {} already exists", default_dir_path.string());
    return true;
  }

  log::info("Creating directory: {}", default_dir_path.string());
  return std::filesystem::create_directories(default_dir_path);
}
#endif  // __ANDROID__

void delete_btsnoop_files(const std::string& log_path) {
  log::info("Deleting logs if they exist");
  if (os::FileExists(log_path)) {
@@ -531,6 +547,13 @@ void SnoopLogger::OpenNextSnoopLogFile() {

  auto last_file_path = get_last_log_path(snoop_log_path_);

#ifdef __ANDROID__
  if (com::android::bluetooth::flags::snoop_logger_recreate_logs_directory() &&
      !create_log_directories()) {
    log::error("Could not recreate log directory");
  }
#endif  // __ANDROID__

  if (os::FileExists(snoop_log_path_)) {
    if (!os::RenameFile(snoop_log_path_, last_file_path)) {
      log::error("Unabled to rename existing snoop log from \"{}\" to \"{}\"", snoop_log_path_,
@@ -547,21 +570,17 @@ void SnoopLogger::OpenNextSnoopLogFile() {
  file_creation_time = fake_timerfd_get_clock();
#endif
  if (!btsnoop_ostream_.good()) {
    log::error("Unable to open snoop log at \"{}\", error: \"{}\"", snoop_log_path_,
    log::fatal("Unable to open snoop log at \"{}\", error: \"{}\"", snoop_log_path_,
               strerror(errno));
    return;
  }
  umask(prevmask);
  if (!btsnoop_ostream_.write(reinterpret_cast<const char*>(&SnoopLoggerCommon::kBtSnoopFileHeader),
                              sizeof(SnoopLoggerCommon::FileHeaderType))) {
    log::error("Unable to write file header to \"{}\", error: \"{}\"", snoop_log_path_,
    log::fatal("Unable to write file header to \"{}\", error: \"{}\"", snoop_log_path_,
               strerror(errno));
    btsnoop_ostream_.close();
    return;
  }
  if (!btsnoop_ostream_.flush()) {
    log::error("Failed to flush, error: \"{}\"", strerror(errno));
    return;
  }
}

@@ -1203,9 +1222,6 @@ void SnoopLogger::Capture(const HciPacket& immutable_packet, Direction direction
    if (packet_counter_ > max_packets_per_file_) {
      OpenNextSnoopLogFile();
    }
    if (!btsnoop_ostream_.is_open() || !btsnoop_ostream_.good()) {
      return;
    }
    if (!btsnoop_ostream_.write(reinterpret_cast<const char*>(&header), sizeof(PacketHeaderType))) {
      log::error("Failed to write packet header for btsnoop, error: \"{}\"", strerror(errno));
    }
+89 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include "hal/snoop_logger.h"

#include <bluetooth/log.h>
#include <com_android_bluetooth_flags.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <netinet/in.h>
@@ -28,6 +29,7 @@
#include "hal/snoop_logger_common.h"
#include "hal/syscall_wrapper_impl.h"
#include "os/fake_timer/fake_timerfd.h"
#include "os/parameter_provider.h"
#include "os/system_properties.h"
#include "os/utils.h"

@@ -161,6 +163,7 @@ protected:
  }

  void TearDown() override {
    com::android::bluetooth::flags::provider_->reset_flags();
    DeleteSnoopLogFiles();
    fake_timerfd_reset();
    test_registry->StopAll();
@@ -389,7 +392,10 @@ TEST_F(SnoopLoggerModuleTest, delete_old_snooz_log_files) {
  handler->Post(bluetooth::common::BindOnce(fake_timerfd_advance, 15));
  sync_handler(handler);
  handler->Post(bluetooth::common::BindOnce(
          [](std::filesystem::path path) { ASSERT_FALSE(std::filesystem::exists(path)); },
          [](std::filesystem::path path) {
            log::info("path: {}", path.string());
            ASSERT_FALSE(std::filesystem::exists(path));
          },
          temp_snooz_log_));
  sync_handler(handler);

@@ -1502,4 +1508,86 @@ TEST_F(SnoopLoggerModuleTest, custom_socket_profiles_filtered_hfp_hf_test) {
  test_registry->StopAll();
  close(socket_fd);
}

#ifdef __ANDROID__
TEST_F(SnoopLoggerModuleTest, recreate_log_directory_when_enabled_test) {
  com::android::bluetooth::flags::provider_->snoop_logger_recreate_logs_directory(true);
  // Actual test
  const testing::TestInfo* const test_info = testing::UnitTest::GetInstance()->current_test_info();
  const std::filesystem::path os_btsnoop_file_path_ = os::ParameterProvider::SnoopLogFilePath();
  std::filesystem::path temp_dir_path_ = os_btsnoop_file_path_.parent_path();

  const std::filesystem::path temp_log_btsnoop_file_ =
          temp_dir_path_ / (std::string(test_info->name()) + "_btsnoop_hci.log");
  const std::filesystem::path temp_log_btsnooz_file_ =
          temp_dir_path_ / (std::string(test_info->name()) + "_btsnooz_hci.log");

  if (std::filesystem::exists(temp_dir_path_)) {
    std::filesystem::remove_all(temp_dir_path_);
  }

  ASSERT_FALSE(std::filesystem::exists(temp_dir_path_));

  auto* snoop_logger = new TestSnoopLoggerModule(temp_log_btsnoop_file_.string(),
                                                 temp_log_btsnooz_file_.string(), 10,
                                                 SnoopLogger::kBtSnoopLogModeFull, false, false);
  test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);

  ASSERT_TRUE(std::filesystem::exists(temp_dir_path_));

  test_registry->StopAll();

  // btsnoop file should exist
  ASSERT_TRUE(std::filesystem::exists(temp_log_btsnoop_file_));
  // btsnooz file should be removed as snoop_log_persists is false
  ASSERT_FALSE(std::filesystem::exists(temp_log_btsnooz_file_));
  // remove after test
  if (std::filesystem::exists(temp_dir_path_)) {
    std::filesystem::remove_all(temp_dir_path_);
  }
}

TEST_F(SnoopLoggerModuleTest, recreate_log_directory_when_filtered_test) {
  com::android::bluetooth::flags::provider_->snoop_logger_recreate_logs_directory(true);
  // Actual test
  const testing::TestInfo* const test_info = testing::UnitTest::GetInstance()->current_test_info();
  const std::filesystem::path os_btsnoop_file_path_ = os::ParameterProvider::SnoopLogFilePath();
  std::filesystem::path temp_dir_path_ = os_btsnoop_file_path_.parent_path();

  const std::filesystem::path temp_log_btsnoop_file_ =
          temp_dir_path_ / (std::string(test_info->name()) + "_btsnoop_hci.log");
  const std::filesystem::path temp_log_btsnooz_file_ =
          temp_dir_path_ / (std::string(test_info->name()) + "_btsnooz_hci.log");

  if (std::filesystem::exists(temp_dir_path_)) {
    std::filesystem::remove_all(temp_dir_path_);
  }

  ASSERT_FALSE(std::filesystem::exists(temp_dir_path_));

  auto* snoop_logger = new TestSnoopLoggerModule(
          temp_log_btsnoop_file_.string(), temp_log_btsnooz_file_.string(), 10,
          SnoopLogger::kBtSnoopLogModeFiltered, false, false);
  test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);

  ASSERT_TRUE(std::filesystem::exists(temp_dir_path_));

  test_registry->StopAll();

  const std::filesystem::path temp_log_btsnoop_filtered_file_ =
          temp_dir_path_ / (std::string(test_info->name()) + "_btsnoop_hci.log.filtered");
  const std::filesystem::path temp_log_btsnooz_filtered_file_ =
          temp_dir_path_ / (std::string(test_info->name()) + "_btsnooz_hci.log.filtered");

  // btsnoop file should exist
  ASSERT_TRUE(std::filesystem::exists(temp_log_btsnoop_filtered_file_));
  // btsnooz file should be removed as snoop_log_persists is false
  ASSERT_FALSE(std::filesystem::exists(temp_log_btsnooz_filtered_file_));
  // remove after test
  if (std::filesystem::exists(temp_dir_path_)) {
    std::filesystem::remove_all(temp_dir_path_);
  }
}
#endif  // __ANDROID__

}  // namespace testing