blob: 1e814ff13fb485b08896020b34bb12b11bdb0bee [file] [log] [blame]
Brian Silvermanf59fe3f2020-09-22 21:04:09 -07001#include "aos/events/logging/lzma_encoder.h"
2
3#include "aos/events/logging/buffer_encoder_param_test.h"
Austin Schuh3bd4c402020-11-06 18:19:06 -08004#include "aos/util/file.h"
5#include "gmock/gmock.h"
Brian Silvermanf59fe3f2020-09-22 21:04:09 -07006#include "gtest/gtest.h"
7
Austin Schuhbe91b342022-06-27 00:53:45 -07008DECLARE_int32(lzma_threads);
9
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070010namespace aos::logger::testing {
11
James Kuszmaulf4bf9fe2021-05-10 22:58:24 -070012INSTANTIATE_TEST_SUITE_P(
Austin Schuhbe91b342022-06-27 00:53:45 -070013 MtLzma, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070014 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuhbe91b342022-06-27 00:53:45 -070015 FLAGS_lzma_threads = 3;
Austin Schuh48d10d62022-10-16 22:19:23 -070016 return std::make_unique<LzmaEncoder>(max_message_size,
17 2, 4096);
Austin Schuhbe91b342022-06-27 00:53:45 -070018 }),
19 ::testing::Values([](std::string_view filename) {
20 return std::make_unique<LzmaDecoder>(filename);
21 }),
22 ::testing::Range(0, 100)));
23
24INSTANTIATE_TEST_SUITE_P(
25 MtLzmaThreaded, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070026 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuhbe91b342022-06-27 00:53:45 -070027 FLAGS_lzma_threads = 3;
Austin Schuh48d10d62022-10-16 22:19:23 -070028 return std::make_unique<LzmaEncoder>(max_message_size,
29 5, 4096);
Austin Schuhbe91b342022-06-27 00:53:45 -070030 }),
31 ::testing::Values([](std::string_view filename) {
32 return std::make_unique<ThreadedLzmaDecoder>(filename);
33 }),
34 ::testing::Range(0, 100)));
35
36INSTANTIATE_TEST_SUITE_P(
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070037 Lzma, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070038 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuhbe91b342022-06-27 00:53:45 -070039 FLAGS_lzma_threads = 1;
Austin Schuh48d10d62022-10-16 22:19:23 -070040 return std::make_unique<LzmaEncoder>(max_message_size,
41 2, 4096);
Brian Silvermanf59fe3f2020-09-22 21:04:09 -070042 }),
43 ::testing::Values([](std::string_view filename) {
44 return std::make_unique<LzmaDecoder>(filename);
45 }),
46 ::testing::Range(0, 100)));
47
Tyler Chatow7df60832021-07-15 21:18:36 -070048INSTANTIATE_TEST_SUITE_P(
49 LzmaThreaded, BufferEncoderTest,
Austin Schuh48d10d62022-10-16 22:19:23 -070050 ::testing::Combine(::testing::Values([](size_t max_message_size) {
Austin Schuhbe91b342022-06-27 00:53:45 -070051 FLAGS_lzma_threads = 1;
Austin Schuh48d10d62022-10-16 22:19:23 -070052 return std::make_unique<LzmaEncoder>(max_message_size,
53 5, 4096);
Tyler Chatow7df60832021-07-15 21:18:36 -070054 }),
55 ::testing::Values([](std::string_view filename) {
56 return std::make_unique<ThreadedLzmaDecoder>(filename);
57 }),
58 ::testing::Range(0, 100)));
59
Austin Schuh3bd4c402020-11-06 18:19:06 -080060// Tests that we return as much of the file as we can read if the end is
61// corrupted.
62TEST_F(BufferEncoderBaseTest, CorruptedBuffer) {
63 std::uniform_int_distribution<int> quantity_distribution(20, 60);
64 const char *const test_dir = CHECK_NOTNULL(getenv("TEST_TMPDIR"));
65 const std::string file_path = std::string(test_dir) + "/foo";
66
67 std::vector<std::vector<uint8_t>> encoded_buffers;
68 {
69 const int encode_chunks = quantity_distribution(*random_number_generator());
Austin Schuh48d10d62022-10-16 22:19:23 -070070 const auto encoder = std::make_unique<LzmaEncoder>(
71 BufferEncoderBaseTest::kMaxMessageSize, 2);
Austin Schuh3bd4c402020-11-06 18:19:06 -080072 encoded_buffers = CreateAndEncode(encode_chunks, encoder.get());
73 encoder->Finish();
74
75 std::string contents = "";
76 for (auto span : encoder->queue()) {
77 absl::StrAppend(
78 &contents,
79 std::string_view(reinterpret_cast<const char *>(span.data()),
80 span.size()));
81 }
82 aos::util::WriteStringToFileOrDie(
83 file_path, contents.substr(0, contents.size() - 200));
84 }
85
86 const size_t total_encoded_size = TotalSize(encoded_buffers);
87
88 // Try decoding in multiple random chunkings.
89 for (int i = 0; i < 20; ++i) {
90 const auto decoder = std::make_unique<LzmaDecoder>(file_path);
91 std::vector<std::vector<uint8_t>> decoded_buffers;
92 size_t total_decoded_size = 0;
93 while (true) {
94 const int chunk_size = quantity_distribution(*random_number_generator());
95 std::vector<uint8_t> chunk(chunk_size);
96 const size_t read_result =
97 decoder->Read(chunk.data(), chunk.data() + chunk_size);
98 // Eventually we'll get here, once the decoder is really sure it's done.
99 if (read_result == 0) {
100 // Sanity check the math in the test code.
101 LOG(INFO) << "Decoded " << total_decoded_size << " encoded "
102 << total_encoded_size;
103 CHECK_EQ(total_decoded_size, TotalSize(decoded_buffers));
104 break;
105 }
106 // If we're at the end, trim off the 0s so our comparison later works out.
107 chunk.resize(read_result);
108 total_decoded_size += read_result;
109 decoded_buffers.emplace_back(std::move(chunk));
110 }
111 auto flattened_encoded = Flatten(encoded_buffers);
112 auto flattened_decoded = Flatten(decoded_buffers);
113
114 ASSERT_LE(flattened_decoded.size(), flattened_encoded.size());
115 flattened_encoded.resize(flattened_decoded.size());
116
117 ASSERT_THAT(flattened_decoded, ::testing::Eq(flattened_encoded));
118 }
119}
120
Brian Silvermanf59fe3f2020-09-22 21:04:09 -0700121} // namespace aos::logger::testing