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