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