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
TestDirname(const char * in,const char * expected_out)22 static void TestDirname(const char* in, const char* expected_out) {
23 char* writable_in = (in != NULL) ? strdup(in) : NULL;
24 errno = 0;
25 const char* out = dirname(&writable_in[0]);
26 ASSERT_STREQ(expected_out, out) << in;
27 ASSERT_EQ(0, errno) << in;
28 free(writable_in);
29 }
30
TEST(libgen,dirname)31 TEST(libgen, dirname) {
32 TestDirname(NULL, ".");
33 TestDirname("", ".");
34 TestDirname("/usr/lib", "/usr");
35 TestDirname("/usr/", "/");
36 TestDirname("usr", ".");
37 TestDirname(".", ".");
38 TestDirname("..", ".");
39 TestDirname("/", "/");
40 }
41
42 #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)43 static void TestBasename(const char* in, const char* expected_out, int expected_rc,
44 char* buf, size_t buf_size, int expected_errno) {
45 errno = 0;
46 int rc = basename_r(in, buf, buf_size);
47 ASSERT_EQ(expected_rc, rc) << in;
48 if (rc != -1 && buf != NULL) {
49 ASSERT_STREQ(expected_out, buf) << in;
50 }
51 ASSERT_EQ(expected_errno, errno) << in;
52 }
53
TestDirname(const char * in,const char * expected_out,int expected_rc,char * buf,size_t buf_size,int expected_errno)54 static void TestDirname(const char* in, const char* expected_out, int expected_rc,
55 char* buf, size_t buf_size, int expected_errno) {
56 errno = 0;
57 int rc = dirname_r(in, buf, buf_size);
58 ASSERT_EQ(expected_rc, rc) << in;
59 if (rc != -1 && buf != NULL) {
60 ASSERT_STREQ(expected_out, buf) << in;
61 }
62 ASSERT_EQ(expected_errno, errno) << in;
63 }
64 #endif // __BIONIC__
65
TEST(libgen,basename_r)66 TEST(libgen, basename_r) {
67 #if defined(__BIONIC__) && !defined(__LP64__)
68 char buf[256];
69 TestBasename("", ".", 1, NULL, 0, 0);
70 TestBasename("", ".", -1, buf, 0, ERANGE);
71 TestBasename("", ".", -1, buf, 1, ERANGE);
72 TestBasename("", ".", 1, buf, 2, 0);
73 TestBasename("", ".", 1, buf, sizeof(buf), 0);
74 TestBasename("/usr/lib", "lib", 3, buf, sizeof(buf), 0);
75 TestBasename("/usr/", "usr", 3, buf, sizeof(buf), 0);
76 TestBasename("usr", "usr", 3, buf, sizeof(buf), 0);
77 TestBasename("/", "/", 1, buf, sizeof(buf), 0);
78 TestBasename(".", ".", 1, buf, sizeof(buf), 0);
79 TestBasename("..", "..", 2, buf, sizeof(buf), 0);
80 #else // __BIONIC__
81 GTEST_LOG_(INFO) << "This test does nothing.\n";
82 #endif // __BIONIC__
83 }
84
TEST(libgen,dirname_r)85 TEST(libgen, dirname_r) {
86 #if defined(__BIONIC__) && !defined(__LP64__)
87 char buf[256];
88 TestDirname("", ".", 1, NULL, 0, 0);
89 TestDirname("", ".", -1, buf, 0, ERANGE);
90 TestDirname("", ".", -1, buf, 1, ERANGE);
91 TestDirname("", ".", 1, buf, 2, 0);
92 TestDirname("/usr/lib", "/usr", 4, buf, sizeof(buf), 0);
93 TestDirname("/usr/", "/", 1, buf, sizeof(buf), 0);
94 TestDirname("usr", ".", 1, buf, sizeof(buf), 0);
95 TestDirname(".", ".", 1, buf, sizeof(buf), 0);
96 TestDirname("..", ".", 1, buf, sizeof(buf), 0);
97 #else // __BIONIC__
98 GTEST_LOG_(INFO) << "This test does nothing.\n";
99 #endif // __BIONIC__
100 }
101