Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 17 | #include <errno.h> |
Steven Moreland | c3060de | 2021-09-22 11:06:57 -0700 | [diff] [blame^] | 18 | |
| 19 | #include <android-base/test_utils.h> |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
Colin Cross | 4408b8a | 2021-07-29 22:45:34 -0700 | [diff] [blame] | 22 | // Defined in string_posix_strerror_r_wrapper.cpp as a wrapper around the posix |
| 23 | // strerror_r to work around an incompatibility between libc++ (required by |
| 24 | // gtest) and !_GNU_SOURCE. |
| 25 | int posix_strerror_r(int errnum, char* buf, size_t buflen); |
| 26 | |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 27 | TEST(string, posix_strerror_r) { |
| 28 | char buf[256]; |
| 29 | |
| 30 | // Valid. |
Colin Cross | 4408b8a | 2021-07-29 22:45:34 -0700 | [diff] [blame] | 31 | ASSERT_EQ(0, posix_strerror_r(0, buf, sizeof(buf))); |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 32 | #if defined(ANDROID_HOST_MUSL) |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 33 | ASSERT_STREQ("No error information", buf); |
| 34 | #else |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 35 | ASSERT_STREQ("Success", buf); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 36 | #endif |
Colin Cross | 4408b8a | 2021-07-29 22:45:34 -0700 | [diff] [blame] | 37 | ASSERT_EQ(0, posix_strerror_r(1, buf, sizeof(buf))); |
Steven Moreland | c3060de | 2021-09-22 11:06:57 -0700 | [diff] [blame^] | 38 | ASSERT_MATCH(buf, "Operation not permitted.*"); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 39 | |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 40 | #if defined(__BIONIC__) || defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 41 | // Invalid. |
Colin Cross | 4408b8a | 2021-07-29 22:45:34 -0700 | [diff] [blame] | 42 | ASSERT_EQ(0, posix_strerror_r(-1, buf, sizeof(buf))); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 43 | # if defined(__BIONIC__) |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 44 | ASSERT_STREQ("Unknown error -1", buf); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 45 | # else |
| 46 | ASSERT_STREQ("No error information", buf); |
| 47 | # endif |
Colin Cross | 4408b8a | 2021-07-29 22:45:34 -0700 | [diff] [blame] | 48 | ASSERT_EQ(0, posix_strerror_r(1234, buf, sizeof(buf))); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 49 | # if defined(__BIONIC__) |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 50 | ASSERT_STREQ("Unknown error 1234", buf); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 51 | # else |
| 52 | ASSERT_STREQ("No error information", buf); |
| 53 | # endif |
Colin Cross | 4408b8a | 2021-07-29 22:45:34 -0700 | [diff] [blame] | 54 | #else |
| 55 | // glibc returns EINVAL for unknown errors |
| 56 | ASSERT_EQ(EINVAL, posix_strerror_r(-1, buf, sizeof(buf))); |
| 57 | ASSERT_EQ(EINVAL, posix_strerror_r(1234, buf, sizeof(buf))); |
| 58 | #endif |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 59 | |
| 60 | // Buffer too small. |
| 61 | errno = 0; |
| 62 | memset(buf, 0, sizeof(buf)); |
Colin Cross | 4408b8a | 2021-07-29 22:45:34 -0700 | [diff] [blame] | 63 | ASSERT_EQ(ERANGE, posix_strerror_r(EPERM, buf, 2)); |
Colin Cross | 695af0d | 2021-07-30 09:36:57 -0700 | [diff] [blame] | 64 | ASSERT_STREQ("O", buf); |
| 65 | // POSIX strerror_r returns an error without updating errno. |
| 66 | ASSERT_EQ(0, errno); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 67 | } |