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/chrono_utils.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 #include <log/log_read.h>
29 #include <log/log_time.h>
30
31 #ifdef __ANDROID__
read_with_wrap()32 static void read_with_wrap() {
33 // Read the last line in the log to get a starting timestamp. We're assuming
34 // the log is not empty.
35 const int mode = ANDROID_LOG_NONBLOCK;
36 struct logger_list* logger_list =
37 android_logger_list_open(LOG_ID_MAIN, mode, 1000, 0);
38
39 ASSERT_NE(logger_list, nullptr);
40
41 log_msg log_msg;
42 int ret = android_logger_list_read(logger_list, &log_msg);
43 android_logger_list_close(logger_list);
44 ASSERT_GT(ret, 0);
45
46 log_time start(log_msg.entry.sec, log_msg.entry.nsec);
47 ASSERT_NE(start, log_time());
48
49 logger_list =
50 android_logger_list_alloc_time(mode | ANDROID_LOG_WRAP, start, 0);
51 ASSERT_NE(logger_list, nullptr);
52
53 struct logger* logger = android_logger_open(logger_list, LOG_ID_MAIN);
54 EXPECT_NE(logger, nullptr);
55 if (logger) {
56 android_logger_list_read(logger_list, &log_msg);
57 }
58
59 android_logger_list_close(logger_list);
60 }
61 #endif
62
63 // b/64143705 confirm fixed
TEST(liblog,wrap_mode_blocks)64 TEST(liblog, wrap_mode_blocks) {
65 #ifdef __ANDROID__
66 // The read call is expected to take up to 2 hours in the happy case. There was a previous bug
67 // where it would take only 30 seconds due to an alarm() in logd_reader.cpp. That alarm has been
68 // removed, so we check here that the read call blocks for a reasonable amount of time (5s).
69
70 struct sigaction ignore = {.sa_handler = [](int) { _exit(0); }};
71 struct sigaction old_sigaction;
72 sigaction(SIGALRM, &ignore, &old_sigaction);
73 alarm(5);
74
75 android::base::Timer timer;
76 read_with_wrap();
77
78 FAIL() << "read_with_wrap() should not return before the alarm is triggered.";
79
80 alarm(0);
81 sigaction(SIGALRM, &old_sigaction, nullptr);
82 #else
83 GTEST_LOG_(INFO) << "This test does nothing.\n";
84 #endif
85 }
86