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 <libgen.h>
18 
19 #include <errno.h>
20 #include <gtest/gtest.h>
21 
TestBasename(const char * in,const char * expected_out)22 static void TestBasename(const char* in, const char* expected_out) {
23   char* writable_in = (in != NULL) ? strdup(in) : NULL;
24   errno = 0;
25   const char* out = basename(&writable_in[0]);
26   ASSERT_STREQ(expected_out, out) << in;
27   ASSERT_EQ(0, errno) << in;
28   free(writable_in);
29 }
30 
TestDirname(const char * in,const char * expected_out)31 static void TestDirname(const char* in, const char* expected_out) {
32   char* writable_in = (in != NULL) ? strdup(in) : NULL;
33   errno = 0;
34   const char* out = dirname(&writable_in[0]);
35   ASSERT_STREQ(expected_out, out) << in;
36   ASSERT_EQ(0, errno) << in;
37   free(writable_in);
38 }
39 
40 // Do not use basename as the test name, it's defined to another value in glibc
41 // so leads to a differently named test on host versus target architectures.
TEST(libgen,posix_basename)42 TEST(libgen, posix_basename) {
43   TestBasename(NULL, ".");
44   TestBasename("", ".");
45   TestBasename("/usr/lib", "lib");
46   TestBasename("/usr/", "usr");
47   TestBasename("usr", "usr");
48   TestBasename("/", "/");
49   TestBasename(".", ".");
50   TestBasename("..", "..");
51   TestBasename("///", "/");
52   TestBasename("//usr//lib//", "lib");
53 }
54 
TEST(libgen,dirname)55 TEST(libgen, dirname) {
56   TestDirname(NULL, ".");
57   TestDirname("", ".");
58   TestDirname("/usr/lib", "/usr");
59   TestDirname("/usr/", "/");
60   TestDirname("usr", ".");
61   TestDirname(".", ".");
62   TestDirname("..", ".");
63   TestDirname("/", "/");
64 }
65 
66 #if defined(__BIONIC__) && !defined(__LP64__)
TestBasename(const char * in,const char * expected_out,int expected_rc,char * buf,size_t buf_size,int expected_errno)67 static void TestBasename(const char* in, const char* expected_out, int expected_rc,
68                          char* buf, size_t buf_size, int expected_errno) {
69   errno = 0;
70   int rc = basename_r(in, buf, buf_size);
71   ASSERT_EQ(expected_rc, rc) << in;
72   if (rc != -1 && buf != NULL) {
73     ASSERT_STREQ(expected_out, buf) << in;
74   }
75   ASSERT_EQ(expected_errno, errno) << in;
76 }
77 
TestDirname(const char * in,const char * expected_out,int expected_rc,char * buf,size_t buf_size,int expected_errno)78 static void TestDirname(const char* in, const char* expected_out, int expected_rc,
79                         char* buf, size_t buf_size, int expected_errno) {
80   errno = 0;
81   int rc = dirname_r(in, buf, buf_size);
82   ASSERT_EQ(expected_rc, rc) << in;
83   if (rc != -1 && buf != NULL) {
84     ASSERT_STREQ(expected_out, buf) << in;
85   }
86   ASSERT_EQ(expected_errno, errno) << in;
87 }
88 #endif // __BIONIC__
89 
TEST(libgen,basename_r)90 TEST(libgen, basename_r) {
91 #if defined(__BIONIC__) && !defined(__LP64__)
92   char buf[256];
93   TestBasename("", ".",  1, NULL, 0, 0);
94   TestBasename("", ".", -1, buf, 0, ERANGE);
95   TestBasename("", ".", -1, buf, 1, ERANGE);
96   TestBasename("", ".", 1, buf, 2, 0);
97   TestBasename("", ".", 1, buf, sizeof(buf), 0);
98   TestBasename("/usr/lib", "lib", 3, buf, sizeof(buf), 0);
99   TestBasename("/usr/", "usr", 3, buf, sizeof(buf), 0);
100   TestBasename("usr", "usr", 3, buf, sizeof(buf), 0);
101   TestBasename("/", "/", 1, buf, sizeof(buf), 0);
102   TestBasename(".", ".", 1, buf, sizeof(buf), 0);
103   TestBasename("..", "..", 2, buf, sizeof(buf), 0);
104 #else // __BIONIC__
105   GTEST_LOG_(INFO) << "This test does nothing.\n";
106 #endif // __BIONIC__
107 }
108 
TEST(libgen,dirname_r)109 TEST(libgen, dirname_r) {
110 #if defined(__BIONIC__) && !defined(__LP64__)
111   char buf[256];
112   TestDirname("", ".",  1, NULL, 0, 0);
113   TestDirname("", ".", -1, buf, 0, ERANGE);
114   TestDirname("", ".", -1, buf, 1, ERANGE);
115   TestDirname("", ".", 1, buf, 2, 0);
116   TestDirname("/usr/lib", "/usr", 4, buf, sizeof(buf), 0);
117   TestDirname("/usr/", "/", 1, buf, sizeof(buf), 0);
118   TestDirname("usr", ".", 1, buf, sizeof(buf), 0);
119   TestDirname(".", ".", 1, buf, sizeof(buf), 0);
120   TestDirname("..", ".", 1, buf, sizeof(buf), 0);
121 #else // __BIONIC__
122   GTEST_LOG_(INFO) << "This test does nothing.\n";
123 #endif // __BIONIC__
124 }
125