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 #undef _GNU_SOURCE
18 #include <features.h> // Get __BIONIC__ or __GLIBC__ so we can tell what we're using.
19
20 #if defined(__GLIBC__)
21
22 // At the time of writing, libcxx -- which is dragged in by gtest -- assumes
23 // declarations from glibc of things that aren't available without __USE_GNU.
24 // This means we can't even build this test (which is a problem because that
25 // means it doesn't get included in CTS).
26 // For glibc 2.15, the symbols in question are:
27 // at_quick_exit, quick_exit, vasprintf, strtoll_l, strtoull_l, and strtold_l.
28
29 # if __GLIBC_PREREQ(2, 19)
30 # error check whether we can build this now...
31 # endif
32
33 #else
34
35 #include <string.h>
36
37 #include <errno.h>
38 #include <gtest/gtest.h>
39
TEST(string,posix_strerror_r)40 TEST(string, posix_strerror_r) {
41 char buf[256];
42
43 // Valid.
44 ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf)));
45 ASSERT_STREQ("Success", buf);
46 ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf)));
47 ASSERT_STREQ("Operation not permitted", buf);
48
49 // Invalid.
50 ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf)));
51 ASSERT_STREQ("Unknown error -1", buf);
52 ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf)));
53 ASSERT_STREQ("Unknown error 1234", buf);
54
55 // Buffer too small.
56 errno = 0;
57 memset(buf, 0, sizeof(buf));
58 ASSERT_EQ(-1, strerror_r(4567, buf, 2));
59 ASSERT_STREQ("U", buf);
60 // The POSIX strerror_r sets errno to ERANGE (the GNU one doesn't).
61 ASSERT_EQ(ERANGE, errno);
62 }
63
64 #endif
65