Skip to content

Commit

Permalink
tests: check padding bytes for state file tests
Browse files Browse the repository at this point in the history
- check that struct member offset is as expected as in the state file
- add exceptional handling for mingw/win32 32bit and 64bit
- code cleanup
  • Loading branch information
franku authored and arogge committed Nov 5, 2020
1 parent 0a65ffa commit 765af93
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
8 changes: 0 additions & 8 deletions core/src/lib/bsys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,14 +513,6 @@ int DeletePidFile(char* dir, const char* progname, int port)
return 1;
}

struct StateFileHeader {
char id[14];
int32_t version;
uint64_t last_jobs_addr;
uint64_t end_of_recent_job_results_list;
uint64_t reserved[19];
};

static struct StateFileHeader state_hdr = {{"Bareos State\n"}, 4, 0, 0, {0}};

static bool CheckHeader(const StateFileHeader& hdr)
Expand Down
8 changes: 8 additions & 0 deletions core/src/lib/bsys.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ bool PathAppend(PoolMem& path, PoolMem& extra);
bool PathCreate(const char* path, mode_t mode = 0750);
bool PathCreate(PoolMem& path, mode_t mode = 0750);

struct StateFileHeader {
char id[14];
int32_t version;
uint64_t last_jobs_addr;
uint64_t end_of_recent_job_results_list;
uint64_t reserved[19];
};

#endif // BAREOS_LIB_BSYS_H_
44 changes: 38 additions & 6 deletions core/src/tests/recent_job_results_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,40 @@
#include <iostream>
#include <fstream>

static auto Is32BitWordsize = []() { return sizeof(void*) == 4; };
static inline bool Is32BitWordsize()
{
#if defined HAVE_WIN32 and defined HAVE_MINGW
return false; // wincross always 64Bit
#else
return sizeof(void*) == 4;
#endif
};

TEST(recent_job_results_list, check_header_padding_bytes)
{
std::string detected;

if (Is32BitWordsize()) {
detected = "32Bit wordsize";
EXPECT_EQ(offsetof(StateFileHeader, version), 16);
EXPECT_EQ(offsetof(StateFileHeader, last_jobs_addr), 20);
EXPECT_EQ(offsetof(StateFileHeader, end_of_recent_job_results_list), 28);
EXPECT_EQ(offsetof(StateFileHeader, reserved), 36);
} else {
detected = "64Bit wordsize";
EXPECT_EQ(offsetof(StateFileHeader, version), 16);
EXPECT_EQ(offsetof(StateFileHeader, last_jobs_addr), 24);
EXPECT_EQ(offsetof(StateFileHeader, end_of_recent_job_results_list), 32);
EXPECT_EQ(offsetof(StateFileHeader, reserved), 40);
}

#if HAVE_WIN32
detected += ", Windows";
#endif
detected += " detected";

std::cout << " <<< " << detected << " >>> " << std::endl;
}

TEST(recent_job_results_list, read_job_results_from_file)
{
Expand All @@ -41,7 +74,7 @@ TEST(recent_job_results_list, read_job_results_from_file)

char orig_path[]{TEST_ORIGINAL_FILE_DIR};

const char *fname = Is32BitWordsize() ? "bareos-dir-32bit" : "bareos-dir";
const char* fname = Is32BitWordsize() ? "bareos-dir-32bit" : "bareos-dir";

ReadStateFile(orig_path, fname, 42001);

Expand All @@ -62,7 +95,7 @@ TEST(recent_job_results_list, write_job_results_to_file)
OSDependentInit();
RecentJobResultsList::Cleanup();

const char *fname = Is32BitWordsize() ? "bareos-dir-32bit" : "bareos-dir";
const char* fname = Is32BitWordsize() ? "bareos-dir-32bit" : "bareos-dir";

char orig_path[]{TEST_ORIGINAL_FILE_DIR};
ReadStateFile(orig_path, fname, 42001);
Expand Down Expand Up @@ -123,9 +156,8 @@ TEST(recent_job_results_list, read_job_results_from_file_truncated_jobs)

char orig_path[]{TEST_ORIGINAL_FILE_DIR};

const char *fname = Is32BitWordsize()
? "bareos-dir-truncated-jobs-32bit"
: "bareos-dir-truncated-jobs";
const char* fname = Is32BitWordsize() ? "bareos-dir-truncated-jobs-32bit"
: "bareos-dir-truncated-jobs";

ASSERT_TRUE(create_file(orig_path, std::string(fname) + ".42001.state"));
ReadStateFile(orig_path, fname, 42001);
Expand Down

0 comments on commit 765af93

Please sign in to comment.