Skip to content

Commit

Permalink
scytale: add test for scytale_reader_send_to() with unencrypted file
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpoelen committed Dec 29, 2023
1 parent c70022c commit 207f387
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/lib/test_scytale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,66 @@ RED_AUTO_TEST_CASE_WD(Testscytale, wd)
}
}

RED_AUTO_TEST_CASE_WD(Test_scytale_reader_send_to, wd)
{
auto derivator = "encrypted.txt"_av;
auto finalname = wd.add_file(derivator.data());
auto hash_finalname = wd.add_file("hash_encrypted.txt");
auto copied_filename = wd.add_file("copied");

// Writer
{
int with_encryption = 0; // int used as boolean 0 false, true otherwise
int with_checksum = 0; // int used as boolean 0 false, true otherwise

auto * handle = scytale_writer_new_with_test_random(
with_encryption, with_checksum,
byte_ptr_cast(finalname.c_str()), checked_int(finalname.size()),
hmac_key, &trace_fn, false, false);
RED_REQUIRE(handle);
RED_CHECK_EQ(scytale_writer_open(handle, finalname, hash_finalname, 0, nullptr, 0), 0);

// more that 40 bytes (internal buffer of InCryptoTransport)
RED_CHECK_EQ(scytale_writer_write(handle, bytes("We write, "), 10), 10);
RED_CHECK_EQ(scytale_writer_write(handle, bytes("and again, "), 11), 11);
RED_CHECK_EQ(scytale_writer_write(handle, bytes("and so on."), 10), 10);
RED_CHECK_EQ(scytale_writer_write(handle, bytes("We write, "), 10), 10);
RED_CHECK_EQ(scytale_writer_write(handle, bytes("and again, "), 11), 11);
RED_CHECK_EQ(scytale_writer_write(handle, bytes("and so on."), 10), 10);

RED_CHECK_EQ(scytale_writer_close(handle), 0);

RED_CHECK_EQ(scytale_writer_get_qhashhex(handle), "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"sv);
RED_CHECK_EQ(scytale_writer_get_fhashhex(handle), "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"sv);

RED_CHECK_EQ(scytale_writer_get_error_message(handle), "No error"sv);

scytale_writer_delete(handle);
}

// Reader with send to
{
auto handle = scytale_reader_new(
byte_ptr_cast(finalname.c_str()), checked_int(finalname.size()),
hmac_key, &trace_fn, 0, 0);
RED_REQUIRE(handle);
RED_CHECK_EQ(scytale_reader_open(
handle, finalname,
byte_ptr_cast(derivator.data()), checked_int(derivator.size())), 0);

{
unique_fd ufd{open(copied_filename, O_WRONLY | O_CREAT, 0644)};
RED_REQUIRE(ufd.is_open());
RED_CHECK(scytale_reader_send_to(handle, ufd.fd()) == 0);
}

std::string content = RED_REQUIRE_GET_FILE_CONTENTS(copied_filename);
RED_CHECK(content == "We write, and again, and so on.We write, and again, and so on."_av);

scytale_reader_delete(handle);
}
}

RED_AUTO_TEST_CASE_WD(TestscytaleWriteUseRandom, wd)
{
auto finalname = wd.add_file("encrypted.txt");
Expand Down

0 comments on commit 207f387

Please sign in to comment.