Skip to content

Commit

Permalink
control lifetime of stream and writer for old mingw64/32 lib compat
Browse files Browse the repository at this point in the history
  • Loading branch information
oschonrock committed Dec 18, 2024
1 parent 98166bb commit b8f70c9
Showing 1 changed file with 95 additions and 83 deletions.
178 changes: 95 additions & 83 deletions test/test_diffutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,40 @@ class DiffTest : public testing::Test {
DiffTest()
: testtmpdir{std::filesystem::canonical(std::filesystem::current_path() / "tmp")},

old_path{testtmpdir / "old.sha1.bin"}, old_stream{old_path, std::ios_base::binary},
oldw{old_stream}, new_path{testtmpdir / "new.sha1.bin"},
new_stream{new_path, std::ios_base::binary}, neww{new_stream} {
old_path{testtmpdir / "old.sha1.bin"}, new_path{testtmpdir / "new.sha1.bin"} {

std::ofstream old_stream{old_path, std::ios_base::binary};

flat_file::stream_writer<PwType> oldw{old_stream};
oldw.write(PwType{"0000000000000000000000000000000000000010:10"});
oldw.write(PwType{"0000000000000000000000000000000000000020:20"});
oldw.write(PwType{"0000000000000000000000000000000000000030:30"});
oldw.flush(true);
}

void write(const std::string& s) { neww.write(PwType{s}); }

void flush() { neww.flush(true); }
void create_new(const std::vector<std::string>& pws) {
std::ofstream new_stream{new_path, std::ios_base::binary};
flat_file::stream_writer<PwType> neww{new_stream};
for (const auto& pw: pws) {
neww.write(PwType{pw});
}
}

std::filesystem::path testtmpdir;

std::filesystem::path old_path;
std::ofstream old_stream;
flat_file::stream_writer<PwType> oldw;

std::filesystem::path new_path;
std::ofstream new_stream;
flat_file::stream_writer<PwType> neww;
std::filesystem::path old_path;
std::filesystem::path new_path;
};

using DiffTestSha1 = DiffTest<hibp::pawned_pw_sha1>;
using DiffTestNtlm = DiffTest<hibp::pawned_pw_ntlm>;

TEST_F(DiffTestSha1, diffI0) {
write("0000000000000000000000000000000000000005:5");

write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000030:30");
flush();
create_new({
"0000000000000000000000000000000000000005:5",
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand All @@ -58,13 +57,14 @@ TEST_F(DiffTestSha1, diffI0) {
}

TEST_F(DiffTestSha1, diffI1) {
write("0000000000000000000000000000000000000010:10");
create_new({
"0000000000000000000000000000000000000010:10",

write("0000000000000000000000000000000000000015:15");
"0000000000000000000000000000000000000015:15",

write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000030:30");
flush();
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand All @@ -73,13 +73,14 @@ TEST_F(DiffTestSha1, diffI1) {
}

TEST_F(DiffTestSha1, diffI2) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:20");
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:20",

write("0000000000000000000000000000000000000025:25");
"0000000000000000000000000000000000000025:25",

write("0000000000000000000000000000000000000030:30");
flush();
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand All @@ -88,12 +89,13 @@ TEST_F(DiffTestSha1, diffI2) {
}

TEST_F(DiffTestSha1, diffI3) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000030:30");
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000030:30",

write("0000000000000000000000000000000000000035:35");
flush();
"0000000000000000000000000000000000000035:35",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand All @@ -102,10 +104,11 @@ TEST_F(DiffTestSha1, diffI3) {
}

TEST_F(DiffTestSha1, diffU0) {
write("0000000000000000000000000000000000000010:11");
write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000030:30");
flush();
create_new({
"0000000000000000000000000000000000000010:11",
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand All @@ -114,10 +117,11 @@ TEST_F(DiffTestSha1, diffU0) {
}

TEST_F(DiffTestSha1, diffU1) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:21");
write("0000000000000000000000000000000000000030:30");
flush();
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:21",
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand All @@ -126,10 +130,11 @@ TEST_F(DiffTestSha1, diffU1) {
}

TEST_F(DiffTestSha1, diffU2) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000030:31");
flush();
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000030:31",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand All @@ -138,73 +143,79 @@ TEST_F(DiffTestSha1, diffU2) {
}

TEST_F(DiffTestSha1, diffNewShort0) {
write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000030:30");
flush();
create_new({
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
EXPECT_THROW(hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff),
std::runtime_error);
}
TEST_F(DiffTestSha1, diffNewShort1) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000030:30");
flush();
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
EXPECT_THROW(hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff),
std::runtime_error);
}
TEST_F(DiffTestSha1, diffNewShort2) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:20");
flush();
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:20",
});

std::stringstream diff;
EXPECT_THROW(hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff),
std::runtime_error);
}

TEST_F(DiffTestSha1, diffOldReplaced0) {

write("0000000000000000000000000000000000000015:10");
write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000030:30");
flush();
create_new({
"0000000000000000000000000000000000000015:10",
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
EXPECT_THROW(hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff),
std::runtime_error);
}
TEST_F(DiffTestSha1, diffOldReplaced1) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000025:20");
write("0000000000000000000000000000000000000030:30");
flush();
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000025:20",
"0000000000000000000000000000000000000030:30",
});

std::stringstream diff;
EXPECT_THROW(hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff),
std::runtime_error);
}
TEST_F(DiffTestSha1, diffOldReplaced2) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000035:30");
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000035:30",

flush();
});

std::stringstream diff;
EXPECT_THROW(hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff),
std::runtime_error);
}

TEST_F(DiffTestSha1, diffAppend2) {
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:20");
write("0000000000000000000000000000000000000030:30");
write("0000000000000000000000000000000000000040:40");
write("0000000000000000000000000000000000000050:50");
flush();
create_new({
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:20",
"0000000000000000000000000000000000000030:30",
"0000000000000000000000000000000000000040:40",
"0000000000000000000000000000000000000050:50",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand All @@ -215,14 +226,15 @@ TEST_F(DiffTestSha1, diffAppend2) {
}

TEST_F(DiffTestSha1, diffCombo1) {
write("0000000000000000000000000000000000000005:5");
write("0000000000000000000000000000000000000010:10");
write("0000000000000000000000000000000000000020:25");
write("0000000000000000000000000000000000000027:27");
write("0000000000000000000000000000000000000030:30");
write("0000000000000000000000000000000000000040:40");
write("0000000000000000000000000000000000000050:50");
flush();
create_new({
"0000000000000000000000000000000000000005:5",
"0000000000000000000000000000000000000010:10",
"0000000000000000000000000000000000000020:25",
"0000000000000000000000000000000000000027:27",
"0000000000000000000000000000000000000030:30",
"0000000000000000000000000000000000000040:40",
"0000000000000000000000000000000000000050:50",
});

std::stringstream diff;
hibp::diffutils::run_diff<hibp::pawned_pw_sha1>(old_path, new_path, diff);
Expand Down

0 comments on commit b8f70c9

Please sign in to comment.