Skip to content

Commit

Permalink
Run clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
SterlingT3485 committed Nov 21, 2024
1 parent 3572b8a commit dac964e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
7 changes: 3 additions & 4 deletions src/common/file_system/local_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void LocalFileSystem::createDir(const std::string& dir) const {
auto directoryToCreate = dir;
if (directoryToCreate.ends_with('/')
#if defined(_WIN32)
|| directoryToCreate.ends_with('\\')
|| directoryToCreate.ends_with('\\')
#endif
) {
// This is a known issue with std::filesystem::create_directories. (link:
Expand Down Expand Up @@ -261,7 +261,7 @@ bool isSubdirectory(const std::filesystem::path& base, const std::filesystem::pa
// Size check for a "." result.
// If the path starts with "..", it's not a subdirectory.
return !relative.empty() && !(relative.starts_with(".."));

} catch (const std::filesystem::filesystem_error& e) {
// Handle errors, e.g., if paths don't exist
std::cerr << "Filesystem error: " << e.what() << std::endl;
Expand All @@ -271,9 +271,8 @@ bool isSubdirectory(const std::filesystem::path& base, const std::filesystem::pa
return false;
}


void LocalFileSystem::removeFileIfExists(const std::string& path) {
std::cout<<"home:"<<<<"path:"<<canonicalSub<<std::endl;
std::cout << "home:" < < < < "path:" << canonicalSub << std::endl;
if (!fileOrPathExists(path)) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ std::string expandPath(main::ClientContext* context, const std::string& path) {
// Normalize the path to resolve '.' and '..'
std::filesystem::path normalizedPath = std::filesystem::absolute(fullPath).lexically_normal();
return normalizedPath.string();

}

void Database::initMembers(std::string_view dbPath, construct_bm_func_t initBmFunc) {
Expand Down
44 changes: 28 additions & 16 deletions test/c_api/database_test.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <fstream>

#include "c_api/kuzu.h"
#include "common/exception/io.h"
#include "common/file_system/virtual_file_system.h"
#include "graph_test/api_graph_test.h"
#include "gtest/gtest.h"
#include "common/file_system/virtual_file_system.h"
#include "common/exception/io.h"
#include <fstream>

using namespace kuzu::main;
using namespace kuzu::testing;
Expand Down Expand Up @@ -115,7 +116,8 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFiles) {
vfs.removeFileIfExists("/tmp/test1");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/test1 is not within the allowed home directory /tmp/dbHome/");
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/test1 is not within the allowed "
"home directory /tmp/dbHome/");
}

vfs.removeFileIfExists("/tmp/dbHome/test1");
Expand All @@ -140,15 +142,17 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWithHome) {
vfs.removeFileIfExists("~/tmp/test1");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path ~/tmp/test1 is not within the allowed home directory ~/tmp/dbHome/");
EXPECT_STREQ(e.what(), "IO exception: Error: Path ~/tmp/test1 is not within the allowed "
"home directory ~/tmp/dbHome/");
}

// Attempt to delete files outside the home directory (should error)
try {
vfs.removeFileIfExists("~");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path ~ is not within the allowed home directory ~/tmp/dbHome/");
EXPECT_STREQ(e.what(),
"IO exception: Error: Path ~ is not within the allowed home directory ~/tmp/dbHome/");
}

vfs.removeFileIfExists("~/tmp/dbHome/test1");
Expand All @@ -174,42 +178,48 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesEdgeCases) {
vfs.removeFileIfExists("/tmp/dbHome/../test2");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/dbHome/../test2 is not within the allowed home directory /tmp/dbHome/");
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/dbHome/../test2 is not within the "
"allowed home directory /tmp/dbHome/");
}

try {
vfs.removeFileIfExists("/tmp");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp is not within the allowed home directory /tmp/dbHome/");
EXPECT_STREQ(e.what(),
"IO exception: Error: Path /tmp is not within the allowed home directory /tmp/dbHome/");
}

try {
vfs.removeFileIfExists("/tmp/");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/ is not within the allowed home directory /tmp/dbHome/");
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/ is not within the allowed home "
"directory /tmp/dbHome/");
}

try {
vfs.removeFileIfExists("/tmp//////////////////");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp////////////////// is not within the allowed home directory /tmp/dbHome/");
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp////////////////// is not within the "
"allowed home directory /tmp/dbHome/");
}

try {
vfs.removeFileIfExists("/tmp/./.././");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/./.././ is not within the allowed home directory /tmp/dbHome/");
EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/./.././ is not within the allowed "
"home directory /tmp/dbHome/");
}

try {
vfs.removeFileIfExists("/");
} catch (const kuzu::common::IOException& e) {
// Expected behavior
EXPECT_STREQ(e.what(), "IO exception: Error: Path / is not within the allowed home directory /tmp/dbHome/");
EXPECT_STREQ(e.what(),
"IO exception: Error: Path / is not within the allowed home directory /tmp/dbHome/");
}

vfs.removeFileIfExists("/tmp/dbHome/test2");
Expand Down Expand Up @@ -240,7 +250,8 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWindowsPaths) {
vfs.removeFileIfExists("C:\\test1");
FAIL() << "Expected exception for path outside home directory.";
} catch (const kuzu::common::IOException& e) {
EXPECT_STREQ(e.what(), "IO exception: Error: Path C:\\test1 is not within the allowed home directory C:\\Desktop\\dir");
EXPECT_STREQ(e.what(), "IO exception: Error: Path C:\\test1 is not within the allowed home "
"directory C:\\Desktop\\dir");
}

// Attempt to delete file inside the home directory with mixed separators (should succeed)
Expand Down Expand Up @@ -274,8 +285,8 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWildcard) {
vfs.removeFileIfExists("/tmp/dbHome_wildcard/test*");
ASSERT_FALSE(std::filesystem::exists("/tmp/dbHome_wildcard/test1_wildcard"));
ASSERT_FALSE(std::filesystem::exists("/tmp/dbHome_wildcard/test2_wildcard"));
ASSERT_TRUE(std::filesystem::exists("/tmp/dbHome_wildcard/nested_wildcard/file2.test")); // Deleted
ASSERT_TRUE(std::filesystem::exists("/tmp/dbHome_wildcard/nested_wildcard/file1.txt"));
ASSERT_TRUE(std::filesystem::exists("/tmp/dbHome_wildcard/nested_wildcard/file2.test")); //
Deleted ASSERT_TRUE(std::filesystem::exists("/tmp/dbHome_wildcard/nested_wildcard/file1.txt"));
// Wildcard pattern: Delete all files in nested directory
vfs.removeFileIfExists("/tmp/dbHome_wildcard/nested_wildcard/*");
Expand All @@ -287,7 +298,8 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWildcard) {
vfs.removeFileIfExists("/tmp/*.test");
FAIL() << "Expected exception for wildcard outside home directory.";
} catch (const kuzu::common::IOException& e) {
EXPECT_STREQ(e.what(), "Error: Path /tmp/*.test is not within the allowed home directory /tmp/dbHome_wildcard/");
EXPECT_STREQ(e.what(), "Error: Path /tmp/*.test is not within the allowed home directory
/tmp/dbHome_wildcard/");
}
// Cleanup
Expand Down

0 comments on commit dac964e

Please sign in to comment.