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