1 /*
2 * Copyright (C) 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 <stdio.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #include <string>
22
23 #include <android-base/file.h>
24 #include <android-base/stringprintf.h>
25 #include <gtest/gtest.h>
26 // Test the APIs in this standalone include file
27 #include <log/log_radio.h>
28
TEST(liblog,RLOG)29 TEST(liblog, RLOG) {
30 static const char content[] = "log_radio.h";
31 static const char content_false[] = "log_radio.h false";
32
33 // ratelimit content to 10/s to keep away from spam filters
34 // do not send identical content together to keep away from spam filters
35
36 #undef LOG_TAG
37 #define LOG_TAG "TEST__RLOGV"
38 RLOGV(content);
39 usleep(100000);
40 #undef LOG_TAG
41 #define LOG_TAG "TEST__RLOGD"
42 RLOGD(content);
43 usleep(100000);
44 #undef LOG_TAG
45 #define LOG_TAG "TEST__RLOGI"
46 RLOGI(content);
47 usleep(100000);
48 #undef LOG_TAG
49 #define LOG_TAG "TEST__RLOGW"
50 RLOGW(content);
51 usleep(100000);
52 #undef LOG_TAG
53 #define LOG_TAG "TEST__RLOGE"
54 RLOGE(content);
55 usleep(100000);
56 #undef LOG_TAG
57 #define LOG_TAG "TEST__RLOGV"
58 RLOGV_IF(true, content);
59 usleep(100000);
60 RLOGV_IF(false, content_false);
61 usleep(100000);
62 #undef LOG_TAG
63 #define LOG_TAG "TEST__RLOGD"
64 RLOGD_IF(true, content);
65 usleep(100000);
66 RLOGD_IF(false, content_false);
67 usleep(100000);
68 #undef LOG_TAG
69 #define LOG_TAG "TEST__RLOGI"
70 RLOGI_IF(true, content);
71 usleep(100000);
72 RLOGI_IF(false, content_false);
73 usleep(100000);
74 #undef LOG_TAG
75 #define LOG_TAG "TEST__RLOGW"
76 RLOGW_IF(true, content);
77 usleep(100000);
78 RLOGW_IF(false, content_false);
79 usleep(100000);
80 #undef LOG_TAG
81 #define LOG_TAG "TEST__RLOGE"
82 RLOGE_IF(true, content);
83 usleep(100000);
84 RLOGE_IF(false, content_false);
85
86 #ifdef __ANDROID__
87 // give time for content to long-path through logger
88 sleep(1);
89
90 std::string buf = android::base::StringPrintf(
91 "logcat -b radio --pid=%u -d -s"
92 " TEST__RLOGV TEST__RLOGD TEST__RLOGI TEST__RLOGW TEST__RLOGE",
93 (unsigned)getpid());
94 FILE* fp = popen(buf.c_str(), "re");
95 int count = 0;
96 int count_false = 0;
97 if (fp) {
98 if (!android::base::ReadFdToString(fileno(fp), &buf)) buf = "";
99 pclose(fp);
100 for (size_t pos = 0; (pos = buf.find(content, pos)) != std::string::npos;
101 ++pos) {
102 ++count;
103 }
104 for (size_t pos = 0;
105 (pos = buf.find(content_false, pos)) != std::string::npos; ++pos) {
106 ++count_false;
107 }
108 }
109 EXPECT_EQ(0, count_false);
110 #if LOG_NDEBUG
111 ASSERT_EQ(8, count);
112 #else
113 ASSERT_EQ(10, count);
114 #endif
115
116 #else
117 GTEST_LOG_(INFO) << "This test does not test end-to-end.\n";
118 #endif
119 }
120