141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes/*
241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * Copyright (C) 2012 The Android Open Source Project
341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes *
441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * you may not use this file except in compliance with the License.
641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * You may obtain a copy of the License at
741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes *
841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes *
1041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * Unless required by applicable law or agreed to in writing, software
1141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * See the License for the specific language governing permissions and
1441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes * limitations under the License.
1541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes */
1641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
1741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes#include <gtest/gtest.h>
1841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
1941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes#if defined(__BIONIC__)
208f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#include "../libc/bionic/libc_logging.cpp"
2141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughesextern int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...);
22f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
2341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
248f2a5a0b40fc82126c691d5c30131d908772aab7Elliott HughesTEST(libc_logging, smoke) {
25f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
2641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  char buf[BUFSIZ];
2741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
2841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a");
2941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a", buf);
3041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
3141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "%%");
3241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("%", buf);
3341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
3441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "01234");
3541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("01234", buf);
3641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
3741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%sb", "01234");
3841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a01234b", buf);
3941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
4041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  char* s = NULL;
4141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%sb", s);
4241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a(null)b", buf);
4341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
4441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "aa%scc", "bb");
4541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("aabbcc", buf);
4641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
4741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%cc", 'b');
4841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("abc", buf);
4941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
5041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%db", 1234);
5141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a1234b", buf);
5241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
5341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%db", -8123);
5441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a-8123b", buf);
5541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
566c7b3cb056509fd8756bc012878a499f6f102114Stephen Hines  __libc_format_buffer(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
5741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a16b", buf);
5841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
596c7b3cb056509fd8756bc012878a499f6f102114Stephen Hines  __libc_format_buffer(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
6041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a16b", buf);
6141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
6241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
6341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a68719476736b", buf);
6441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
6541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%ldb", 70000L);
6641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a70000b", buf);
6741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
6841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
6941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a0xb0001234b", buf);
7041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
7141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%xz", 0x12ab);
7241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a12abz", buf);
7341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
7441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%Xz", 0x12ab);
7541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a12ABz", buf);
7641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
7741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%08xz", 0x123456);
7841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a00123456z", buf);
7941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
8041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%5dz", 1234);
8141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a 1234z", buf);
8241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
8341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%05dz", 1234);
8441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a01234z", buf);
8541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
8641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%8dz", 1234);
8741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a    1234z", buf);
8841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
8941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%-8dz", 1234);
9041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a1234    z", buf);
9141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
9241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "A%-11sZ", "abcdef");
9341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("Aabcdef     Z", buf);
9441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
9541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
9641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("Ahello:1234Z", buf);
9741b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
9841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
9941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a005:5:05z", buf);
10041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
10141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  void* p = NULL;
10241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%d,%pz", 5, p);
10341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a5,0x0z", buf);
10441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
10541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
10641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("a68719476736,6,7,8z", buf);
107f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
108f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
109f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
11041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes}
11141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
1128f2a5a0b40fc82126c691d5c30131d908772aab7Elliott HughesTEST(libc_logging, d_INT_MAX) {
113f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
11441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  char buf[BUFSIZ];
11541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "%d", INT_MAX);
11641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("2147483647", buf);
117f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
118f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
119f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
12041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes}
12141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
1228f2a5a0b40fc82126c691d5c30131d908772aab7Elliott HughesTEST(libc_logging, d_INT_MIN) {
123f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
12441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  char buf[BUFSIZ];
12541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "%d", INT_MIN);
12641b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("-2147483648", buf);
127f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
128f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
129f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
13041b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes}
13141b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
1328f2a5a0b40fc82126c691d5c30131d908772aab7Elliott HughesTEST(libc_logging, ld_LONG_MAX) {
133f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
13441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  char buf[BUFSIZ];
13541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
136b36efa4343d79e3fb548d12d039193850246b892Josh Gao#if defined(__LP64__)
137925753aa1175ae58b24bbfe2d9e38eb4fe3f579dElliott Hughes  EXPECT_STREQ("9223372036854775807", buf);
138925753aa1175ae58b24bbfe2d9e38eb4fe3f579dElliott Hughes#else
13941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("2147483647", buf);
140925753aa1175ae58b24bbfe2d9e38eb4fe3f579dElliott Hughes#endif
141f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
142f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
143f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
14441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes}
14541b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
1468f2a5a0b40fc82126c691d5c30131d908772aab7Elliott HughesTEST(libc_logging, ld_LONG_MIN) {
147f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
14841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  char buf[BUFSIZ];
14941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
150b36efa4343d79e3fb548d12d039193850246b892Josh Gao#if defined(__LP64__)
151925753aa1175ae58b24bbfe2d9e38eb4fe3f579dElliott Hughes  EXPECT_STREQ("-9223372036854775808", buf);
152925753aa1175ae58b24bbfe2d9e38eb4fe3f579dElliott Hughes#else
15341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("-2147483648", buf);
154925753aa1175ae58b24bbfe2d9e38eb4fe3f579dElliott Hughes#endif
155f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
156f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
157f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
15841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes}
15941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
1608f2a5a0b40fc82126c691d5c30131d908772aab7Elliott HughesTEST(libc_logging, lld_LLONG_MAX) {
161f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
16241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  char buf[BUFSIZ];
16341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "%lld", LLONG_MAX);
16441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("9223372036854775807", buf);
165f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
166f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
167f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
16841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes}
16941b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes
1708f2a5a0b40fc82126c691d5c30131d908772aab7Elliott HughesTEST(libc_logging, lld_LLONG_MIN) {
171f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
17241b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  char buf[BUFSIZ];
17341b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  __libc_format_buffer(buf, sizeof(buf), "%lld", LLONG_MIN);
17441b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes  EXPECT_STREQ("-9223372036854775808", buf);
175f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
176f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
177f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
17841b3179c9ef03ebb447cac7f5e8405dce399cb17Elliott Hughes}
179416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes
180416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott HughesTEST(libc_logging, buffer_overrun) {
181416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes#if defined(__BIONIC__)
182416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes  char buf[BUFSIZ];
183416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes  ASSERT_EQ(11, __libc_format_buffer(buf, sizeof(buf), "hello %s", "world"));
184416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes  EXPECT_STREQ("hello world", buf);
185416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes  ASSERT_EQ(11, __libc_format_buffer(buf, 8, "hello %s", "world"));
186416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes  EXPECT_STREQ("hello w", buf);
187416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes#else // __BIONIC__
188416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes  GTEST_LOG_(INFO) << "This test does nothing.\n";
189416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes#endif // __BIONIC__
190416d7ddaff0946d480b6aa945a741b3eeaca5569Elliott Hughes}
191