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

Commit c896099a authored by Jakub Rotkiewicz's avatar Jakub Rotkiewicz
Browse files

snoop_logger: recreate log directory if not present

Bug: 350816694
Bug: 383876267
Flag: com.android.bluetooth.flags.snoop_logger_recreate_logs_directory
Test: atest SnoopLoggerModuleTest
Change-Id: I960be412b476d548479bd510713d4cf432a48f28
parent d7fa67c0
Loading
Loading
Loading
Loading
+23 −0
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_,
+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