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