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_logger_get_)32 TEST(liblog, android_logger_get_) {
33 #ifdef __ANDROID__
34 // This test assumes the log buffers are filled with noise from
35 // normal operations. It will fail if done immediately after a
36 // logcat -c.
37 struct logger_list* logger_list =
38 android_logger_list_alloc(ANDROID_LOG_WRONLY, 0, 0);
39
40 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
41 log_id_t id = static_cast<log_id_t>(i);
42 const char* name = android_log_id_to_name(id);
43 if (id != android_name_to_log_id(name)) {
44 continue;
45 }
46 fprintf(stderr, "log buffer %s\r", name);
47 struct logger* logger;
48 EXPECT_TRUE(NULL != (logger = android_logger_open(logger_list, id)));
49 EXPECT_EQ(id, android_logger_get_id(logger));
50 ssize_t get_log_size = android_logger_get_log_size(logger);
51 /* security buffer is allowed to be denied */
52 if (strcmp("security", name)) {
53 EXPECT_LT(0, get_log_size);
54 // crash buffer is allowed to be empty, that is actually healthy!
55 // kernel buffer is allowed to be empty on "user" builds
56 // stats buffer is allowed to be empty TEMPORARILY.
57 // TODO: remove stats buffer from here once we start to use it in
58 // framework (b/68266385).
59 EXPECT_LE( // boolean 1 or 0 depending on expected content or empty
60 !!((strcmp("crash", name) != 0) &&
61 ((strcmp("kernel", name) != 0) ||
62 __android_logger_property_get_bool(
63 "ro.logd.kernel", BOOL_DEFAULT_TRUE | BOOL_DEFAULT_FLAG_ENG |
64 BOOL_DEFAULT_FLAG_SVELTE)) &&
65 (strcmp("stats", name) != 0)),
66 android_logger_get_log_readable_size(logger));
67 } else {
68 EXPECT_NE(0, get_log_size);
69 if (get_log_size < 0) {
70 EXPECT_GT(0, android_logger_get_log_readable_size(logger));
71 } else {
72 EXPECT_LE(0, android_logger_get_log_readable_size(logger));
73 }
74 }
75 EXPECT_LT(0, android_logger_get_log_version(logger));
76 }
77
78 android_logger_list_close(logger_list);
79 #else
80 GTEST_LOG_(INFO) << "This test does nothing.\n";
81 #endif
82 }
83