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/properties.h>
24 #include <android-base/stringprintf.h>
25 #include <android/log.h>  // minimal logging API
26 #include <gtest/gtest.h>
27 #include <log/log_properties.h>
28 // Test the APIs in this standalone include file
29 #include <log/log_read.h>
30 // Do not use anything in log/log_time.h despite side effects of the above.
31 #include <private/android_logger.h>
32 
33 using android::base::GetBoolProperty;
34 
TEST(liblog,android_logger_get_)35 TEST(liblog, android_logger_get_) {
36 #ifdef __ANDROID__
37   // This test assumes the log buffers are filled with noise from
38   // normal operations. It will fail if done immediately after a
39   // logcat -c.
40   struct logger_list* logger_list = android_logger_list_alloc(0, 0, 0);
41 
42   for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
43     log_id_t id = static_cast<log_id_t>(i);
44     std::string name = android_log_id_to_name(id);
45     fprintf(stderr, "log buffer %s\r", name.c_str());
46     struct logger* logger;
47     EXPECT_TRUE(NULL != (logger = android_logger_open(logger_list, id)));
48     EXPECT_EQ(id, android_logger_get_id(logger));
49     ssize_t get_log_size = android_logger_get_log_size(logger);
50     /* security buffer is allowed to be denied */
51     if (name != "security") {
52       EXPECT_GT(get_log_size, 0);
53       // crash buffer is allowed to be empty, that is actually healthy!
54       // stats buffer is no longer in use.
55       if (name == "crash" || name == "stats") {
56         continue;
57       }
58 
59       // kernel buffer is empty if ro.logd.kernel is false
60       if (name == "kernel" && !GetBoolProperty("ro.logd.kernel", false)) {
61         continue;
62       }
63 
64       EXPECT_LE(0, android_logger_get_log_readable_size(logger));
65     } else {
66       EXPECT_NE(0, get_log_size);
67       if (get_log_size < 0) {
68         EXPECT_GT(0, android_logger_get_log_readable_size(logger));
69       } else {
70         EXPECT_LE(0, android_logger_get_log_readable_size(logger));
71       }
72     }
73   }
74 
75   android_logger_list_close(logger_list);
76 #else
77   GTEST_LOG_(INFO) << "This test does nothing.\n";
78 #endif
79 }
80