Skip to content

Commit

Permalink
test windows
Browse files Browse the repository at this point in the history
  • Loading branch information
SterlingT3485 committed Nov 21, 2024
1 parent e488e21 commit 3572b8a
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/common/file_system/local_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ void LocalFileSystem::createDir(const std::string& dir) const {
// LCOV_EXCL_STOP
}
auto directoryToCreate = dir;
if (directoryToCreate.ends_with('/') || directoryToCreate.ends_with('\\')) {
if (directoryToCreate.ends_with('/')
#if defined(_WIN32)
|| directoryToCreate.ends_with('\\')
#endif
) {
// This is a known issue with std::filesystem::create_directories. (link:
// https://github.com/llvm/llvm-project/issues/60634). We have to manually remove the
// last '/' if the path ends with '/'. (Added the second one for windows)
Expand Down Expand Up @@ -269,6 +273,7 @@ bool isSubdirectory(const std::filesystem::path& base, const std::filesystem::pa


void LocalFileSystem::removeFileIfExists(const std::string& path) {
std::cout<<"home:"<<<<"path:"<<canonicalSub<<std::endl;
if (!fileOrPathExists(path)) {
return;
}
Expand Down
150 changes: 146 additions & 4 deletions test/c_api/database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#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 @@ -127,15 +128,47 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFiles) {
std::filesystem::remove_all("/tmp/dbHome/test1");
}

TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesEdge) {
#ifndef __WASM__ // home directory is not available in WASM
TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWithHome) {
std::string homeDir = "~/tmp/dbHome/";
kuzu::common::VirtualFileSystem vfs(homeDir);
std::filesystem::create_directories("~/tmp/test1");
std::filesystem::create_directories("~/tmp/dbHome/test1");

// Attempt to delete files outside the home directory (should error)
try {
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/");
}

// 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/");
}

vfs.removeFileIfExists("~/tmp/dbHome/test1");

ASSERT_TRUE(std::filesystem::exists("~/tmp/test1"));
ASSERT_FALSE(std::filesystem::exists("~/tmp/dbHome/test1"));

// Cleanup: Remove directories after the test
std::filesystem::remove_all("~/tmp/test1");
std::filesystem::remove_all("~/tmp/dbHome/test1");
}
#endif

TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesEdgeCases) {
std::string homeDir = "/tmp/dbHome/";
kuzu::common::VirtualFileSystem vfs(homeDir);
std::filesystem::create_directories("/tmp/dbHome/../test2");
std::filesystem::create_directories("/tmp");
std::filesystem::create_directories("/tmp/dbHome/test2");

std::cout << "Resolved Path: " << std::filesystem::absolute("/tmp/dbHome/../test2") << std::endl;
std::cout << "Exists: " << std::filesystem::exists("/tmp/test2") << std::endl;

// Attempt to delete files outside the home directory (should error)
try {
vfs.removeFileIfExists("/tmp/dbHome/../test2");
Expand All @@ -144,6 +177,41 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesEdge) {
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/");
}

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/");
}

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/");
}

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/");
}

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/");
}

vfs.removeFileIfExists("/tmp/dbHome/test2");

ASSERT_TRUE(std::filesystem::exists("/tmp/test2"));
Expand All @@ -153,3 +221,77 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesEdge) {
std::filesystem::remove_all("/tmp/test2");
std::filesystem::remove_all("/tmp/dbHome/test2");
}

#if defined(_WIN32)
TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWindowsPaths) {
// Test Home Directory
std::string homeDir = "C:\\Desktop\\dir";
kuzu::common::VirtualFileSystem vfs(homeDir);

// Setup directories for testing
std::filesystem::create_directories("C:\\test1");
std::filesystem::create_directories("C:\\Desktop\\dir\\test1");

// Mixed separators: HomeDir uses '\' while path uses '/'
std::string mixedSeparatorPath = "C:\\Desktop/dir/test1";

// Attempt to delete files outside the home directory (should error)
try {
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");
}

// Attempt to delete file inside the home directory with mixed separators (should succeed)
try {
vfs.removeFileIfExists(mixedSeparatorPath);
} catch (const kuzu::common::IOException& e) {
FAIL() << "Unexpected exception when deleting files: " << e.what();
}

ASSERT_FALSE(std::filesystem::exists("C:\\Desktop\\dir\\test1")); // Should be deleted

// Cleanup
std::filesystem::remove_all("C:\\test1");
std::filesystem::remove_all("C:\\Desktop\\dir\\test1");
}
#endif
/*
TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWildcard) {
// Test Home Directory
std::string homeDir = "/tmp/dbHome_wildcard/";
kuzu::common::VirtualFileSystem vfs(homeDir);
// Setup files and directories
std::filesystem::create_directories("/tmp/dbHome_wildcard/test1_wildcard");
std::filesystem::create_directories("/tmp/dbHome_wildcard/test2_wildcard");
std::filesystem::create_directories("/tmp/dbHome_wildcard/nested_wildcard");
std::ofstream("/tmp/dbHome_wildcard/nested_wildcard/file1.txt").close();
std::ofstream("/tmp/dbHome_wildcard/nested_wildcard/file2.test").close();
// Wildcard pattern: Delete all matching files in home directory
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"));
// Wildcard pattern: Delete all files in nested directory
vfs.removeFileIfExists("/tmp/dbHome_wildcard/nested_wildcard/*");
ASSERT_FALSE(std::filesystem::exists("/tmp/dbHome_wildcard/nested_wildcard/file1.txt"));
ASSERT_FALSE(std::filesystem::exists("/tmp/dbHome_wildcard/nested_wildcard/file2.test"));
// Wildcard pattern outside home directory: Should throw
try {
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/");
}
// Cleanup
std::filesystem::remove_all("/tmp/dbHome_wildcard");
}
*/

0 comments on commit 3572b8a

Please sign in to comment.