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