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 31 TEST(liblog, __android_log_write__android_logger_list_read) { 32 #ifdef __ANDROID__ 33 pid_t pid = getpid(); 34 35 struct logger_list* logger_list; 36 ASSERT_TRUE( 37 NULL != 38 (logger_list = android_logger_list_open( 39 LOG_ID_MAIN, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 1000, pid))); 40 41 struct timespec ts; 42 clock_gettime(CLOCK_MONOTONIC, &ts); 43 std::string buf = android::base::StringPrintf("pid=%u ts=%ld.%09ld", pid, 44 ts.tv_sec, ts.tv_nsec); 45 static const char tag[] = 46 "liblog.__android_log_write__android_logger_list_read"; 47 static const char prio = ANDROID_LOG_DEBUG; 48 ASSERT_LT(0, __android_log_write(prio, tag, buf.c_str())); 49 usleep(1000000); 50 51 buf = std::string(&prio, sizeof(prio)) + tag + std::string("", 1) + buf + 52 std::string("", 1); 53 54 int count = 0; 55 56 for (;;) { 57 log_msg log_msg; 58 if (android_logger_list_read(logger_list, &log_msg) <= 0) break; 59 60 EXPECT_EQ(log_msg.entry.pid, pid); 61 // There may be a future where we leak "liblog" tagged LOG_ID_EVENT 62 // binary messages through so that logger losses can be correlated? 63 EXPECT_EQ(log_msg.id(), LOG_ID_MAIN); 64 65 if (log_msg.entry.len != buf.length()) continue; 66 67 if (buf != std::string(log_msg.msg(), log_msg.entry.len)) continue; 68 69 ++count; 70 } 71 android_logger_list_close(logger_list); 72 73 EXPECT_EQ(1, count); 74 #else 75 GTEST_LOG_(INFO) << "This test does nothing.\n"; 76 #endif 77 } 78 79 TEST(liblog, android_logger_get_) { 80 #ifdef __ANDROID__ 81 // This test assumes the log buffers are filled with noise from 82 // normal operations. It will fail if done immediately after a 83 // logcat -c. 84 struct logger_list* logger_list = 85 android_logger_list_alloc(ANDROID_LOG_WRONLY, 0, 0); 86 87 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { 88 log_id_t id = static_cast<log_id_t>(i); 89 const char* name = android_log_id_to_name(id); 90 if (id != android_name_to_log_id(name)) { 91 continue; 92 } 93 fprintf(stderr, "log buffer %s\r", name); 94 struct logger* logger; 95 EXPECT_TRUE(NULL != (logger = android_logger_open(logger_list, id))); 96 EXPECT_EQ(id, android_logger_get_id(logger)); 97 ssize_t get_log_size = android_logger_get_log_size(logger); 98 /* security buffer is allowed to be denied */ 99 if (strcmp("security", name)) { 100 EXPECT_LT(0, get_log_size); 101 // crash buffer is allowed to be empty, that is actually healthy! 102 // kernel buffer is allowed to be empty on "user" builds 103 EXPECT_LE( // boolean 1 or 0 depending on expected content or empty 104 !!((strcmp("crash", name) != 0) && 105 ((strcmp("kernel", name) != 0) || __android_log_is_debuggable())), 106 android_logger_get_log_readable_size(logger)); 107 } else { 108 EXPECT_NE(0, get_log_size); 109 if (get_log_size < 0) { 110 EXPECT_GT(0, android_logger_get_log_readable_size(logger)); 111 } else { 112 EXPECT_LE(0, android_logger_get_log_readable_size(logger)); 113 } 114 } 115 EXPECT_LT(0, android_logger_get_log_version(logger)); 116 } 117 118 android_logger_list_close(logger_list); 119 #else 120 GTEST_LOG_(INFO) << "This test does nothing.\n"; 121 #endif 122 } 123