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 "../libc/bionic/libc_logging.cpp"
21 extern int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...);
22 #endif // __BIONIC__
23 
TEST(libc_logging,smoke)24 TEST(libc_logging, smoke) {
25 #if defined(__BIONIC__)
26   char buf[BUFSIZ];
27 
28   __libc_format_buffer(buf, sizeof(buf), "a");
29   EXPECT_STREQ("a", buf);
30 
31   __libc_format_buffer(buf, sizeof(buf), "%%");
32   EXPECT_STREQ("%", buf);
33 
34   __libc_format_buffer(buf, sizeof(buf), "01234");
35   EXPECT_STREQ("01234", buf);
36 
37   __libc_format_buffer(buf, sizeof(buf), "a%sb", "01234");
38   EXPECT_STREQ("a01234b", buf);
39 
40   char* s = NULL;
41   __libc_format_buffer(buf, sizeof(buf), "a%sb", s);
42   EXPECT_STREQ("a(null)b", buf);
43 
44   __libc_format_buffer(buf, sizeof(buf), "aa%scc", "bb");
45   EXPECT_STREQ("aabbcc", buf);
46 
47   __libc_format_buffer(buf, sizeof(buf), "a%cc", 'b');
48   EXPECT_STREQ("abc", buf);
49 
50   __libc_format_buffer(buf, sizeof(buf), "a%db", 1234);
51   EXPECT_STREQ("a1234b", buf);
52 
53   __libc_format_buffer(buf, sizeof(buf), "a%db", -8123);
54   EXPECT_STREQ("a-8123b", buf);
55 
56   __libc_format_buffer(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
57   EXPECT_STREQ("a16b", buf);
58 
59   __libc_format_buffer(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
60   EXPECT_STREQ("a16b", buf);
61 
62   __libc_format_buffer(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
63   EXPECT_STREQ("a68719476736b", buf);
64 
65   __libc_format_buffer(buf, sizeof(buf), "a%ldb", 70000L);
66   EXPECT_STREQ("a70000b", buf);
67 
68   __libc_format_buffer(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
69   EXPECT_STREQ("a0xb0001234b", buf);
70 
71   __libc_format_buffer(buf, sizeof(buf), "a%xz", 0x12ab);
72   EXPECT_STREQ("a12abz", buf);
73 
74   __libc_format_buffer(buf, sizeof(buf), "a%Xz", 0x12ab);
75   EXPECT_STREQ("a12ABz", buf);
76 
77   __libc_format_buffer(buf, sizeof(buf), "a%08xz", 0x123456);
78   EXPECT_STREQ("a00123456z", buf);
79 
80   __libc_format_buffer(buf, sizeof(buf), "a%5dz", 1234);
81   EXPECT_STREQ("a 1234z", buf);
82 
83   __libc_format_buffer(buf, sizeof(buf), "a%05dz", 1234);
84   EXPECT_STREQ("a01234z", buf);
85 
86   __libc_format_buffer(buf, sizeof(buf), "a%8dz", 1234);
87   EXPECT_STREQ("a    1234z", buf);
88 
89   __libc_format_buffer(buf, sizeof(buf), "a%-8dz", 1234);
90   EXPECT_STREQ("a1234    z", buf);
91 
92   __libc_format_buffer(buf, sizeof(buf), "A%-11sZ", "abcdef");
93   EXPECT_STREQ("Aabcdef     Z", buf);
94 
95   __libc_format_buffer(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
96   EXPECT_STREQ("Ahello:1234Z", buf);
97 
98   __libc_format_buffer(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
99   EXPECT_STREQ("a005:5:05z", buf);
100 
101   void* p = NULL;
102   __libc_format_buffer(buf, sizeof(buf), "a%d,%pz", 5, p);
103   EXPECT_STREQ("a5,0x0z", buf);
104 
105   __libc_format_buffer(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
106   EXPECT_STREQ("a68719476736,6,7,8z", buf);
107 #else // __BIONIC__
108   GTEST_LOG_(INFO) << "This test does nothing.\n";
109 #endif // __BIONIC__
110 }
111 
TEST(libc_logging,d_INT_MAX)112 TEST(libc_logging, d_INT_MAX) {
113 #if defined(__BIONIC__)
114   char buf[BUFSIZ];
115   __libc_format_buffer(buf, sizeof(buf), "%d", INT_MAX);
116   EXPECT_STREQ("2147483647", buf);
117 #else // __BIONIC__
118   GTEST_LOG_(INFO) << "This test does nothing.\n";
119 #endif // __BIONIC__
120 }
121 
TEST(libc_logging,d_INT_MIN)122 TEST(libc_logging, d_INT_MIN) {
123 #if defined(__BIONIC__)
124   char buf[BUFSIZ];
125   __libc_format_buffer(buf, sizeof(buf), "%d", INT_MIN);
126   EXPECT_STREQ("-2147483648", buf);
127 #else // __BIONIC__
128   GTEST_LOG_(INFO) << "This test does nothing.\n";
129 #endif // __BIONIC__
130 }
131 
TEST(libc_logging,ld_LONG_MAX)132 TEST(libc_logging, ld_LONG_MAX) {
133 #if defined(__BIONIC__)
134   char buf[BUFSIZ];
135   __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
136 #if defined(__LP64__)
137   EXPECT_STREQ("9223372036854775807", buf);
138 #else
139   EXPECT_STREQ("2147483647", buf);
140 #endif
141 #else // __BIONIC__
142   GTEST_LOG_(INFO) << "This test does nothing.\n";
143 #endif // __BIONIC__
144 }
145 
TEST(libc_logging,ld_LONG_MIN)146 TEST(libc_logging, ld_LONG_MIN) {
147 #if defined(__BIONIC__)
148   char buf[BUFSIZ];
149   __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
150 #if defined(__LP64__)
151   EXPECT_STREQ("-9223372036854775808", buf);
152 #else
153   EXPECT_STREQ("-2147483648", buf);
154 #endif
155 #else // __BIONIC__
156   GTEST_LOG_(INFO) << "This test does nothing.\n";
157 #endif // __BIONIC__
158 }
159 
TEST(libc_logging,lld_LLONG_MAX)160 TEST(libc_logging, lld_LLONG_MAX) {
161 #if defined(__BIONIC__)
162   char buf[BUFSIZ];
163   __libc_format_buffer(buf, sizeof(buf), "%lld", LLONG_MAX);
164   EXPECT_STREQ("9223372036854775807", buf);
165 #else // __BIONIC__
166   GTEST_LOG_(INFO) << "This test does nothing.\n";
167 #endif // __BIONIC__
168 }
169 
TEST(libc_logging,lld_LLONG_MIN)170 TEST(libc_logging, lld_LLONG_MIN) {
171 #if defined(__BIONIC__)
172   char buf[BUFSIZ];
173   __libc_format_buffer(buf, sizeof(buf), "%lld", LLONG_MIN);
174   EXPECT_STREQ("-9223372036854775808", buf);
175 #else // __BIONIC__
176   GTEST_LOG_(INFO) << "This test does nothing.\n";
177 #endif // __BIONIC__
178 }
179 
TEST(libc_logging,buffer_overrun)180 TEST(libc_logging, buffer_overrun) {
181 #if defined(__BIONIC__)
182   char buf[BUFSIZ];
183   ASSERT_EQ(11, __libc_format_buffer(buf, sizeof(buf), "hello %s", "world"));
184   EXPECT_STREQ("hello world", buf);
185   ASSERT_EQ(11, __libc_format_buffer(buf, 8, "hello %s", "world"));
186   EXPECT_STREQ("hello w", buf);
187 #else // __BIONIC__
188   GTEST_LOG_(INFO) << "This test does nothing.\n";
189 #endif // __BIONIC__
190 }
191