blob: 300282c04929bec8c98a3c4502e795a015b9ec5f [file] [log] [blame]
Josh Gaoeb9b9252015-11-03 18:46:02 -08001/*
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 Cross4c5595c2021-08-16 15:51:59 -070017#include <sys/cdefs.h>
18
Josh Gaoeb9b9252015-11-03 18:46:02 -080019#ifndef _GNU_SOURCE
20 #define _GNU_SOURCE 1
21#endif
22
Colin Cross4c5595c2021-08-16 15:51:59 -070023#if !defined(ANDROID_HOST_MUSL)
Josh Gaoeb9b9252015-11-03 18:46:02 -080024#include <string.h>
25
26#if defined(basename)
27 #error basename should not be defined at this point
28#endif
29
30static const char* gnu_basename(const char* in) {
31 return basename(in);
32}
33
Colin Cross7da20342021-07-28 11:18:11 -070034#endif
35
Josh Gaoeb9b9252015-11-03 18:46:02 -080036#include <libgen.h>
37
Colin Cross4c5595c2021-08-16 15:51:59 -070038#if !defined(basename) && !defined(ANDROID_HOST_MUSL)
Colin Cross14d15072021-08-16 16:35:27 -070039#error basename should be defined at this point
Josh Gaoeb9b9252015-11-03 18:46:02 -080040#endif
41
42static char* posix_basename(char* in) {
43 return basename(in);
44}
45
46#include <errno.h>
47#include <gtest/gtest.h>
48
Colin Cross4c5595c2021-08-16 15:51:59 -070049#if !defined(ANDROID_HOST_MUSL)
Josh Gaoeb9b9252015-11-03 18:46:02 -080050static void __TestGnuBasename(const char* in, const char* expected_out, int line) {
Josh Gao09fac862015-11-09 16:04:50 -080051 errno = 0;
Josh Gaoeb9b9252015-11-03 18:46:02 -080052 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 Cross7da20342021-07-28 11:18:11 -070056#endif
Josh Gaoeb9b9252015-11-03 18:46:02 -080057
58static void __TestPosixBasename(const char* in, const char* expected_out, int line) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070059 char* writable_in = (in != nullptr) ? strdup(in) : nullptr;
Josh Gaoeb9b9252015-11-03 18:46:02 -080060 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
70TEST(libgen_basename, gnu_basename) {
Colin Cross4c5595c2021-08-16 15:51:59 -070071#if !defined(ANDROID_HOST_MUSL)
Josh Gaoeb9b9252015-11-03 18:46:02 -080072 // 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 Cross7da20342021-07-28 11:18:11 -070084#else
85 GTEST_SKIP() << "musl doesn't have GNU basename";
86 #endif
Josh Gaoeb9b9252015-11-03 18:46:02 -080087}
88
89TEST(libgen_basename, posix_basename) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070090 TestPosixBasename(nullptr, ".");
Josh Gaoeb9b9252015-11-03 18:46:02 -080091 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}