clang-format
This commit is contained in:
@@ -10,13 +10,17 @@ class RealFilesystemTest : public ::testing::Test {
|
|||||||
protected:
|
protected:
|
||||||
void SetUp() override {
|
void SetUp() override {
|
||||||
// Create a unique temporary directory for this test run
|
// Create a unique temporary directory for this test run
|
||||||
temp_dir = std::filesystem::temp_directory_path() /
|
temp_dir =
|
||||||
("finger_test_" + std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(
|
std::filesystem::temp_directory_path() /
|
||||||
std::chrono::high_resolution_clock::now().time_since_epoch()).count()));
|
("finger_test_" +
|
||||||
|
std::to_string(
|
||||||
|
std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||||
|
std::chrono::high_resolution_clock::now().time_since_epoch())
|
||||||
|
.count()));
|
||||||
|
|
||||||
// Create the temporary directory
|
// Create the temporary directory
|
||||||
std::filesystem::create_directories(temp_dir);
|
std::filesystem::create_directories(temp_dir);
|
||||||
|
|
||||||
// Set up test base path within temp directory
|
// Set up test base path within temp directory
|
||||||
test_base_path = temp_dir / "users";
|
test_base_path = temp_dir / "users";
|
||||||
std::filesystem::create_directories(test_base_path);
|
std::filesystem::create_directories(test_base_path);
|
||||||
@@ -30,7 +34,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to create a test file with specified content
|
// Helper method to create a test file with specified content
|
||||||
void createTestFile(const std::string& filename, const std::string& content) {
|
void createTestFile(const std::string &filename, const std::string &content) {
|
||||||
std::filesystem::path file_path = test_base_path / filename;
|
std::filesystem::path file_path = test_base_path / filename;
|
||||||
std::ofstream file(file_path);
|
std::ofstream file(file_path);
|
||||||
if (file.is_open()) {
|
if (file.is_open()) {
|
||||||
@@ -40,13 +44,13 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to create a test directory
|
// Helper method to create a test directory
|
||||||
void createTestDirectory(const std::string& dirname) {
|
void createTestDirectory(const std::string &dirname) {
|
||||||
std::filesystem::path dir_path = test_base_path / dirname;
|
std::filesystem::path dir_path = test_base_path / dirname;
|
||||||
std::filesystem::create_directories(dir_path);
|
std::filesystem::create_directories(dir_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to check if a file exists in the test directory
|
// Helper method to check if a file exists in the test directory
|
||||||
bool testFileExists(const std::string& filename) {
|
bool testFileExists(const std::string &filename) {
|
||||||
return std::filesystem::exists(test_base_path / filename);
|
return std::filesystem::exists(test_base_path / filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +63,7 @@ protected:
|
|||||||
TEST_F(RealFilesystemTest, ExistsMethodWithRealFile) {
|
TEST_F(RealFilesystemTest, ExistsMethodWithRealFile) {
|
||||||
// Create a test file
|
// Create a test file
|
||||||
createTestFile("testuser", "Test content");
|
createTestFile("testuser", "Test content");
|
||||||
|
|
||||||
// Test that exists() returns true for existing file
|
// Test that exists() returns true for existing file
|
||||||
std::filesystem::path file_path = test_base_path / "testuser";
|
std::filesystem::path file_path = test_base_path / "testuser";
|
||||||
EXPECT_TRUE(real_fs.exists(file_path));
|
EXPECT_TRUE(real_fs.exists(file_path));
|
||||||
@@ -75,10 +79,10 @@ TEST_F(RealFilesystemTest, ExistsMethodWithNonExistentFile) {
|
|||||||
TEST_F(RealFilesystemTest, ReadFileWithSimpleContent) {
|
TEST_F(RealFilesystemTest, ReadFileWithSimpleContent) {
|
||||||
std::string expected_content = "Hello, World!";
|
std::string expected_content = "Hello, World!";
|
||||||
createTestFile("simple", expected_content);
|
createTestFile("simple", expected_content);
|
||||||
|
|
||||||
std::filesystem::path file_path = test_base_path / "simple";
|
std::filesystem::path file_path = test_base_path / "simple";
|
||||||
std::string result = real_fs.read_file(file_path);
|
std::string result = real_fs.read_file(file_path);
|
||||||
|
|
||||||
// RealFilesystemWrapper adds \r\n at the end
|
// RealFilesystemWrapper adds \r\n at the end
|
||||||
EXPECT_EQ(result, expected_content + "\r\n");
|
EXPECT_EQ(result, expected_content + "\r\n");
|
||||||
}
|
}
|
||||||
@@ -86,20 +90,20 @@ TEST_F(RealFilesystemTest, ReadFileWithSimpleContent) {
|
|||||||
TEST_F(RealFilesystemTest, ReadFileWithMultilineContent) {
|
TEST_F(RealFilesystemTest, ReadFileWithMultilineContent) {
|
||||||
std::string expected_content = "Line 1\nLine 2\nLine 3";
|
std::string expected_content = "Line 1\nLine 2\nLine 3";
|
||||||
createTestFile("multiline", expected_content);
|
createTestFile("multiline", expected_content);
|
||||||
|
|
||||||
std::filesystem::path file_path = test_base_path / "multiline";
|
std::filesystem::path file_path = test_base_path / "multiline";
|
||||||
std::string result = real_fs.read_file(file_path);
|
std::string result = real_fs.read_file(file_path);
|
||||||
|
|
||||||
// RealFilesystemWrapper processes line by line and adds \r\n
|
// RealFilesystemWrapper processes line by line and adds \r\n
|
||||||
EXPECT_EQ(result, "Line 1\nLine 2\nLine 3\r\n");
|
EXPECT_EQ(result, "Line 1\nLine 2\nLine 3\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(RealFilesystemTest, ReadFileWithEmptyFile) {
|
TEST_F(RealFilesystemTest, ReadFileWithEmptyFile) {
|
||||||
createTestFile("empty", "");
|
createTestFile("empty", "");
|
||||||
|
|
||||||
std::filesystem::path file_path = test_base_path / "empty";
|
std::filesystem::path file_path = test_base_path / "empty";
|
||||||
std::string result = real_fs.read_file(file_path);
|
std::string result = real_fs.read_file(file_path);
|
||||||
|
|
||||||
// Empty files return empty string
|
// Empty files return empty string
|
||||||
EXPECT_EQ(result, "");
|
EXPECT_EQ(result, "");
|
||||||
}
|
}
|
||||||
@@ -107,7 +111,7 @@ TEST_F(RealFilesystemTest, ReadFileWithEmptyFile) {
|
|||||||
TEST_F(RealFilesystemTest, ReadFileNonExistentFile) {
|
TEST_F(RealFilesystemTest, ReadFileNonExistentFile) {
|
||||||
std::filesystem::path file_path = test_base_path / "nonexistent";
|
std::filesystem::path file_path = test_base_path / "nonexistent";
|
||||||
std::string result = real_fs.read_file(file_path);
|
std::string result = real_fs.read_file(file_path);
|
||||||
|
|
||||||
// Non-existent files return empty string
|
// Non-existent files return empty string
|
||||||
EXPECT_EQ(result, "");
|
EXPECT_EQ(result, "");
|
||||||
}
|
}
|
||||||
@@ -116,7 +120,7 @@ TEST_F(RealFilesystemTest, ReadFileNonExistentFile) {
|
|||||||
TEST_F(RealFilesystemTest, ProcessWithExistingUserFile) {
|
TEST_F(RealFilesystemTest, ProcessWithExistingUserFile) {
|
||||||
std::string user_content = "John Doe\nSoftware Engineer\nLoves C++";
|
std::string user_content = "John Doe\nSoftware Engineer\nLoves C++";
|
||||||
createTestFile("johndoe", user_content);
|
createTestFile("johndoe", user_content);
|
||||||
|
|
||||||
std::string result = process("johndoe", real_fs, test_base_path);
|
std::string result = process("johndoe", real_fs, test_base_path);
|
||||||
EXPECT_EQ(result, "John Doe\nSoftware Engineer\nLoves C++\r\n");
|
EXPECT_EQ(result, "John Doe\nSoftware Engineer\nLoves C++\r\n");
|
||||||
}
|
}
|
||||||
@@ -128,22 +132,22 @@ TEST_F(RealFilesystemTest, ProcessWithNonExistentUserFile) {
|
|||||||
|
|
||||||
TEST_F(RealFilesystemTest, ProcessWithEmptyUserFile) {
|
TEST_F(RealFilesystemTest, ProcessWithEmptyUserFile) {
|
||||||
createTestFile("emptyuser", "");
|
createTestFile("emptyuser", "");
|
||||||
|
|
||||||
std::string result = process("emptyuser", real_fs, test_base_path);
|
std::string result = process("emptyuser", real_fs, test_base_path);
|
||||||
EXPECT_EQ(result, "emptyuser");
|
EXPECT_EQ(result, "emptyuser");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(RealFilesystemTest, ProcessWithComplexUserInfo) {
|
TEST_F(RealFilesystemTest, ProcessWithComplexUserInfo) {
|
||||||
std::string user_content = "Name: Alice Smith\n"
|
std::string user_content = "Name: Alice Smith\n"
|
||||||
"Title: Senior Developer\n"
|
"Title: Senior Developer\n"
|
||||||
"Department: Engineering\n"
|
"Department: Engineering\n"
|
||||||
"Office: Building A, Room 123\n"
|
"Office: Building A, Room 123\n"
|
||||||
"Phone: +1-555-0123\n"
|
"Phone: +1-555-0123\n"
|
||||||
"Email: [email protected]\n"
|
"Email: [email protected]\n"
|
||||||
"Projects: Project Alpha, Project Beta\n"
|
"Projects: Project Alpha, Project Beta\n"
|
||||||
"Skills: C++, Python, JavaScript";
|
"Skills: C++, Python, JavaScript";
|
||||||
createTestFile("alice", user_content);
|
createTestFile("alice", user_content);
|
||||||
|
|
||||||
std::string result = process("alice", real_fs, test_base_path);
|
std::string result = process("alice", real_fs, test_base_path);
|
||||||
EXPECT_EQ(result, user_content + "\r\n");
|
EXPECT_EQ(result, user_content + "\r\n");
|
||||||
}
|
}
|
||||||
@@ -152,7 +156,7 @@ TEST_F(RealFilesystemTest, ProcessWithComplexUserInfo) {
|
|||||||
TEST_F(RealFilesystemTest, ProcessWithSingleLineFile) {
|
TEST_F(RealFilesystemTest, ProcessWithSingleLineFile) {
|
||||||
std::string user_content = "Simple one-line user info";
|
std::string user_content = "Simple one-line user info";
|
||||||
createTestFile("simpleuser", user_content);
|
createTestFile("simpleuser", user_content);
|
||||||
|
|
||||||
std::string result = process("simpleuser", real_fs, test_base_path);
|
std::string result = process("simpleuser", real_fs, test_base_path);
|
||||||
EXPECT_EQ(result, user_content + "\r\n");
|
EXPECT_EQ(result, user_content + "\r\n");
|
||||||
}
|
}
|
||||||
@@ -160,10 +164,10 @@ TEST_F(RealFilesystemTest, ProcessWithSingleLineFile) {
|
|||||||
TEST_F(RealFilesystemTest, ProcessWithFileContainingOnlyNewlines) {
|
TEST_F(RealFilesystemTest, ProcessWithFileContainingOnlyNewlines) {
|
||||||
std::string user_content = "\n\n\n";
|
std::string user_content = "\n\n\n";
|
||||||
createTestFile("newlineuser", user_content);
|
createTestFile("newlineuser", user_content);
|
||||||
|
|
||||||
std::string result = process("newlineuser", real_fs, test_base_path);
|
std::string result = process("newlineuser", real_fs, test_base_path);
|
||||||
// RealFilesystemWrapper reads line by line, so the last empty line after the final \n
|
// RealFilesystemWrapper reads line by line, so the last empty line after the
|
||||||
// is not read as a separate line, resulting in "\n\n\r\n"
|
// final \n is not read as a separate line, resulting in "\n\n\r\n"
|
||||||
EXPECT_EQ(result, "\n\n\r\n");
|
EXPECT_EQ(result, "\n\n\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,7 +178,7 @@ TEST_F(RealFilesystemTest, ProcessDirectoryTraversalProtectionWithRealFS) {
|
|||||||
std::ofstream secret_file(outside_file);
|
std::ofstream secret_file(outside_file);
|
||||||
secret_file << "Secret content";
|
secret_file << "Secret content";
|
||||||
secret_file.close();
|
secret_file.close();
|
||||||
|
|
||||||
// Try to access it via directory traversal
|
// Try to access it via directory traversal
|
||||||
std::string result = process("../secret", real_fs, test_base_path);
|
std::string result = process("../secret", real_fs, test_base_path);
|
||||||
EXPECT_TRUE(result.find("InvalidInput:") == 0);
|
EXPECT_TRUE(result.find("InvalidInput:") == 0);
|
||||||
@@ -186,13 +190,13 @@ TEST_F(RealFilesystemTest, ProcessWithCustomBasePath) {
|
|||||||
// Create a different base directory
|
// Create a different base directory
|
||||||
std::filesystem::path custom_base = temp_dir / "custom_users";
|
std::filesystem::path custom_base = temp_dir / "custom_users";
|
||||||
std::filesystem::create_directories(custom_base);
|
std::filesystem::create_directories(custom_base);
|
||||||
|
|
||||||
// Create a user file in the custom base
|
// Create a user file in the custom base
|
||||||
std::filesystem::path user_file = custom_base / "customuser";
|
std::filesystem::path user_file = custom_base / "customuser";
|
||||||
std::ofstream file(user_file);
|
std::ofstream file(user_file);
|
||||||
file << "Custom base path user";
|
file << "Custom base path user";
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
std::string result = process("customuser", real_fs, custom_base);
|
std::string result = process("customuser", real_fs, custom_base);
|
||||||
EXPECT_EQ(result, "Custom base path user\r\n");
|
EXPECT_EQ(result, "Custom base path user\r\n");
|
||||||
}
|
}
|
||||||
@@ -201,10 +205,10 @@ TEST_F(RealFilesystemTest, ProcessWithCustomBasePath) {
|
|||||||
TEST_F(RealFilesystemTest, ReadFileWithSpecialCharacters) {
|
TEST_F(RealFilesystemTest, ReadFileWithSpecialCharacters) {
|
||||||
std::string special_content = "User with special chars: àáâãäåæçèéêë";
|
std::string special_content = "User with special chars: àáâãäåæçèéêë";
|
||||||
createTestFile("specialuser", special_content);
|
createTestFile("specialuser", special_content);
|
||||||
|
|
||||||
std::filesystem::path file_path = test_base_path / "specialuser";
|
std::filesystem::path file_path = test_base_path / "specialuser";
|
||||||
std::string result = real_fs.read_file(file_path);
|
std::string result = real_fs.read_file(file_path);
|
||||||
|
|
||||||
EXPECT_EQ(result, special_content + "\r\n");
|
EXPECT_EQ(result, special_content + "\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user