From 3572b8adba61b2be8bd19acb48f321a6fa685dc2 Mon Sep 17 00:00:00 2001 From: sterling Date: Thu, 21 Nov 2024 12:29:47 -0500 Subject: [PATCH] test windows --- src/common/file_system/local_file_system.cpp | 7 +- test/c_api/database_test.cpp | 150 ++++++++++++++++++- 2 files changed, 152 insertions(+), 5 deletions(-) diff --git a/src/common/file_system/local_file_system.cpp b/src/common/file_system/local_file_system.cpp index f66322acdb..aacd95b44f 100644 --- a/src/common/file_system/local_file_system.cpp +++ b/src/common/file_system/local_file_system.cpp @@ -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) @@ -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:"< using namespace kuzu::main; using namespace kuzu::testing; @@ -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"); @@ -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")); @@ -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"); +} + +*/ \ No newline at end of file