2013-01-28 19:36:31 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-03-21 00:31:18 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2013-01-28 19:36:31 +01:00
|
|
|
#if defined(__BIONIC__)
|
2017-04-25 02:48:32 +02:00
|
|
|
#include <async_safe/log.h>
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-01-28 19:36:31 +01:00
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
TEST(async_safe_log, smoke) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__)
|
2013-01-28 19:36:31 +01:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a");
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "%%");
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("%", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "01234");
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("01234", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%sb", "01234");
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a01234b", buf);
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
char* s = nullptr;
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%sb", s);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a(null)b", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "aa%scc", "bb");
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("aabbcc", buf);
|
|
|
|
|
2023-08-22 22:50:39 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%bb", 1234);
|
|
|
|
EXPECT_STREQ("a10011010010b", buf);
|
|
|
|
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#bb", 1234);
|
|
|
|
EXPECT_STREQ("a0b10011010010b", buf);
|
|
|
|
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#Bb", 1234);
|
|
|
|
EXPECT_STREQ("a0B10011010010b", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%cc", 'b');
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("abc", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%db", 1234);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a1234b", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%db", -8123);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a-8123b", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a16b", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a16b", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a68719476736b", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%ldb", 70000L);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a70000b", buf);
|
|
|
|
|
2022-01-28 20:22:24 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%mZ");
|
|
|
|
EXPECT_STREQ("aInvalid argumentZ", buf);
|
|
|
|
|
2023-09-22 03:32:39 +02:00
|
|
|
#if __ANDROID_API_LEVEL__ >= 35
|
|
|
|
errno = EINVAL;
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#mZ");
|
|
|
|
EXPECT_STREQ("aEINVALZ", buf);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __ANDROID_API_LEVEL__ >= 35
|
|
|
|
errno = -1;
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#mZ");
|
|
|
|
EXPECT_STREQ("a-1Z", buf);
|
|
|
|
#endif
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a0xb0001234b", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%xz", 0x12ab);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a12abz", buf);
|
|
|
|
|
2023-08-22 22:50:39 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#xz", 0x12ab);
|
|
|
|
EXPECT_STREQ("a0x12abz", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%Xz", 0x12ab);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a12ABz", buf);
|
|
|
|
|
2023-08-22 22:50:39 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#Xz", 0x12ab);
|
|
|
|
EXPECT_STREQ("a0X12ABz", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%08xz", 0x123456);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a00123456z", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%5dz", 1234);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a 1234z", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%05dz", 1234);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a01234z", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%8dz", 1234);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a 1234z", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%-8dz", 1234);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a1234 z", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "A%-11sZ", "abcdef");
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("Aabcdef Z", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("Ahello:1234Z", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a005:5:05z", buf);
|
|
|
|
|
2022-01-28 20:22:24 +01:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#xZ", 34);
|
|
|
|
EXPECT_STREQ("a0x22Z", buf);
|
|
|
|
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#xZ", 0);
|
|
|
|
EXPECT_STREQ("a0Z", buf);
|
|
|
|
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#5xZ", 20);
|
|
|
|
EXPECT_STREQ("a 0x14Z", buf);
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "a%#08.8xZ", 1);
|
|
|
|
EXPECT_STREQ("a0x00000001Z", buf);
|
|
|
|
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#oZ", 777);
|
|
|
|
EXPECT_STREQ("a01411Z", buf);
|
|
|
|
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#oZ", 0);
|
|
|
|
EXPECT_STREQ("a0Z", buf);
|
|
|
|
|
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%#6oZ", 15);
|
|
|
|
EXPECT_STREQ("a 017Z", buf);
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "a%#08.8oZ", 11);
|
|
|
|
EXPECT_STREQ("a00000013Z", buf);
|
|
|
|
|
2018-08-03 02:31:13 +02:00
|
|
|
void* p = nullptr;
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%d,%pz", 5, p);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a5,0x0z", buf);
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("a68719476736,6,7,8z", buf);
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "bionic-only test";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-01-28 19:36:31 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
TEST(async_safe_log, d_INT_MAX) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__)
|
2013-01-28 19:36:31 +01:00
|
|
|
char buf[BUFSIZ];
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "%d", INT_MAX);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("2147483647", buf);
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "bionic-only test";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-01-28 19:36:31 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
TEST(async_safe_log, d_INT_MIN) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__)
|
2013-01-28 19:36:31 +01:00
|
|
|
char buf[BUFSIZ];
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "%d", INT_MIN);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("-2147483648", buf);
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "bionic-only test";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-01-28 19:36:31 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
TEST(async_safe_log, ld_LONG_MAX) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__)
|
2013-01-28 19:36:31 +01:00
|
|
|
char buf[BUFSIZ];
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "%ld", LONG_MAX);
|
2016-09-15 22:55:41 +02:00
|
|
|
#if defined(__LP64__)
|
2013-10-18 22:17:18 +02:00
|
|
|
EXPECT_STREQ("9223372036854775807", buf);
|
|
|
|
#else
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("2147483647", buf);
|
2013-10-18 22:17:18 +02:00
|
|
|
#endif
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "bionic-only test";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-01-28 19:36:31 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
TEST(async_safe_log, ld_LONG_MIN) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__)
|
2013-01-28 19:36:31 +01:00
|
|
|
char buf[BUFSIZ];
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "%ld", LONG_MIN);
|
2016-09-15 22:55:41 +02:00
|
|
|
#if defined(__LP64__)
|
2013-10-18 22:17:18 +02:00
|
|
|
EXPECT_STREQ("-9223372036854775808", buf);
|
|
|
|
#else
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("-2147483648", buf);
|
2013-10-18 22:17:18 +02:00
|
|
|
#endif
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "bionic-only test";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-01-28 19:36:31 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
TEST(async_safe_log, lld_LLONG_MAX) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__)
|
2013-01-28 19:36:31 +01:00
|
|
|
char buf[BUFSIZ];
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "%lld", LLONG_MAX);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("9223372036854775807", buf);
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "bionic-only test";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-01-28 19:36:31 +01:00
|
|
|
}
|
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
TEST(async_safe_log, lld_LLONG_MIN) {
|
2013-12-21 03:43:21 +01:00
|
|
|
#if defined(__BIONIC__)
|
2013-01-28 19:36:31 +01:00
|
|
|
char buf[BUFSIZ];
|
2017-04-25 02:48:32 +02:00
|
|
|
async_safe_format_buffer(buf, sizeof(buf), "%lld", LLONG_MIN);
|
2013-01-28 19:36:31 +01:00
|
|
|
EXPECT_STREQ("-9223372036854775808", buf);
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "bionic-only test";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2013-01-28 19:36:31 +01:00
|
|
|
}
|
2014-08-19 02:28:32 +02:00
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
TEST(async_safe_log, buffer_overrun) {
|
2014-08-19 02:28:32 +02:00
|
|
|
#if defined(__BIONIC__)
|
|
|
|
char buf[BUFSIZ];
|
2017-04-25 02:48:32 +02:00
|
|
|
ASSERT_EQ(11, async_safe_format_buffer(buf, sizeof(buf), "hello %s", "world"));
|
2014-08-19 02:28:32 +02:00
|
|
|
EXPECT_STREQ("hello world", buf);
|
2017-08-22 20:24:09 +02:00
|
|
|
|
2017-04-25 02:48:32 +02:00
|
|
|
ASSERT_EQ(11, async_safe_format_buffer(buf, 8, "hello %s", "world"));
|
2014-08-19 02:28:32 +02:00
|
|
|
EXPECT_STREQ("hello w", buf);
|
2017-08-22 20:24:09 +02:00
|
|
|
|
|
|
|
ASSERT_EQ(11, async_safe_format_buffer(buf, 6, "hello %s", "world"));
|
|
|
|
EXPECT_STREQ("hello", buf);
|
|
|
|
|
|
|
|
ASSERT_EQ(4, async_safe_format_buffer(nullptr, 0, "xxxx"));
|
|
|
|
|
|
|
|
ASSERT_EQ(4, async_safe_format_buffer(buf, 1, "xxxx"));
|
|
|
|
EXPECT_STREQ("", buf);
|
|
|
|
|
|
|
|
ASSERT_EQ(4, async_safe_format_buffer(buf, 2, "xxxx"));
|
|
|
|
EXPECT_STREQ("x", buf);
|
2014-08-19 02:28:32 +02:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "bionic-only test";
|
2014-08-19 02:28:32 +02:00
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|
2023-03-21 00:31:18 +01:00
|
|
|
|
|
|
|
// Verify that using %m is never cut off.
|
|
|
|
TEST(async_safe_format_buffer, percent_m_fits_in_buffer) {
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
errno = i;
|
|
|
|
char async_buf[256];
|
|
|
|
async_safe_format_buffer(async_buf, sizeof(async_buf), "%m");
|
|
|
|
char strerror_buf[1024];
|
|
|
|
strerror_r(errno, strerror_buf, sizeof(strerror_buf));
|
|
|
|
ASSERT_STREQ(strerror_buf, async_buf);
|
|
|
|
}
|
|
|
|
#else // __BIONIC__
|
|
|
|
GTEST_SKIP() << "bionic-only test";
|
|
|
|
#endif // __BIONIC__
|
|
|
|
}
|