xref: /aosp_15_r20/system/nfc/tests/src/debug_nfcsnoop_test.cc (revision 7eba2f3b06c51ae21384f6a4f14577b668a869b3)
1 //
2 // Copyright (C) 2024 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 <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include "debug_nfcsnoop.h"
23 
24 #define BUFFER_SIZE 3
25 extern uint8_t* buffers[BUFFER_SIZE];
26 // Mock for RingBuffer
27 class MockRingBuffer {
28 public:
29     MOCK_METHOD(bool, insert, (const uint8_t* data, size_t length), ());
30     MOCK_METHOD(bool, pop, (uint8_t* buffer, size_t length), ());
31 };
32 
33 struct ringbuffer_t {
34     size_t size;
35 };
36 
CleanUpTestFile(const std::string & path)37 void CleanUpTestFile(const std::string& path) {
38     unlink(path.c_str());
39 }
40 
TEST(NfcSnoopTest,DumpWithDataTest)41 TEST(NfcSnoopTest, DumpWithDataTest) {
42     NFC_HDR mock_hdr;
43     mock_hdr.len = 4;
44     mock_hdr.offset = 0;
45     uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
46     int fd = open("/tmp/nfc_snoop_test_dump", O_RDWR | O_CREAT | O_TRUNC, 0644);
47     ASSERT_GE(fd, 0);
48     debug_nfcsnoop_dump(fd);
49 
50     struct stat st;
51     int ret = stat("/tmp/nfc_snoop_test_dump", &st);
52     ASSERT_EQ(ret, 0);
53     ASSERT_GT(st.st_size, 0);
54 
55     close(fd);
56     CleanUpTestFile("/tmp/nfc_snoop_test_dump");
57 }
58 
TEST(NfcSnoopTest,DumpEmptyBuffersTest)59 TEST(NfcSnoopTest, DumpEmptyBuffersTest) {
60     int fd = open("/tmp/nfc_snoop_test_dump_empty", O_RDWR | O_CREAT | O_TRUNC, 0644);
61     ASSERT_GE(fd, 0);
62     debug_nfcsnoop_dump(fd);
63     struct stat st;
64     int ret = stat("/tmp/nfc_snoop_test_dump_empty", &st);
65     ASSERT_EQ(ret, 0);
66     ASSERT_EQ(st.st_size, 56);
67     close(fd);
68     CleanUpTestFile("/tmp/nfc_snoop_test_dump_empty");
69 }
70 
71 // Test for simulating a ringbuffer allocation failure
TEST(NfcSnoopTest,DumpRingbufferInitFailureTest)72 TEST(NfcSnoopTest, DumpRingbufferInitFailureTest) {
73     uint8_t* buffers[BUFFER_SIZE];
74     ringbuffer_t* ringbuffers[BUFFER_SIZE];
75     buffers[0] = new uint8_t[256];
76     buffers[1] = new uint8_t[256];
77     ringbuffers[0] = new ringbuffer_t;
78     ringbuffers[1] = nullptr;
79     const std::string test_file = "/tmp/nfc_snoop_test_ringbuffer_init_failure";
80     int fd = open(test_file.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0644);
81     ASSERT_GE(fd, 0) << "Failed to open the test file";
82     debug_nfcsnoop_dump(fd);
83     close(fd);
84     struct stat st;
85     int ret = stat(test_file.c_str(), &st);
86     ASSERT_EQ(ret, 0) << "File should exist";
87     char buffer[1024];
88     fd = open(test_file.c_str(), O_RDONLY);
89     ssize_t bytesRead = read(fd, buffer, sizeof(buffer));
90     ASSERT_GT(bytesRead, 0) << "Expected content in the file, but it is empty";
91     buffer[bytesRead] = '\0';
92     std::cout << "File content:\n" << buffer << std::endl;
93     std::string content(buffer);
94     bool foundReadyMessage = content.find(
95             "Nfcsnoop is not ready (LOG_SUMMARY)") != std::string::npos;
96     bool foundAllocationMessage = content.find(
97             "Unable to allocate memory for compression") != std::string::npos;
98     ASSERT_TRUE(foundReadyMessage || foundAllocationMessage)
99     << "Expected one of the error messages, but neither was found.";
100     if (foundReadyMessage) {
101         std::cout << "Found 'Nfcsnoop is not ready' message. Likely caused by nullptr buffer."
102         << std::endl;
103     }
104     if (foundAllocationMessage) {
105         std::cout << "Found 'Unable to allocate memory for compression' message. "
106         << "Ringbuffer allocation failed." << std::endl;
107     }
108     close(fd);
109     CleanUpTestFile(test_file);
110     delete[] buffers[0];
111     delete[] buffers[1];
112 }
113 
TEST(NfcSnoopTest,StoreLogsSuccessTest)114 TEST(NfcSnoopTest, StoreLogsSuccessTest) {
115     const std::string log_data = "Test NFC log data";
116     bool result = storeNfcSnoopLogs("/tmp/nfc_snoop_log", 1024);
117     ASSERT_TRUE(result);
118     struct stat st;
119     int ret = stat("/tmp/nfc_snoop_log", &st);
120     ASSERT_EQ(ret, 0);
121     ASSERT_GT(st.st_size, 0);
122     CleanUpTestFile("/tmp/nfc_snoop_log");
123 }
124 
TEST(NfcSnoopTest,StoreLogsValidPathTest)125 TEST(NfcSnoopTest, StoreLogsValidPathTest) {
126     const std::string log_data = "Valid NFC log data";
127     bool result = storeNfcSnoopLogs("/tmp/nfc_snoop_valid_log", 1024);
128     ASSERT_TRUE(result);
129     struct stat st;
130     int ret = stat("/tmp/nfc_snoop_valid_log", &st);
131     ASSERT_EQ(ret, 0);
132     ASSERT_GT(st.st_size, 0);
133     CleanUpTestFile("/tmp/nfc_snoop_valid_log");
134 }
135 
TEST(NfcSnoopTest,StoreLogsEmptyDataTest)136 TEST(NfcSnoopTest, StoreLogsEmptyDataTest) {
137     const std::string log_data = "";
138     bool result = storeNfcSnoopLogs("/tmp/nfc_snoop_empty_log", 1024);
139     ASSERT_TRUE(result);
140     struct stat st;
141     int ret = stat("/tmp/nfc_snoop_empty_log", &st);
142     ASSERT_EQ(ret, 0);
143     ASSERT_GT(st.st_size, 0);
144     ASSERT_LT(st.st_size, 1024);
145     CleanUpTestFile("/tmp/nfc_snoop_empty_log");
146 }
147 
148 
TEST(NfcSnoopTest,StoreLogsFileCreationFailTest)149 TEST(NfcSnoopTest, StoreLogsFileCreationFailTest) {
150     const std::string log_data = "Some NFC log data";
151     bool result = storeNfcSnoopLogs("/root/nfc_snoop_fail_log", 1024);
152     ASSERT_FALSE(result);
153     struct stat st;
154     int ret = stat("/root/nfc_snoop_fail_log", &st);
155     ASSERT_EQ(ret, -1);
156 }
157