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

Commit 54443f9c authored by Jakub Rotkiewicz's avatar Jakub Rotkiewicz
Browse files

SnoopLogger socket: filter data on socket fix

Bug: 291899553
Test: atest bluetooth_test_gd_unit
Change-Id: Iae611b7b11a65ae5cee5ab4f8b7b874b5abc9269
parent 2673180e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1259,7 +1259,7 @@ void SnoopLogger::Capture(HciPacket& packet, Direction direction, PacketType typ

    if (socket_ != nullptr) {
      socket_->Write(&header, sizeof(PacketHeaderType));
      socket_->Write(packet.data(), packet.size());
      socket_->Write(packet.data(), (size_t)(length - 1));
    }

    // std::ofstream::flush() pushes user data into kernel memory. The data will be written even if this process
+113 −0
Original line number Diff line number Diff line
@@ -1589,4 +1589,117 @@ TEST_F(SnoopLoggerModuleTest, custom_socket_interface_register_logging_enabled_t

  test_registry->StopAll();
}

TEST_F(SnoopLoggerModuleTest, custom_socket_profiles_filtered_hfp_hf_test) {
  uint16_t conn_handle = 0x000b;
  uint16_t local_cid = 0x0043;
  uint16_t remote_cid = 0x3040;
  uint8_t dlci = 0x06;
  uint16_t psm = 0x0003;
  uint16_t profile_uuid_hfp_hf = 0x111f;
  bool flow = true;
  const std::string clcc_pattern = "\x0d\x0a+CLCC:";
  const uint16_t HEADER_SIZE = 12;
  size_t expected_data_size = HEADER_SIZE + strlen(clcc_pattern.c_str());
  std::vector<uint8_t> kPhoneNumber = {
      0x0b, 0x00, 0x30, 0x00, 0x2c, 0x00, 0x40, 0x30, 0x19, 0xff, 0x4f, 0x01, 0x0d,
      0x0a, 0x2b, 0x43, 0x4c, 0x43, 0x43, 0x3a, 0x20, 0x31, 0x2c, 0x31, 0x2c, 0x34,
      0x2c, 0x30, 0x2c, 0x30, 0x2c, 0x22, 0x2b, 0x39, 0x39, 0x31, 0x32, 0x33, 0x34,
      0x35, 0x36, 0x37, 0x38, 0x39, 0x22, 0x2c, 0x31, 0x34, 0x35, 0x0d, 0x0a, 0x49,
  };

  // Set pbap and map filtering modes
  ASSERT_TRUE(bluetooth::os::SetSystemProperty(
      SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
      SnoopLogger::kBtSnoopLogFilterProfileModeMagic));
  auto filterPbapModeProperty =
      bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty);
  ASSERT_TRUE(
      filterPbapModeProperty &&
      (filterPbapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeMagic) !=
       std::string::npos));
  ASSERT_TRUE(bluetooth::os::SetSystemProperty(
      SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
      SnoopLogger::kBtSnoopLogFilterProfileModeMagic));
  auto filterMapModeProperty =
      bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty);
  ASSERT_TRUE(
      filterMapModeProperty &&
      (filterMapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeMagic) !=
       std::string::npos));

  auto* snoop_logger = new TestSnoopLoggerModule(
      temp_snoop_log_.string(),
      temp_snooz_log_.string(),
      10,
      SnoopLogger::kBtSnoopLogModeFiltered,
      false,
      false);

  test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);

  int new_port = 8873;
  SyscallWrapperImpl syscall_if;
  auto sls = std::make_unique<SnoopLoggerSocket>(
      &syscall_if, SnoopLoggerSocket::DEFAULT_LOCALHOST_, new_port);
  SnoopLoggerSocketThread slsThread(std::move(sls));
  auto thread_start_future = slsThread.Start();
  thread_start_future.wait();
  ASSERT_TRUE(thread_start_future.get());

  snoop_logger->RegisterSocket(&slsThread);

  snoop_logger->SetL2capChannelOpen(conn_handle, local_cid, remote_cid, psm, false);
  snoop_logger->SetRfcommPortOpen(conn_handle, local_cid, dlci, profile_uuid_hfp_hf, flow);

  // // Create a TCP socket file descriptor
  int socket_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
  ASSERT_TRUE(socket_fd != INVALID_FD);

  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(SnoopLoggerSocket::DEFAULT_LOCALHOST_);
  addr.sin_port = htons(new_port);

  int ret = 0;
  // Connect to snoop logger socket
  RUN_NO_INTR(ret = connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr)));
  ASSERT_TRUE(ret == 0);

  char recv_buf1[sizeof(SnoopLoggerCommon::FileHeaderType)];
  char recv_buf2[sizeof(SnoopLogger::PacketHeaderType)];
  char recv_buf3[kPhoneNumber.size()];
  int bytes_read = -1;

  auto a = std::async(std::launch::async, [socket_fd, &recv_buf1, &recv_buf2, &recv_buf3] {
    recv(socket_fd, recv_buf1, sizeof(recv_buf1), 0);
    recv(socket_fd, recv_buf2, sizeof(recv_buf2), 0);
    return recv(socket_fd, recv_buf3, sizeof(recv_buf3), 0);
  });

  slsThread.GetSocket()->WaitForClientSocketConnected();

  snoop_logger->Capture(
      kPhoneNumber, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
  snoop_logger->SetL2capChannelClose(conn_handle, local_cid, remote_cid);
  snoop_logger->SetRfcommPortClose(conn_handle, local_cid, dlci, profile_uuid_hfp_hf);

  a.wait();
  bytes_read = a.get();

  ASSERT_TRUE(
      std::memcmp(recv_buf1, &SnoopLoggerCommon::kBtSnoopFileHeader, sizeof(recv_buf1)) == 0);
  ASSERT_EQ(bytes_read, static_cast<int>(expected_data_size));
  ASSERT_TRUE(std::memcmp(recv_buf3, kPhoneNumber.data(), expected_data_size) == 0);

  ASSERT_TRUE(bluetooth::os::SetSystemProperty(
      SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
      SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
  ASSERT_TRUE(bluetooth::os::SetSystemProperty(
      SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
      SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));

  test_registry->StopAll();
  close(socket_fd);
}
}  // namespace testing