blob: c4757aef6eac75506aaec49698489406c02b84e9 [file] [log] [blame]
Elliott Hughes416d7dd2014-08-18 17:28:32 -07001/*
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 Hughes416d7dd2014-08-18 17:28:32 -070017#include <errno.h>
18#include <gtest/gtest.h>
19
Colin Cross4408b8a2021-07-29 22:45:34 -070020// 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.
23int posix_strerror_r(int errnum, char* buf, size_t buflen);
24
Elliott Hughes416d7dd2014-08-18 17:28:32 -070025TEST(string, posix_strerror_r) {
26 char buf[256];
27
28 // Valid.
Colin Cross4408b8a2021-07-29 22:45:34 -070029 ASSERT_EQ(0, posix_strerror_r(0, buf, sizeof(buf)));
Elliott Hughes416d7dd2014-08-18 17:28:32 -070030 ASSERT_STREQ("Success", buf);
Colin Cross4408b8a2021-07-29 22:45:34 -070031 ASSERT_EQ(0, posix_strerror_r(1, buf, sizeof(buf)));
Elliott Hughes416d7dd2014-08-18 17:28:32 -070032 ASSERT_STREQ("Operation not permitted", buf);
33
Colin Cross4408b8a2021-07-29 22:45:34 -070034#if defined(__BIONIC__)
Elliott Hughes416d7dd2014-08-18 17:28:32 -070035 // Invalid.
Colin Cross4408b8a2021-07-29 22:45:34 -070036 ASSERT_EQ(0, posix_strerror_r(-1, buf, sizeof(buf)));
Elliott Hughes416d7dd2014-08-18 17:28:32 -070037 ASSERT_STREQ("Unknown error -1", buf);
Colin Cross4408b8a2021-07-29 22:45:34 -070038 ASSERT_EQ(0, posix_strerror_r(1234, buf, sizeof(buf)));
Elliott Hughes416d7dd2014-08-18 17:28:32 -070039 ASSERT_STREQ("Unknown error 1234", buf);
Colin Cross4408b8a2021-07-29 22:45:34 -070040#else
41 // glibc returns EINVAL for unknown errors
42 ASSERT_EQ(EINVAL, posix_strerror_r(-1, buf, sizeof(buf)));
43 ASSERT_EQ(EINVAL, posix_strerror_r(1234, buf, sizeof(buf)));
44#endif
Elliott Hughes416d7dd2014-08-18 17:28:32 -070045
46 // Buffer too small.
47 errno = 0;
48 memset(buf, 0, sizeof(buf));
Colin Cross4408b8a2021-07-29 22:45:34 -070049 ASSERT_EQ(ERANGE, posix_strerror_r(EPERM, buf, 2));
Colin Cross695af0d2021-07-30 09:36:57 -070050 ASSERT_STREQ("O", buf);
51 // POSIX strerror_r returns an error without updating errno.
52 ASSERT_EQ(0, errno);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070053}