|
6 | 6 | #include <test/util/random.h>
|
7 | 7 | #include <test/util/setup_common.h>
|
8 | 8 | #include <util/fs.h>
|
| 9 | +#include <util/strencodings.h> |
9 | 10 |
|
10 | 11 | #include <boost/test/unit_test.hpp>
|
11 | 12 |
|
12 | 13 | using namespace std::string_literals;
|
13 | 14 |
|
14 | 15 | BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
|
15 | 16 |
|
| 17 | +BOOST_AUTO_TEST_CASE(xor_file) |
| 18 | +{ |
| 19 | + fs::path xor_path{m_args.GetDataDirBase() / "test_xor.bin"}; |
| 20 | + auto raw_file{[&](const auto& mode) { return fsbridge::fopen(xor_path, mode); }}; |
| 21 | + const std::vector<uint8_t> test1{1, 2, 3}; |
| 22 | + const std::vector<uint8_t> test2{4, 5}; |
| 23 | + const std::vector<std::byte> xor_pat{std::byte{0xff}, std::byte{0x00}}; |
| 24 | + { |
| 25 | + // Check errors for missing file |
| 26 | + AutoFile xor_file{raw_file("rb"), xor_pat}; |
| 27 | + BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullpt"}); |
| 28 | + BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullpt"}); |
| 29 | + BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullpt"}); |
| 30 | + } |
| 31 | + { |
| 32 | + AutoFile xor_file{raw_file("wbx"), xor_pat}; |
| 33 | + xor_file << test1 << test2; |
| 34 | + } |
| 35 | + { |
| 36 | + // Read raw from disk |
| 37 | + AutoFile non_xor_file{raw_file("rb")}; |
| 38 | + std::vector<std::byte> raw(7); |
| 39 | + non_xor_file >> Span{raw}; |
| 40 | + BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa"); |
| 41 | + // Check that no padding exists |
| 42 | + BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"}); |
| 43 | + } |
| 44 | + { |
| 45 | + AutoFile xor_file{raw_file("rb"), xor_pat}; |
| 46 | + std::vector<std::byte> read1, read2; |
| 47 | + xor_file >> read1 >> read2; |
| 48 | + BOOST_CHECK_EQUAL(HexStr(read1), HexStr(test1)); |
| 49 | + BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2)); |
| 50 | + // Check that eof was reached |
| 51 | + BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"}); |
| 52 | + } |
| 53 | + { |
| 54 | + AutoFile xor_file{raw_file("rb"), xor_pat}; |
| 55 | + std::vector<std::byte> read2; |
| 56 | + // Check that ignore works |
| 57 | + xor_file.ignore(4); |
| 58 | + xor_file >> read2; |
| 59 | + BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2)); |
| 60 | + // Check that ignore and read fail now |
| 61 | + BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"}); |
| 62 | + BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"}); |
| 63 | + } |
| 64 | +} |
| 65 | + |
16 | 66 | BOOST_AUTO_TEST_CASE(streams_vector_writer)
|
17 | 67 | {
|
18 | 68 | unsigned char a(1);
|
|
0 commit comments