Skip to content

Commit

Permalink
Added test for empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed Mar 1, 2024
1 parent 7ed82e4 commit a858f5e
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions tests/fineftp_test/src/fineftp_stresstest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,85 @@ TEST(FineFTPTest, SimpleUploadDownload) {
}
#endif

#if 1
TEST(FineFTPTest, EmptyFile)
{
const auto test_working_dir = std::filesystem::current_path();
const auto ftp_root_dir = test_working_dir / "ftp_root";
const auto local_root_dir = test_working_dir / "local_root";

if (std::filesystem::exists(ftp_root_dir))
std::filesystem::remove_all(ftp_root_dir);

if (std::filesystem::exists(local_root_dir))
std::filesystem::remove_all(local_root_dir);

// Make sure that we start clean, so no old dir exists
ASSERT_FALSE(std::filesystem::exists(ftp_root_dir));
ASSERT_FALSE(std::filesystem::exists(local_root_dir));

std::filesystem::create_directory(ftp_root_dir);
std::filesystem::create_directory(local_root_dir);

// Make sure that we were able to create the dir
ASSERT_TRUE(std::filesystem::is_directory(ftp_root_dir));
ASSERT_TRUE(std::filesystem::is_directory(local_root_dir));

fineftp::FtpServer server(2121);
server.start(1);

server.addUserAnonymous(ftp_root_dir.string(), fineftp::Permission::All);

// Create an empty file in the local root dir
auto local_file = local_root_dir / "empty_file.txt";
std::ofstream ofs(local_file.string());
ofs.close();

// Make sure that the file exists
ASSERT_TRUE(std::filesystem::exists(local_file));
ASSERT_TRUE(std::filesystem::is_regular_file(local_file));

// Upload the file to the FTP server using curl
{
const std::string curl_command = "curl -S -s -T \"" + local_file.string() + "\" \"ftp://localhost:2121/\"";
const auto curl_result = std::system(curl_command.c_str());

// Make sure that the upload was successful
ASSERT_EQ(curl_result, 0);

// Make sure that the file exists in the FTP root dir
auto ftp_file = ftp_root_dir / "empty_file.txt";
ASSERT_TRUE(std::filesystem::exists(ftp_file));
ASSERT_TRUE(std::filesystem::is_regular_file(ftp_file));

// Make sure that the file is empty
std::ifstream ifs(ftp_file.string());
const std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
ASSERT_TRUE(content.empty());
}

// Download the file again
{
const std::string curl_command_download = "curl -S -s -o \"" + local_root_dir.string() + "/empty_file_download.txt\" \"ftp://localhost:2121/empty_file.txt\"";
const auto curl_result = std::system(curl_command_download.c_str());
ASSERT_EQ(curl_result, 0);

// Make sure that the files are identical
auto local_file_download = local_root_dir / "empty_file_download.txt";

ASSERT_TRUE(std::filesystem::exists(local_file_download));
ASSERT_TRUE(std::filesystem::is_regular_file(local_file_download));

std::ifstream ifs(local_file_download.string());
const std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
ASSERT_TRUE(content.empty());
}


// Stop the server
}
#endif

#if 1
TEST(FineFTPTest, BigFilesMultipleClients)
{
Expand Down

0 comments on commit a858f5e

Please sign in to comment.