1*7eba2f3bSAndroid Build Coastguard Worker //
2*7eba2f3bSAndroid Build Coastguard Worker // Copyright (C) 2024 The Android Open Source Project
3*7eba2f3bSAndroid Build Coastguard Worker //
4*7eba2f3bSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*7eba2f3bSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*7eba2f3bSAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*7eba2f3bSAndroid Build Coastguard Worker //
8*7eba2f3bSAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
9*7eba2f3bSAndroid Build Coastguard Worker //
10*7eba2f3bSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*7eba2f3bSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*7eba2f3bSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*7eba2f3bSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*7eba2f3bSAndroid Build Coastguard Worker // limitations under the License.
15*7eba2f3bSAndroid Build Coastguard Worker //
16*7eba2f3bSAndroid Build Coastguard Worker
17*7eba2f3bSAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*7eba2f3bSAndroid Build Coastguard Worker #include <gmock/gmock.h>
19*7eba2f3bSAndroid Build Coastguard Worker #include <fcntl.h>
20*7eba2f3bSAndroid Build Coastguard Worker #include <unistd.h>
21*7eba2f3bSAndroid Build Coastguard Worker #include <sys/stat.h>
22*7eba2f3bSAndroid Build Coastguard Worker #include "debug_nfcsnoop.h"
23*7eba2f3bSAndroid Build Coastguard Worker
24*7eba2f3bSAndroid Build Coastguard Worker #define BUFFER_SIZE 3
25*7eba2f3bSAndroid Build Coastguard Worker extern uint8_t* buffers[BUFFER_SIZE];
26*7eba2f3bSAndroid Build Coastguard Worker // Mock for RingBuffer
27*7eba2f3bSAndroid Build Coastguard Worker class MockRingBuffer {
28*7eba2f3bSAndroid Build Coastguard Worker public:
29*7eba2f3bSAndroid Build Coastguard Worker MOCK_METHOD(bool, insert, (const uint8_t* data, size_t length), ());
30*7eba2f3bSAndroid Build Coastguard Worker MOCK_METHOD(bool, pop, (uint8_t* buffer, size_t length), ());
31*7eba2f3bSAndroid Build Coastguard Worker };
32*7eba2f3bSAndroid Build Coastguard Worker
33*7eba2f3bSAndroid Build Coastguard Worker struct ringbuffer_t {
34*7eba2f3bSAndroid Build Coastguard Worker size_t size;
35*7eba2f3bSAndroid Build Coastguard Worker };
36*7eba2f3bSAndroid Build Coastguard Worker
CleanUpTestFile(const std::string & path)37*7eba2f3bSAndroid Build Coastguard Worker void CleanUpTestFile(const std::string& path) {
38*7eba2f3bSAndroid Build Coastguard Worker unlink(path.c_str());
39*7eba2f3bSAndroid Build Coastguard Worker }
40*7eba2f3bSAndroid Build Coastguard Worker
TEST(NfcSnoopTest,DumpWithDataTest)41*7eba2f3bSAndroid Build Coastguard Worker TEST(NfcSnoopTest, DumpWithDataTest) {
42*7eba2f3bSAndroid Build Coastguard Worker NFC_HDR mock_hdr;
43*7eba2f3bSAndroid Build Coastguard Worker mock_hdr.len = 4;
44*7eba2f3bSAndroid Build Coastguard Worker mock_hdr.offset = 0;
45*7eba2f3bSAndroid Build Coastguard Worker uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
46*7eba2f3bSAndroid Build Coastguard Worker int fd = open("/tmp/nfc_snoop_test_dump", O_RDWR | O_CREAT | O_TRUNC, 0644);
47*7eba2f3bSAndroid Build Coastguard Worker ASSERT_GE(fd, 0);
48*7eba2f3bSAndroid Build Coastguard Worker debug_nfcsnoop_dump(fd);
49*7eba2f3bSAndroid Build Coastguard Worker
50*7eba2f3bSAndroid Build Coastguard Worker struct stat st;
51*7eba2f3bSAndroid Build Coastguard Worker int ret = stat("/tmp/nfc_snoop_test_dump", &st);
52*7eba2f3bSAndroid Build Coastguard Worker ASSERT_EQ(ret, 0);
53*7eba2f3bSAndroid Build Coastguard Worker ASSERT_GT(st.st_size, 0);
54*7eba2f3bSAndroid Build Coastguard Worker
55*7eba2f3bSAndroid Build Coastguard Worker close(fd);
56*7eba2f3bSAndroid Build Coastguard Worker CleanUpTestFile("/tmp/nfc_snoop_test_dump");
57*7eba2f3bSAndroid Build Coastguard Worker }
58*7eba2f3bSAndroid Build Coastguard Worker
TEST(NfcSnoopTest,DumpEmptyBuffersTest)59*7eba2f3bSAndroid Build Coastguard Worker TEST(NfcSnoopTest, DumpEmptyBuffersTest) {
60*7eba2f3bSAndroid Build Coastguard Worker int fd = open("/tmp/nfc_snoop_test_dump_empty", O_RDWR | O_CREAT | O_TRUNC, 0644);
61*7eba2f3bSAndroid Build Coastguard Worker ASSERT_GE(fd, 0);
62*7eba2f3bSAndroid Build Coastguard Worker debug_nfcsnoop_dump(fd);
63*7eba2f3bSAndroid Build Coastguard Worker struct stat st;
64*7eba2f3bSAndroid Build Coastguard Worker int ret = stat("/tmp/nfc_snoop_test_dump_empty", &st);
65*7eba2f3bSAndroid Build Coastguard Worker ASSERT_EQ(ret, 0);
66*7eba2f3bSAndroid Build Coastguard Worker ASSERT_EQ(st.st_size, 56);
67*7eba2f3bSAndroid Build Coastguard Worker close(fd);
68*7eba2f3bSAndroid Build Coastguard Worker CleanUpTestFile("/tmp/nfc_snoop_test_dump_empty");
69*7eba2f3bSAndroid Build Coastguard Worker }
70*7eba2f3bSAndroid Build Coastguard Worker
71*7eba2f3bSAndroid Build Coastguard Worker // Test for simulating a ringbuffer allocation failure
TEST(NfcSnoopTest,DumpRingbufferInitFailureTest)72*7eba2f3bSAndroid Build Coastguard Worker TEST(NfcSnoopTest, DumpRingbufferInitFailureTest) {
73*7eba2f3bSAndroid Build Coastguard Worker uint8_t* buffers[BUFFER_SIZE];
74*7eba2f3bSAndroid Build Coastguard Worker ringbuffer_t* ringbuffers[BUFFER_SIZE];
75*7eba2f3bSAndroid Build Coastguard Worker buffers[0] = new uint8_t[256];
76*7eba2f3bSAndroid Build Coastguard Worker buffers[1] = new uint8_t[256];
77*7eba2f3bSAndroid Build Coastguard Worker ringbuffers[0] = new ringbuffer_t;
78*7eba2f3bSAndroid Build Coastguard Worker ringbuffers[1] = nullptr;
79*7eba2f3bSAndroid Build Coastguard Worker const std::string test_file = "/tmp/nfc_snoop_test_ringbuffer_init_failure";
80*7eba2f3bSAndroid Build Coastguard Worker int fd = open(test_file.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
81*7eba2f3bSAndroid Build Coastguard Worker ASSERT_GE(fd, 0) << "Failed to open the test file";
82*7eba2f3bSAndroid Build Coastguard Worker debug_nfcsnoop_dump(fd);
83*7eba2f3bSAndroid Build Coastguard Worker close(fd);
84*7eba2f3bSAndroid Build Coastguard Worker struct stat st;
85*7eba2f3bSAndroid Build Coastguard Worker int ret = stat(test_file.c_str(), &st);
86*7eba2f3bSAndroid Build Coastguard Worker ASSERT_EQ(ret, 0) << "File should exist";
87*7eba2f3bSAndroid Build Coastguard Worker char buffer[1024];
88*7eba2f3bSAndroid Build Coastguard Worker fd = open(test_file.c_str(), O_RDONLY);
89*7eba2f3bSAndroid Build Coastguard Worker ssize_t bytesRead = read(fd, buffer, sizeof(buffer));
90*7eba2f3bSAndroid Build Coastguard Worker ASSERT_GT(bytesRead, 0) << "Expected content in the file, but it is empty";
91*7eba2f3bSAndroid Build Coastguard Worker buffer[bytesRead] = '\0';
92*7eba2f3bSAndroid Build Coastguard Worker std::cout << "File content:\n" << buffer << std::endl;
93*7eba2f3bSAndroid Build Coastguard Worker std::string content(buffer);
94*7eba2f3bSAndroid Build Coastguard Worker bool foundReadyMessage = content.find(
95*7eba2f3bSAndroid Build Coastguard Worker "Nfcsnoop is not ready (LOG_SUMMARY)") != std::string::npos;
96*7eba2f3bSAndroid Build Coastguard Worker bool foundAllocationMessage = content.find(
97*7eba2f3bSAndroid Build Coastguard Worker "Unable to allocate memory for compression") != std::string::npos;
98*7eba2f3bSAndroid Build Coastguard Worker ASSERT_TRUE(foundReadyMessage || foundAllocationMessage)
99*7eba2f3bSAndroid Build Coastguard Worker << "Expected one of the error messages, but neither was found.";
100*7eba2f3bSAndroid Build Coastguard Worker if (foundReadyMessage) {
101*7eba2f3bSAndroid Build Coastguard Worker std::cout << "Found 'Nfcsnoop is not ready' message. Likely caused by nullptr buffer."
102*7eba2f3bSAndroid Build Coastguard Worker << std::endl;
103*7eba2f3bSAndroid Build Coastguard Worker }
104*7eba2f3bSAndroid Build Coastguard Worker if (foundAllocationMessage) {
105*7eba2f3bSAndroid Build Coastguard Worker std::cout << "Found 'Unable to allocate memory for compression' message. "
106*7eba2f3bSAndroid Build Coastguard Worker << "Ringbuffer allocation failed." << std::endl;
107*7eba2f3bSAndroid Build Coastguard Worker }
108*7eba2f3bSAndroid Build Coastguard Worker close(fd);
109*7eba2f3bSAndroid Build Coastguard Worker CleanUpTestFile(test_file);
110*7eba2f3bSAndroid Build Coastguard Worker delete[] buffers[0];
111*7eba2f3bSAndroid Build Coastguard Worker delete[] buffers[1];
112*7eba2f3bSAndroid Build Coastguard Worker }
113*7eba2f3bSAndroid Build Coastguard Worker
TEST(NfcSnoopTest,StoreLogsSuccessTest)114*7eba2f3bSAndroid Build Coastguard Worker TEST(NfcSnoopTest, StoreLogsSuccessTest) {
115*7eba2f3bSAndroid Build Coastguard Worker const std::string log_data = "Test NFC log data";
116*7eba2f3bSAndroid Build Coastguard Worker bool result = storeNfcSnoopLogs("/tmp/nfc_snoop_log", 1024);
117*7eba2f3bSAndroid Build Coastguard Worker ASSERT_TRUE(result);
118*7eba2f3bSAndroid Build Coastguard Worker struct stat st;
119*7eba2f3bSAndroid Build Coastguard Worker int ret = stat("/tmp/nfc_snoop_log", &st);
120*7eba2f3bSAndroid Build Coastguard Worker ASSERT_EQ(ret, 0);
121*7eba2f3bSAndroid Build Coastguard Worker ASSERT_GT(st.st_size, 0);
122*7eba2f3bSAndroid Build Coastguard Worker CleanUpTestFile("/tmp/nfc_snoop_log");
123*7eba2f3bSAndroid Build Coastguard Worker }
124*7eba2f3bSAndroid Build Coastguard Worker
TEST(NfcSnoopTest,StoreLogsValidPathTest)125*7eba2f3bSAndroid Build Coastguard Worker TEST(NfcSnoopTest, StoreLogsValidPathTest) {
126*7eba2f3bSAndroid Build Coastguard Worker const std::string log_data = "Valid NFC log data";
127*7eba2f3bSAndroid Build Coastguard Worker bool result = storeNfcSnoopLogs("/tmp/nfc_snoop_valid_log", 1024);
128*7eba2f3bSAndroid Build Coastguard Worker ASSERT_TRUE(result);
129*7eba2f3bSAndroid Build Coastguard Worker struct stat st;
130*7eba2f3bSAndroid Build Coastguard Worker int ret = stat("/tmp/nfc_snoop_valid_log", &st);
131*7eba2f3bSAndroid Build Coastguard Worker ASSERT_EQ(ret, 0);
132*7eba2f3bSAndroid Build Coastguard Worker ASSERT_GT(st.st_size, 0);
133*7eba2f3bSAndroid Build Coastguard Worker CleanUpTestFile("/tmp/nfc_snoop_valid_log");
134*7eba2f3bSAndroid Build Coastguard Worker }
135*7eba2f3bSAndroid Build Coastguard Worker
TEST(NfcSnoopTest,StoreLogsEmptyDataTest)136*7eba2f3bSAndroid Build Coastguard Worker TEST(NfcSnoopTest, StoreLogsEmptyDataTest) {
137*7eba2f3bSAndroid Build Coastguard Worker const std::string log_data = "";
138*7eba2f3bSAndroid Build Coastguard Worker bool result = storeNfcSnoopLogs("/tmp/nfc_snoop_empty_log", 1024);
139*7eba2f3bSAndroid Build Coastguard Worker ASSERT_TRUE(result);
140*7eba2f3bSAndroid Build Coastguard Worker struct stat st;
141*7eba2f3bSAndroid Build Coastguard Worker int ret = stat("/tmp/nfc_snoop_empty_log", &st);
142*7eba2f3bSAndroid Build Coastguard Worker ASSERT_EQ(ret, 0);
143*7eba2f3bSAndroid Build Coastguard Worker ASSERT_GT(st.st_size, 0);
144*7eba2f3bSAndroid Build Coastguard Worker ASSERT_LT(st.st_size, 1024);
145*7eba2f3bSAndroid Build Coastguard Worker CleanUpTestFile("/tmp/nfc_snoop_empty_log");
146*7eba2f3bSAndroid Build Coastguard Worker }
147*7eba2f3bSAndroid Build Coastguard Worker
148*7eba2f3bSAndroid Build Coastguard Worker
TEST(NfcSnoopTest,StoreLogsFileCreationFailTest)149*7eba2f3bSAndroid Build Coastguard Worker TEST(NfcSnoopTest, StoreLogsFileCreationFailTest) {
150*7eba2f3bSAndroid Build Coastguard Worker const std::string log_data = "Some NFC log data";
151*7eba2f3bSAndroid Build Coastguard Worker bool result = storeNfcSnoopLogs("/root/nfc_snoop_fail_log", 1024);
152*7eba2f3bSAndroid Build Coastguard Worker ASSERT_FALSE(result);
153*7eba2f3bSAndroid Build Coastguard Worker struct stat st;
154*7eba2f3bSAndroid Build Coastguard Worker int ret = stat("/root/nfc_snoop_fail_log", &st);
155*7eba2f3bSAndroid Build Coastguard Worker ASSERT_EQ(ret, -1);
156*7eba2f3bSAndroid Build Coastguard Worker }
157