blob: 6c4758e713a356ce52b77d969f512df9c2d9b3aa [file] [log] [blame]
Christopher Ferris7a3681e2017-04-24 17:48:32 -07001/*
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#if defined(__BIONIC__)
20#include <async_safe/log.h>
21#endif // __BIONIC__
22
23TEST(async_safe_log, smoke) {
24#if defined(__BIONIC__)
25 char buf[BUFSIZ];
26
27 async_safe_format_buffer(buf, sizeof(buf), "a");
28 EXPECT_STREQ("a", buf);
29
30 async_safe_format_buffer(buf, sizeof(buf), "%%");
31 EXPECT_STREQ("%", buf);
32
33 async_safe_format_buffer(buf, sizeof(buf), "01234");
34 EXPECT_STREQ("01234", buf);
35
36 async_safe_format_buffer(buf, sizeof(buf), "a%sb", "01234");
37 EXPECT_STREQ("a01234b", buf);
38
Yi Kong32bc0fc2018-08-02 17:31:13 -070039 char* s = nullptr;
Christopher Ferris7a3681e2017-04-24 17:48:32 -070040 async_safe_format_buffer(buf, sizeof(buf), "a%sb", s);
41 EXPECT_STREQ("a(null)b", buf);
42
43 async_safe_format_buffer(buf, sizeof(buf), "aa%scc", "bb");
44 EXPECT_STREQ("aabbcc", buf);
45
46 async_safe_format_buffer(buf, sizeof(buf), "a%cc", 'b');
47 EXPECT_STREQ("abc", buf);
48
49 async_safe_format_buffer(buf, sizeof(buf), "a%db", 1234);
50 EXPECT_STREQ("a1234b", buf);
51
52 async_safe_format_buffer(buf, sizeof(buf), "a%db", -8123);
53 EXPECT_STREQ("a-8123b", buf);
54
55 async_safe_format_buffer(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
56 EXPECT_STREQ("a16b", buf);
57
58 async_safe_format_buffer(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
59 EXPECT_STREQ("a16b", buf);
60
61 async_safe_format_buffer(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
62 EXPECT_STREQ("a68719476736b", buf);
63
64 async_safe_format_buffer(buf, sizeof(buf), "a%ldb", 70000L);
65 EXPECT_STREQ("a70000b", buf);
66
67 async_safe_format_buffer(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
68 EXPECT_STREQ("a0xb0001234b", buf);
69
70 async_safe_format_buffer(buf, sizeof(buf), "a%xz", 0x12ab);
71 EXPECT_STREQ("a12abz", buf);
72
73 async_safe_format_buffer(buf, sizeof(buf), "a%Xz", 0x12ab);
74 EXPECT_STREQ("a12ABz", buf);
75
76 async_safe_format_buffer(buf, sizeof(buf), "a%08xz", 0x123456);
77 EXPECT_STREQ("a00123456z", buf);
78
79 async_safe_format_buffer(buf, sizeof(buf), "a%5dz", 1234);
80 EXPECT_STREQ("a 1234z", buf);
81
82 async_safe_format_buffer(buf, sizeof(buf), "a%05dz", 1234);
83 EXPECT_STREQ("a01234z", buf);
84
85 async_safe_format_buffer(buf, sizeof(buf), "a%8dz", 1234);
86 EXPECT_STREQ("a 1234z", buf);
87
88 async_safe_format_buffer(buf, sizeof(buf), "a%-8dz", 1234);
89 EXPECT_STREQ("a1234 z", buf);
90
91 async_safe_format_buffer(buf, sizeof(buf), "A%-11sZ", "abcdef");
92 EXPECT_STREQ("Aabcdef Z", buf);
93
94 async_safe_format_buffer(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
95 EXPECT_STREQ("Ahello:1234Z", buf);
96
97 async_safe_format_buffer(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
98 EXPECT_STREQ("a005:5:05z", buf);
99
Yi Kong32bc0fc2018-08-02 17:31:13 -0700100 void* p = nullptr;
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700101 async_safe_format_buffer(buf, sizeof(buf), "a%d,%pz", 5, p);
102 EXPECT_STREQ("a5,0x0z", buf);
103
104 async_safe_format_buffer(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
105 EXPECT_STREQ("a68719476736,6,7,8z", buf);
106#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800107 GTEST_SKIP() << "bionic-only test";
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700108#endif // __BIONIC__
109}
110
111TEST(async_safe_log, d_INT_MAX) {
112#if defined(__BIONIC__)
113 char buf[BUFSIZ];
114 async_safe_format_buffer(buf, sizeof(buf), "%d", INT_MAX);
115 EXPECT_STREQ("2147483647", buf);
116#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800117 GTEST_SKIP() << "bionic-only test";
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700118#endif // __BIONIC__
119}
120
121TEST(async_safe_log, d_INT_MIN) {
122#if defined(__BIONIC__)
123 char buf[BUFSIZ];
124 async_safe_format_buffer(buf, sizeof(buf), "%d", INT_MIN);
125 EXPECT_STREQ("-2147483648", buf);
126#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800127 GTEST_SKIP() << "bionic-only test";
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700128#endif // __BIONIC__
129}
130
131TEST(async_safe_log, ld_LONG_MAX) {
132#if defined(__BIONIC__)
133 char buf[BUFSIZ];
134 async_safe_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
135#if defined(__LP64__)
136 EXPECT_STREQ("9223372036854775807", buf);
137#else
138 EXPECT_STREQ("2147483647", buf);
139#endif
140#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800141 GTEST_SKIP() << "bionic-only test";
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700142#endif // __BIONIC__
143}
144
145TEST(async_safe_log, ld_LONG_MIN) {
146#if defined(__BIONIC__)
147 char buf[BUFSIZ];
148 async_safe_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
149#if defined(__LP64__)
150 EXPECT_STREQ("-9223372036854775808", buf);
151#else
152 EXPECT_STREQ("-2147483648", buf);
153#endif
154#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800155 GTEST_SKIP() << "bionic-only test";
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700156#endif // __BIONIC__
157}
158
159TEST(async_safe_log, lld_LLONG_MAX) {
160#if defined(__BIONIC__)
161 char buf[BUFSIZ];
162 async_safe_format_buffer(buf, sizeof(buf), "%lld", LLONG_MAX);
163 EXPECT_STREQ("9223372036854775807", buf);
164#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800165 GTEST_SKIP() << "bionic-only test";
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700166#endif // __BIONIC__
167}
168
169TEST(async_safe_log, lld_LLONG_MIN) {
170#if defined(__BIONIC__)
171 char buf[BUFSIZ];
172 async_safe_format_buffer(buf, sizeof(buf), "%lld", LLONG_MIN);
173 EXPECT_STREQ("-9223372036854775808", buf);
174#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800175 GTEST_SKIP() << "bionic-only test";
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700176#endif // __BIONIC__
177}
178
179TEST(async_safe_log, buffer_overrun) {
180#if defined(__BIONIC__)
181 char buf[BUFSIZ];
182 ASSERT_EQ(11, async_safe_format_buffer(buf, sizeof(buf), "hello %s", "world"));
183 EXPECT_STREQ("hello world", buf);
Christopher Ferris92476402017-08-22 11:24:09 -0700184
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700185 ASSERT_EQ(11, async_safe_format_buffer(buf, 8, "hello %s", "world"));
186 EXPECT_STREQ("hello w", buf);
Christopher Ferris92476402017-08-22 11:24:09 -0700187
188 ASSERT_EQ(11, async_safe_format_buffer(buf, 6, "hello %s", "world"));
189 EXPECT_STREQ("hello", buf);
190
191 ASSERT_EQ(4, async_safe_format_buffer(nullptr, 0, "xxxx"));
192
193 ASSERT_EQ(4, async_safe_format_buffer(buf, 1, "xxxx"));
194 EXPECT_STREQ("", buf);
195
196 ASSERT_EQ(4, async_safe_format_buffer(buf, 2, "xxxx"));
197 EXPECT_STREQ("x", buf);
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700198#else // __BIONIC__
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800199 GTEST_SKIP() << "bionic-only test";
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700200#endif // __BIONIC__
201}