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