Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Random UT Failure: PosixFile (#2835) #2860

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Os/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ File::Status File::open(const CHAR* filepath, File::Mode requested_mode, File::O
if (status == File::Status::OP_OK) {
this->m_mode = requested_mode;
this->m_path = filepath;
// Reset any open CRC calculations
this->m_crc = File::INITIAL_CRC;
}
// Reset any open CRC calculations
this->m_crc = File::INITIAL_CRC;

return status;
}

Expand Down
38 changes: 35 additions & 3 deletions Os/test/ut/file/FileRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// \title Os/test/ut/file/MyRules.cpp
// \brief rule implementations for common testing
// ======================================================================

#include <cstdio>
#include "RulesHeaders.hpp"
#include "STest/Pick/Pick.hpp"
extern "C" {
Expand All @@ -20,8 +20,8 @@ Os::File::Status Os::Test::File::Tester::shadow_open(const std::string &path, Os
this->m_independent_crc = Os::File::INITIAL_CRC;
} else {
this->m_current_path.clear();
this->m_mode = Os::File::Mode::OPEN_NO_MODE;
}

return status;
}

Expand Down Expand Up @@ -127,6 +127,8 @@ void Os::Test::File::Tester::assert_valid_mode_status(Os::File::Status &status)
void Os::Test::File::Tester::assert_file_consistent() {
// Ensure file mode
ASSERT_EQ(this->m_mode, this->m_file.m_mode);
// Ensure CRC match
ASSERT_EQ(this->m_file.m_crc, this->m_independent_crc);
if (this->m_file.m_path == nullptr) {
ASSERT_EQ(this->m_current_path, std::string(""));
} else {
Expand Down Expand Up @@ -256,6 +258,7 @@ bool Os::Test::File::Tester::OpenBaseRule::precondition(const Os::Test::File::Te

void Os::Test::File::Tester::OpenBaseRule::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be implemented at the STest level? It seems pretty convenient

How does one get access to those logs btw? I am assuming it gets captured by GoogleTest and put in a log file somewhere?

// Initial variables used for this test
std::shared_ptr<const std::string> filename = state.get_filename(this->m_random);

Expand Down Expand Up @@ -344,6 +347,7 @@ bool Os::Test::File::Tester::CloseFile::precondition(const Os::Test::File::Teste

void Os::Test::File::Tester::CloseFile::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
// Make sure test state and file state synchronized
state.assert_file_consistent();
state.assert_file_opened(state.m_current_path);
Expand Down Expand Up @@ -375,6 +379,7 @@ bool Os::Test::File::Tester::Read::precondition(
void Os::Test::File::Tester::Read::action(
Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
U8 buffer[FILE_DATA_MAXIMUM];
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
Expand Down Expand Up @@ -411,6 +416,7 @@ bool Os::Test::File::Tester::Write::precondition(
void Os::Test::File::Tester::Write::action(
Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
U8 buffer[FILE_DATA_MAXIMUM];
state.assert_file_consistent();
FwSignedSizeType size_desired = static_cast<FwSignedSizeType>(STest::Pick::lowerUpper(0, FILE_DATA_MAXIMUM));
Expand Down Expand Up @@ -448,6 +454,7 @@ bool Os::Test::File::Tester::Seek::precondition(
void Os::Test::File::Tester::Seek::action(
Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
FwSignedSizeType seek_offset = 0;
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
Expand Down Expand Up @@ -486,6 +493,7 @@ bool Os::Test::File::Tester::Preallocate::precondition(
void Os::Test::File::Tester::Preallocate::action(
Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
FwSignedSizeType offset = static_cast<FwSignedSizeType>(STest::Pick::lowerUpper(0, FILE_DATA_MAXIMUM));
Expand Down Expand Up @@ -519,6 +527,7 @@ bool Os::Test::File::Tester::Flush::precondition(
void Os::Test::File::Tester::Flush::action(
Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
Os::File::Status status = state.m_file.flush();
Expand Down Expand Up @@ -549,6 +558,7 @@ bool Os::Test::File::Tester::OpenInvalidModes::precondition(const Os::Test::File

void Os::Test::File::Tester::OpenInvalidModes::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
// Check initial file state
Expand Down Expand Up @@ -580,6 +590,7 @@ bool Os::Test::File::Tester::PreallocateWithoutOpen::precondition(

void Os::Test::File::Tester::PreallocateWithoutOpen::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
// Check initial file state
state.assert_file_closed();
Expand All @@ -606,6 +617,7 @@ bool Os::Test::File::Tester::SeekWithoutOpen::precondition(const Os::Test::File:

void Os::Test::File::Tester::SeekWithoutOpen::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
// Check initial file state
state.assert_file_closed();
Expand All @@ -632,6 +644,7 @@ bool Os::Test::File::Tester::SeekInvalidSize::precondition(const Os::Test::File:

void Os::Test::File::Tester::SeekInvalidSize::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
// Open file of given filename
Expand Down Expand Up @@ -662,6 +675,7 @@ bool Os::Test::File::Tester::FlushInvalidModes::precondition(const Os::Test::Fil

void Os::Test::File::Tester::FlushInvalidModes::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
ASSERT_TRUE(Os::File::Mode::OPEN_NO_MODE == state.m_file.m_mode || Os::File::Mode::OPEN_READ == state.m_file.m_mode);
Expand Down Expand Up @@ -689,6 +703,7 @@ bool Os::Test::File::Tester::ReadInvalidModes::precondition(const Os::Test::File

void Os::Test::File::Tester::ReadInvalidModes::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
U8 buffer[10];
FwSignedSizeType size = sizeof buffer;
state.assert_file_consistent();
Expand Down Expand Up @@ -720,6 +735,7 @@ bool Os::Test::File::Tester::WriteInvalidModes::precondition(const Os::Test::Fil

void Os::Test::File::Tester::WriteInvalidModes::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
U8 buffer[10];
FwSignedSizeType size = sizeof buffer;
state.assert_file_consistent();
Expand Down Expand Up @@ -756,6 +772,7 @@ Os::Test::File::Tester::OpenIllegalPath::OpenIllegalPath() : Os::Test::File::Tes

void Os::Test::File::Tester::OpenIllegalPath::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
Os::File::Mode random_mode =
static_cast<Os::File::Mode>(STest::Pick::lowerUpper(Os::File::Mode::OPEN_READ,
Expand All @@ -777,6 +794,7 @@ Os::Test::File::Tester::OpenIllegalMode::OpenIllegalMode() : Os::Test::File::Tes

void Os::Test::File::Tester::OpenIllegalMode::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
std::shared_ptr<const std::string> random_filename = state.get_filename(true);
U32 mode = STest::Pick::lowerUpper(0, 1);
Expand All @@ -799,6 +817,7 @@ Os::Test::File::Tester::PreallocateIllegalOffset::PreallocateIllegalOffset()

void Os::Test::File::Tester::PreallocateIllegalOffset::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FwSignedSizeType length = static_cast<FwSignedSizeType>(STest::Pick::any());
FwSignedSizeType invalid_offset =
Expand All @@ -818,6 +837,7 @@ Os::Test::File::Tester::PreallocateIllegalLength::PreallocateIllegalLength()

void Os::Test::File::Tester::PreallocateIllegalLength::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FwSignedSizeType offset = static_cast<FwSignedSizeType>(STest::Pick::any());
FwSignedSizeType invalid_length =
Expand All @@ -836,6 +856,7 @@ Os::Test::File::Tester::SeekIllegal::SeekIllegal() : Os::Test::File::Tester::Ass

void Os::Test::File::Tester::SeekIllegal::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
ASSERT_DEATH_IF_SUPPORTED(state.m_file.seek(-1, Os::File::SeekType::ABSOLUTE), Os::Test::File::Tester::ASSERT_IN_FILE_CPP);
state.assert_file_consistent();
Expand All @@ -851,6 +872,7 @@ Os::Test::File::Tester::ReadIllegalBuffer::ReadIllegalBuffer()

void Os::Test::File::Tester::ReadIllegalBuffer::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FwSignedSizeType size = static_cast<FwSignedSizeType>(STest::Pick::any());
bool random_wait = static_cast<bool>(STest::Pick::lowerUpper(0, 1));
Expand All @@ -869,6 +891,7 @@ Os::Test::File::Tester::ReadIllegalSize::ReadIllegalSize() : Os::Test::File::Tes

void Os::Test::File::Tester::ReadIllegalSize::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
U8 buffer[10] = {};
state.assert_file_consistent();
FwSignedSizeType invalid_size = -1 * static_cast<FwSignedSizeType>(STest::Pick::lowerUpper(0, std::numeric_limits<U32>::max()));
Expand All @@ -890,6 +913,7 @@ Os::Test::File::Tester::WriteIllegalBuffer::WriteIllegalBuffer()

void Os::Test::File::Tester::WriteIllegalBuffer::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FwSignedSizeType size = static_cast<FwSignedSizeType>(STest::Pick::any());
bool random_wait = static_cast<bool>(STest::Pick::lowerUpper(0, 1));
Expand All @@ -908,6 +932,7 @@ Os::Test::File::Tester::WriteIllegalSize::WriteIllegalSize() : Os::Test::File::T

void Os::Test::File::Tester::WriteIllegalSize::action(Os::Test::File::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
U8 buffer[10] = {};
state.assert_file_consistent();
FwSignedSizeType invalid_size = -1 * static_cast<FwSignedSizeType>(STest::Pick::lowerUpper(0, std::numeric_limits<U32>::max()));
Expand Down Expand Up @@ -935,6 +960,7 @@ bool Os::Test::File::Tester::CopyAssignment::precondition(const Os::Test::File::

void Os::Test::File::Tester::CopyAssignment::action(Os::Test::File::Tester& state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
Os::File temp = state.m_file;
state.assert_file_consistent(); // Prevents optimization
Expand All @@ -959,6 +985,7 @@ bool Os::Test::File::Tester::CopyConstruction::precondition(const Os::Test::File

void Os::Test::File::Tester::CopyConstruction::action(Os::Test::File::Tester& state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
Os::File temp(state.m_file);
state.assert_file_consistent(); // Interim check to ensure original file did not change
Expand All @@ -984,6 +1011,7 @@ bool Os::Test::File::Tester::FullCrc::precondition(
void Os::Test::File::Tester::FullCrc::action(
Os::Test::File::Tester& state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
U32 crc = 1;
U32 shadow_crc = 2;
state.assert_file_consistent();
Expand Down Expand Up @@ -1013,6 +1041,7 @@ bool Os::Test::File::Tester::IncrementalCrc::precondition(
void Os::Test::File::Tester::IncrementalCrc::action(
Os::Test::File::Tester& state //!< The test state
){
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FwSignedSizeType size_desired = static_cast<FwSignedSizeType>(STest::Pick::lowerUpper(0, FW_FILE_CHUNK_SIZE));
FwSignedSizeType shadow_size = size_desired;
Expand Down Expand Up @@ -1041,10 +1070,11 @@ bool Os::Test::File::Tester::FinalizeCrc::precondition(
void Os::Test::File::Tester::FinalizeCrc::action(
Os::Test::File::Tester& state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
U32 crc = 1;
U32 shadow_crc = 2;
state.assert_file_consistent();
Os::File::Status status = state.m_file.finalizeCrc(crc);
Os::File::Status status = state.m_file.finalizeCrc(crc);
state.shadow_finalize(shadow_crc);
ASSERT_EQ(status, Os::File::Status::OP_OK);
ASSERT_EQ(crc, shadow_crc);
Expand All @@ -1069,6 +1099,7 @@ bool Os::Test::File::Tester::FullCrcInvalidModes::precondition(
void Os::Test::File::Tester::FullCrcInvalidModes::action(
Os::Test::File::Tester& state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
ASSERT_TRUE(Os::File::Mode::OPEN_READ != state.m_file.m_mode);
Expand Down Expand Up @@ -1102,6 +1133,7 @@ bool Os::Test::File::Tester::IncrementalCrcInvalidModes::precondition(
void Os::Test::File::Tester::IncrementalCrcInvalidModes::action(
Os::Test::File::Tester& state //!< The test state
) {
printf("--> Rule: %s \n", this->getName());
state.assert_file_consistent();
FileState original_file_state = state.current_file_state();
ASSERT_TRUE(Os::File::Mode::OPEN_READ != state.m_file.m_mode);
Expand Down
Loading