| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -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 | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 17 | #include <libgen.h> | 
|  | 18 |  | 
|  | 19 | #include <errno.h> | 
| Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 21 |  | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 22 | static void TestDirname(const char* in, const char* expected_out) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 23 | char* writable_in = (in != nullptr) ? strdup(in) : nullptr; | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 24 | errno = 0; | 
|  | 25 | const char* out = dirname(&writable_in[0]); | 
|  | 26 | ASSERT_STREQ(expected_out, out) << in; | 
|  | 27 | ASSERT_EQ(0, errno) << in; | 
|  | 28 | free(writable_in); | 
|  | 29 | } | 
|  | 30 |  | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 31 | TEST(libgen, dirname) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 32 | TestDirname(nullptr, "."); | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 33 | TestDirname("", "."); | 
|  | 34 | TestDirname("/usr/lib", "/usr"); | 
|  | 35 | TestDirname("/usr/", "/"); | 
|  | 36 | TestDirname("usr", "."); | 
|  | 37 | TestDirname(".", "."); | 
|  | 38 | TestDirname("..", "."); | 
|  | 39 | TestDirname("/", "/"); | 
|  | 40 | } | 
|  | 41 |  | 
| Elliott Hughes | f0e9458 | 2014-09-05 16:12:42 -0700 | [diff] [blame] | 42 | #if defined(__BIONIC__) && !defined(__LP64__) | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 43 | static void TestBasename(const char* in, const char* expected_out, int expected_rc, | 
|  | 44 | char* buf, size_t buf_size, int expected_errno) { | 
|  | 45 | errno = 0; | 
|  | 46 | int rc = basename_r(in, buf, buf_size); | 
|  | 47 | ASSERT_EQ(expected_rc, rc) << in; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 48 | if (rc != -1 && buf != nullptr) { | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 49 | ASSERT_STREQ(expected_out, buf) << in; | 
|  | 50 | } | 
|  | 51 | ASSERT_EQ(expected_errno, errno) << in; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | static void TestDirname(const char* in, const char* expected_out, int expected_rc, | 
|  | 55 | char* buf, size_t buf_size, int expected_errno) { | 
|  | 56 | errno = 0; | 
|  | 57 | int rc = dirname_r(in, buf, buf_size); | 
|  | 58 | ASSERT_EQ(expected_rc, rc) << in; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 59 | if (rc != -1 && buf != nullptr) { | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 60 | ASSERT_STREQ(expected_out, buf) << in; | 
|  | 61 | } | 
|  | 62 | ASSERT_EQ(expected_errno, errno) << in; | 
|  | 63 | } | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 64 | #endif // __BIONIC__ | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 65 |  | 
|  | 66 | TEST(libgen, basename_r) { | 
| Elliott Hughes | f0e9458 | 2014-09-05 16:12:42 -0700 | [diff] [blame] | 67 | #if defined(__BIONIC__) && !defined(__LP64__) | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 68 | char buf[256]; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 69 | TestBasename("", ".",  1, nullptr, 0, 0); | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 70 | TestBasename("", ".", -1, buf, 0, ERANGE); | 
|  | 71 | TestBasename("", ".", -1, buf, 1, ERANGE); | 
|  | 72 | TestBasename("", ".", 1, buf, 2, 0); | 
|  | 73 | TestBasename("", ".", 1, buf, sizeof(buf), 0); | 
|  | 74 | TestBasename("/usr/lib", "lib", 3, buf, sizeof(buf), 0); | 
|  | 75 | TestBasename("/usr/", "usr", 3, buf, sizeof(buf), 0); | 
|  | 76 | TestBasename("usr", "usr", 3, buf, sizeof(buf), 0); | 
|  | 77 | TestBasename("/", "/", 1, buf, sizeof(buf), 0); | 
|  | 78 | TestBasename(".", ".", 1, buf, sizeof(buf), 0); | 
|  | 79 | TestBasename("..", "..", 2, buf, sizeof(buf), 0); | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 80 | #else // __BIONIC__ | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 81 | GTEST_SKIP() << "basename_r is only available on 32-bit bionic"; | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 82 | #endif // __BIONIC__ | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
|  | 85 | TEST(libgen, dirname_r) { | 
| Elliott Hughes | f0e9458 | 2014-09-05 16:12:42 -0700 | [diff] [blame] | 86 | #if defined(__BIONIC__) && !defined(__LP64__) | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 87 | char buf[256]; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 88 | TestDirname("", ".",  1, nullptr, 0, 0); | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 89 | TestDirname("", ".", -1, buf, 0, ERANGE); | 
|  | 90 | TestDirname("", ".", -1, buf, 1, ERANGE); | 
|  | 91 | TestDirname("", ".", 1, buf, 2, 0); | 
|  | 92 | TestDirname("/usr/lib", "/usr", 4, buf, sizeof(buf), 0); | 
|  | 93 | TestDirname("/usr/", "/", 1, buf, sizeof(buf), 0); | 
|  | 94 | TestDirname("usr", ".", 1, buf, sizeof(buf), 0); | 
|  | 95 | TestDirname(".", ".", 1, buf, sizeof(buf), 0); | 
|  | 96 | TestDirname("..", ".", 1, buf, sizeof(buf), 0); | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 97 | #else // __BIONIC__ | 
| Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 98 | GTEST_SKIP() << "dirname_r is only available on 32-bit bionic"; | 
| Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 99 | #endif // __BIONIC__ | 
| Elliott Hughes | 58b5754 | 2012-10-29 14:27:10 -0700 | [diff] [blame] | 100 | } |