blob: 1202af6e2b7055c30d3440869da9692952e37149 [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
17#ifndef _GNU_SOURCE
18 #define _GNU_SOURCE 1
19#endif
20
Colin Cross7da20342021-07-28 11:18:11 -070021#if !defined(MUSL)
Josh Gaoeb9b9252015-11-03 18:46:02 -080022#include <string.h>
23
24#if defined(basename)
25 #error basename should not be defined at this point
26#endif
27
28static const char* gnu_basename(const char* in) {
29 return basename(in);
30}
31
Colin Cross7da20342021-07-28 11:18:11 -070032#endif
33
Josh Gaoeb9b9252015-11-03 18:46:02 -080034#include <libgen.h>
35
Colin Cross7da20342021-07-28 11:18:11 -070036#if !defined(basename) && !defined(MUSL)
Josh Gaoeb9b9252015-11-03 18:46:02 -080037 #error basename should be defined at this point
38#endif
39
40static char* posix_basename(char* in) {
41 return basename(in);
42}
43
44#include <errno.h>
45#include <gtest/gtest.h>
46
Colin Cross7da20342021-07-28 11:18:11 -070047#if !defined(MUSL)
Josh Gaoeb9b9252015-11-03 18:46:02 -080048static void __TestGnuBasename(const char* in, const char* expected_out, int line) {
Josh Gao09fac862015-11-09 16:04:50 -080049 errno = 0;
Josh Gaoeb9b9252015-11-03 18:46:02 -080050 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 Cross7da20342021-07-28 11:18:11 -070054#endif
Josh Gaoeb9b9252015-11-03 18:46:02 -080055
56static void __TestPosixBasename(const char* in, const char* expected_out, int line) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070057 char* writable_in = (in != nullptr) ? strdup(in) : nullptr;
Josh Gaoeb9b9252015-11-03 18:46:02 -080058 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
68TEST(libgen_basename, gnu_basename) {
Colin Cross7da20342021-07-28 11:18:11 -070069#if !defined(MUSL)
Josh Gaoeb9b9252015-11-03 18:46:02 -080070 // 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 Cross7da20342021-07-28 11:18:11 -070082#else
83 GTEST_SKIP() << "musl doesn't have GNU basename";
84 #endif
Josh Gaoeb9b9252015-11-03 18:46:02 -080085}
86
87TEST(libgen_basename, posix_basename) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070088 TestPosixBasename(nullptr, ".");
Josh Gaoeb9b9252015-11-03 18:46:02 -080089 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}