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