OSDN Git Service

Merge "greylist is no longer enabled by default."
[android-x86/bionic.git] / tests / async_safe_test.cpp
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 #if defined(__BIONIC__)
20 #include <async_safe/log.h>
21 #endif // __BIONIC__
22
23 TEST(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
39   char* s = NULL;
40   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
100   void* p = NULL;
101   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__
107   GTEST_LOG_(INFO) << "This test does nothing.\n";
108 #endif // __BIONIC__
109 }
110
111 TEST(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__
117   GTEST_LOG_(INFO) << "This test does nothing.\n";
118 #endif // __BIONIC__
119 }
120
121 TEST(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__
127   GTEST_LOG_(INFO) << "This test does nothing.\n";
128 #endif // __BIONIC__
129 }
130
131 TEST(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__
141   GTEST_LOG_(INFO) << "This test does nothing.\n";
142 #endif // __BIONIC__
143 }
144
145 TEST(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__
155   GTEST_LOG_(INFO) << "This test does nothing.\n";
156 #endif // __BIONIC__
157 }
158
159 TEST(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__
165   GTEST_LOG_(INFO) << "This test does nothing.\n";
166 #endif // __BIONIC__
167 }
168
169 TEST(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__
175   GTEST_LOG_(INFO) << "This test does nothing.\n";
176 #endif // __BIONIC__
177 }
178
179 TEST(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);
184   ASSERT_EQ(11, async_safe_format_buffer(buf, 8, "hello %s", "world"));
185   EXPECT_STREQ("hello w", buf);
186 #else // __BIONIC__
187   GTEST_LOG_(INFO) << "This test does nothing.\n";
188 #endif // __BIONIC__
189 }