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 #include <sys/cdefs.h>
18
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE 1
21 #endif
22
23 #if !defined(ANDROID_HOST_MUSL)
24 #include <string.h>
25
26 #if defined(basename)
27 #error basename should not be defined at this point
28 #endif
29
gnu_basename(const char * in)30 static const char* gnu_basename(const char* in) {
31 return basename(in);
32 }
33
34 #endif
35
36 #include <libgen.h>
37
38 #if !defined(basename) && !defined(ANDROID_HOST_MUSL)
39 #error basename should be defined at this point
40 #endif
41
posix_basename(char * in)42 static char* posix_basename(char* in) {
43 return basename(in);
44 }
45
46 #include <errno.h>
47 #include <gtest/gtest.h>
48
49 #if !defined(ANDROID_HOST_MUSL)
__TestGnuBasename(const char * in,const char * expected_out,int line)50 static void __TestGnuBasename(const char* in, const char* expected_out, int line) {
51 errno = 0;
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 }
56 #endif
57
__TestPosixBasename(const char * in,const char * expected_out,int line)58 static void __TestPosixBasename(const char* in, const char* expected_out, int line) {
59 char* writable_in = (in != nullptr) ? strdup(in) : nullptr;
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
TEST(libgen_basename,gnu_basename)70 TEST(libgen_basename, gnu_basename) {
71 #if !defined(ANDROID_HOST_MUSL)
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//", "");
84 #else
85 GTEST_SKIP() << "musl doesn't have GNU basename";
86 #endif
87 }
88
TEST(libgen_basename,posix_basename)89 TEST(libgen_basename, posix_basename) {
90 TestPosixBasename(nullptr, ".");
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 }
102