From cb14a1337ff4b0130de32b147a2af36e121649fc Mon Sep 17 00:00:00 2001 From: William Woodall Date: Fri, 11 May 2018 11:44:17 -0700 Subject: [PATCH 1/3] ensure malloc and free are called in only one location --- include/rcutils/allocator.h | 15 +++++----- include/rcutils/concat.h | 42 ---------------------------- include/rcutils/filesystem.h | 10 +++++-- src/concat.c | 53 ------------------------------------ src/filesystem.c | 12 +++++--- test/test_concat.cpp | 49 --------------------------------- test/test_logging_macros.c | 6 ++-- 7 files changed, 28 insertions(+), 159 deletions(-) delete mode 100644 include/rcutils/concat.h delete mode 100644 src/concat.c delete mode 100644 test/test_concat.cpp diff --git a/include/rcutils/allocator.h b/include/rcutils/allocator.h index dc727654..a7d8008d 100644 --- a/include/rcutils/allocator.h +++ b/include/rcutils/allocator.h @@ -29,7 +29,7 @@ extern "C" /// Encapsulation of an allocator. /** - * The default allocator uses std::malloc(), std::free(), std::calloc(), and std::realloc(). + * The default allocator uses malloc(), free(), calloc(), and realloc(). * It can be obtained using rcutils_get_default_allocator(). * * The allocator should be trivially copyable. @@ -47,7 +47,7 @@ typedef struct rcutils_allocator_t /// Allocate memory, given a size and the `state` pointer. /** An error should be indicated by returning `NULL`. */ void * (*allocate)(size_t size, void * state); - /// Deallocate previously allocated memory, mimicking std::free(). + /// Deallocate previously allocated memory, mimicking free(). /** Also takes the `state` pointer. */ void (* deallocate)(void * pointer, void * state); /// Reallocate if possible, otherwise it deallocates and allocates. @@ -55,9 +55,9 @@ typedef struct rcutils_allocator_t * Also takes the `state` pointer. * * If unsupported then do deallocate and then allocate. - * This should behave as std::realloc() does, as opposed to posix's + * This should behave as realloc() does, as opposed to posix's * [reallocf](https://linux.die.net/man/3/reallocf), i.e. the memory given - * by pointer will not be free'd automatically if std::realloc() fails. + * by pointer will not be free'd automatically if realloc() fails. * For reallocf-like behavior use rcutils_reallocf(). * This function must be able to take an input pointer of `NULL` and succeed. */ @@ -87,9 +87,10 @@ rcutils_get_zero_initialized_allocator(void); /** * This defaults to: * - * - allocate = wraps std::malloc() - * - deallocate = wraps std::free() - * - reallocate = wrapps std::realloc() + * - allocate = wraps malloc() + * - deallocate = wraps free() + * - reallocate = wraps realloc() + * - zero_allocate = wraps calloc() * - state = `NULL` * *
diff --git a/include/rcutils/concat.h b/include/rcutils/concat.h deleted file mode 100644 index 475d104b..00000000 --- a/include/rcutils/concat.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2017 Open Source Robotics Foundation, Inc. -// -// 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. - -#ifndef RCUTILS__CONCAT_H_ -#define RCUTILS__CONCAT_H_ - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include "rcutils/visibility_control.h" - -/// Return a newly allocated string that contains lhs, followed by delimiter, followed by rhs. -/** - * This function allocates memory and returns it to the caller. - * It is up to the caller to release the memory once it is done with it by calling `free`. - * - * \return char * concatenated string on success - * NULL on invalid arguments - * NULL on failure - */ -RCUTILS_PUBLIC -char * -rcutils_concat(const char * lhs, const char * rhs, const char * delimiter); - -#ifdef __cplusplus -} -#endif - -#endif // RCUTILS__CONCAT_H_ diff --git a/include/rcutils/filesystem.h b/include/rcutils/filesystem.h index d9133771..0e7632e8 100644 --- a/include/rcutils/filesystem.h +++ b/include/rcutils/filesystem.h @@ -23,6 +23,7 @@ extern "C" #include #include +#include "rcutils/allocator.h" #include "rcutils/macros.h" #include "rcutils/visibility_control.h" @@ -108,17 +109,22 @@ rcutils_is_readable_and_writable(const char * abs_path); /// Return newly allocated string with arguments separated by correct delimiter for the platform. /** * This function allocates memory and returns it to the caller. - * It is up to the caller to release the memory once it is done with it by calling `free`. + * It is up to the caller to release the memory once it is done with it by + * calling `deallocate` on the same allocator passed here. * * \param[in] left_hand_path * \param[in] right_hand_path + * \param[in] allocator * \return char * concatenated path on success * NULL on invalid arguments * NULL on failure */ RCUTILS_PUBLIC char * -rcutils_join_path(const char * left_hand_path, const char * right_hand_path); +rcutils_join_path( + const char * left_hand_path, + const char * right_hand_path, + rcutils_allocator_t allocator); #ifdef __cplusplus } diff --git a/src/concat.c b/src/concat.c deleted file mode 100644 index 65325228..00000000 --- a/src/concat.c +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2017 Open Source Robotics Foundation, Inc. -// -// 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. - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include -#include -#include - -#include "rcutils/concat.h" - -char * -rcutils_concat(const char * lhs, const char * rhs, const char * delimiter) -{ - if (NULL == lhs) { - return NULL; - } - if (NULL == rhs) { - return NULL; - } - - size_t lhs_len = strlen(lhs); - size_t rhs_len = strlen(rhs); - size_t del_len = (delimiter) ? strlen(delimiter) : 0; - - char * concat = (char *) malloc((lhs_len + rhs_len + del_len + 1) * sizeof(char)); - if (NULL == concat) { - return NULL; - } - - int n = snprintf(concat, lhs_len + 1, "%s", lhs); - snprintf(concat + n, rhs_len + del_len + 1, "%s%s", (delimiter) ? delimiter : "", rhs); - - return concat; -} - -#ifdef __cplusplus -} -#endif diff --git a/src/filesystem.c b/src/filesystem.c index 3375de16..3b7d28cf 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -16,6 +16,7 @@ extern "C" { #endif +#include "rcutils/filesystem.h" #include #include @@ -26,8 +27,8 @@ extern "C" #else #include #endif // _WIN32 -#include "rcutils/concat.h" -#include "rcutils/filesystem.h" + +#include "rcutils/format_string.h" bool rcutils_get_cwd(char * buffer, size_t max_length) @@ -142,7 +143,10 @@ rcutils_is_readable_and_writable(const char * abs_path) } char * -rcutils_join_path(const char * left_hand_path, const char * right_hand_path) +rcutils_join_path( + const char * left_hand_path, + const char * right_hand_path, + rcutils_allocator_t allocator) { if (NULL == left_hand_path) { return NULL; @@ -157,7 +161,7 @@ rcutils_join_path(const char * left_hand_path, const char * right_hand_path) const char * delimiter = "/"; #endif // _WIN32 - return rcutils_concat(left_hand_path, right_hand_path, delimiter); + return rcutils_format_string(allocator, "%s%s%s", left_hand_path, delimiter, right_hand_path); } #ifdef __cplusplus diff --git a/test/test_concat.cpp b/test/test_concat.cpp deleted file mode 100644 index 996a3e0b..00000000 --- a/test/test_concat.cpp +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2017 Open Source Robotics Foundation, Inc. -// -// 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 "gtest/gtest.h" - -#include "rcutils/concat.h" - -#define ENABLE_LOGGING 0 - -#if ENABLE_LOGGING -#define LOG(expected, actual) { \ - printf("Expected: %s Actual: %s\n", expected, actual);} -#else -#define LOG(X, arg) {} -#endif - -TEST(test_concat, concat) { - const char * lhs = NULL; - const char * rhs = NULL; - const char * delimiter = NULL; - - char * res = rcutils_concat(lhs, rhs, delimiter); - EXPECT_STREQ(NULL, res); - - lhs = static_cast("foo"); - res = rcutils_concat(lhs, rhs, delimiter); - EXPECT_STREQ(NULL, res); - - rhs = static_cast("bar"); - res = rcutils_concat(lhs, rhs, delimiter); - LOG("foobar", res); - EXPECT_STREQ("foobar", res); - - delimiter = static_cast("/"); - res = rcutils_concat(lhs, rhs, delimiter); - LOG("foo/bar", res); - EXPECT_STREQ("foo/bar", res); -} diff --git a/test/test_logging_macros.c b/test/test_logging_macros.c index 3ae6079d..0c71b863 100644 --- a/test/test_logging_macros.c +++ b/test/test_logging_macros.c @@ -14,6 +14,7 @@ #include +#include "rcutils/allocator.h" #include "rcutils/logging_macros.h" #include "rcutils/types/rcutils_ret.h" @@ -32,15 +33,16 @@ void custom_handler( const rcutils_log_location_t * location, int severity, const char * name, const char * format, va_list * args) { + rcutils_allocator_t allocator = rcutils_get_default_allocator(); g_log_calls += 1; g_last_log_event.location = location; g_last_log_event.severity = severity; g_last_log_event.name = name ? name : ""; if (g_last_log_event.message) { - free(g_last_log_event.message); + allocator.deallocate(g_last_log_event.message, allocator.state); } const size_t size = 1024; - g_last_log_event.message = malloc(size); + g_last_log_event.message = allocator.allocate(size, allocator.state); vsnprintf(g_last_log_event.message, size, format, *args); } From 70baef0187e2cf54f8e87197a79be23a78f511ab Mon Sep 17 00:00:00 2001 From: William Woodall Date: Mon, 21 May 2018 21:16:13 -0700 Subject: [PATCH 2/3] fixup --- CMakeLists.txt | 8 ----- test/test_filesystem.cpp | 68 +++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index daba957f..2c59eb24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,6 @@ endif() set(rcutils_sources src/allocator.c src/cmdline_parser.c - src/concat.c src/error_handling.c src/filesystem.c src/find.c @@ -213,13 +212,6 @@ if(BUILD_TESTING) target_link_libraries(test_find ${PROJECT_NAME}) endif() - rcutils_custom_add_gtest(test_concat - test/test_concat.cpp - ) - if(TARGET test_concat) - target_link_libraries(test_concat ${PROJECT_NAME}) - endif() - rcutils_custom_add_gtest(test_string_array test/test_string_array.cpp ) diff --git a/test/test_filesystem.cpp b/test/test_filesystem.cpp index 988b6470..9a6c2de9 100644 --- a/test/test_filesystem.cpp +++ b/test/test_filesystem.cpp @@ -19,8 +19,10 @@ static char cwd[1024]; +static rcutils_allocator_t g_allocator = rcutils_get_default_allocator(); + TEST(test_filesystem, join_path) { - char * path = rcutils_join_path("foo", "bar"); + char * path = rcutils_join_path("foo", "bar", g_allocator); #ifdef _WIN32 const char * ref_str = "foo\\bar"; #else @@ -33,47 +35,47 @@ TEST(test_filesystem, join_path) { TEST(test_filesystem, exists) { EXPECT_FALSE(rcutils_get_cwd(NULL, 1024)); EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - char * path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_readable_file.txt"); + char * path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_readable_file.txt", g_allocator); EXPECT_TRUE(rcutils_exists(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_folder"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_folder", g_allocator); EXPECT_TRUE(rcutils_exists(path)); } TEST(test_filesystem, is_directory) { EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - char * path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_readable_file.txt"); + char * path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_readable_file.txt", g_allocator); EXPECT_FALSE(rcutils_is_directory(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_folder"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_folder", g_allocator); EXPECT_TRUE(rcutils_is_directory(path)); } TEST(test_filesystem, is_file) { EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - char * path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_readable_file.txt"); + char * path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_readable_file.txt", g_allocator); EXPECT_TRUE(rcutils_is_file(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_folder"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_folder", g_allocator); EXPECT_FALSE(rcutils_is_file(path)); } TEST(test_filesystem, is_readable) { EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - char * path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_readable_file.txt"); + char * path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_readable_file.txt", g_allocator); EXPECT_TRUE(rcutils_is_readable(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_folder"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_folder", g_allocator); EXPECT_TRUE(rcutils_is_readable(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_readable_writable_file.txt"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_readable_writable_file.txt", g_allocator); EXPECT_TRUE(rcutils_is_readable(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_nonexisting_file.txt"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_nonexisting_file.txt", g_allocator); EXPECT_FALSE(rcutils_is_readable(path)); } @@ -82,14 +84,14 @@ TEST(test_filesystem, is_writable) { // path = std::string(cwd) + delimiter + std::string("test") + delimiter + std::string( // "dummy_readable_file.txt"); // EXPECT_FALSE(rcutils_is_writable(path.c_str())); - char * path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_folder"); + char * path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_folder", g_allocator); EXPECT_TRUE(rcutils_is_writable(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_readable_writable_file.txt"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_readable_writable_file.txt", g_allocator); EXPECT_TRUE(rcutils_is_writable(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_nonexisting_file.txt"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_nonexisting_file.txt", g_allocator); EXPECT_FALSE(rcutils_is_writable(path)); } @@ -97,13 +99,13 @@ TEST(test_filesystem, is_readable_and_writable) { EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); // path = std::string(cwd) + std::string("/test/dummy_readable_file.txt"); // EXPECT_FALSE(rcutils_is_readable_and_writable(path.c_str())); - char * path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_folder"); + char * path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_folder", g_allocator); EXPECT_TRUE(rcutils_is_readable_and_writable(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_readable_writable_file.txt"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_readable_writable_file.txt", g_allocator); EXPECT_TRUE(rcutils_is_readable_and_writable(path)); - path = rcutils_join_path(cwd, "test"); - path = rcutils_join_path(path, "dummy_nonexisting_file.txt"); + path = rcutils_join_path(cwd, "test", g_allocator); + path = rcutils_join_path(path, "dummy_nonexisting_file.txt", g_allocator); EXPECT_FALSE(rcutils_is_readable_and_writable(path)); } From 717e0443ee1a87f56f5bfe135f9cf04e9114b11d Mon Sep 17 00:00:00 2001 From: William Woodall Date: Fri, 15 Jun 2018 18:27:39 -0700 Subject: [PATCH 3/3] fix memory leaks in test_filesystem.cpp --- CMakeLists.txt | 1 + test/test_filesystem.cpp | 236 +++++++++++++++++++++++++++------------ 2 files changed, 168 insertions(+), 69 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c59eb24..b51dee35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -233,6 +233,7 @@ if(BUILD_TESTING) test/test_filesystem.cpp ) if(TARGET test_filesystem) + target_include_directories(test_filesystem PUBLIC ${osrf_testing_tools_cpp_INCLUDE_DIRS}) target_link_libraries(test_filesystem ${PROJECT_NAME}) endif() diff --git a/test/test_filesystem.cpp b/test/test_filesystem.cpp index 9a6c2de9..c7a1ac15 100644 --- a/test/test_filesystem.cpp +++ b/test/test_filesystem.cpp @@ -17,95 +17,193 @@ #include "rcutils/filesystem.h" -static char cwd[1024]; +#include "osrf_testing_tools_cpp/scope_exit.hpp" static rcutils_allocator_t g_allocator = rcutils_get_default_allocator(); -TEST(test_filesystem, join_path) { +class TestFilesystemFixture : public ::testing::Test +{ +public: + void SetUp() + { + EXPECT_TRUE(rcutils_get_cwd(this->cwd, sizeof(this->cwd))); + + test_path = rcutils_join_path(this->cwd, "test", g_allocator); + ASSERT_FALSE(nullptr == test_path); + } + + void TearDown() + { + g_allocator.deallocate(test_path, g_allocator.state); + } + + char cwd[1024]; + char * test_path = nullptr; +}; + +TEST_F(TestFilesystemFixture, get_cwd_nullptr) { + EXPECT_FALSE(rcutils_get_cwd(NULL, sizeof(this->cwd))); +} + +TEST_F(TestFilesystemFixture, join_path) { char * path = rcutils_join_path("foo", "bar", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); #ifdef _WIN32 const char * ref_str = "foo\\bar"; #else const char * ref_str = "foo/bar"; #endif // _WIN32 - EXPECT_FALSE(NULL == path); + ASSERT_FALSE(nullptr == path); EXPECT_STREQ(ref_str, path); } -TEST(test_filesystem, exists) { - EXPECT_FALSE(rcutils_get_cwd(NULL, 1024)); - EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - char * path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_readable_file.txt", g_allocator); - EXPECT_TRUE(rcutils_exists(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_folder", g_allocator); - EXPECT_TRUE(rcutils_exists(path)); +TEST_F(TestFilesystemFixture, exists) { + { + char * path = rcutils_join_path(this->test_path, "dummy_readable_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_exists(path)); + } + { + char * path = rcutils_join_path(this->test_path, "dummy_folder", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_exists(path)); + } } -TEST(test_filesystem, is_directory) { - EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - char * path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_readable_file.txt", g_allocator); - EXPECT_FALSE(rcutils_is_directory(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_folder", g_allocator); - EXPECT_TRUE(rcutils_is_directory(path)); +TEST_F(TestFilesystemFixture, is_directory) { + { + char * path = rcutils_join_path(this->test_path, "dummy_readable_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_FALSE(rcutils_is_directory(path)); + } + { + char * path = rcutils_join_path(this->test_path, "dummy_folder", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_directory(path)); + } } -TEST(test_filesystem, is_file) { - EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - char * path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_readable_file.txt", g_allocator); - EXPECT_TRUE(rcutils_is_file(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_folder", g_allocator); - EXPECT_FALSE(rcutils_is_file(path)); +TEST_F(TestFilesystemFixture, is_file) { + { + char * path = rcutils_join_path(this->test_path, "dummy_readable_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_file(path)); + } + { + char * path = rcutils_join_path(this->test_path, "dummy_folder", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_FALSE(rcutils_is_file(path)); + } } -TEST(test_filesystem, is_readable) { - EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - char * path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_readable_file.txt", g_allocator); - EXPECT_TRUE(rcutils_is_readable(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_folder", g_allocator); - EXPECT_TRUE(rcutils_is_readable(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_readable_writable_file.txt", g_allocator); - EXPECT_TRUE(rcutils_is_readable(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_nonexisting_file.txt", g_allocator); - EXPECT_FALSE(rcutils_is_readable(path)); +TEST_F(TestFilesystemFixture, is_readable) { + { + char * path = rcutils_join_path(this->test_path, "dummy_readable_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_readable(path)); + } + { + char * path = rcutils_join_path(this->test_path, "dummy_folder", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_readable(path)); + } + { + char * path = + rcutils_join_path(this->test_path, "dummy_readable_writable_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_readable(path)); + } + { + char * path = rcutils_join_path(this->test_path, "dummy_nonexisting_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_FALSE(rcutils_is_readable(path)); + } } -TEST(test_filesystem, is_writable) { - EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - // path = std::string(cwd) + delimiter + std::string("test") + delimiter + std::string( - // "dummy_readable_file.txt"); - // EXPECT_FALSE(rcutils_is_writable(path.c_str())); - char * path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_folder", g_allocator); - EXPECT_TRUE(rcutils_is_writable(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_readable_writable_file.txt", g_allocator); - EXPECT_TRUE(rcutils_is_writable(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_nonexisting_file.txt", g_allocator); - EXPECT_FALSE(rcutils_is_writable(path)); +TEST_F(TestFilesystemFixture, is_writable) { + { + char * path = rcutils_join_path(this->test_path, "dummy_folder", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_writable(path)); + } + { + char * path = + rcutils_join_path(this->test_path, "dummy_readable_writable_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_writable(path)); + } + { + char * path = rcutils_join_path(this->test_path, "dummy_nonexisting_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_FALSE(rcutils_is_writable(path)); + } } -TEST(test_filesystem, is_readable_and_writable) { - EXPECT_TRUE(rcutils_get_cwd(cwd, 1024)); - // path = std::string(cwd) + std::string("/test/dummy_readable_file.txt"); - // EXPECT_FALSE(rcutils_is_readable_and_writable(path.c_str())); - char * path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_folder", g_allocator); - EXPECT_TRUE(rcutils_is_readable_and_writable(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_readable_writable_file.txt", g_allocator); - EXPECT_TRUE(rcutils_is_readable_and_writable(path)); - path = rcutils_join_path(cwd, "test", g_allocator); - path = rcutils_join_path(path, "dummy_nonexisting_file.txt", g_allocator); - EXPECT_FALSE(rcutils_is_readable_and_writable(path)); +TEST_F(TestFilesystemFixture, is_readable_and_writable) { + { + char * path = rcutils_join_path(this->test_path, "dummy_folder", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_readable_and_writable(path)); + } + { + char * path = + rcutils_join_path(this->test_path, "dummy_readable_writable_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_TRUE(rcutils_is_readable_and_writable(path)); + } + { + char * path = rcutils_join_path(this->test_path, "dummy_nonexisting_file.txt", g_allocator); + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + g_allocator.deallocate(path, g_allocator.state); + }); + ASSERT_FALSE(nullptr == path); + EXPECT_FALSE(rcutils_is_readable_and_writable(path)); + } }