xref: /aosp_15_r20/system/logging/liblog/tests/log_radio_test.cpp (revision 598139dc91b21518d67c408eaea2644226490971)
1*598139dcSAndroid Build Coastguard Worker /*
2*598139dcSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*598139dcSAndroid Build Coastguard Worker  *
4*598139dcSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*598139dcSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*598139dcSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*598139dcSAndroid Build Coastguard Worker  *
8*598139dcSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*598139dcSAndroid Build Coastguard Worker  *
10*598139dcSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*598139dcSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*598139dcSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*598139dcSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*598139dcSAndroid Build Coastguard Worker  * limitations under the License.
15*598139dcSAndroid Build Coastguard Worker  */
16*598139dcSAndroid Build Coastguard Worker 
17*598139dcSAndroid Build Coastguard Worker #include <stdio.h>
18*598139dcSAndroid Build Coastguard Worker #include <sys/types.h>
19*598139dcSAndroid Build Coastguard Worker #include <unistd.h>
20*598139dcSAndroid Build Coastguard Worker 
21*598139dcSAndroid Build Coastguard Worker #include <string>
22*598139dcSAndroid Build Coastguard Worker 
23*598139dcSAndroid Build Coastguard Worker #include <android-base/file.h>
24*598139dcSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
25*598139dcSAndroid Build Coastguard Worker #include <gtest/gtest.h>
26*598139dcSAndroid Build Coastguard Worker // Test the APIs in this standalone include file
27*598139dcSAndroid Build Coastguard Worker #include <log/log_radio.h>
28*598139dcSAndroid Build Coastguard Worker 
TEST(liblog,RLOG)29*598139dcSAndroid Build Coastguard Worker TEST(liblog, RLOG) {
30*598139dcSAndroid Build Coastguard Worker   static const char content[] = "log_radio.h";
31*598139dcSAndroid Build Coastguard Worker   static const char content_false[] = "log_radio.h false";
32*598139dcSAndroid Build Coastguard Worker 
33*598139dcSAndroid Build Coastguard Worker // ratelimit content to 10/s to keep away from spam filters
34*598139dcSAndroid Build Coastguard Worker // do not send identical content together to keep away from spam filters
35*598139dcSAndroid Build Coastguard Worker 
36*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
37*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGV"
38*598139dcSAndroid Build Coastguard Worker   RLOGV(content);
39*598139dcSAndroid Build Coastguard Worker   usleep(100000);
40*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
41*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGD"
42*598139dcSAndroid Build Coastguard Worker   RLOGD(content);
43*598139dcSAndroid Build Coastguard Worker   usleep(100000);
44*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
45*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGI"
46*598139dcSAndroid Build Coastguard Worker   RLOGI(content);
47*598139dcSAndroid Build Coastguard Worker   usleep(100000);
48*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
49*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGW"
50*598139dcSAndroid Build Coastguard Worker   RLOGW(content);
51*598139dcSAndroid Build Coastguard Worker   usleep(100000);
52*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
53*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGE"
54*598139dcSAndroid Build Coastguard Worker   RLOGE(content);
55*598139dcSAndroid Build Coastguard Worker   usleep(100000);
56*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
57*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGV"
58*598139dcSAndroid Build Coastguard Worker   RLOGV_IF(true, content);
59*598139dcSAndroid Build Coastguard Worker   usleep(100000);
60*598139dcSAndroid Build Coastguard Worker   RLOGV_IF(false, content_false);
61*598139dcSAndroid Build Coastguard Worker   usleep(100000);
62*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
63*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGD"
64*598139dcSAndroid Build Coastguard Worker   RLOGD_IF(true, content);
65*598139dcSAndroid Build Coastguard Worker   usleep(100000);
66*598139dcSAndroid Build Coastguard Worker   RLOGD_IF(false, content_false);
67*598139dcSAndroid Build Coastguard Worker   usleep(100000);
68*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
69*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGI"
70*598139dcSAndroid Build Coastguard Worker   RLOGI_IF(true, content);
71*598139dcSAndroid Build Coastguard Worker   usleep(100000);
72*598139dcSAndroid Build Coastguard Worker   RLOGI_IF(false, content_false);
73*598139dcSAndroid Build Coastguard Worker   usleep(100000);
74*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
75*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGW"
76*598139dcSAndroid Build Coastguard Worker   RLOGW_IF(true, content);
77*598139dcSAndroid Build Coastguard Worker   usleep(100000);
78*598139dcSAndroid Build Coastguard Worker   RLOGW_IF(false, content_false);
79*598139dcSAndroid Build Coastguard Worker   usleep(100000);
80*598139dcSAndroid Build Coastguard Worker #undef LOG_TAG
81*598139dcSAndroid Build Coastguard Worker #define LOG_TAG "TEST__RLOGE"
82*598139dcSAndroid Build Coastguard Worker   RLOGE_IF(true, content);
83*598139dcSAndroid Build Coastguard Worker   usleep(100000);
84*598139dcSAndroid Build Coastguard Worker   RLOGE_IF(false, content_false);
85*598139dcSAndroid Build Coastguard Worker 
86*598139dcSAndroid Build Coastguard Worker #ifdef __ANDROID__
87*598139dcSAndroid Build Coastguard Worker   // give time for content to long-path through logger
88*598139dcSAndroid Build Coastguard Worker   sleep(1);
89*598139dcSAndroid Build Coastguard Worker 
90*598139dcSAndroid Build Coastguard Worker   std::string buf = android::base::StringPrintf(
91*598139dcSAndroid Build Coastguard Worker       "logcat -b radio --pid=%u -d -s"
92*598139dcSAndroid Build Coastguard Worker       " TEST__RLOGV TEST__RLOGD TEST__RLOGI TEST__RLOGW TEST__RLOGE",
93*598139dcSAndroid Build Coastguard Worker       (unsigned)getpid());
94*598139dcSAndroid Build Coastguard Worker   FILE* fp = popen(buf.c_str(), "re");
95*598139dcSAndroid Build Coastguard Worker   int count = 0;
96*598139dcSAndroid Build Coastguard Worker   int count_false = 0;
97*598139dcSAndroid Build Coastguard Worker   if (fp) {
98*598139dcSAndroid Build Coastguard Worker     if (!android::base::ReadFdToString(fileno(fp), &buf)) buf = "";
99*598139dcSAndroid Build Coastguard Worker     pclose(fp);
100*598139dcSAndroid Build Coastguard Worker     for (size_t pos = 0; (pos = buf.find(content, pos)) != std::string::npos;
101*598139dcSAndroid Build Coastguard Worker          ++pos) {
102*598139dcSAndroid Build Coastguard Worker       ++count;
103*598139dcSAndroid Build Coastguard Worker     }
104*598139dcSAndroid Build Coastguard Worker     for (size_t pos = 0;
105*598139dcSAndroid Build Coastguard Worker          (pos = buf.find(content_false, pos)) != std::string::npos; ++pos) {
106*598139dcSAndroid Build Coastguard Worker       ++count_false;
107*598139dcSAndroid Build Coastguard Worker     }
108*598139dcSAndroid Build Coastguard Worker   }
109*598139dcSAndroid Build Coastguard Worker   EXPECT_EQ(0, count_false);
110*598139dcSAndroid Build Coastguard Worker #if LOG_NDEBUG
111*598139dcSAndroid Build Coastguard Worker   ASSERT_EQ(8, count);
112*598139dcSAndroid Build Coastguard Worker #else
113*598139dcSAndroid Build Coastguard Worker   ASSERT_EQ(10, count);
114*598139dcSAndroid Build Coastguard Worker #endif
115*598139dcSAndroid Build Coastguard Worker 
116*598139dcSAndroid Build Coastguard Worker #else
117*598139dcSAndroid Build Coastguard Worker   GTEST_LOG_(INFO) << "This test does not test end-to-end.\n";
118*598139dcSAndroid Build Coastguard Worker #endif
119*598139dcSAndroid Build Coastguard Worker }
120