File: liblog_host_test.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (166 lines) | stat: -rw-r--r-- 7,410 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <log/log.h>
#include <private/android_logger.h>

#include <stdlib.h>
#include <unistd.h>

#include <regex>
#include <string>

#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <android-base/test_utils.h>
#include <gtest/gtest.h>

using android::base::InitLogging;
using android::base::StderrLogger;
using android::base::StringPrintf;

static std::string MakeLogPattern(int priority, const char* tag, const char* message) {
  static const char log_characters[] = "XXVDIWEF";
  static_assert(arraysize(log_characters) - 1 == ANDROID_LOG_SILENT,
                "Mismatch in size of log_characters and values in android_LogPriority");
  priority = priority > ANDROID_LOG_SILENT ? ANDROID_LOG_FATAL : priority;
  char log_char = log_characters[priority];

  return StringPrintf(R"(\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} \s*\d+ \s*\d+ %c %s\s*: %s)",
                      log_char, tag, message);
}

static void CheckMessage(bool expected, const std::string& output, int priority, const char* tag,
                         const char* message) {
  std::regex message_regex(MakeLogPattern(priority, tag, message));
  EXPECT_EQ(expected, std::regex_search(output, message_regex)) << message;
}

static void GenerateLogContent() {
  __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_VERBOSE, "tag", "verbose main");
  __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, "tag", "info main");
  __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_ERROR, "tag", "error main");

  __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, "tag", "verbose radio");
  __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, "tag", "info radio");
  __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, "tag", "error radio");

  __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, "tag", "verbose system");
  __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, "tag", "info system");
  __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, "tag", "error system");

  __android_log_buf_print(LOG_ID_CRASH, ANDROID_LOG_VERBOSE, "tag", "verbose crash");
  __android_log_buf_print(LOG_ID_CRASH, ANDROID_LOG_INFO, "tag", "info crash");
  __android_log_buf_print(LOG_ID_CRASH, ANDROID_LOG_ERROR, "tag", "error crash");
}

std::string GetPidString() {
  int pid = getpid();
  return StringPrintf("%5d", pid);
}

TEST(liblog, default_write) {
  CapturedStderr captured_stderr;
  InitLogging(nullptr, StderrLogger);

  GenerateLogContent();

  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose main");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info main");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error main");

  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose radio");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info radio");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error radio");

  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose system");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info system");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error system");

  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose crash");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info crash");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error crash");
}

TEST(liblog, verbose_write) {
  setenv("ANDROID_LOG_TAGS", "*:v", true);
  CapturedStderr captured_stderr;
  InitLogging(nullptr, StderrLogger);

  GenerateLogContent();

  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose main");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info main");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error main");

  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose radio");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info radio");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error radio");

  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose system");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info system");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error system");

  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose crash");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info crash");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error crash");
}

TEST(liblog, error_write) {
  setenv("ANDROID_LOG_TAGS", "*:e", true);
  CapturedStderr captured_stderr;
  InitLogging(nullptr, StderrLogger);

  GenerateLogContent();

  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose main");
  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info main");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error main");

  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose radio");
  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info radio");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error radio");

  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose system");
  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info system");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error system");

  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_VERBOSE, "tag", "verbose crash");
  CheckMessage(false, captured_stderr.str(), ANDROID_LOG_INFO, "tag", "info crash");
  CheckMessage(true, captured_stderr.str(), ANDROID_LOG_ERROR, "tag", "error crash");
}

TEST(liblog, kernel_no_write) {
  CapturedStderr captured_stderr;
  InitLogging(nullptr, StderrLogger);
  __android_log_buf_print(LOG_ID_KERNEL, ANDROID_LOG_ERROR, "tag", "kernel error");
  EXPECT_EQ("", captured_stderr.str());
}

TEST(liblog, binary_no_write) {
  CapturedStderr captured_stderr;
  InitLogging(nullptr, StderrLogger);
  __android_log_buf_print(LOG_ID_EVENTS, ANDROID_LOG_ERROR, "tag", "error events");
  __android_log_buf_print(LOG_ID_STATS, ANDROID_LOG_ERROR, "tag", "error stats");
  __android_log_buf_print(LOG_ID_SECURITY, ANDROID_LOG_ERROR, "tag", "error security");

  __android_log_bswrite(0x12, "events");
  __android_log_stats_bwrite(0x34, "stats", strlen("stats"));
  __android_log_security_bswrite(0x56, "security");

  EXPECT_EQ("", captured_stderr.str());
}