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 | |
| 17 | #undef _GNU_SOURCE |
| 18 | |
| 19 | // Old versions of glibc (like our current host prebuilt sysroot one) have |
| 20 | // headers that don't work if you #undef _GNU_SOURCE, which makes it |
| 21 | // impossible to build this test. |
| 22 | #include <features.h> |
| 23 | |
| 24 | #if !defined(__GLIBC__) |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include <errno.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | TEST(string, posix_strerror_r) { |
| 31 | char buf[256]; |
| 32 | |
| 33 | // Valid. |
| 34 | ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf))); |
| 35 | ASSERT_STREQ("Success", buf); |
| 36 | ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf))); |
| 37 | ASSERT_STREQ("Operation not permitted", buf); |
| 38 | |
| 39 | // Invalid. |
| 40 | ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf))); |
| 41 | ASSERT_STREQ("Unknown error -1", buf); |
| 42 | ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf))); |
| 43 | ASSERT_STREQ("Unknown error 1234", buf); |
| 44 | |
| 45 | // Buffer too small. |
| 46 | errno = 0; |
| 47 | memset(buf, 0, sizeof(buf)); |
| 48 | ASSERT_EQ(-1, strerror_r(4567, buf, 2)); |
| 49 | ASSERT_STREQ("U", buf); |
| 50 | // The POSIX strerror_r sets errno to ERANGE (the GNU one doesn't). |
| 51 | ASSERT_EQ(ERANGE, errno); |
| 52 | } |
| 53 | #else |
| 54 | # if __GLIBC_PREREQ(2, 15) |
| 55 | # error this test should work now |
| 56 | # endif |
| 57 | #endif |