• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013-2017 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 <sys/types.h>
18 #include <time.h>
19 #include <unistd.h>
20 
21 #include <string>
22 
23 #include <android-base/stringprintf.h>
24 #include <android/log.h>  // minimal logging API
25 #include <gtest/gtest.h>
26 #include <log/log_properties.h>
27 // Test the APIs in this standalone include file
28 #include <log/log_read.h>
29 // Do not use anything in log/log_time.h despite side effects of the above.
30 #include <private/android_logger.h>
31 
TEST(liblog,__android_log_write__android_logger_list_read)32 TEST(liblog, __android_log_write__android_logger_list_read) {
33 #ifdef __ANDROID__
34   pid_t pid = getpid();
35 
36   struct logger_list* logger_list;
37   ASSERT_TRUE(
38       NULL !=
39       (logger_list = android_logger_list_open(
40            LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid)));
41 
42   struct timespec ts;
43   clock_gettime(CLOCK_MONOTONIC, &ts);
44   std::string buf = android::base::StringPrintf("pid=%u ts=%ld.%09ld", pid,
45                                                 ts.tv_sec, ts.tv_nsec);
46   static const char tag[] =
47       "liblog.__android_log_write__android_logger_list_read";
48   static const char prio = ANDROID_LOG_DEBUG;
49   ASSERT_LT(0, __android_log_write(prio, tag, buf.c_str()));
50   usleep(1000000);
51 
52   buf = std::string(&prio, sizeof(prio)) + tag + std::string("", 1) + buf +
53         std::string("", 1);
54 
55   int count = 0;
56 
57   for (;;) {
58     log_msg log_msg;
59     if (android_logger_list_read(logger_list, &log_msg) <= 0) break;
60 
61     EXPECT_EQ(log_msg.entry.pid, pid);
62     // There may be a future where we leak "liblog" tagged LOG_ID_EVENT
63     // binary messages through so that logger losses can be correlated?
64     EXPECT_EQ(log_msg.id(), LOG_ID_MAIN);
65 
66     if (log_msg.entry.len != buf.length()) continue;
67 
68     if (buf != std::string(log_msg.msg(), log_msg.entry.len)) continue;
69 
70     ++count;
71   }
72   android_logger_list_close(logger_list);
73 
74   EXPECT_EQ(1, count);
75 #else
76   GTEST_LOG_(INFO) << "This test does nothing.\n";
77 #endif
78 }
79 
TEST(liblog,android_logger_get_)80 TEST(liblog, android_logger_get_) {
81 #ifdef __ANDROID__
82   // This test assumes the log buffers are filled with noise from
83   // normal operations. It will fail if done immediately after a
84   // logcat -c.
85   struct logger_list* logger_list =
86       android_logger_list_alloc(ANDROID_LOG_WRONLY, 0, 0);
87 
88   for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
89     log_id_t id = static_cast<log_id_t>(i);
90     const char* name = android_log_id_to_name(id);
91     if (id != android_name_to_log_id(name)) {
92       continue;
93     }
94     fprintf(stderr, "log buffer %s\r", name);
95     struct logger* logger;
96     EXPECT_TRUE(NULL != (logger = android_logger_open(logger_list, id)));
97     EXPECT_EQ(id, android_logger_get_id(logger));
98     ssize_t get_log_size = android_logger_get_log_size(logger);
99     /* security buffer is allowed to be denied */
100     if (strcmp("security", name)) {
101       EXPECT_LT(0, get_log_size);
102       // crash buffer is allowed to be empty, that is actually healthy!
103       // kernel buffer is allowed to be empty on "user" builds
104       // stats buffer is allowed to be empty TEMPORARILY.
105       // TODO: remove stats buffer from here once we start to use it in
106       // framework (b/68266385).
107       EXPECT_LE(  // boolean 1 or 0 depending on expected content or empty
108           !!((strcmp("crash", name) != 0) &&
109              ((strcmp("kernel", name) != 0) ||
110               __android_logger_property_get_bool(
111                   "ro.logd.kernel", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG |
112                                         BOOL_DEFAULT_FLAG_SVELTE)) &&
113              (strcmp("stats", name) != 0)),
114           android_logger_get_log_readable_size(logger));
115     } else {
116       EXPECT_NE(0, get_log_size);
117       if (get_log_size < 0) {
118         EXPECT_GT(0, android_logger_get_log_readable_size(logger));
119       } else {
120         EXPECT_LE(0, android_logger_get_log_readable_size(logger));
121       }
122     }
123     EXPECT_LT(0, android_logger_get_log_version(logger));
124   }
125 
126   android_logger_list_close(logger_list);
127 #else
128   GTEST_LOG_(INFO) << "This test does nothing.\n";
129 #endif
130 }
131