1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hal/snoop_logger.h"
18 
19 #include <bluetooth/log.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <netinet/in.h>
23 #include <sys/socket.h>
24 
25 #include <future>
26 #include <unordered_map>
27 
28 #include "common/init_flags.h"
29 #include "hal/snoop_logger_common.h"
30 #include "hal/syscall_wrapper_impl.h"
31 #include "os/fake_timer/fake_timerfd.h"
32 #include "os/system_properties.h"
33 #include "os/utils.h"
34 
35 namespace testing {
36 
37 using bluetooth::hal::SnoopLoggerCommon;
38 using bluetooth::hal::SnoopLoggerSocket;
39 using bluetooth::hal::SnoopLoggerSocketInterface;
40 using bluetooth::hal::SnoopLoggerSocketThread;
41 using bluetooth::hal::SyscallWrapperImpl;
42 using bluetooth::os::fake_timer::fake_timerfd_advance;
43 using bluetooth::os::fake_timer::fake_timerfd_reset;
44 using namespace bluetooth;
45 
46 namespace {
47 std::vector<uint8_t> kInformationRequest = {
48     0xfe,
49     0x2e,
50     0x0a,
51     0x00,
52     0x06,
53     0x00,
54     0x01,
55     0x00,
56     0x0a,
57     0x02,
58     0x02,
59     0x00,
60     0x02,
61     0x00,
62 };
63 
64 std::vector<uint8_t> kSdpConnectionRequest = {
65     0x08, 0x20, 0x0c, 0x00, 0x08, 0x00, 0x01, 0x00, 0x02, 0x0c, 0x04, 0x00, 0x01, 0x00, 0x44, 0x00};
66 
67 std::vector<uint8_t> kAvdtpSuspend = {0x02, 0x02, 0x00, 0x07, 0x00, 0x03, 0x00, 0x8d, 0x00, 0x90, 0x09, 0x04};
68 
69 std::vector<uint8_t> kHfpAtNrec0 = {0x02, 0x02, 0x20, 0x13, 0x00, 0x0f, 0x00, 0x41, 0x00, 0x09, 0xff, 0x15,
70                                     0x01, 0x41, 0x54, 0x2b, 0x4e, 0x52, 0x45, 0x43, 0x3d, 0x30, 0x0d, 0x5c};
71 
72 std::vector<uint8_t> kQualcommConnectionRequest = {0xdc, 0x2e, 0x54, 0x00, 0x50, 0x00, 0xff, 0x00, 0x00, 0x0a,
73                                                    0x0f, 0x09, 0x01, 0x00, 0x5c, 0x93, 0x01, 0x00, 0x42, 0x00};
74 
75 std::vector<uint8_t> kA2dpMediaPacket = {
76     0x0b, 0x20, 0x3a, 0x00, 0x36, 0x00, 0x40, 0xa0, 0x80, 0xe0, 0x07, 0x7f, 0x00, 0x1e, 0x08, 0x00,
77     0x00, 0x00, 0x00, 0x02, 0x47, 0xfc, 0x00, 0x00, 0xb0, 0x90, 0x80, 0x03, 0x00, 0x20, 0x21, 0x11,
78     0x45, 0x00, 0x14, 0x50, 0x01, 0x46, 0xf0, 0x81, 0x0a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a,
79     0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e,
80 };
81 
82 std::vector<bluetooth::hal::HciPacket> kTestData = {
83     {0x02, 0x20, 0x11, 0x00, 0x0d, 0x00, 0x41, 0x00, 0x9d, 0xef, 0x35,
84      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
85     {0x02, 0x00, 0x12, 0x00, 0x0e, 0x00, 0x40, 0x00, 0x9f, 0xff, 0x3f,
86      0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
87     {0x02, 0x20, 0x11, 0x00, 0x0d, 0x00, 0x41, 0x00, 0x9d, 0xef, 0x85,
88      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
89     {0x02, 0x00, 0x12, 0x00, 0x0e, 0x00, 0x40, 0x00, 0x9f, 0xff, 0x1f,
90      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
91     {0x02, 0x20, 0x11, 0x00, 0x0d, 0x00, 0x41, 0x00, 0x9d, 0xef, 0x99,
92      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
93     {0x02, 0x00, 0x13, 0x00, 0x0f, 0x00, 0x40, 0x00, 0x9f, 0xff, 0xc6, 0x01,
94      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
95     {0x02, 0x20, 0x11, 0x00, 0x0d, 0x00, 0x41, 0x00, 0x9d, 0xef, 0x99,
96      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
97     {0x02, 0x00, 0x12, 0x00, 0x0e, 0x00, 0x40, 0x00, 0x9f, 0xff, 0x1f,
98      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
99     {0x02, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x41, 0x00, 0x9d, 0xff, 0x01,
100      0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
101     {0x02, 0x00, 0x12, 0x00, 0x0e, 0x00, 0x40, 0x00, 0x9f, 0xff, 0x8f,
102      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
103     {0x02, 0x20, 0x11, 0x00, 0x0d, 0x00, 0x41, 0x00, 0x9d, 0xef, 0x89,
104      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
105     {0x02, 0x00, 0x12, 0x00, 0x0e, 0x00, 0x40, 0x00, 0x9f, 0xff, 0x1f,
106      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
107     {0x02, 0x20, 0x11, 0x00, 0x0d, 0x00, 0x41, 0x00, 0x9d, 0xef, 0x43,
108      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
109     {0x02, 0x00, 0x12, 0x00, 0x0e, 0x00, 0x40, 0x00, 0x9f, 0xff, 0x11,
110      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
111 
112 }  // namespace
113 
114 using bluetooth::TestModuleRegistry;
115 using bluetooth::hal::SnoopLogger;
116 using namespace std::chrono_literals;
117 
118 // Expose protected constructor for test
119 class TestSnoopLoggerModule : public SnoopLogger {
120  public:
TestSnoopLoggerModule(std::string snoop_log_path,std::string snooz_log_path,size_t max_packets_per_file,const std::string & btsnoop_mode,bool qualcomm_debug_log_enabled,bool snoop_log_persists)121   TestSnoopLoggerModule(
122       std::string snoop_log_path,
123       std::string snooz_log_path,
124       size_t max_packets_per_file,
125       const std::string& btsnoop_mode,
126       bool qualcomm_debug_log_enabled,
127       bool snoop_log_persists)
128       : SnoopLogger(
129             std::move(snoop_log_path),
130             std::move(snooz_log_path),
131             max_packets_per_file,
132             SnoopLogger::GetMaxPacketsPerBuffer(),
133             btsnoop_mode,
134             qualcomm_debug_log_enabled,
135             20ms,
136             5ms,
137             snoop_log_persists) {}
138 
ToString() const139   std::string ToString() const override {
140     return std::string("TestSnoopLoggerModule");
141   }
142 
CallGetDumpsysData(flatbuffers::FlatBufferBuilder * builder)143   void CallGetDumpsysData(flatbuffers::FlatBufferBuilder* builder) {
144     GetDumpsysData(builder);
145   }
146 
GetSocketThread()147   SnoopLoggerSocketThread* GetSocketThread() {
148     return snoop_logger_socket_thread_.get();
149   }
150 
GetL2capHeaderSize()151   static uint32_t GetL2capHeaderSize() {
152     return L2CAP_HEADER_SIZE;
153   }
154 
GetMaxFilteredSize()155   static size_t GetMaxFilteredSize() {
156     return MAX_HCI_ACL_LEN - PACKET_TYPE_LENGTH;
157   }
158 };
159 
160 class SnoopLoggerModuleTest : public Test {
161  public:
162   flatbuffers::FlatBufferBuilder* builder_;
163   TestModuleRegistry* test_registry;
164 
165  protected:
SetUp()166   void SetUp() override {
167     const testing::TestInfo* const test_info =
168         testing::UnitTest::GetInstance()->current_test_info();
169 
170     log::debug(
171         "Setup for test {} in test suite {}.", test_info->name(), test_info->test_suite_name());
172     const std::filesystem::path temp_dir_ = std::filesystem::temp_directory_path();
173 
174     temp_snoop_log_ = temp_dir_ / (std::string(test_info->name()) + "_btsnoop_hci.log");
175     temp_snoop_log_last_ = temp_dir_ / (std::string(test_info->name()) + "_btsnoop_hci.log.last");
176     temp_snooz_log_ = temp_dir_ / (std::string(test_info->name()) + "_btsnooz_hci.log");
177     temp_snooz_log_last_ = temp_dir_ / (std::string(test_info->name()) + "_btsnooz_hci.log.last");
178     temp_snoop_log_filtered =
179         temp_dir_ / (std::string(test_info->name()) + "_btsnoop_hci.log.filtered");
180     temp_snoop_log_filtered_last =
181         temp_dir_ / (std::string(test_info->name()) + "_btsnoop_hci.log.filtered.last");
182     builder_ = new flatbuffers::FlatBufferBuilder();
183 
184     DeleteSnoopLogFiles();
185     ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
186     ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
187     ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_filtered));
188     ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_filtered_last));
189     ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
190     ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_last_));
191 
192     test_registry = new TestModuleRegistry();
193   }
194 
TearDown()195   void TearDown() override {
196     DeleteSnoopLogFiles();
197     delete builder_;
198     fake_timerfd_reset();
199     test_registry->StopAll();
200     delete test_registry;
201 
202     const testing::TestInfo* const test_info =
203         testing::UnitTest::GetInstance()->current_test_info();
204     log::debug(
205         "TearDown for test {} in test suite {}.", test_info->name(), test_info->test_suite_name());
206   }
207 
208   std::filesystem::path temp_snoop_log_;
209   std::filesystem::path temp_snoop_log_last_;
210   std::filesystem::path temp_snooz_log_;
211   std::filesystem::path temp_snooz_log_last_;
212   std::filesystem::path temp_snoop_log_filtered;
213   std::filesystem::path temp_snoop_log_filtered_last;
214 
215  private:
DeleteSnoopLogFiles()216   void DeleteSnoopLogFiles() {
217     if (std::filesystem::exists(temp_snoop_log_)) {
218       ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_));
219     }
220     if (std::filesystem::exists(temp_snoop_log_last_)) {
221       ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_last_));
222     }
223     if (std::filesystem::exists(temp_snoop_log_filtered)) {
224       ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
225     }
226     if (std::filesystem::exists(temp_snoop_log_filtered_last)) {
227       ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered_last));
228     }
229     if (std::filesystem::exists(temp_snooz_log_)) {
230       ASSERT_TRUE(std::filesystem::remove(temp_snooz_log_));
231     }
232     if (std::filesystem::exists(temp_snooz_log_last_)) {
233       ASSERT_TRUE(std::filesystem::remove(temp_snooz_log_last_));
234     }
235   }
236 };
237 
TEST_F(SnoopLoggerModuleTest,empty_snoop_log_test)238 TEST_F(SnoopLoggerModuleTest, empty_snoop_log_test) {
239   // Actual test
240   auto* snoop_logger = new TestSnoopLoggerModule(
241       temp_snoop_log_.string(),
242       temp_snooz_log_.string(),
243       10,
244       SnoopLogger::kBtSnoopLogModeFull,
245       false,
246       false);
247   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
248   test_registry->StopAll();
249 
250   // Verify states after test
251   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_));
252   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
253   ASSERT_EQ(std::filesystem::file_size(temp_snoop_log_), sizeof(SnoopLoggerCommon::FileHeaderType));
254 }
255 
TEST_F(SnoopLoggerModuleTest,disable_snoop_log_test)256 TEST_F(SnoopLoggerModuleTest, disable_snoop_log_test) {
257   // Actual test
258   auto* snoop_logger = new TestSnoopLoggerModule(
259       temp_snoop_log_.string(),
260       temp_snooz_log_.string(),
261       10,
262       SnoopLogger::kBtSnoopLogModeDisabled,
263       false,
264       false);
265   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
266   test_registry->StopAll();
267 
268   // Verify states after test
269   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
270   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
271   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
272 }
273 
TEST_F(SnoopLoggerModuleTest,capture_one_packet_test)274 TEST_F(SnoopLoggerModuleTest, capture_one_packet_test) {
275   // Actual test
276   auto* snoop_logger = new TestSnoopLoggerModule(
277       temp_snoop_log_.string(),
278       temp_snooz_log_.string(),
279       10,
280       SnoopLogger::kBtSnoopLogModeFull,
281       false,
282       false);
283   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
284 
285   snoop_logger->Capture(kInformationRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
286 
287   test_registry->StopAll();
288 
289   // Verify states after test
290   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_));
291   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
292   ASSERT_EQ(
293       std::filesystem::file_size(temp_snoop_log_),
294       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + kInformationRequest.size());
295 }
296 
TEST_F(SnoopLoggerModuleTest,capture_hci_cmd_btsnooz_test)297 TEST_F(SnoopLoggerModuleTest, capture_hci_cmd_btsnooz_test) {
298   // Actual test
299   auto* snoop_logger = new TestSnoopLoggerModule(
300       temp_snoop_log_.string(),
301       temp_snooz_log_.string(),
302       10,
303       SnoopLogger::kBtSnoopLogModeDisabled,
304       false,
305       false);
306   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
307 
308   snoop_logger->Capture(kInformationRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
309   snoop_logger->CallGetDumpsysData(builder_);
310 
311   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
312   ASSERT_EQ(
313       std::filesystem::file_size(temp_snooz_log_),
314       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + kInformationRequest.size());
315 
316   test_registry->StopAll();
317 
318   // Verify states after test
319   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
320   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
321   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
322 }
323 
TEST_F(SnoopLoggerModuleTest,capture_l2cap_signal_packet_btsnooz_test)324 TEST_F(SnoopLoggerModuleTest, capture_l2cap_signal_packet_btsnooz_test) {
325   // Actual test
326   auto* snoop_logger = new TestSnoopLoggerModule(
327       temp_snoop_log_.string(),
328       temp_snooz_log_.string(),
329       10,
330       SnoopLogger::kBtSnoopLogModeDisabled,
331       false,
332       false);
333   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
334 
335   snoop_logger->Capture(kSdpConnectionRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
336   snoop_logger->CallGetDumpsysData(builder_);
337 
338   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
339   ASSERT_EQ(
340       std::filesystem::file_size(temp_snooz_log_),
341       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + kSdpConnectionRequest.size());
342 
343   test_registry->StopAll();
344 
345   // Verify states after test
346   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
347   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
348   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
349 }
350 
TEST_F(SnoopLoggerModuleTest,capture_l2cap_short_data_packet_btsnooz_test)351 TEST_F(SnoopLoggerModuleTest, capture_l2cap_short_data_packet_btsnooz_test) {
352   // Actual test
353   auto* snoop_logger = new TestSnoopLoggerModule(
354       temp_snoop_log_.string(),
355       temp_snooz_log_.string(),
356       10,
357       SnoopLogger::kBtSnoopLogModeDisabled,
358       false,
359       false);
360   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
361 
362   snoop_logger->Capture(kAvdtpSuspend, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
363   snoop_logger->CallGetDumpsysData(builder_);
364 
365   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
366   ASSERT_EQ(
367       std::filesystem::file_size(temp_snooz_log_),
368       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + kAvdtpSuspend.size());
369 
370   test_registry->StopAll();
371 
372   // Verify states after test
373   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
374   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
375   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
376 }
377 
TEST_F(SnoopLoggerModuleTest,capture_l2cap_long_data_packet_btsnooz_test)378 TEST_F(SnoopLoggerModuleTest, capture_l2cap_long_data_packet_btsnooz_test) {
379   // Actual test
380   auto* snoop_logger = new TestSnoopLoggerModule(
381       temp_snoop_log_.string(),
382       temp_snooz_log_.string(),
383       10,
384       SnoopLogger::kBtSnoopLogModeDisabled,
385       false,
386       false);
387   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
388 
389   snoop_logger->Capture(kHfpAtNrec0, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
390   snoop_logger->CallGetDumpsysData(builder_);
391 
392   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
393   ASSERT_EQ(
394       std::filesystem::file_size(temp_snooz_log_),
395       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + 14);
396 
397   test_registry->StopAll();
398 
399   // Verify states after test
400   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
401   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
402   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
403 }
404 
TEST_F(SnoopLoggerModuleTest,snoop_log_persists)405 TEST_F(SnoopLoggerModuleTest, snoop_log_persists) {
406   // Actual test
407   auto* snoop_logger = new TestSnoopLoggerModule(
408       temp_snoop_log_.string(),
409       temp_snooz_log_.string(),
410       10,
411       SnoopLogger::kBtSnoopLogModeDisabled,
412       false,
413       true);
414   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
415 
416   snoop_logger->Capture(
417       kHfpAtNrec0, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
418   snoop_logger->CallGetDumpsysData(builder_);
419 
420   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
421   ASSERT_EQ(
422       std::filesystem::file_size(temp_snooz_log_),
423       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + 14);
424 
425   test_registry->StopAll();
426   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
427 }
428 
sync_handler(bluetooth::os::Handler * handler)429 void sync_handler(bluetooth::os::Handler* handler) {
430   std::promise<void> promise;
431   auto future = promise.get_future();
432   handler->Post(bluetooth::common::BindOnce(
433       &std::promise<void>::set_value, bluetooth::common::Unretained(&promise)));
434   auto future_status = future.wait_for(std::chrono::seconds(1));
435   ASSERT_EQ(future_status, std::future_status::ready);
436 }
437 
TEST_F(SnoopLoggerModuleTest,delete_old_snooz_log_files)438 TEST_F(SnoopLoggerModuleTest, delete_old_snooz_log_files) {
439   // Actual test
440   auto* snoop_logger = new TestSnoopLoggerModule(
441       temp_snoop_log_.string(),
442       temp_snooz_log_.string(),
443       10,
444       SnoopLogger::kBtSnoopLogModeDisabled,
445       false,
446       false);
447   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
448 
449   std::filesystem::create_directories(temp_snooz_log_);
450 
451   auto* handler = test_registry->GetTestModuleHandler(&SnoopLogger::Factory);
452   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
453   handler->Post(bluetooth::common::BindOnce(fake_timerfd_advance, 10));
454   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
455   handler->Post(bluetooth::common::BindOnce(fake_timerfd_advance, 15));
456   sync_handler(handler);
457   handler->Post(bluetooth::common::BindOnce(
458       [](std::filesystem::path path) { ASSERT_FALSE(std::filesystem::exists(path)); }, temp_snooz_log_));
459   sync_handler(handler);
460 
461   test_registry->StopAll();
462   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
463 }
464 
TEST_F(SnoopLoggerModuleTest,rotate_file_at_new_session_test)465 TEST_F(SnoopLoggerModuleTest, rotate_file_at_new_session_test) {
466   // Start once
467   {
468     auto* snoop_logger = new TestSnoopLoggerModule(
469         temp_snoop_log_.string(),
470         temp_snooz_log_.string(),
471         10,
472         SnoopLogger::kBtSnoopLogModeFull,
473         false,
474         false);
475     test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
476     snoop_logger->Capture(
477         kInformationRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
478     test_registry->StopAll();
479   }
480 
481   // Verify states after test
482   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_));
483   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
484   ASSERT_EQ(
485       std::filesystem::file_size(temp_snoop_log_),
486       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + kInformationRequest.size());
487 
488   // Start again
489   {
490     auto* snoop_logger = new TestSnoopLoggerModule(
491         temp_snoop_log_.string(),
492         temp_snooz_log_.string(),
493         10,
494         SnoopLogger::kBtSnoopLogModeFull,
495         false,
496         false);
497     test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
498     snoop_logger->Capture(
499         kInformationRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
500     snoop_logger->Capture(
501         kInformationRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
502     test_registry->StopAll();
503   }
504 
505   // Verify states after test
506   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_));
507   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_last_));
508   ASSERT_EQ(
509       std::filesystem::file_size(temp_snoop_log_),
510       sizeof(SnoopLoggerCommon::FileHeaderType) +
511           (sizeof(SnoopLogger::PacketHeaderType) + kInformationRequest.size()) * 2);
512   ASSERT_EQ(
513       std::filesystem::file_size(temp_snoop_log_last_),
514       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + kInformationRequest.size());
515 }
516 
TEST_F(SnoopLoggerModuleTest,rotate_file_after_full_test)517 TEST_F(SnoopLoggerModuleTest, rotate_file_after_full_test) {
518   // Actual test
519   auto* snoop_logger = new TestSnoopLoggerModule(
520       temp_snoop_log_.string(),
521       temp_snooz_log_.string(),
522       10,
523       SnoopLogger::kBtSnoopLogModeFull,
524       false,
525       false);
526   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
527 
528   for (int i = 0; i < 11; i++) {
529     snoop_logger->Capture(kInformationRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
530   }
531 
532   test_registry->StopAll();
533 
534   // Verify states after test
535   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_));
536   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_last_));
537   ASSERT_EQ(
538       std::filesystem::file_size(temp_snoop_log_),
539       sizeof(SnoopLoggerCommon::FileHeaderType) +
540           (sizeof(SnoopLogger::PacketHeaderType) + kInformationRequest.size()) * 1);
541   ASSERT_EQ(
542       std::filesystem::file_size(temp_snoop_log_last_),
543       sizeof(SnoopLoggerCommon::FileHeaderType) +
544           (sizeof(SnoopLogger::PacketHeaderType) + kInformationRequest.size()) * 10);
545 }
546 
TEST_F(SnoopLoggerModuleTest,qualcomm_debug_log_test)547 TEST_F(SnoopLoggerModuleTest, qualcomm_debug_log_test) {
548   auto* snoop_logger = new TestSnoopLoggerModule(
549       temp_snoop_log_.string(),
550       temp_snooz_log_.string(),
551       10,
552       SnoopLogger::kBtSnoopLogModeDisabled,
553       true,
554       false);
555   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
556   snoop_logger->Capture(
557       kQualcommConnectionRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
558   snoop_logger->CallGetDumpsysData(builder_);
559 
560   ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
561   ASSERT_EQ(
562       std::filesystem::file_size(temp_snooz_log_),
563       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) +
564           kQualcommConnectionRequest.size());
565 
566   test_registry->StopAll();
567 
568   // Verify states after test
569   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
570   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
571   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
572 }
573 
TEST_F(SnoopLoggerModuleTest,qualcomm_debug_log_regression_test)574 TEST_F(SnoopLoggerModuleTest, qualcomm_debug_log_regression_test) {
575   {
576     auto* snoop_logger = new TestSnoopLoggerModule(
577         temp_snoop_log_.string(),
578         temp_snooz_log_.string(),
579         10,
580         SnoopLogger::kBtSnoopLogModeDisabled,
581         true,
582         false);
583     test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
584     snoop_logger->Capture(
585         kHfpAtNrec0, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
586     snoop_logger->CallGetDumpsysData(builder_);
587 
588     ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
589     ASSERT_EQ(
590         std::filesystem::file_size(temp_snooz_log_),
591         sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + 14);
592     test_registry->StopAll();
593   }
594 
595   // Verify states after test
596   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
597   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
598   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
599 
600   {
601     auto* snoop_logger = new TestSnoopLoggerModule(
602         temp_snoop_log_.string(),
603         temp_snooz_log_.string(),
604         10,
605         SnoopLogger::kBtSnoopLogModeDisabled,
606         false,
607         false);
608     test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
609     snoop_logger->Capture(
610         kQualcommConnectionRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
611     snoop_logger->CallGetDumpsysData(builder_);
612 
613     ASSERT_TRUE(std::filesystem::exists(temp_snooz_log_));
614     ASSERT_EQ(
615         std::filesystem::file_size(temp_snooz_log_),
616         sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) + 14);
617     test_registry->StopAll();
618   }
619 
620   // Verify states after test
621   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_));
622   ASSERT_FALSE(std::filesystem::exists(temp_snoop_log_last_));
623   ASSERT_FALSE(std::filesystem::exists(temp_snooz_log_));
624 }
625 
TEST_F(SnoopLoggerModuleTest,filter_tracker_test)626 TEST_F(SnoopLoggerModuleTest, filter_tracker_test) {
627   std::unordered_map<uint16_t, bluetooth::hal::FilterTracker> filter_list;
628   uint16_t handle = 1;
629   uint16_t local_cid = 0x40;
630   uint16_t remote_cid = 0x41;
631   uint8_t dlci = 0x02;
632 
633   filter_list[handle].AddL2capCid(local_cid, remote_cid);
634   ASSERT_TRUE(filter_list[handle].IsAcceptlistedL2cap(true, local_cid));
635   ASSERT_TRUE(filter_list[handle].IsAcceptlistedL2cap(false, remote_cid));
636 
637   filter_list[handle].RemoveL2capCid(local_cid, remote_cid);
638   ASSERT_FALSE(filter_list[handle].IsAcceptlistedL2cap(true, local_cid));
639   ASSERT_FALSE(filter_list[handle].IsAcceptlistedL2cap(false, remote_cid));
640 
641   filter_list[handle].AddRfcommDlci(dlci);
642   ASSERT_TRUE(filter_list[handle].IsAcceptlistedDlci(dlci));
643 
644   filter_list[handle].SetRfcommCid(local_cid, remote_cid);
645   ASSERT_TRUE(filter_list[handle].IsRfcommChannel(true, local_cid));
646   ASSERT_TRUE(filter_list[handle].IsRfcommChannel(false, remote_cid));
647 
648   filter_list[handle].RemoveL2capCid(local_cid, remote_cid);
649   ASSERT_FALSE(filter_list[handle].IsAcceptlistedL2cap(true, local_cid));
650   ASSERT_FALSE(filter_list[handle].IsAcceptlistedL2cap(false, remote_cid));
651   ASSERT_FALSE(filter_list[handle].IsAcceptlistedDlci(dlci));
652 }
653 
TEST_F(SnoopLoggerModuleTest,a2dp_packets_filtered_test)654 TEST_F(SnoopLoggerModuleTest, a2dp_packets_filtered_test) {
655   // Actual test
656   uint16_t conn_handle = 0x000b;
657   uint16_t local_cid = 0x0001;
658   uint16_t remote_cid = 0xa040;
659 
660   ASSERT_TRUE(
661       bluetooth::os::SetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileA2dpProperty, "true"));
662   auto filter_a2dp_property =
663       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileA2dpProperty);
664   ASSERT_TRUE(filter_a2dp_property && filter_a2dp_property.value() == "true");
665 
666   auto* snoop_logger = new TestSnoopLoggerModule(
667       temp_snoop_log_.string(),
668       temp_snooz_log_.string(),
669       10,
670       SnoopLogger::kBtSnoopLogModeFiltered,
671       false,
672       false);
673 
674   TestModuleRegistry test_registry;
675   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
676 
677   // Simulate A2dp Media channel setup
678   snoop_logger->AddA2dpMediaChannel(conn_handle, local_cid, remote_cid);
679 
680   snoop_logger->Capture(
681       kA2dpMediaPacket, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
682 
683   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
684 
685   test_registry.StopAll();
686 
687   ASSERT_TRUE(
688       bluetooth::os::SetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileA2dpProperty, "false"));
689 
690   // Verify states after test
691   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
692   // Should filter packet
693   ASSERT_EQ(
694       std::filesystem::file_size(temp_snoop_log_filtered),
695       sizeof(SnoopLoggerCommon::FileHeaderType));
696   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
697 }
698 
TEST_F(SnoopLoggerModuleTest,a2dp_packets_filtered_negative_test)699 TEST_F(SnoopLoggerModuleTest, a2dp_packets_filtered_negative_test) {
700   // Actual test
701   uint16_t conn_handle = 0x000b;
702   uint16_t local_cid = 0x0001;
703   uint16_t remote_cid = 0xa040;
704 
705   ASSERT_TRUE(
706       bluetooth::os::SetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileA2dpProperty, "true"));
707   auto filter_a2dp_property =
708       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileA2dpProperty);
709   ASSERT_TRUE(filter_a2dp_property && filter_a2dp_property.value() == "true");
710 
711   auto* snoop_logger = new TestSnoopLoggerModule(
712       temp_snoop_log_.string(),
713       temp_snooz_log_.string(),
714       10,
715       SnoopLogger::kBtSnoopLogModeFiltered,
716       false,
717       false);
718 
719   TestModuleRegistry test_registry;
720   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
721 
722   // Simulate A2dp Media channel setup
723   snoop_logger->AddA2dpMediaChannel(conn_handle, local_cid, remote_cid);
724   snoop_logger->RemoveA2dpMediaChannel(conn_handle, local_cid);
725 
726   snoop_logger->Capture(
727       kA2dpMediaPacket, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
728 
729   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
730 
731   test_registry.StopAll();
732 
733   ASSERT_TRUE(
734       bluetooth::os::SetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileA2dpProperty, "false"));
735 
736   // Verify states after test
737   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
738   // Should not filter
739   ASSERT_EQ(
740       std::filesystem::file_size(temp_snoop_log_filtered),
741       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) +
742           kA2dpMediaPacket.size());
743   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
744 }
745 
TEST_F(SnoopLoggerModuleTest,headers_filtered_test)746 TEST_F(SnoopLoggerModuleTest, headers_filtered_test) {
747   ASSERT_TRUE(
748       bluetooth::os::SetSystemProperty(SnoopLogger::kBtSnoopLogFilterHeadersProperty, "true"));
749   auto filter_headers_property =
750       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterHeadersProperty);
751   ASSERT_TRUE(filter_headers_property && filter_headers_property.value() == "true");
752 
753   auto* snoop_logger = new TestSnoopLoggerModule(
754       temp_snoop_log_.string(),
755       temp_snooz_log_.string(),
756       10,
757       SnoopLogger::kBtSnoopLogModeFiltered,
758       false,
759       false);
760 
761   TestModuleRegistry test_registry;
762   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
763 
764   std::vector<uint8_t> kAclPacket = {
765       0x0b, 0x20, 0x18, 0x00, 0x14, 0x00, 0x44, 0x00, 0x1b, 0x2f, 0x21, 0x41, 0x54, 0x2b,
766       0x43, 0x4d, 0x45, 0x52, 0x3d, 0x33, 0x2c, 0x30, 0x2c, 0x30, 0x2c, 0x31, 0x0d, 0x8f,
767   };
768 
769   snoop_logger->Capture(kAclPacket, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
770 
771   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
772 
773   test_registry.StopAll();
774 
775   ASSERT_TRUE(
776       bluetooth::os::SetSystemProperty(SnoopLogger::kBtSnoopLogFilterHeadersProperty, "false"));
777 
778   // Verify states after test
779   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
780   log::info(
781       "const size: {}",
782       (int)(sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType)));
783 
784   // Packet should be filtered
785   const size_t file_size = (size_t)std::filesystem::file_size(temp_snoop_log_filtered);
786   const size_t expected_file_size = sizeof(SnoopLoggerCommon::FileHeaderType) +
787                                     sizeof(SnoopLogger::PacketHeaderType) +
788                                     TestSnoopLoggerModule::GetMaxFilteredSize();
789   ASSERT_EQ(file_size, expected_file_size);
790   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
791 }
792 
TEST_F(SnoopLoggerModuleTest,rfcomm_channel_filtered_sabme_ua_test)793 TEST_F(SnoopLoggerModuleTest, rfcomm_channel_filtered_sabme_ua_test) {
794   // Actual test
795   uint16_t conn_handle = 0x000b;
796   uint16_t local_cid = 0x0044;
797   uint16_t remote_cid = 0x3040;
798 
799   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
800       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "true"));
801   auto filter_rfcomm_property =
802       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty);
803   ASSERT_TRUE(filter_rfcomm_property);
804   ASSERT_TRUE(filter_rfcomm_property.value() == "true");
805 
806   auto* snoop_logger = new TestSnoopLoggerModule(
807       temp_snoop_log_.string(),
808       temp_snooz_log_.string(),
809       10,
810       SnoopLogger::kBtSnoopLogModeFiltered,
811       false,
812       false);
813 
814   TestModuleRegistry test_registry;
815   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
816 
817   // Simulate Rfcomm channel
818   snoop_logger->AddRfcommL2capChannel(conn_handle, local_cid, remote_cid);
819   std::vector<uint8_t> kRfcommSabme = {
820       0x0b, 0x20, 0x18, 0x00, 0x14, 0x00, 0x44, 0x00, 0x1b, 0x2f, 0x21, 0x41, 0x54, 0x2b,
821       0x43, 0x4d, 0x45, 0x52, 0x3d, 0x33, 0x2c, 0x30, 0x2c, 0x30, 0x2c, 0x31, 0x0d, 0x8f,
822   };
823   std::vector<uint8_t> kRfcommUa = {
824       0x0b, 0x20, 0x18, 0x00, 0x14, 0x00, 0x44, 0x00, 0x1b, 0x63, 0x21, 0x41, 0x54, 0x2b,
825       0x43, 0x4d, 0x45, 0x52, 0x3d, 0x33, 0x2c, 0x30, 0x2c, 0x30, 0x2c, 0x31, 0x0d, 0x8f,
826   };
827 
828   snoop_logger->Capture(
829       kRfcommSabme, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
830   snoop_logger->Capture(kRfcommUa, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
831   snoop_logger->ClearL2capAcceptlist(conn_handle, local_cid, remote_cid);
832   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
833 
834   test_registry.StopAll();
835 
836   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
837       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "false"));
838 
839   // Verify states after test
840   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
841 
842   // Packets should not be filtered because because they are SAMBE and UA events.
843   ASSERT_EQ(
844       std::filesystem::file_size(temp_snoop_log_filtered),
845       sizeof(SnoopLoggerCommon::FileHeaderType) + 2 * sizeof(SnoopLogger::PacketHeaderType) +
846           kRfcommSabme.size() + kRfcommUa.size());
847   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
848 }
849 
TEST_F(SnoopLoggerModuleTest,rfcomm_channel_filtered_acceptlisted_dlci_test)850 TEST_F(SnoopLoggerModuleTest, rfcomm_channel_filtered_acceptlisted_dlci_test) {
851   // Actual test
852   uint16_t conn_handle = 0x000b;
853   uint16_t local_cid = 0x0041;
854   uint16_t remote_cid = 0x3040;
855   uint8_t dlci = 0x04;
856   uint8_t dlci_byte = dlci << 2;
857 
858   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
859       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "true"));
860   auto filter_rfcomm_property =
861       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty);
862   ASSERT_TRUE(filter_rfcomm_property);
863   ASSERT_TRUE(filter_rfcomm_property.value() == "true");
864 
865   auto* snoop_logger = new TestSnoopLoggerModule(
866       temp_snoop_log_.string(),
867       temp_snooz_log_.string(),
868       10,
869       SnoopLogger::kBtSnoopLogModeFiltered,
870       false,
871       false);
872 
873   TestModuleRegistry test_registry;
874   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
875 
876   // Simulate Rfcomm channel
877   snoop_logger->AddRfcommL2capChannel(conn_handle, local_cid, remote_cid);
878   snoop_logger->AcceptlistRfcommDlci(conn_handle, local_cid, dlci);
879 
880   std::vector<uint8_t> kRfcommDlci = {
881       0x0b, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x41, 0x00, dlci_byte, 0xef, 0x15,
882       0x83, 0x11, 0x06, 0xf0, 0x07, 0x00, 0x9d, 0x02, 0x00,      0x07, 0x70,
883   };
884 
885   snoop_logger->Capture(
886       kRfcommDlci, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
887   snoop_logger->ClearL2capAcceptlist(conn_handle, local_cid, remote_cid);
888   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
889 
890   test_registry.StopAll();
891 
892   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
893       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "false"));
894 
895   // Verify states after test
896   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
897 
898   // Packet should not be filtered because DLCI acceptlisted
899   ASSERT_EQ(
900       std::filesystem::file_size(temp_snoop_log_filtered),
901       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) +
902           kRfcommDlci.size());
903   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
904 }
905 
TEST_F(SnoopLoggerModuleTest,rfcomm_channel_filtered_not_acceptlisted_dlci_test)906 TEST_F(SnoopLoggerModuleTest, rfcomm_channel_filtered_not_acceptlisted_dlci_test) {
907   // Actual test
908   uint16_t conn_handle = 0x000b;
909   uint16_t local_cid = 0x0041;
910   uint16_t remote_cid = 0x3040;
911   uint8_t dlci = 0x04;
912   uint8_t dlci_byte = dlci << 2;
913 
914   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
915       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "true"));
916   auto filter_rfcomm_property =
917       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty);
918   ASSERT_TRUE(filter_rfcomm_property);
919   ASSERT_TRUE(filter_rfcomm_property.value() == "true");
920 
921   auto* snoop_logger = new TestSnoopLoggerModule(
922       temp_snoop_log_.string(),
923       temp_snooz_log_.string(),
924       10,
925       SnoopLogger::kBtSnoopLogModeFiltered,
926       false,
927       false);
928 
929   TestModuleRegistry test_registry;
930   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
931 
932   // Simulate Rfcomm channel
933   snoop_logger->AddRfcommL2capChannel(conn_handle, local_cid, remote_cid);
934 
935   std::vector<uint8_t> kRfcommDlci = {
936       0x0b, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x41, 0x00, dlci_byte, 0xef, 0x15,
937       0x83, 0x11, 0x06, 0xf0, 0x07, 0x00, 0x9d, 0x02, 0x00,      0x07, 0x70,
938   };
939 
940   snoop_logger->Capture(
941       kRfcommDlci, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
942   snoop_logger->ClearL2capAcceptlist(conn_handle, local_cid, remote_cid);
943 
944   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
945 
946   test_registry.StopAll();
947 
948   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
949       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "false"));
950 
951   // Verify states after test
952   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
953 
954   // Packet should be filtered because DLCI not acceptlisted
955   ASSERT_EQ(
956       std::filesystem::file_size(temp_snoop_log_filtered),
957       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) +
958           TestSnoopLoggerModule::GetL2capHeaderSize());
959   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
960 }
961 
TEST_F(SnoopLoggerModuleTest,rfcomm_channel_filtered_not_acceptlisted_l2cap_channel_test)962 TEST_F(SnoopLoggerModuleTest, rfcomm_channel_filtered_not_acceptlisted_l2cap_channel_test) {
963   // Actual test
964   uint16_t conn_handle = 0x000b;
965   uint16_t local_cid = 0x0041;
966   uint16_t remote_cid = 0x3040;
967 
968   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
969       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "true"));
970   auto filter_rfcomm_property =
971       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty);
972   ASSERT_TRUE(filter_rfcomm_property);
973   ASSERT_TRUE(filter_rfcomm_property.value() == "true");
974 
975   auto* snoop_logger = new TestSnoopLoggerModule(
976       temp_snoop_log_.string(),
977       temp_snooz_log_.string(),
978       10,
979       SnoopLogger::kBtSnoopLogModeFiltered,
980       false,
981       false);
982 
983   TestModuleRegistry test_registry;
984   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
985 
986   std::vector<uint8_t> kRfcommL2capChannel = {
987       0x0b, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x41, 0x00, 0x00, 0xef, 0x15,
988       0x83, 0x11, 0x06, 0xf0, 0x07, 0x00, 0x9d, 0x02, 0x00, 0x07, 0x70,
989   };
990 
991   snoop_logger->Capture(
992       kRfcommL2capChannel, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
993   snoop_logger->ClearL2capAcceptlist(conn_handle, local_cid, remote_cid);
994 
995   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
996 
997   test_registry.StopAll();
998 
999   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1000       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "false"));
1001 
1002   // Verify states after test
1003   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1004 
1005   // Packet should be filtered because L2CAP channel not acceptlisted
1006   ASSERT_EQ(
1007       std::filesystem::file_size(temp_snoop_log_filtered),
1008       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) +
1009           TestSnoopLoggerModule::GetL2capHeaderSize());
1010   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
1011 }
1012 
TEST_F(SnoopLoggerModuleTest,rfcomm_channel_filtered_acceptlisted_l2cap_channel_test)1013 TEST_F(SnoopLoggerModuleTest, rfcomm_channel_filtered_acceptlisted_l2cap_channel_test) {
1014   // Actual test
1015   uint16_t conn_handle = 0x000b;
1016   uint16_t local_cid = 0x0041;
1017   uint16_t remote_cid = 0x3040;
1018 
1019   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1020       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "true"));
1021   auto filter_rfcomm_property =
1022       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty);
1023   ASSERT_TRUE(filter_rfcomm_property);
1024   ASSERT_TRUE(filter_rfcomm_property.value() == "true");
1025 
1026   auto* snoop_logger = new TestSnoopLoggerModule(
1027       temp_snoop_log_.string(),
1028       temp_snooz_log_.string(),
1029       10,
1030       SnoopLogger::kBtSnoopLogModeFiltered,
1031       false,
1032       false);
1033 
1034   TestModuleRegistry test_registry;
1035   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1036 
1037   snoop_logger->AcceptlistL2capChannel(conn_handle, local_cid, remote_cid);
1038 
1039   std::vector<uint8_t> kRfcommL2capChannel = {
1040       0x0b, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x41, 0x00, 0x00, 0xef, 0x15,
1041       0x83, 0x11, 0x06, 0xf0, 0x07, 0x00, 0x9d, 0x02, 0x00, 0x07, 0x70,
1042   };
1043 
1044   snoop_logger->Capture(
1045       kRfcommL2capChannel, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
1046   snoop_logger->ClearL2capAcceptlist(conn_handle, local_cid, remote_cid);
1047 
1048   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1049 
1050   test_registry.StopAll();
1051 
1052   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1053       SnoopLogger::kBtSnoopLogFilterProfileRfcommProperty, "false"));
1054 
1055   // Verify states after test
1056   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1057 
1058   // Packet should not be filtered because L2CAP channel acceptlisted
1059   ASSERT_EQ(
1060       std::filesystem::file_size(temp_snoop_log_filtered),
1061       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) +
1062           kRfcommL2capChannel.size());
1063   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
1064 }
1065 
TEST_F(SnoopLoggerModuleTest,profiles_filtered_hfp_hf_test)1066 TEST_F(SnoopLoggerModuleTest, profiles_filtered_hfp_hf_test) {
1067   // Actual test
1068   uint16_t conn_handle = 0x000b;
1069   uint16_t local_cid = 0x0043;
1070   uint16_t remote_cid = 0x3040;
1071   uint8_t dlci = 0x06;
1072   uint16_t psm = 0x0003;
1073   uint16_t profile_uuid_hfp_hf = 0x111f;
1074   bool flow = true;
1075 
1076   const std::string clcc_pattern = "\x0d\x0a+CLCC:";
1077   const uint16_t HEADER_SIZE = 12;
1078 
1079   // Set pbap and map filtering modes
1080   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1081       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1082       SnoopLogger::kBtSnoopLogFilterProfileModeMagic));
1083   auto filterPbapModeProperty =
1084       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty);
1085   ASSERT_TRUE(
1086       filterPbapModeProperty &&
1087       (filterPbapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeMagic) !=
1088        std::string::npos));
1089   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1090       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1091       SnoopLogger::kBtSnoopLogFilterProfileModeMagic));
1092   auto filterMapModeProperty =
1093       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty);
1094   ASSERT_TRUE(
1095       filterMapModeProperty &&
1096       (filterMapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeMagic) !=
1097        std::string::npos));
1098 
1099   auto* snoop_logger = new TestSnoopLoggerModule(
1100       temp_snoop_log_.string(),
1101       temp_snooz_log_.string(),
1102       10,
1103       SnoopLogger::kBtSnoopLogModeFiltered,
1104       false,
1105       false);
1106 
1107   TestModuleRegistry test_registry;
1108   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1109 
1110   snoop_logger->SetL2capChannelOpen(conn_handle, local_cid, remote_cid, psm, false);
1111   snoop_logger->SetRfcommPortOpen(conn_handle, local_cid, dlci, profile_uuid_hfp_hf, flow);
1112 
1113   std::vector<uint8_t> kPhoneNumber = {
1114       0x0b, 0x00, 0x30, 0x00, 0x2c, 0x00, 0x40, 0x30, 0x19, 0xff, 0x4f, 0x01, 0x0d,
1115       0x0a, 0x2b, 0x43, 0x4c, 0x43, 0x43, 0x3a, 0x20, 0x31, 0x2c, 0x31, 0x2c, 0x34,
1116       0x2c, 0x30, 0x2c, 0x30, 0x2c, 0x22, 0x2b, 0x39, 0x39, 0x31, 0x32, 0x33, 0x34,
1117       0x35, 0x36, 0x37, 0x38, 0x39, 0x22, 0x2c, 0x31, 0x34, 0x35, 0x0d, 0x0a, 0x49,
1118   };
1119 
1120   snoop_logger->Capture(
1121       kPhoneNumber, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
1122   snoop_logger->SetL2capChannelClose(conn_handle, local_cid, remote_cid);
1123   snoop_logger->SetRfcommPortClose(conn_handle, local_cid, dlci, profile_uuid_hfp_hf);
1124 
1125   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1126 
1127   test_registry.StopAll();
1128 
1129   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1130       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1131       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1132   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1133       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1134       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1135 
1136   // Verify states after test
1137   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1138 
1139   // Packet should be filtered
1140   ASSERT_EQ(
1141       std::filesystem::file_size(temp_snoop_log_filtered),
1142       sizeof(SnoopLoggerCommon::FileHeaderType) + sizeof(SnoopLogger::PacketHeaderType) +
1143           HEADER_SIZE + strlen(clcc_pattern.c_str()));
1144   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
1145 }
1146 
TEST_F(SnoopLoggerModuleTest,profiles_filtered_pbap_magic_test)1147 TEST_F(SnoopLoggerModuleTest, profiles_filtered_pbap_magic_test) {
1148   // Actual test
1149   constexpr uint16_t PROFILE_PSM_PBAP = 0x1025;
1150   constexpr uint16_t PROFILE_UUID_PBAP = 0x112f;
1151   uint16_t conn_handle = 0x0002;
1152   uint16_t local_cid = 0x0041;
1153   uint16_t remote_cid = 0x0040;
1154   uint8_t dlci = 0x27;
1155   uint16_t psm = PROFILE_PSM_PBAP;
1156   uint16_t profile_uuid_pbap = PROFILE_UUID_PBAP;
1157   bool flow = true;
1158   const std::string magic_string = "PROHIBITED";
1159   const uint16_t HEADER_SIZE = 8;
1160 
1161   // Set pbap and map filtering modes
1162   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1163       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1164       SnoopLogger::kBtSnoopLogFilterProfileModeMagic));
1165   auto filterPbapModeProperty =
1166       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty);
1167   ASSERT_TRUE(
1168       filterPbapModeProperty &&
1169       (filterPbapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeMagic) !=
1170        std::string::npos));
1171   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1172       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1173       SnoopLogger::kBtSnoopLogFilterProfileModeMagic));
1174   auto filterMapModeProperty =
1175       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty);
1176   ASSERT_TRUE(
1177       filterMapModeProperty &&
1178       (filterMapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeMagic) !=
1179        std::string::npos));
1180 
1181   auto* snoop_logger = new TestSnoopLoggerModule(
1182       temp_snoop_log_.string(),
1183       temp_snooz_log_.string(),
1184       15,
1185       SnoopLogger::kBtSnoopLogModeFiltered,
1186       false,
1187       false);
1188 
1189   TestModuleRegistry test_registry;
1190   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1191 
1192   snoop_logger->SetL2capChannelOpen(conn_handle, local_cid, remote_cid, psm, false);
1193   snoop_logger->SetRfcommPortOpen(conn_handle, local_cid, dlci, profile_uuid_pbap, flow);
1194 
1195   for (int i = 0; i < (int)kTestData.size(); i++) {
1196     snoop_logger->Capture(
1197         kTestData[i], (SnoopLogger::Direction)(i % 2), SnoopLogger::PacketType::ACL);
1198   }
1199 
1200   snoop_logger->SetL2capChannelClose(conn_handle, local_cid, remote_cid);
1201   snoop_logger->SetRfcommPortClose(conn_handle, local_cid, dlci, profile_uuid_pbap);
1202 
1203   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1204 
1205   test_registry.StopAll();
1206 
1207   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1208       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1209       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1210   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1211       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1212       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1213 
1214   // Verify states after test
1215   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1216 
1217   // Packets should be filtered
1218   ASSERT_EQ(
1219       std::filesystem::file_size(temp_snoop_log_filtered),
1220       sizeof(SnoopLoggerCommon::FileHeaderType) +
1221           (int)kTestData.size() *
1222               (sizeof(SnoopLogger::PacketHeaderType) + HEADER_SIZE + strlen(magic_string.c_str())));
1223 
1224   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
1225 }
1226 
TEST_F(SnoopLoggerModuleTest,profiles_filtered_pbap_header_test)1227 TEST_F(SnoopLoggerModuleTest, profiles_filtered_pbap_header_test) {
1228   // Actual test
1229   constexpr uint16_t PROFILE_PSM_PBAP = 0x1025;
1230   constexpr uint16_t PROFILE_UUID_PBAP = 0x112f;
1231   uint16_t conn_handle = 0x0002;
1232   uint16_t local_cid = 0x0041;
1233   uint16_t remote_cid = 0x0040;
1234   uint8_t dlci = 0x27;
1235   uint16_t psm = PROFILE_PSM_PBAP;
1236   uint16_t profile_uuid_pbap = PROFILE_UUID_PBAP;
1237   bool flow = true;
1238   const uint16_t HEADER_SIZE = 8;
1239 
1240   // Set pbap and map filtering modes
1241   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1242       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1243       SnoopLogger::kBtSnoopLogFilterProfileModeHeader));
1244   auto filterPbapModeProperty =
1245       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty);
1246   ASSERT_TRUE(
1247       filterPbapModeProperty &&
1248       (filterPbapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeHeader) !=
1249        std::string::npos));
1250   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1251       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1252       SnoopLogger::kBtSnoopLogFilterProfileModeHeader));
1253   auto filterMapModeProperty =
1254       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty);
1255   ASSERT_TRUE(
1256       filterMapModeProperty &&
1257       (filterMapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeHeader) !=
1258        std::string::npos));
1259 
1260   auto* snoop_logger = new TestSnoopLoggerModule(
1261       temp_snoop_log_.string(),
1262       temp_snooz_log_.string(),
1263       15,
1264       SnoopLogger::kBtSnoopLogModeFiltered,
1265       false,
1266       false);
1267 
1268   TestModuleRegistry test_registry;
1269   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1270 
1271   snoop_logger->SetL2capChannelOpen(conn_handle, local_cid, remote_cid, psm, false);
1272   snoop_logger->SetRfcommPortOpen(conn_handle, local_cid, dlci, profile_uuid_pbap, flow);
1273 
1274   for (int i = 0; i < (int)kTestData.size(); i++) {
1275     snoop_logger->Capture(
1276         kTestData[i], (SnoopLogger::Direction)(i % 2), SnoopLogger::PacketType::ACL);
1277   }
1278 
1279   snoop_logger->SetL2capChannelClose(conn_handle, local_cid, remote_cid);
1280   snoop_logger->SetRfcommPortClose(conn_handle, local_cid, dlci, profile_uuid_pbap);
1281 
1282   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1283 
1284   test_registry.StopAll();
1285 
1286   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1287       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1288       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1289   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1290       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1291       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1292 
1293   // Verify states after test
1294   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1295 
1296   // Packets should be filtered
1297   ASSERT_EQ(
1298       std::filesystem::file_size(temp_snoop_log_filtered),
1299       sizeof(SnoopLoggerCommon::FileHeaderType) +
1300           (int)kTestData.size() * (sizeof(SnoopLogger::PacketHeaderType) + HEADER_SIZE));
1301 
1302   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
1303 }
1304 
TEST_F(SnoopLoggerModuleTest,profiles_filtered_pbap_fullfilter_test)1305 TEST_F(SnoopLoggerModuleTest, profiles_filtered_pbap_fullfilter_test) {
1306   // Actual test
1307   constexpr uint16_t PROFILE_PSM_PBAP = 0x1025;
1308   constexpr uint16_t PROFILE_UUID_PBAP = 0x112f;
1309   uint16_t conn_handle = 0x0002;
1310   uint16_t local_cid = 0x0041;
1311   uint16_t remote_cid = 0x0040;
1312   uint8_t dlci = 0x27;
1313   uint16_t psm = PROFILE_PSM_PBAP;
1314   uint16_t profile_uuid_pbap = PROFILE_UUID_PBAP;
1315   bool flow = true;
1316 
1317   // Set pbap and map filtering modes
1318   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1319       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1320       SnoopLogger::kBtSnoopLogFilterProfileModeFullfillter));
1321   auto filterPbapModeProperty =
1322       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty);
1323   ASSERT_TRUE(
1324       filterPbapModeProperty &&
1325       (filterPbapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeFullfillter) !=
1326        std::string::npos));
1327   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1328       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1329       SnoopLogger::kBtSnoopLogFilterProfileModeFullfillter));
1330   auto filterMapModeProperty =
1331       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty);
1332   ASSERT_TRUE(
1333       filterMapModeProperty &&
1334       (filterMapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeFullfillter) !=
1335        std::string::npos));
1336 
1337   auto* snoop_logger = new TestSnoopLoggerModule(
1338       temp_snoop_log_.string(),
1339       temp_snooz_log_.string(),
1340       15,
1341       SnoopLogger::kBtSnoopLogModeFiltered,
1342       false,
1343       false);
1344 
1345   TestModuleRegistry test_registry;
1346   test_registry.InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1347 
1348   snoop_logger->SetL2capChannelOpen(conn_handle, local_cid, remote_cid, psm, false);
1349   snoop_logger->SetRfcommPortOpen(conn_handle, local_cid, dlci, profile_uuid_pbap, flow);
1350 
1351   for (int i = 0; i < (int)kTestData.size(); i++) {
1352     snoop_logger->Capture(
1353         kTestData[i], (SnoopLogger::Direction)(i % 2), SnoopLogger::PacketType::ACL);
1354   }
1355 
1356   snoop_logger->SetL2capChannelClose(conn_handle, local_cid, remote_cid);
1357   snoop_logger->SetRfcommPortClose(conn_handle, local_cid, dlci, profile_uuid_pbap);
1358 
1359   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1360 
1361   test_registry.StopAll();
1362 
1363   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1364       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1365       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1366   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1367       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1368       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1369 
1370   // Verify states after test
1371   ASSERT_TRUE(std::filesystem::exists(temp_snoop_log_filtered));
1372 
1373   // Packets should be filtered
1374   ASSERT_EQ(
1375       std::filesystem::file_size(temp_snoop_log_filtered),
1376       sizeof(SnoopLoggerCommon::FileHeaderType));
1377 
1378   ASSERT_TRUE(std::filesystem::remove(temp_snoop_log_filtered));
1379 }
1380 
1381 static constexpr int INVALID_FD = -1;
1382 
TEST_F(SnoopLoggerModuleTest,socket_disabled_connect_fail_test)1383 TEST_F(SnoopLoggerModuleTest, socket_disabled_connect_fail_test) {
1384   auto* snoop_logger = new TestSnoopLoggerModule(
1385       temp_snoop_log_.string(),
1386       temp_snooz_log_.string(),
1387       10,
1388       SnoopLogger::kBtSnoopLogModeDisabled,
1389       true,
1390       false);
1391   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1392 
1393   // // Create a TCP socket file descriptor
1394   int socket_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
1395   ASSERT_TRUE(socket_fd != INVALID_FD);
1396 
1397   struct sockaddr_in addr;
1398   addr.sin_family = AF_INET;
1399   addr.sin_addr.s_addr = htonl(SnoopLoggerSocket::DEFAULT_LOCALHOST_);
1400   addr.sin_port = htons(SnoopLoggerSocket::DEFAULT_LISTEN_PORT_);
1401 
1402   int ret;
1403 
1404   // Connect to snoop logger socket
1405   RUN_NO_INTR(ret = connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr)));
1406   ASSERT_TRUE(ret != 0);
1407 
1408   test_registry->StopAll();
1409   close(socket_fd);
1410 }
1411 
TEST_F(SnoopLoggerModuleTest,default_socket_enabled_capture_recv_test)1412 TEST_F(SnoopLoggerModuleTest, default_socket_enabled_capture_recv_test) {
1413   int ret;
1414   auto* snoop_logger = new TestSnoopLoggerModule(
1415       temp_snoop_log_.string(),
1416       temp_snooz_log_.string(),
1417       10,
1418       SnoopLogger::kBtSnoopLogModeFull,
1419       true,
1420       false);
1421   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1422 
1423   // // Create a TCP socket file descriptor
1424   int socket_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
1425   ASSERT_TRUE(socket_fd != INVALID_FD);
1426 
1427   struct sockaddr_in addr;
1428   addr.sin_family = AF_INET;
1429   addr.sin_addr.s_addr = htonl(SnoopLoggerSocket::DEFAULT_LOCALHOST_);
1430   addr.sin_port = htons(SnoopLoggerSocket::DEFAULT_LISTEN_PORT_);
1431 
1432   // Connect to snoop logger socket
1433   RUN_NO_INTR(ret = connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr)));
1434   ASSERT_TRUE(ret == 0);
1435 
1436   char recv_buf1[sizeof(SnoopLoggerCommon::FileHeaderType)];
1437   char recv_buf2[sizeof(SnoopLogger::PacketHeaderType)];
1438   char recv_buf3[99];
1439   int bytes_read = -1;
1440 
1441   auto a = std::async(std::launch::async, [&] {
1442     recv(socket_fd, recv_buf1, sizeof(recv_buf1), 0);
1443     recv(socket_fd, recv_buf2, sizeof(recv_buf2), 0);
1444     return recv(socket_fd, recv_buf3, sizeof(recv_buf3), 0);
1445   });
1446 
1447   snoop_logger->GetSocketThread()->GetSocket()->WaitForClientSocketConnected();
1448 
1449   snoop_logger->Capture(kHfpAtNrec0, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
1450 
1451   a.wait();
1452   bytes_read = a.get();
1453 
1454   ASSERT_TRUE(std::memcmp(recv_buf1, &SnoopLoggerCommon::kBtSnoopFileHeader, sizeof(recv_buf1)) == 0);
1455   ASSERT_EQ(bytes_read, static_cast<int>(kHfpAtNrec0.size()));
1456   ASSERT_TRUE(std::memcmp(recv_buf3, kHfpAtNrec0.data(), kHfpAtNrec0.size()) == 0);
1457 
1458   test_registry->StopAll();
1459   close(socket_fd);
1460 }
1461 
TEST_F(SnoopLoggerModuleTest,custom_socket_register_enabled_capture_recv_test)1462 TEST_F(SnoopLoggerModuleTest, custom_socket_register_enabled_capture_recv_test) {
1463   auto* snoop_logger = new TestSnoopLoggerModule(
1464       temp_snoop_log_.string(),
1465       temp_snooz_log_.string(),
1466       10,
1467       SnoopLogger::kBtSnoopLogModeFull,
1468       true,
1469       false);
1470   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1471 
1472   int new_port = 8873;
1473   SyscallWrapperImpl syscall_if;
1474   auto sls = std::make_unique<SnoopLoggerSocket>(&syscall_if, SnoopLoggerSocket::DEFAULT_LOCALHOST_, new_port);
1475   SnoopLoggerSocketThread slsThread(std::move(sls));
1476   auto thread_start_future = slsThread.Start();
1477   thread_start_future.wait();
1478   ASSERT_TRUE(thread_start_future.get());
1479 
1480   snoop_logger->RegisterSocket(&slsThread);
1481 
1482   // // Create a TCP socket file descriptor
1483   int socket_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
1484   ASSERT_TRUE(socket_fd != INVALID_FD);
1485 
1486   struct sockaddr_in addr;
1487   addr.sin_family = AF_INET;
1488   addr.sin_addr.s_addr = htonl(SnoopLoggerSocket::DEFAULT_LOCALHOST_);
1489   addr.sin_port = htons(new_port);
1490 
1491   int ret = 0;
1492   // Connect to snoop logger socket
1493   RUN_NO_INTR(ret = connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr)));
1494   ASSERT_TRUE(ret == 0);
1495 
1496   char recv_buf1[sizeof(SnoopLoggerCommon::FileHeaderType)];
1497   char recv_buf2[sizeof(SnoopLogger::PacketHeaderType)];
1498   char recv_buf3[99];
1499   int bytes_read = -1;
1500 
1501   auto a = std::async(std::launch::async, [socket_fd, &recv_buf1, &recv_buf2, &recv_buf3] {
1502     recv(socket_fd, recv_buf1, sizeof(recv_buf1), 0);
1503     recv(socket_fd, recv_buf2, sizeof(recv_buf2), 0);
1504     return recv(socket_fd, recv_buf3, sizeof(recv_buf3), 0);
1505   });
1506 
1507   slsThread.GetSocket()->WaitForClientSocketConnected();
1508 
1509   snoop_logger->Capture(kHfpAtNrec0, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
1510 
1511   a.wait();
1512   bytes_read = a.get();
1513 
1514   ASSERT_TRUE(std::memcmp(recv_buf1, &SnoopLoggerCommon::kBtSnoopFileHeader, sizeof(recv_buf1)) == 0);
1515   ASSERT_EQ(bytes_read, static_cast<int>(kHfpAtNrec0.size()));
1516   ASSERT_TRUE(std::memcmp(recv_buf3, kHfpAtNrec0.data(), kHfpAtNrec0.size()) == 0);
1517 
1518   test_registry->StopAll();
1519   close(socket_fd);
1520 }
1521 
TEST_F(SnoopLoggerModuleTest,custom_socket_interface_register_logging_disabled_test)1522 TEST_F(SnoopLoggerModuleTest, custom_socket_interface_register_logging_disabled_test) {
1523   auto* snoop_logger = new TestSnoopLoggerModule(
1524       temp_snoop_log_.string(),
1525       temp_snooz_log_.string(),
1526       10,
1527       SnoopLogger::kBtSnoopLogModeDisabled,
1528       true,
1529       false);
1530   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1531 
1532   class SnoopLoggerSocketMock : public SnoopLoggerSocketInterface {
1533    public:
1534     bool write_called;
1535     SnoopLoggerSocketMock() {
1536       write_called = false;
1537     }
1538     virtual void Write(const void* /* data */, size_t /* length */) {
1539       write_called = true;
1540     }
1541   };
1542 
1543   SnoopLoggerSocketMock mock;
1544 
1545   snoop_logger->RegisterSocket(&mock);
1546   snoop_logger->Capture(kQualcommConnectionRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
1547 
1548   ASSERT_FALSE(mock.write_called);
1549 
1550   test_registry->StopAll();
1551 }
1552 
TEST_F(SnoopLoggerModuleTest,custom_socket_interface_register_logging_enabled_test)1553 TEST_F(SnoopLoggerModuleTest, custom_socket_interface_register_logging_enabled_test) {
1554   auto* snoop_logger = new TestSnoopLoggerModule(
1555       temp_snoop_log_.string(),
1556       temp_snooz_log_.string(),
1557       10,
1558       SnoopLogger::kBtSnoopLogModeFull,
1559       true,
1560       false);
1561   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1562 
1563   class SnoopLoggerSocketMock : public SnoopLoggerSocketInterface {
1564    public:
1565     bool write_called;
1566     SnoopLoggerSocketMock() {
1567       write_called = false;
1568     }
1569     virtual void Write(const void* /* data */, size_t /* length */) {
1570       write_called = true;
1571     }
1572   };
1573 
1574   SnoopLoggerSocketMock mock;
1575 
1576   snoop_logger->RegisterSocket(&mock);
1577   snoop_logger->Capture(kQualcommConnectionRequest, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
1578 
1579   ASSERT_TRUE(mock.write_called);
1580 
1581   test_registry->StopAll();
1582 }
1583 
TEST_F(SnoopLoggerModuleTest,custom_socket_profiles_filtered_hfp_hf_test)1584 TEST_F(SnoopLoggerModuleTest, custom_socket_profiles_filtered_hfp_hf_test) {
1585   uint16_t conn_handle = 0x000b;
1586   uint16_t local_cid = 0x0043;
1587   uint16_t remote_cid = 0x3040;
1588   uint8_t dlci = 0x06;
1589   uint16_t psm = 0x0003;
1590   uint16_t profile_uuid_hfp_hf = 0x111f;
1591   bool flow = true;
1592   const std::string clcc_pattern = "\x0d\x0a+CLCC:";
1593   const uint16_t HEADER_SIZE = 12;
1594   size_t expected_data_size = HEADER_SIZE + strlen(clcc_pattern.c_str());
1595   std::vector<uint8_t> kPhoneNumber = {
1596       0x0b, 0x00, 0x30, 0x00, 0x2c, 0x00, 0x40, 0x30, 0x19, 0xff, 0x4f, 0x01, 0x0d,
1597       0x0a, 0x2b, 0x43, 0x4c, 0x43, 0x43, 0x3a, 0x20, 0x31, 0x2c, 0x31, 0x2c, 0x34,
1598       0x2c, 0x30, 0x2c, 0x30, 0x2c, 0x22, 0x2b, 0x39, 0x39, 0x31, 0x32, 0x33, 0x34,
1599       0x35, 0x36, 0x37, 0x38, 0x39, 0x22, 0x2c, 0x31, 0x34, 0x35, 0x0d, 0x0a, 0x49,
1600   };
1601 
1602   std::vector<uint8_t> kExpectedPhoneNumber = {
1603       0x0b, 0x00, 0x30, 0x00, 0x0c, 0x00, 0x40, 0x30, 0x19, 0xff, 0x4f, 0x01, 0x0d,
1604       0x0a, 0x2b, 0x43, 0x4c, 0x43, 0x43, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1605       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1606       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1607   };
1608 
1609   // Set pbap and map filtering modes
1610   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1611       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1612       SnoopLogger::kBtSnoopLogFilterProfileModeMagic));
1613   auto filterPbapModeProperty =
1614       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty);
1615   ASSERT_TRUE(
1616       filterPbapModeProperty &&
1617       (filterPbapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeMagic) !=
1618        std::string::npos));
1619   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1620       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1621       SnoopLogger::kBtSnoopLogFilterProfileModeMagic));
1622   auto filterMapModeProperty =
1623       bluetooth::os::GetSystemProperty(SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty);
1624   ASSERT_TRUE(
1625       filterMapModeProperty &&
1626       (filterMapModeProperty->find(SnoopLogger::kBtSnoopLogFilterProfileModeMagic) !=
1627        std::string::npos));
1628 
1629   auto* snoop_logger = new TestSnoopLoggerModule(
1630       temp_snoop_log_.string(),
1631       temp_snooz_log_.string(),
1632       10,
1633       SnoopLogger::kBtSnoopLogModeFiltered,
1634       false,
1635       false);
1636 
1637   test_registry->InjectTestModule(&SnoopLogger::Factory, snoop_logger);
1638 
1639   int new_port = 8873;
1640   SyscallWrapperImpl syscall_if;
1641   auto sls = std::make_unique<SnoopLoggerSocket>(
1642       &syscall_if, SnoopLoggerSocket::DEFAULT_LOCALHOST_, new_port);
1643   SnoopLoggerSocketThread slsThread(std::move(sls));
1644   auto thread_start_future = slsThread.Start();
1645   thread_start_future.wait();
1646   ASSERT_TRUE(thread_start_future.get());
1647 
1648   snoop_logger->RegisterSocket(&slsThread);
1649 
1650   snoop_logger->SetL2capChannelOpen(conn_handle, local_cid, remote_cid, psm, false);
1651   snoop_logger->SetRfcommPortOpen(conn_handle, local_cid, dlci, profile_uuid_hfp_hf, flow);
1652 
1653   // // Create a TCP socket file descriptor
1654   int socket_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
1655   ASSERT_TRUE(socket_fd != INVALID_FD);
1656 
1657   struct sockaddr_in addr;
1658   addr.sin_family = AF_INET;
1659   addr.sin_addr.s_addr = htonl(SnoopLoggerSocket::DEFAULT_LOCALHOST_);
1660   addr.sin_port = htons(new_port);
1661 
1662   int ret = 0;
1663   // Connect to snoop logger socket
1664   RUN_NO_INTR(ret = connect(socket_fd, (struct sockaddr*)&addr, sizeof(addr)));
1665   ASSERT_TRUE(ret == 0);
1666 
1667   char recv_buf1[sizeof(SnoopLoggerCommon::FileHeaderType)];
1668   char recv_buf2[sizeof(SnoopLogger::PacketHeaderType)];
1669   char recv_buf3[kPhoneNumber.size()];
1670   int bytes_read = -1;
1671 
1672   auto a = std::async(std::launch::async, [socket_fd, &recv_buf1, &recv_buf2, &recv_buf3] {
1673     recv(socket_fd, recv_buf1, sizeof(recv_buf1), 0);
1674     recv(socket_fd, recv_buf2, sizeof(recv_buf2), 0);
1675     return recv(socket_fd, recv_buf3, sizeof(recv_buf3), 0);
1676   });
1677 
1678   slsThread.GetSocket()->WaitForClientSocketConnected();
1679 
1680   snoop_logger->Capture(
1681       kPhoneNumber, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
1682   snoop_logger->SetL2capChannelClose(conn_handle, local_cid, remote_cid);
1683   snoop_logger->SetRfcommPortClose(conn_handle, local_cid, dlci, profile_uuid_hfp_hf);
1684 
1685   a.wait();
1686   bytes_read = a.get();
1687 
1688   ASSERT_TRUE(
1689       std::memcmp(recv_buf1, &SnoopLoggerCommon::kBtSnoopFileHeader, sizeof(recv_buf1)) == 0);
1690   ASSERT_EQ(bytes_read, static_cast<int>(expected_data_size));
1691   ASSERT_TRUE(std::memcmp(recv_buf3, kExpectedPhoneNumber.data(), expected_data_size) == 0);
1692 
1693   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1694       SnoopLogger::kBtSnoopLogFilterProfileMapModeProperty,
1695       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1696   ASSERT_TRUE(bluetooth::os::SetSystemProperty(
1697       SnoopLogger::kBtSnoopLogFilterProfilePbapModeProperty,
1698       SnoopLogger::kBtSnoopLogFilterProfileModeDisabled));
1699 
1700   test_registry->StopAll();
1701   close(socket_fd);
1702 }
1703 }  // namespace testing
1704