Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/lzma_encoder.h" |
| 2 | |
| 3 | #include "aos/events/logging/buffer_encoder_param_test.h" |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 4 | #include "aos/util/file.h" |
| 5 | #include "gmock/gmock.h" |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 6 | #include "gtest/gtest.h" |
| 7 | |
| 8 | namespace aos::logger::testing { |
| 9 | |
James Kuszmaul | f4bf9fe | 2021-05-10 22:58:24 -0700 | [diff] [blame] | 10 | INSTANTIATE_TEST_SUITE_P( |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 11 | Lzma, BufferEncoderTest, |
| 12 | ::testing::Combine(::testing::Values([]() { |
| 13 | return std::make_unique<LzmaEncoder>(2); |
| 14 | }), |
| 15 | ::testing::Values([](std::string_view filename) { |
| 16 | return std::make_unique<LzmaDecoder>(filename); |
| 17 | }), |
| 18 | ::testing::Range(0, 100))); |
| 19 | |
Tyler Chatow | 7df6083 | 2021-07-15 21:18:36 -0700 | [diff] [blame^] | 20 | INSTANTIATE_TEST_SUITE_P( |
| 21 | LzmaThreaded, BufferEncoderTest, |
| 22 | ::testing::Combine(::testing::Values([]() { |
| 23 | return std::make_unique<LzmaEncoder>(2); |
| 24 | }), |
| 25 | ::testing::Values([](std::string_view filename) { |
| 26 | return std::make_unique<ThreadedLzmaDecoder>(filename); |
| 27 | }), |
| 28 | ::testing::Range(0, 100))); |
| 29 | |
Austin Schuh | 3bd4c40 | 2020-11-06 18:19:06 -0800 | [diff] [blame] | 30 | // Tests that we return as much of the file as we can read if the end is |
| 31 | // corrupted. |
| 32 | TEST_F(BufferEncoderBaseTest, CorruptedBuffer) { |
| 33 | std::uniform_int_distribution<int> quantity_distribution(20, 60); |
| 34 | const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR")); |
| 35 | const std::string file_path = std::string(test_dir) + "/foo"; |
| 36 | |
| 37 | std::vector<std::vector<uint8_t>> encoded_buffers; |
| 38 | { |
| 39 | const int encode_chunks = quantity_distribution(*random_number_generator()); |
| 40 | const auto encoder = std::make_unique<LzmaEncoder>(2); |
| 41 | encoded_buffers = CreateAndEncode(encode_chunks, encoder.get()); |
| 42 | encoder->Finish(); |
| 43 | |
| 44 | std::string contents = ""; |
| 45 | for (auto span : encoder->queue()) { |
| 46 | absl::StrAppend( |
| 47 | &contents, |
| 48 | std::string_view(reinterpret_cast<const char *>(span.data()), |
| 49 | span.size())); |
| 50 | } |
| 51 | aos::util::WriteStringToFileOrDie( |
| 52 | file_path, contents.substr(0, contents.size() - 200)); |
| 53 | } |
| 54 | |
| 55 | const size_t total_encoded_size = TotalSize(encoded_buffers); |
| 56 | |
| 57 | // Try decoding in multiple random chunkings. |
| 58 | for (int i = 0; i < 20; ++i) { |
| 59 | const auto decoder = std::make_unique<LzmaDecoder>(file_path); |
| 60 | std::vector<std::vector<uint8_t>> decoded_buffers; |
| 61 | size_t total_decoded_size = 0; |
| 62 | while (true) { |
| 63 | const int chunk_size = quantity_distribution(*random_number_generator()); |
| 64 | std::vector<uint8_t> chunk(chunk_size); |
| 65 | const size_t read_result = |
| 66 | decoder->Read(chunk.data(), chunk.data() + chunk_size); |
| 67 | // Eventually we'll get here, once the decoder is really sure it's done. |
| 68 | if (read_result == 0) { |
| 69 | // Sanity check the math in the test code. |
| 70 | LOG(INFO) << "Decoded " << total_decoded_size << " encoded " |
| 71 | << total_encoded_size; |
| 72 | CHECK_EQ(total_decoded_size, TotalSize(decoded_buffers)); |
| 73 | break; |
| 74 | } |
| 75 | // If we're at the end, trim off the 0s so our comparison later works out. |
| 76 | chunk.resize(read_result); |
| 77 | total_decoded_size += read_result; |
| 78 | decoded_buffers.emplace_back(std::move(chunk)); |
| 79 | } |
| 80 | auto flattened_encoded = Flatten(encoded_buffers); |
| 81 | auto flattened_decoded = Flatten(decoded_buffers); |
| 82 | |
| 83 | ASSERT_LE(flattened_decoded.size(), flattened_encoded.size()); |
| 84 | flattened_encoded.resize(flattened_decoded.size()); |
| 85 | |
| 86 | ASSERT_THAT(flattened_decoded, ::testing::Eq(flattened_encoded)); |
| 87 | } |
| 88 | } |
| 89 | |
Brian Silverman | f59fe3f | 2020-09-22 21:04:09 -0700 | [diff] [blame] | 90 | } // namespace aos::logger::testing |