summaryrefslogtreecommitdiff
path: root/test/test_common.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_common.cpp')
-rw-r--r--test/test_common.cpp60
1 files changed, 56 insertions, 4 deletions
diff --git a/test/test_common.cpp b/test/test_common.cpp
index d3e1704..784cb0d 100644
--- a/test/test_common.cpp
+++ b/test/test_common.cpp
@@ -16,7 +16,12 @@
*/
#include "test_common.hpp"
+
+#include "fud_string.hpp"
+
#include <cstdlib>
+#include <ftw.h>
+#include <gtest/gtest.h>
namespace fud {
@@ -34,22 +39,69 @@ MockFudAlloc globalDefaultMockAlloc{};
MockFudDealloc globalDefaultMockDealloc{};
-void* MockFudAllocator::allocate(size_t size) {
+void* MockFudAllocator::allocate(size_t size)
+{
return (*m_allocator)(size);
}
-void MockFudAllocator::deallocate(void* pointer) {
+void MockFudAllocator::deallocate(void* pointer)
+{
return (*m_deallocator)(pointer);
}
MockFudAllocator globalMockFudAlloc{};
-void* fudAlloc(size_t size) {
+void* fudAlloc(size_t size)
+{
return globalMockFudAlloc.allocate(size);
}
-void fudFree(void* ptr) {
+void fudFree(void* ptr)
+{
return globalMockFudAlloc.deallocate(ptr);
}
+int unlink_cb(const char* fpath, const struct stat* sb_unused, int typeflag, struct FTW* ftwbuf)
+{
+ static_cast<void>(sb_unused);
+ int retValue = remove(fpath);
+
+ EXPECT_EQ(retValue, 0);
+ if (retValue != 0) {
+ perror(fpath);
+ }
+
+ return retValue;
+}
+
+FudStatus removeRecursive(const String& path)
+{
+ if (!path.utf8Valid()) {
+ return FudStatus::Utf8Invalid;
+ }
+ if (path.length() < 5) {
+ return FudStatus::ArgumentInvalid;
+ }
+ auto prefix{String::makeFromCString("/tmp/").takeOkay()};
+ auto diffResult = compareMem(path.data(), path.length(), prefix.data(), prefix.length());
+ if (diffResult.isError()) {
+ return FudStatus::ArgumentInvalid;
+ }
+ auto diff = diffResult.getOkay();
+ if (diff != 0) {
+ return FudStatus::ArgumentInvalid;
+ }
+ constexpr int maxOpenFd = 64;
+ auto status = nftw(path.c_str(), unlink_cb, maxOpenFd, FTW_DEPTH | FTW_PHYS);
+ if (status == 0) {
+ return FudStatus::Success;
+ }
+
+ if (errno == ENOENT) {
+ return FudStatus::Success;
+ }
+
+ return FudStatus::Failure;
+}
+
} // namespace fud