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