1 /*
2  * Copyright (C) 2012 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 <gtest/gtest.h>
18 
19 #include <errno.h>
20 
21 #if defined(__BIONIC__)
22 #include <async_safe/log.h>
23 #endif // __BIONIC__
24 
TEST(async_safe_log,smoke)25 TEST(async_safe_log, smoke) {
26 #if defined(__BIONIC__)
27   char buf[BUFSIZ];
28 
29   async_safe_format_buffer(buf, sizeof(buf), "a");
30   EXPECT_STREQ("a", buf);
31 
32   async_safe_format_buffer(buf, sizeof(buf), "%%");
33   EXPECT_STREQ("%", buf);
34 
35   async_safe_format_buffer(buf, sizeof(buf), "01234");
36   EXPECT_STREQ("01234", buf);
37 
38   async_safe_format_buffer(buf, sizeof(buf), "a%sb", "01234");
39   EXPECT_STREQ("a01234b", buf);
40 
41   char* s = nullptr;
42   async_safe_format_buffer(buf, sizeof(buf), "a%sb", s);
43   EXPECT_STREQ("a(null)b", buf);
44 
45   async_safe_format_buffer(buf, sizeof(buf), "aa%scc", "bb");
46   EXPECT_STREQ("aabbcc", buf);
47 
48   async_safe_format_buffer(buf, sizeof(buf), "a%bb", 1234);
49   EXPECT_STREQ("a10011010010b", buf);
50 
51   async_safe_format_buffer(buf, sizeof(buf), "a%#bb", 1234);
52   EXPECT_STREQ("a0b10011010010b", buf);
53 
54   async_safe_format_buffer(buf, sizeof(buf), "a%#Bb", 1234);
55   EXPECT_STREQ("a0B10011010010b", buf);
56 
57   async_safe_format_buffer(buf, sizeof(buf), "a%cc", 'b');
58   EXPECT_STREQ("abc", buf);
59 
60   async_safe_format_buffer(buf, sizeof(buf), "a%db", 1234);
61   EXPECT_STREQ("a1234b", buf);
62 
63   async_safe_format_buffer(buf, sizeof(buf), "a%db", -8123);
64   EXPECT_STREQ("a-8123b", buf);
65 
66   async_safe_format_buffer(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
67   EXPECT_STREQ("a16b", buf);
68 
69   async_safe_format_buffer(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
70   EXPECT_STREQ("a16b", buf);
71 
72   async_safe_format_buffer(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
73   EXPECT_STREQ("a68719476736b", buf);
74 
75   async_safe_format_buffer(buf, sizeof(buf), "a%ldb", 70000L);
76   EXPECT_STREQ("a70000b", buf);
77 
78   errno = EINVAL;
79   async_safe_format_buffer(buf, sizeof(buf), "a%mZ");
80   EXPECT_STREQ("aInvalid argumentZ", buf);
81 
82 #if __ANDROID_API_LEVEL__ >= 35
83   errno = EINVAL;
84   async_safe_format_buffer(buf, sizeof(buf), "a%#mZ");
85   EXPECT_STREQ("aEINVALZ", buf);
86 #endif
87 
88 #if __ANDROID_API_LEVEL__ >= 35
89   errno = -1;
90   async_safe_format_buffer(buf, sizeof(buf), "a%#mZ");
91   EXPECT_STREQ("a-1Z", buf);
92 #endif
93 
94   async_safe_format_buffer(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
95   EXPECT_STREQ("a0xb0001234b", buf);
96 
97   async_safe_format_buffer(buf, sizeof(buf), "a%xz", 0x12ab);
98   EXPECT_STREQ("a12abz", buf);
99 
100   async_safe_format_buffer(buf, sizeof(buf), "a%#xz", 0x12ab);
101   EXPECT_STREQ("a0x12abz", buf);
102 
103   async_safe_format_buffer(buf, sizeof(buf), "a%Xz", 0x12ab);
104   EXPECT_STREQ("a12ABz", buf);
105 
106   async_safe_format_buffer(buf, sizeof(buf), "a%#Xz", 0x12ab);
107   EXPECT_STREQ("a0X12ABz", buf);
108 
109   async_safe_format_buffer(buf, sizeof(buf), "a%08xz", 0x123456);
110   EXPECT_STREQ("a00123456z", buf);
111 
112   async_safe_format_buffer(buf, sizeof(buf), "a%5dz", 1234);
113   EXPECT_STREQ("a 1234z", buf);
114 
115   async_safe_format_buffer(buf, sizeof(buf), "a%05dz", 1234);
116   EXPECT_STREQ("a01234z", buf);
117 
118   async_safe_format_buffer(buf, sizeof(buf), "a%8dz", 1234);
119   EXPECT_STREQ("a    1234z", buf);
120 
121   async_safe_format_buffer(buf, sizeof(buf), "a%-8dz", 1234);
122   EXPECT_STREQ("a1234    z", buf);
123 
124   async_safe_format_buffer(buf, sizeof(buf), "A%-11sZ", "abcdef");
125   EXPECT_STREQ("Aabcdef     Z", buf);
126 
127   async_safe_format_buffer(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
128   EXPECT_STREQ("Ahello:1234Z", buf);
129 
130   async_safe_format_buffer(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
131   EXPECT_STREQ("a005:5:05z", buf);
132 
133   async_safe_format_buffer(buf, sizeof(buf), "a%#xZ", 34);
134   EXPECT_STREQ("a0x22Z", buf);
135 
136   async_safe_format_buffer(buf, sizeof(buf), "a%#xZ", 0);
137   EXPECT_STREQ("a0Z", buf);
138 
139   async_safe_format_buffer(buf, sizeof(buf), "a%#5xZ", 20);
140   EXPECT_STREQ("a 0x14Z", buf);
141 
142   snprintf(buf, sizeof(buf), "a%#08.8xZ", 1);
143   EXPECT_STREQ("a0x00000001Z", buf);
144 
145   async_safe_format_buffer(buf, sizeof(buf), "a%#oZ", 777);
146   EXPECT_STREQ("a01411Z", buf);
147 
148   async_safe_format_buffer(buf, sizeof(buf), "a%#oZ", 0);
149   EXPECT_STREQ("a0Z", buf);
150 
151   async_safe_format_buffer(buf, sizeof(buf), "a%#6oZ", 15);
152   EXPECT_STREQ("a   017Z", buf);
153 
154   snprintf(buf, sizeof(buf), "a%#08.8oZ", 11);
155   EXPECT_STREQ("a00000013Z", buf);
156 
157   void* p = nullptr;
158   async_safe_format_buffer(buf, sizeof(buf), "a%d,%pz", 5, p);
159   EXPECT_STREQ("a5,0x0z", buf);
160 
161   async_safe_format_buffer(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
162   EXPECT_STREQ("a68719476736,6,7,8z", buf);
163 #else // __BIONIC__
164   GTEST_SKIP() << "bionic-only test";
165 #endif // __BIONIC__
166 }
167 
TEST(async_safe_log,d_INT_MAX)168 TEST(async_safe_log, d_INT_MAX) {
169 #if defined(__BIONIC__)
170   char buf[BUFSIZ];
171   async_safe_format_buffer(buf, sizeof(buf), "%d", INT_MAX);
172   EXPECT_STREQ("2147483647", buf);
173 #else // __BIONIC__
174   GTEST_SKIP() << "bionic-only test";
175 #endif // __BIONIC__
176 }
177 
TEST(async_safe_log,d_INT_MIN)178 TEST(async_safe_log, d_INT_MIN) {
179 #if defined(__BIONIC__)
180   char buf[BUFSIZ];
181   async_safe_format_buffer(buf, sizeof(buf), "%d", INT_MIN);
182   EXPECT_STREQ("-2147483648", buf);
183 #else // __BIONIC__
184   GTEST_SKIP() << "bionic-only test";
185 #endif // __BIONIC__
186 }
187 
TEST(async_safe_log,ld_LONG_MAX)188 TEST(async_safe_log, ld_LONG_MAX) {
189 #if defined(__BIONIC__)
190   char buf[BUFSIZ];
191   async_safe_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
192 #if defined(__LP64__)
193   EXPECT_STREQ("9223372036854775807", buf);
194 #else
195   EXPECT_STREQ("2147483647", buf);
196 #endif
197 #else // __BIONIC__
198   GTEST_SKIP() << "bionic-only test";
199 #endif // __BIONIC__
200 }
201 
TEST(async_safe_log,ld_LONG_MIN)202 TEST(async_safe_log, ld_LONG_MIN) {
203 #if defined(__BIONIC__)
204   char buf[BUFSIZ];
205   async_safe_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
206 #if defined(__LP64__)
207   EXPECT_STREQ("-9223372036854775808", buf);
208 #else
209   EXPECT_STREQ("-2147483648", buf);
210 #endif
211 #else // __BIONIC__
212   GTEST_SKIP() << "bionic-only test";
213 #endif // __BIONIC__
214 }
215 
TEST(async_safe_log,lld_LLONG_MAX)216 TEST(async_safe_log, lld_LLONG_MAX) {
217 #if defined(__BIONIC__)
218   char buf[BUFSIZ];
219   async_safe_format_buffer(buf, sizeof(buf), "%lld", LLONG_MAX);
220   EXPECT_STREQ("9223372036854775807", buf);
221 #else // __BIONIC__
222   GTEST_SKIP() << "bionic-only test";
223 #endif // __BIONIC__
224 }
225 
TEST(async_safe_log,lld_LLONG_MIN)226 TEST(async_safe_log, lld_LLONG_MIN) {
227 #if defined(__BIONIC__)
228   char buf[BUFSIZ];
229   async_safe_format_buffer(buf, sizeof(buf), "%lld", LLONG_MIN);
230   EXPECT_STREQ("-9223372036854775808", buf);
231 #else // __BIONIC__
232   GTEST_SKIP() << "bionic-only test";
233 #endif // __BIONIC__
234 }
235 
TEST(async_safe_log,buffer_overrun)236 TEST(async_safe_log, buffer_overrun) {
237 #if defined(__BIONIC__)
238   char buf[BUFSIZ];
239   ASSERT_EQ(11, async_safe_format_buffer(buf, sizeof(buf), "hello %s", "world"));
240   EXPECT_STREQ("hello world", buf);
241 
242   ASSERT_EQ(11, async_safe_format_buffer(buf, 8, "hello %s", "world"));
243   EXPECT_STREQ("hello w", buf);
244 
245   ASSERT_EQ(11, async_safe_format_buffer(buf, 6, "hello %s", "world"));
246   EXPECT_STREQ("hello", buf);
247 
248   ASSERT_EQ(4, async_safe_format_buffer(nullptr, 0, "xxxx"));
249 
250   ASSERT_EQ(4, async_safe_format_buffer(buf, 1, "xxxx"));
251   EXPECT_STREQ("", buf);
252 
253   ASSERT_EQ(4, async_safe_format_buffer(buf, 2, "xxxx"));
254   EXPECT_STREQ("x", buf);
255 #else // __BIONIC__
256   GTEST_SKIP() << "bionic-only test";
257 #endif // __BIONIC__
258 }
259 
260 // Verify that using %m is never cut off.
TEST(async_safe_format_buffer,percent_m_fits_in_buffer)261 TEST(async_safe_format_buffer, percent_m_fits_in_buffer) {
262 #if defined(__BIONIC__)
263   for (int i = 0; i < 256; i++) {
264     errno = i;
265     char async_buf[256];
266     async_safe_format_buffer(async_buf, sizeof(async_buf), "%m");
267     char strerror_buf[1024];
268     strerror_r(errno, strerror_buf, sizeof(strerror_buf));
269     ASSERT_STREQ(strerror_buf, async_buf);
270   }
271 #else   // __BIONIC__
272   GTEST_SKIP() << "bionic-only test";
273 #endif  // __BIONIC__
274 }
275