• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_system.h>
28 
TEST(liblog,SLOG)29 TEST(liblog, SLOG) {
30   static const char content[] = "log_system.h";
31   static const char content_false[] = "log_system.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__SLOGV"
38   SLOGV(content);
39   usleep(100000);
40 #undef LOG_TAG
41 #define LOG_TAG "TEST__SLOGD"
42   SLOGD(content);
43   usleep(100000);
44 #undef LOG_TAG
45 #define LOG_TAG "TEST__SLOGI"
46   SLOGI(content);
47   usleep(100000);
48 #undef LOG_TAG
49 #define LOG_TAG "TEST__SLOGW"
50   SLOGW(content);
51   usleep(100000);
52 #undef LOG_TAG
53 #define LOG_TAG "TEST__SLOGE"
54   SLOGE(content);
55   usleep(100000);
56 #undef LOG_TAG
57 #define LOG_TAG "TEST__SLOGV"
58   SLOGV_IF(true, content);
59   usleep(100000);
60   SLOGV_IF(false, content_false);
61   usleep(100000);
62 #undef LOG_TAG
63 #define LOG_TAG "TEST__SLOGD"
64   SLOGD_IF(true, content);
65   usleep(100000);
66   SLOGD_IF(false, content_false);
67   usleep(100000);
68 #undef LOG_TAG
69 #define LOG_TAG "TEST__SLOGI"
70   SLOGI_IF(true, content);
71   usleep(100000);
72   SLOGI_IF(false, content_false);
73   usleep(100000);
74 #undef LOG_TAG
75 #define LOG_TAG "TEST__SLOGW"
76   SLOGW_IF(true, content);
77   usleep(100000);
78   SLOGW_IF(false, content_false);
79   usleep(100000);
80 #undef LOG_TAG
81 #define LOG_TAG "TEST__SLOGE"
82   SLOGE_IF(true, content);
83   usleep(100000);
84   SLOGE_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 system --pid=%u -d -s"
92       " TEST__SLOGV TEST__SLOGD TEST__SLOGI TEST__SLOGW TEST__SLOGE",
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