xref: /aosp_15_r20/system/logging/liblog/tests/logprint_test.cpp (revision 598139dc91b21518d67c408eaea2644226490971)
1 /*
2  * Copyright (C) 2019 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 <log/logprint.h>
18 
19 #include <string>
20 
21 #include <gtest/gtest.h>
22 
23 #include <log/log_read.h>
24 
25 using namespace std::string_literals;
26 
27 size_t convertPrintable(char*, const char*, size_t);
28 
TEST(liblog,convertPrintable_ascii)29 TEST(liblog, convertPrintable_ascii) {
30   auto input = "easy string, output same";
31   auto output_size = convertPrintable(nullptr, input, strlen(input));
32   EXPECT_EQ(output_size, strlen(input));
33 
34   char output[output_size + 1];
35   memset(output, 'x', sizeof(output));
36 
37   output_size = convertPrintable(output, input, strlen(input));
38   EXPECT_EQ(output_size, strlen(input));
39   EXPECT_STREQ(input, output);
40 }
41 
TEST(liblog,convertPrintable_escapes)42 TEST(liblog, convertPrintable_escapes) {
43   std::string input = "escape\x00\x7f\a\b\t\n\v\f\r\\"s;
44   // We want to test escaping of ASCII NUL at the end too.
45   auto input_size = input.size() + 1;
46 
47   // Note that \t is not escaped.
48   std::string expected_output = "escape\\x00\\x7F\\a\\b\t\\n\\v\\f\\r\\\\\\x00"s;
49   auto expected_output_size = expected_output.size();
50 
51   auto output_size = convertPrintable(nullptr, input.c_str(), input_size);
52   EXPECT_EQ(output_size, expected_output_size) << input_size;
53 
54   char output[output_size + 1];
55   memset(output, 'x', sizeof(output));
56 
57   output_size = convertPrintable(output, input.c_str(), input_size);
58   EXPECT_EQ(output_size, expected_output_size);
59   EXPECT_STREQ(expected_output.c_str(), output);
60 }
61 
TEST(liblog,convertPrintable_validutf8)62 TEST(liblog, convertPrintable_validutf8) {
63   setlocale(LC_ALL, "C.UTF-8");
64 
65   const char* input = "¢ह€��";
66   size_t output_size = convertPrintable(nullptr, input, strlen(input));
67   EXPECT_EQ(output_size, strlen(input));
68 
69   char output[output_size + 1];
70   memset(output, 'x', sizeof(output));
71 
72   output_size = convertPrintable(output, input, strlen(input));
73   EXPECT_EQ(output_size, strlen(input));
74   EXPECT_STREQ(input, output);
75 }
76 
TEST(liblog,convertPrintable_invalidutf8)77 TEST(liblog, convertPrintable_invalidutf8) {
78   auto input = "\x80\xC2\x01\xE0\xA4\x06\xE0\x06\xF0\x90\x8D\x06\xF0\x90\x06\xF0\x0E";
79   auto expected_output =
80       "\\x80\\xC2\\x01\\xE0\\xA4\\x06\\xE0\\x06\\xF0\\x90\\x8D\\x06\\xF0\\x90\\x06\\xF0\\x0E";
81   auto output_size = convertPrintable(nullptr, input, strlen(input));
82   EXPECT_EQ(output_size, strlen(expected_output));
83 
84   char output[output_size + 1];
85   memset(output, 'x', sizeof(output));
86 
87   output_size = convertPrintable(output, input, strlen(input));
88   EXPECT_EQ(output_size, strlen(expected_output));
89   EXPECT_STREQ(expected_output, output);
90 }
91 
TEST(liblog,convertPrintable_mixed)92 TEST(liblog, convertPrintable_mixed) {
93   setlocale(LC_ALL, "C.UTF-8");
94 
95   const char* input =
96       "\x80\xC2¢ह€��\x01\xE0\xA4\x06¢ह€��\xE0\x06\a\b\xF0\x90¢ह€��\x8D\x06\xF0\t\t\x90\x06\xF0\x0E";
97   const char* expected_output =
98       "\\x80\\xC2¢ह€��\\x01\\xE0\\xA4\\x06¢ह€��\\xE0\\x06\\a\\b\\xF0\\x90¢ह€��\\x8D\\x06\\xF0\t\t"
99       "\\x90\\x06\\xF0\\x0E";
100   size_t output_size = convertPrintable(nullptr, input, strlen(input));
101   EXPECT_EQ(output_size, strlen(expected_output));
102 
103   char output[output_size + 1];
104   memset(output, 'x', sizeof(output));
105 
106   output_size = convertPrintable(output, input, strlen(input));
107   EXPECT_EQ(output_size, strlen(expected_output));
108   EXPECT_STREQ(expected_output, output);
109 }
110 
TEST(liblog,log_print_different_header_size)111 TEST(liblog, log_print_different_header_size) {
112   constexpr int32_t kPid = 123;
113   constexpr uint32_t kTid = 456;
114   constexpr uint32_t kSec = 1000;
115   constexpr uint32_t kNsec = 999;
116   constexpr uint32_t kLid = LOG_ID_MAIN;
117   constexpr uint32_t kUid = 987;
118   constexpr char kPriority = ANDROID_LOG_ERROR;
119 
120   auto create_buf = [](char* buf, size_t len, uint16_t hdr_size) {
121     memset(buf, 0, len);
122     logger_entry* header = reinterpret_cast<logger_entry*>(buf);
123     header->hdr_size = hdr_size;
124     header->pid = kPid;
125     header->tid = kTid;
126     header->sec = kSec;
127     header->nsec = kNsec;
128     header->lid = kLid;
129     header->uid = kUid;
130     char* message = buf + header->hdr_size;
131     uint16_t message_len = 0;
132     message[message_len++] = kPriority;
133     message[message_len++] = 'T';
134     message[message_len++] = 'a';
135     message[message_len++] = 'g';
136     message[message_len++] = '\0';
137     message[message_len++] = 'm';
138     message[message_len++] = 's';
139     message[message_len++] = 'g';
140     message[message_len++] = '!';
141     message[message_len++] = '\0';
142     header->len = message_len;
143   };
144 
145   auto check_entry = [&](const AndroidLogEntry& entry) {
146     EXPECT_EQ(kSec, static_cast<uint32_t>(entry.tv_sec));
147     EXPECT_EQ(kNsec, static_cast<uint32_t>(entry.tv_nsec));
148     EXPECT_EQ(kPriority, entry.priority);
149     EXPECT_EQ(kUid, static_cast<uint32_t>(entry.uid));
150     EXPECT_EQ(kPid, entry.pid);
151     EXPECT_EQ(kTid, static_cast<uint32_t>(entry.tid));
152     EXPECT_STREQ("Tag", entry.tag);
153     EXPECT_EQ(4U, entry.tagLen);  // Apparently taglen includes the nullptr?
154     EXPECT_EQ(4U, entry.messageLen);
155     EXPECT_STREQ("msg!", entry.message);
156   };
157   alignas(logger_entry) char buf[LOGGER_ENTRY_MAX_LEN];
158   create_buf(buf, sizeof(buf), sizeof(logger_entry));
159 
160   AndroidLogEntry entry_normal_size;
161   ASSERT_EQ(0,
162             android_log_processLogBuffer(reinterpret_cast<logger_entry*>(buf), &entry_normal_size));
163   check_entry(entry_normal_size);
164 
165   create_buf(buf, sizeof(buf), sizeof(logger_entry) + 3);
166   AndroidLogEntry entry_odd_size;
167   ASSERT_EQ(0, android_log_processLogBuffer(reinterpret_cast<logger_entry*>(buf), &entry_odd_size));
168   check_entry(entry_odd_size);
169 }
170