From 4408b8abb0feb5c58648e448dc7bd7134a1cf92c Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 29 Jul 2021 22:45:34 -0700 Subject: Enable posix strerror_r test in glibc Work around the incompatibility between libc++ and !_GNU_SOURCE by defining a wrapper function around the posix strerror_r in a file that does not include any libc++ or gtest headers. Test: bionic-unit-tests-glibc Change-Id: I6b097079777b5dcd802e295cc566964fe1c01382 --- tests/Android.bp | 1 + tests/string_posix_strerror_r_test.cpp | 43 ++++++++++++------------------- tests/string_posix_strerror_r_wrapper.cpp | 29 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 tests/string_posix_strerror_r_wrapper.cpp diff --git a/tests/Android.bp b/tests/Android.bp index 476b8f5fb..0f9af4153 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -406,6 +406,7 @@ cc_test_library { "string_nofortify_test.cpp", "string_test.cpp", "string_posix_strerror_r_test.cpp", + "string_posix_strerror_r_wrapper.cpp", "strings_nofortify_test.cpp", "strings_test.cpp", "struct_layout_test.cpp", diff --git a/tests/string_posix_strerror_r_test.cpp b/tests/string_posix_strerror_r_test.cpp index 67b3c1f90..c4757aef6 100644 --- a/tests/string_posix_strerror_r_test.cpp +++ b/tests/string_posix_strerror_r_test.cpp @@ -14,51 +14,40 @@ * limitations under the License. */ -#undef _GNU_SOURCE -#include // Get __BIONIC__ or __GLIBC__ so we can tell what we're using. - -#if defined(__GLIBC__) - -// At the time of writing, libcxx -- which is dragged in by gtest -- assumes -// declarations from glibc of things that aren't available without _GNU_SOURCE. -// This means we can't even build this test (which is a problem because that -// means it doesn't get included in CTS). -// For glibc 2.15, the symbols in question are: -// at_quick_exit, quick_exit, vasprintf, strtoll_l, strtoull_l, and strtold_l. - -# if __GLIBC_PREREQ(2, 19) -# error check whether we can build this now... -# endif - -#else - -#include - #include #include +// Defined in string_posix_strerror_r_wrapper.cpp as a wrapper around the posix +// strerror_r to work around an incompatibility between libc++ (required by +// gtest) and !_GNU_SOURCE. +int posix_strerror_r(int errnum, char* buf, size_t buflen); + TEST(string, posix_strerror_r) { char buf[256]; // Valid. - ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf))); + ASSERT_EQ(0, posix_strerror_r(0, buf, sizeof(buf))); ASSERT_STREQ("Success", buf); - ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf))); + ASSERT_EQ(0, posix_strerror_r(1, buf, sizeof(buf))); ASSERT_STREQ("Operation not permitted", buf); +#if defined(__BIONIC__) // Invalid. - ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf))); + ASSERT_EQ(0, posix_strerror_r(-1, buf, sizeof(buf))); ASSERT_STREQ("Unknown error -1", buf); - ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf))); + ASSERT_EQ(0, posix_strerror_r(1234, buf, sizeof(buf))); ASSERT_STREQ("Unknown error 1234", buf); +#else + // glibc returns EINVAL for unknown errors + ASSERT_EQ(EINVAL, posix_strerror_r(-1, buf, sizeof(buf))); + ASSERT_EQ(EINVAL, posix_strerror_r(1234, buf, sizeof(buf))); +#endif // Buffer too small. errno = 0; memset(buf, 0, sizeof(buf)); - ASSERT_EQ(ERANGE, strerror_r(EPERM, buf, 2)); + ASSERT_EQ(ERANGE, posix_strerror_r(EPERM, buf, 2)); ASSERT_STREQ("O", buf); // POSIX strerror_r returns an error without updating errno. ASSERT_EQ(0, errno); } - -#endif diff --git a/tests/string_posix_strerror_r_wrapper.cpp b/tests/string_posix_strerror_r_wrapper.cpp new file mode 100644 index 000000000..78d5d9047 --- /dev/null +++ b/tests/string_posix_strerror_r_wrapper.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2021 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. + */ + +#undef _GNU_SOURCE +#include + +// At the time of writing, libcxx -- which is dragged in by gtest -- assumes +// declarations from glibc of things that aren't available without _GNU_SOURCE. +// This means we can't even build a test that directly calls the posix +// strerror_r. Add a wrapper in a separate file that doesn't use any gtest. +// For glibc 2.15, the symbols in question are: +// at_quick_exit, quick_exit, vasprintf, strtoll_l, strtoull_l, and strtold_l. + +int posix_strerror_r(int errnum, char* buf, size_t buflen) { + return strerror_r(errnum, buf, buflen); +} -- cgit v1.2.3