Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [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 | #ifndef _GNU_SOURCE |
| 18 | #define _GNU_SOURCE 1 |
| 19 | #endif |
| 20 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 21 | #if !defined(MUSL) |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
| 24 | #if defined(basename) |
| 25 | #error basename should not be defined at this point |
| 26 | #endif |
| 27 | |
| 28 | static const char* gnu_basename(const char* in) { |
| 29 | return basename(in); |
| 30 | } |
| 31 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 32 | #endif |
| 33 | |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 34 | #include <libgen.h> |
| 35 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 36 | #if !defined(basename) && !defined(MUSL) |
Colin Cross | 14d1507 | 2021-08-16 16:35:27 -0700 | [diff] [blame^] | 37 | #error basename should be defined at this point |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | static char* posix_basename(char* in) { |
| 41 | return basename(in); |
| 42 | } |
| 43 | |
| 44 | #include <errno.h> |
| 45 | #include <gtest/gtest.h> |
| 46 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 47 | #if !defined(MUSL) |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 48 | static void __TestGnuBasename(const char* in, const char* expected_out, int line) { |
Josh Gao | 09fac86 | 2015-11-09 16:04:50 -0800 | [diff] [blame] | 49 | errno = 0; |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 50 | const char* out = gnu_basename(in); |
| 51 | ASSERT_STREQ(expected_out, out) << "(" << line << "): " << in << std::endl; |
| 52 | ASSERT_EQ(0, errno) << "(" << line << "): " << in << std::endl; |
| 53 | } |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 54 | #endif |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 55 | |
| 56 | static void __TestPosixBasename(const char* in, const char* expected_out, int line) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 57 | char* writable_in = (in != nullptr) ? strdup(in) : nullptr; |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 58 | errno = 0; |
| 59 | const char* out = posix_basename(&writable_in[0]); |
| 60 | ASSERT_STREQ(expected_out, out) << "(" << line << "): " << in << std::endl; |
| 61 | ASSERT_EQ(0, errno) << "(" << line << "): " << in << std::endl; |
| 62 | free(writable_in); |
| 63 | } |
| 64 | |
| 65 | #define TestGnuBasename(in, expected) __TestGnuBasename(in, expected, __LINE__) |
| 66 | #define TestPosixBasename(in, expected) __TestPosixBasename(in, expected, __LINE__) |
| 67 | |
| 68 | TEST(libgen_basename, gnu_basename) { |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 69 | #if !defined(MUSL) |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 70 | // GNU's basename doesn't accept NULL |
| 71 | // TestGnuBasename(NULL, "."); |
| 72 | TestGnuBasename("", ""); |
| 73 | TestGnuBasename("/usr/lib", "lib"); |
| 74 | TestGnuBasename("/system/bin/sh/", ""); |
| 75 | TestGnuBasename("/usr/", ""); |
| 76 | TestGnuBasename("usr", "usr"); |
| 77 | TestGnuBasename("/", ""); |
| 78 | TestGnuBasename(".", "."); |
| 79 | TestGnuBasename("..", ".."); |
| 80 | TestGnuBasename("///", ""); |
| 81 | TestGnuBasename("//usr//lib//", ""); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 82 | #else |
| 83 | GTEST_SKIP() << "musl doesn't have GNU basename"; |
| 84 | #endif |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | TEST(libgen_basename, posix_basename) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 88 | TestPosixBasename(nullptr, "."); |
Josh Gao | eb9b925 | 2015-11-03 18:46:02 -0800 | [diff] [blame] | 89 | TestPosixBasename("", "."); |
| 90 | TestPosixBasename("/usr/lib", "lib"); |
| 91 | TestPosixBasename("/system/bin/sh/", "sh"); |
| 92 | TestPosixBasename("/usr/", "usr"); |
| 93 | TestPosixBasename("usr", "usr"); |
| 94 | TestPosixBasename("/", "/"); |
| 95 | TestPosixBasename(".", "."); |
| 96 | TestPosixBasename("..", ".."); |
| 97 | TestPosixBasename("///", "/"); |
| 98 | TestPosixBasename("//usr//lib//", "lib"); |
| 99 | } |