2012-10-29 22:27:10 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libgen.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2014-08-19 23:30:30 +02:00
|
|
|
#include <gtest/gtest.h>
|
2012-10-29 22:27:10 +01:00
|
|
|
|
|
|
|
static void TestDirname(const char* in, const char* expected_out) {
|
2018-08-03 02:31:13 +02:00
|
|
|
char* writable_in = (in != nullptr) ? strdup(in) : nullptr;
|
2012-10-29 22:27:10 +01:00
|
|
|
errno = 0;
|
|
|
|
const char* out = dirname(&writable_in[0]);
|
|
|
|
ASSERT_STREQ(expected_out, out) << in;
|
|
|
|
ASSERT_EQ(0, errno) << in;
|
|
|
|
free(writable_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(libgen, dirname) {
|
2018-08-03 02:31:13 +02:00
|
|
|
TestDirname(nullptr, ".");
|
2012-10-29 22:27:10 +01:00
|
|
|
TestDirname("", ".");
|
|
|
|
TestDirname("/usr/lib", "/usr");
|
|
|
|
TestDirname("/usr/", "/");
|
|
|
|
TestDirname("usr", ".");
|
|
|
|
TestDirname(".", ".");
|
|
|
|
TestDirname("..", ".");
|
|
|
|
TestDirname("/", "/");
|
|
|
|
}
|
|
|
|
|
2014-09-06 01:12:42 +02:00
|
|
|
#if defined(__BIONIC__) && !defined(__LP64__)
|
2012-10-29 22:27:10 +01:00
|
|
|
static void TestBasename(const char* in, const char* expected_out, int expected_rc,
|
|
|
|
char* buf, size_t buf_size, int expected_errno) {
|
|
|
|
errno = 0;
|
|
|
|
int rc = basename_r(in, buf, buf_size);
|
|
|
|
ASSERT_EQ(expected_rc, rc) << in;
|
2018-08-03 02:31:13 +02:00
|
|
|
if (rc != -1 && buf != nullptr) {
|
2012-10-29 22:27:10 +01:00
|
|
|
ASSERT_STREQ(expected_out, buf) << in;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(expected_errno, errno) << in;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestDirname(const char* in, const char* expected_out, int expected_rc,
|
|
|
|
char* buf, size_t buf_size, int expected_errno) {
|
|
|
|
errno = 0;
|
|
|
|
int rc = dirname_r(in, buf, buf_size);
|
|
|
|
ASSERT_EQ(expected_rc, rc) << in;
|
2018-08-03 02:31:13 +02:00
|
|
|
if (rc != -1 && buf != nullptr) {
|
2012-10-29 22:27:10 +01:00
|
|
|
ASSERT_STREQ(expected_out, buf) << in;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(expected_errno, errno) << in;
|
|
|
|
}
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2012-10-29 22:27:10 +01:00
|
|
|
|
|
|
|
TEST(libgen, basename_r) {
|
2014-09-06 01:12:42 +02:00
|
|
|
#if defined(__BIONIC__) && !defined(__LP64__)
|
2012-10-29 22:27:10 +01:00
|
|
|
char buf[256];
|
2018-08-03 02:31:13 +02:00
|
|
|
TestBasename("", ".", 1, nullptr, 0, 0);
|
2012-10-29 22:27:10 +01:00
|
|
|
TestBasename("", ".", -1, buf, 0, ERANGE);
|
|
|
|
TestBasename("", ".", -1, buf, 1, ERANGE);
|
|
|
|
TestBasename("", ".", 1, buf, 2, 0);
|
|
|
|
TestBasename("", ".", 1, buf, sizeof(buf), 0);
|
|
|
|
TestBasename("/usr/lib", "lib", 3, buf, sizeof(buf), 0);
|
|
|
|
TestBasename("/usr/", "usr", 3, buf, sizeof(buf), 0);
|
|
|
|
TestBasename("usr", "usr", 3, buf, sizeof(buf), 0);
|
|
|
|
TestBasename("/", "/", 1, buf, sizeof(buf), 0);
|
|
|
|
TestBasename(".", ".", 1, buf, sizeof(buf), 0);
|
|
|
|
TestBasename("..", "..", 2, buf, sizeof(buf), 0);
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "basename_r is only available on 32-bit bionic";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2012-10-29 22:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(libgen, dirname_r) {
|
2014-09-06 01:12:42 +02:00
|
|
|
#if defined(__BIONIC__) && !defined(__LP64__)
|
2012-10-29 22:27:10 +01:00
|
|
|
char buf[256];
|
2018-08-03 02:31:13 +02:00
|
|
|
TestDirname("", ".", 1, nullptr, 0, 0);
|
2012-10-29 22:27:10 +01:00
|
|
|
TestDirname("", ".", -1, buf, 0, ERANGE);
|
|
|
|
TestDirname("", ".", -1, buf, 1, ERANGE);
|
|
|
|
TestDirname("", ".", 1, buf, 2, 0);
|
|
|
|
TestDirname("/usr/lib", "/usr", 4, buf, sizeof(buf), 0);
|
|
|
|
TestDirname("/usr/", "/", 1, buf, sizeof(buf), 0);
|
|
|
|
TestDirname("usr", ".", 1, buf, sizeof(buf), 0);
|
|
|
|
TestDirname(".", ".", 1, buf, sizeof(buf), 0);
|
|
|
|
TestDirname("..", ".", 1, buf, sizeof(buf), 0);
|
2013-12-21 03:43:21 +01:00
|
|
|
#else // __BIONIC__
|
2019-03-09 00:20:23 +01:00
|
|
|
GTEST_SKIP() << "dirname_r is only available on 32-bit bionic";
|
2013-12-21 03:43:21 +01:00
|
|
|
#endif // __BIONIC__
|
2012-10-29 22:27:10 +01:00
|
|
|
}
|