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