Skip to content

Commit

Permalink
system_tests: desperate attempts against segfaults
Browse files Browse the repository at this point in the history
It turns out stack smashing seems to happen in create_temp_file, for
whatever reason. This is a very desperate attempt to try to avoid it.

Signed-off-by: Julian Oes <[email protected]>
  • Loading branch information
julianoes committed Oct 20, 2023
1 parent 6a747bb commit 97a6d62
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 85 deletions.
21 changes: 11 additions & 10 deletions src/system_tests/fs_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
#include <iterator>
#include <filesystem>
#include <fstream>
#include <vector>
#include <cstring>

bool create_temp_file(const fs::path& path, size_t len, uint8_t start)
{
std::vector<uint8_t> data;
data.reserve(len);
const auto parent_path = path.parent_path();
create_directories(parent_path);

for (size_t i = 0; i < len; ++i) {
data.push_back(static_cast<uint8_t>((i + start) % 256));
std::ofstream tempfile{};
tempfile.open(path, std::ios::out | std::ios::binary | std::ios::trunc);
if (tempfile.fail()) {
std::cout << "Failed to open temp file.";
return false;
}

const auto parent_path = path.parent_path();
create_directories(parent_path);
for (size_t i = 0; i < len; ++i) {
const char c = (i + start) % 256;
tempfile.write(&c, 1);
}

std::ofstream tempfile;
tempfile.open(path, std::ios::out | std::ios::binary);
tempfile.write((const char*)data.data(), data.size());
tempfile.close();

return true;
Expand Down
10 changes: 5 additions & 5 deletions src/system_tests/ftp_compare_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ static const fs::path temp_file_different = "rhubarb.bin";

TEST(SystemTest, FtpCompareFiles)
{
ASSERT_TRUE(reset_directories(temp_dir_provided));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 1000));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file_same, 1000));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file_different, 1000, 42));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -46,11 +51,6 @@ TEST(SystemTest, FtpCompareFiles)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(reset_directories(temp_dir_provided));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 1000));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file_same, 1000));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file_different, 1000, 42));

auto ftp = Ftp{system};

// First we try to compare the file without the root directory set.
Expand Down
30 changes: 15 additions & 15 deletions src/system_tests/ftp_download_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ static const fs::path temp_file = "data.bin";

TEST(SystemTest, FtpDownloadFile)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -45,9 +48,6 @@ TEST(SystemTest, FtpDownloadFile)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

// First we try to access the file without the root directory set.
Expand Down Expand Up @@ -96,6 +96,9 @@ TEST(SystemTest, FtpDownloadFile)

TEST(SystemTest, FtpDownloadBigFile)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -121,9 +124,6 @@ TEST(SystemTest, FtpDownloadBigFile)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

auto prom = std::promise<Ftp::Result>();
Expand Down Expand Up @@ -151,6 +151,9 @@ TEST(SystemTest, FtpDownloadBigFile)

TEST(SystemTest, FtpDownloadBigFileLossy)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 10000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand Down Expand Up @@ -182,9 +185,6 @@ TEST(SystemTest, FtpDownloadBigFileLossy)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 10000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

auto prom = std::promise<Ftp::Result>();
Expand Down Expand Up @@ -217,6 +217,9 @@ TEST(SystemTest, FtpDownloadBigFileLossy)

TEST(SystemTest, FtpDownloadStopAndTryAgain)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 1000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand Down Expand Up @@ -249,9 +252,6 @@ TEST(SystemTest, FtpDownloadStopAndTryAgain)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 1000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

auto prom = std::promise<Ftp::Result>();
Expand Down Expand Up @@ -306,6 +306,9 @@ TEST(SystemTest, FtpDownloadStopAndTryAgain)

TEST(SystemTest, FtpDownloadFileOutsideOfRoot)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -329,9 +332,6 @@ TEST(SystemTest, FtpDownloadFileOutsideOfRoot)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

// Now we set the root dir and expect it to work.
Expand Down
30 changes: 15 additions & 15 deletions src/system_tests/ftp_download_file_burst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ static const fs::path temp_file = "data.bin";

TEST(SystemTest, FtpDownloadBurstFile)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -45,9 +48,6 @@ TEST(SystemTest, FtpDownloadBurstFile)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

// First we try to access the file without the root directory set.
Expand Down Expand Up @@ -96,6 +96,9 @@ TEST(SystemTest, FtpDownloadBurstFile)

TEST(SystemTest, FtpDownloadBurstBigFile)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -121,9 +124,6 @@ TEST(SystemTest, FtpDownloadBurstBigFile)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

auto prom = std::promise<Ftp::Result>();
Expand Down Expand Up @@ -151,6 +151,9 @@ TEST(SystemTest, FtpDownloadBurstBigFile)

TEST(SystemTest, FtpDownloadBurstBigFileLossy)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 10000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand Down Expand Up @@ -182,9 +185,6 @@ TEST(SystemTest, FtpDownloadBurstBigFileLossy)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 10000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

auto prom = std::promise<Ftp::Result>();
Expand Down Expand Up @@ -217,6 +217,9 @@ TEST(SystemTest, FtpDownloadBurstBigFileLossy)

TEST(SystemTest, FtpDownloadBurstStopAndTryAgain)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 1000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand Down Expand Up @@ -249,9 +252,6 @@ TEST(SystemTest, FtpDownloadBurstStopAndTryAgain)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 1000));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

auto prom = std::promise<Ftp::Result>();
Expand Down Expand Up @@ -306,6 +306,9 @@ TEST(SystemTest, FtpDownloadBurstStopAndTryAgain)

TEST(SystemTest, FtpDownloadBurstFileOutsideOfRoot)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -329,9 +332,6 @@ TEST(SystemTest, FtpDownloadBurstFileOutsideOfRoot)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));
ASSERT_TRUE(reset_directories(temp_dir_downloaded));

auto ftp = Ftp{system};

// Now we set the root dir and expect it to work.
Expand Down
33 changes: 16 additions & 17 deletions src/system_tests/ftp_list_dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ static const std::string temp_file = "file";

TEST(SystemTest, FtpListDir)
{
ASSERT_TRUE(reset_directories(temp_dir_provided));

std::vector<std::string> truth_list;

for (unsigned i = 0; i < 100; ++i) {
auto foldername = std::string(temp_dir + std::to_string(i));
auto filename = std::string(temp_file + std::to_string(i));
ASSERT_TRUE(reset_directories(temp_dir_provided / fs::path(foldername)));
ASSERT_TRUE(create_temp_file(temp_dir_provided / fs::path(filename), i));

truth_list.push_back(std::string("D") + foldername);
truth_list.push_back(std::string("F") + filename + std::string("\t") + std::to_string(i));
}

std::sort(truth_list.begin(), truth_list.end());

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -44,23 +60,6 @@ TEST(SystemTest, FtpListDir)
auto system = maybe_system.value();

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(reset_directories(temp_dir_provided));

std::vector<std::string> truth_list;

for (unsigned i = 0; i < 100; ++i) {
auto foldername = std::string(temp_dir + std::to_string(i));
auto filename = std::string(temp_file + std::to_string(i));
ASSERT_TRUE(reset_directories(temp_dir_provided / fs::path(foldername)));
ASSERT_TRUE(create_temp_file(temp_dir_provided / fs::path(filename), i));

truth_list.push_back(std::string("D") + foldername);
truth_list.push_back(std::string("F") + filename + std::string("\t") + std::to_string(i));
}

std::sort(truth_list.begin(), truth_list.end());

auto ftp = Ftp{system};

// First we try to list a folder without the root directory set.
Expand Down
6 changes: 3 additions & 3 deletions src/system_tests/ftp_remove_dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ TEST(SystemTest, FtpRemoveDir)

TEST(SystemTest, FtpRemoveDirNotEmpty)
{
ASSERT_TRUE(reset_directories(temp_dir_provided / temp_dir));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_dir / temp_file, 100));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -85,9 +88,6 @@ TEST(SystemTest, FtpRemoveDirNotEmpty)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(reset_directories(temp_dir_provided / temp_dir));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_dir / temp_file, 100));

auto ftp = Ftp{system};

// Now we set the root dir and expect it to work.
Expand Down
4 changes: 2 additions & 2 deletions src/system_tests/ftp_remove_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ static const fs::path temp_file = "data.bin";

TEST(SystemTest, FtpRemoveFile)
{
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -44,8 +46,6 @@ TEST(SystemTest, FtpRemoveFile)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));

auto ftp = Ftp{system};

// First we try to remove the file without the root directory set.
Expand Down
6 changes: 3 additions & 3 deletions src/system_tests/ftp_rename_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ static const fs::path temp_file_renamed = "rhubarb.bin";

TEST(SystemTest, FtpRenameFile)
{
ASSERT_TRUE(reset_directories(temp_dir_provided));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));

Mavsdk mavsdk_groundstation;
mavsdk_groundstation.set_configuration(
Mavsdk::Configuration{Mavsdk::Configuration::UsageType::GroundStation});
Expand All @@ -45,9 +48,6 @@ TEST(SystemTest, FtpRenameFile)

ASSERT_TRUE(system->has_autopilot());

ASSERT_TRUE(reset_directories(temp_dir_provided));
ASSERT_TRUE(create_temp_file(temp_dir_provided / temp_file, 50));

auto ftp = Ftp{system};

// First we try to rename the file without the root directory set.
Expand Down
Loading

0 comments on commit 97a6d62

Please sign in to comment.